Tuesday, October 5, 2010

Generate QJML from Java Bean class...

Quick tool by IBM is a very useful tool for creating parsing logic for conversion from XML to java.

Have a look at the following link...

http://www.ibm.com/developerworks/library/x-quick/index.html

But this converts from DTD -> QJML -> Java.

I have eliminiated the need for creating DTD.
You can create QJML directly from java Bean class if you have.
Just provide the java class and it will generate 90% code for QJML.
You can use it with minor tweeking...




private static String getQJML(Class c) throws ClassNotFoundException
{
    suffix = c.getSimpleName().substring(c.getSimpleName().length()-2);
    System.out.println(suffix);
    String tagName = getTagName(c);
    StringBuilder output=new StringBuilder("");
    output.append("<!-- ********************************************************************************** -->");
    output.append("\n\t<bean tag=\"" + tagName + "\">");
    if(!containsArrayOrDate(c))
        output.append("\n\t\t<targetClass>" + c.getName() + "</targetClass>");
    else
        output.append("\n\t\t<targetClass>" + "wrappers.ui.vo." + c.getSimpleName() + "</targetClass>");
    output.append("\n\t\t<elements>");
    output.append("\n\t\t\t<selection optional=\"True\" repeating=\"True\">");
    Field fields[]= c.getDeclaredFields();
    for(Field f: fields)
    {
        if(f.getName().charAt(0)>='A' && f.getName().charAt(0)<='Z')
        {

            if(!Modifier.toString(f.getModifiers()).contains("final"))
            {
                System.out.println("Class property " + f.getName() + " starts with upper case.");
                System.out.println("Cannot proceed");
                return "";
            }
            else
            {
                continue;
            }
        }
        // if property type does not ends with a VO
        if(!f.getType().getName().endsWith(suffix))
        {
            // Means it is not a custom object (like AssetVO)
            // <item coin="asset.id">
            output.append("\n\t\t\t\t<item coin=\"" + tagName + "." + f.getName() + "\">");
        }
        else
        {
            // it ends in a VO e.g. AssetRangeVO
            // tag should be <item coin="assetRange">
            output.append("\n\t\t\t\t<item coin=\"" + getTagName(f.getType())+ "\">");    
        }
        if(f.getType().isArray())
        {
            //<property kind="list" name="wrapperSchedule"/>
            output.append("\n\t\t\t\t\t<property kind=\"list\" name=\"wrapper" + firstCaps(f.getName()) + "\"/>");
        }
        else if(f.getType().equals(java.util.Date.class))
        {
            // <property name="wrapperDate"/>
            output.append("\n\t\t\t\t\t<property name=\"wrapper" + firstCaps(f.getName()) + "\"/>");
        }
        else if(f.getType().equals(java.util.List.class))
        {
            String fldType = f.getGenericType().toString();
            String typeOfList = fldType.substring("java.util.List<".length(), fldType.length()-1);
            if(typeOfList.endsWith(suffix))
            {
                // It is a list of custom objects
                // For example, lets say if it is List<GoalVO>, then it should be defined as follows.
                // <property kind="list" name="goal"/>
                output.append("\n\t\t\t\t\t<property kind=\"list\" name=\"" + getTagName(Class.forName(typeOfList)) + "\"/>");

            }
            else
            {
                //<property kind="list" name="propValues"/>
                output.append("\n\t\t\t\t\t<property kind=\"list\" name=\"" + f.getName() + "\"/>");
            }
        }
        else
        {
            //<property name="propertName"/>
            output.append("\n\t\t\t\t\t<property name=\"" + f.getName() + "\"/>");
        }
        output.append("\n\t\t\t\t</item>");
    }
    output.append("\n\t\t\t</selection>");
    output.append("\n\t\t</elements>");
    output.append("\n\t</bean>");
    for(Field fd: fields)
    {
        if(Modifier.toString(fd.getModifiers()).contains("final"))
        {
            // if the field is final, it 
            // cannot be set via XML.
            continue;
        }

        // No need to generate if property Name ends in VO because the tag should be already defined.
        // Most likely it will be a VO object
        if(fd.getType().getName().endsWith(suffix))
            continue;
        if(fd.getType().equals(java.util.List.class))
        {

            // if it is a list of custom objects
            String fldType = fd.getGenericType().toString();
            String typeOfList = fldType.substring("java.util.List<".length(), fldType.length()-1);
            if(typeOfList.endsWith(suffix))
            {
                // It is a list of custom objects
                // so no need to definie its <text tag 
                continue;
            }
        }
        if(fd.getType().isArray() && fd.getType().getComponentType().getClass().getName().endsWith(suffix))
        {
            // it is an array and each element is a custom object
            continue;
        }

        output.append("\n\t<text label=\"" + tagName + "." + fd.getName() + "\" tag=\"" + fd.getName()+"\"" );
        // Specify type based on the type of field.
        if(fd.getType().equals(int.class) || fd.getType().equals(Integer.class) || 
        (fd.getType().isArray() && fd.getType().getComponentType().equals(int.class)))
        {
            output.append(" type=\"int\"/>");
            continue;
        }
        if(fd.getType().equals(boolean.class) || fd.getType().equals(Boolean.class))
        {
            output.append(" type=\"boolean\"/>");
            continue;
        }
        if(fd.getType().equals(double.class)||fd.getType().equals(Double.class) ||
        (fd.getType().isArray() && fd.getType().getComponentType().equals(double.class))) 
        {
            output.append(" type=\"double\"/>");
            continue;
        }
        if(fd.getType().equals(long.class)||fd.getType().equals(Long.class)||
        (fd.getType().isArray() && fd.getType().getComponentType().equals(long.class)))
        {
            output.append(" type=\"long\"/>");
            continue;
        } 
        if(fd.getType().equals(java.util.List.class))
        {
            output.append(" type=\"Needs to be filled\"/>");
            continue;
        }
        output.append("/>");
    }
    return output.toString();
}

No comments:

Post a Comment