Exemplo n.º 1
0
  // -------------------------------------------------------------------------
  // Overriden methods
  // -------------------------------------------------------------------------
  public void loadChildren() {
    Field[] fields = getFields();
    if (fields != null) {
      TreeChildrenSorter sorter = new TreeChildrenSorter(this);

      for (int i = 0; i < fields.length; i++) {
        Field field = fields[i];

        ObjectHolder holder = new FieldObjectHolder(getObjectHolder(), field);
        String name = field.getName();

        MutableTreeNode newNode = createTreeNode(name, holder);
        // MutableTreeNode newNode = getTreeNodeFactory().getTreeNode( name, holder );
        // insert( newNode, pos++ );
        sorter.add(newNode);
      }
      sorter.sort();
    }
  }
Exemplo n.º 2
0
 private Object createObject(
     Node node, String name, String classPackage, String type, String value, boolean setProperty) {
   Bean parentBean = null;
   if (beansStack.size() > 0) parentBean = (Bean) beansStack.peek();
   Object object = null;
   // param is either an XSD type or a bean,
   // check if it can be converted to an XSD type
   XSDatatype dt = null;
   try {
     dt = DatatypeFactory.getTypeByName(type);
   } catch (DatatypeException dte) {
     // the type is not a valid XSD data type
   }
   // convert null value to default
   if ((dt != null) && (value == null)) {
     Class objType = dt.getJavaObjectType();
     if (objType == String.class) value = "";
     else if ((objType == java.math.BigInteger.class)
         || (objType == Long.class)
         || (objType == Integer.class)
         || (objType == Short.class)
         || (objType == Byte.class)) value = "0";
     else if ((objType == java.math.BigDecimal.class)
         || (objType == Double.class)
         || (objType == Float.class)) value = "0.0";
     else if (objType == Boolean.class) value = "false";
     else if (objType == java.util.Date.class) value = DateUtils.getCurrentDate();
     else if (objType == java.util.Calendar.class) value = DateUtils.getCurrentDateTime();
   }
   //  check whether the type was converted to an XSD datatype
   if ((dt != null) && dt.isValid(value, null)) {
     // create and return an XSD Java object (e.g. String, Integer, Boolean, etc)
     object = dt.createJavaObject(value, null);
     if (object instanceof java.util.Calendar) {
       // check that the object is truly a Calendar
       // because DatatypeFactory converts xsd:date
       // types to GregorianCalendar instead of Date.
       if (type.equals("date")) {
         java.text.DateFormat df = new java.text.SimpleDateFormat("yyyy-MM-dd");
         try {
           object = df.parse(value);
         } catch (java.text.ParseException pe) {
           object = new java.util.Date();
         }
       }
     }
   } else {
     // Create a bean object
     if (topLevelBean == null) topLevelBean = parentBean;
     object = pushBeanOnStack(classPackage, type);
     // Check fields to see if this property is the 'value' for the object
     Field[] fields = object.getClass().getDeclaredFields();
     for (int i = 0; i < fields.length; i++) {
       if (fields[i].isAnnotationPresent(ObjectXmlAsValue.class)) {
         try {
           StringBuffer fieldName = new StringBuffer(fields[i].getName());
           char c = fieldName.charAt(0);
           if (c >= 'a' && c <= 'z') {
             c += 'A' - 'a';
           }
           fieldName.setCharAt(0, c);
           fieldName.insert(0, "set");
           Method method =
               object
                   .getClass()
                   .getMethod(fieldName.toString(), new Class[] {fields[i].getType()});
           method.invoke(object, value);
           break;
         } catch (Exception e) {
           System.err.println(e.getMessage());
         }
       }
     }
     NamedNodeMap nodeAttrs = node.getAttributes();
     // iterate attributes and set field values for property attributes
     for (int i = 0; i < nodeAttrs.getLength(); i++) {
       String nodePrefix = nodeAttrs.item(i).getPrefix();
       if (nodePrefix.equals(nsPrefix)) {
         String nodeName = nodeAttrs.item(i).getLocalName();
         String nodeValue = nodeAttrs.item(i).getNodeValue();
         try {
           Field field = object.getClass().getDeclaredField(nodeName);
           if (field.isAnnotationPresent(ObjectXmlAsAttribute.class)) {
             StringBuffer fieldName = new StringBuffer(field.getName());
             char c = fieldName.charAt(0);
             if (c >= 'a' && c <= 'z') {
               c += 'A' - 'a';
             }
             fieldName.setCharAt(0, c);
             fieldName.insert(0, "set");
             Method method =
                 object.getClass().getMethod(fieldName.toString(), new Class[] {field.getType()});
             if (field.getType() == String.class) method.invoke(object, nodeValue);
             else if (field.getType() == Boolean.TYPE)
               method.invoke(object, StringUtils.strToBool(nodeValue, "true"));
             else if (field.getType() == Byte.TYPE)
               method.invoke(object, Byte.valueOf(nodeValue).byteValue());
             else if (field.getType() == Character.TYPE)
               method.invoke(object, Character.valueOf(nodeValue.charAt(0)));
             else if (field.getType() == Double.TYPE)
               method.invoke(object, Double.valueOf(nodeValue).doubleValue());
             else if (field.getType() == Float.TYPE)
               method.invoke(object, Float.valueOf(nodeValue).floatValue());
             else if (field.getType() == Integer.TYPE)
               method.invoke(object, Integer.valueOf(nodeValue).intValue());
             else if (field.getType() == Long.TYPE)
               method.invoke(object, Long.valueOf(nodeValue).longValue());
             else if (field.getType() == Short.TYPE)
               method.invoke(object, Short.valueOf(nodeValue).shortValue());
           }
         } catch (Exception e) {
           System.err.println(e.getMessage());
         }
       }
     }
   }
   if ((parentBean != null) && (setProperty)) {
     parentBean.setProperty(name, object);
   }
   return object;
 }
  public String getPublicPropertiesForClasses(String classNames[]) {
    StringBuilder result = new StringBuilder();
    String className = null;
    ArrayList publicFields = new ArrayList();
    result.append("<classDefinitions>");
    if (classNames != null && classNames.length > 0) {
      for (int i = 0; i < classNames.length; i++) {
        className = classNames[i];
        if (className != null) {
          result.append("<classDefinition>");
          try {
            Class c = Class.forName(className);
            Field fields[] = c.getFields();
            Field field = null;
            if (fields != null) {
              for (int k = 0; k < fields.length; k++) {
                field = fields[k];
                if (field != null)
                  publicFields.add(
                      (new StringBuilder(String.valueOf(field.getName())))
                          .append(",")
                          .append(field.getType().getName())
                          .toString());
              }
            }
            try {
              BeanInfo b = Introspector.getBeanInfo(c);
              result.append(
                  (new StringBuilder("<classSimpleName>"))
                      .append(c.getSimpleName())
                      .append("</classSimpleName>")
                      .toString());
              result.append(
                  (new StringBuilder("<classFullName>"))
                      .append(c.getName())
                      .append("</classFullName>")
                      .toString());
              Package pack = c.getPackage();
              String packStr = "";
              if (pack != null) packStr = pack.getName();
              result.append(
                  (new StringBuilder("<packageName>"))
                      .append(packStr)
                      .append("</packageName>")
                      .toString());
              PropertyDescriptor pds[] = b.getPropertyDescriptors();
              if (pds != null) {
                for (int propCount = 0; propCount < pds.length; propCount++) {
                  PropertyDescriptor pd = pds[propCount];
                  String propertyName = pd.getName();
                  Method readMethod = pd.getReadMethod();
                  Method writeMethod = pd.getWriteMethod();
                  if (readMethod != null
                      && isPublicAccessor(readMethod.getModifiers())
                      && writeMethod != null
                      && isPublicAccessor(writeMethod.getModifiers()))
                    publicFields.add(
                        (new StringBuilder(String.valueOf(propertyName)))
                            .append(",")
                            .append(pd.getPropertyType().getName())
                            .toString());
                }
              }
            } catch (Exception e) {
              e.printStackTrace();
            }
            if (publicFields != null && publicFields.size() > 0) {
              String temp = null;
              result.append("<publicFields>");
              for (int counter = 0; counter < publicFields.size(); counter++) {
                temp = (String) publicFields.get(counter);
                if (temp != null) {
                  String pubTemp[] = temp.split(",");
                  if (pubTemp.length == 2) {
                    result.append("<publicField>");
                    result.append(
                        (new StringBuilder("<publicFieldName>"))
                            .append(pubTemp[0])
                            .append("</publicFieldName>")
                            .toString());
                    result.append(
                        (new StringBuilder("<publicFieldType>"))
                            .append(pubTemp[1])
                            .append("</publicFieldType>")
                            .toString());
                    result.append("</publicField>");
                  }
                }
              }

              result.append("</publicFields>");
            }
          } catch (ClassNotFoundException e) {
            result.append(
                (new StringBuilder("<classFullName>"))
                    .append(className)
                    .append("</classFullName>")
                    .toString());
            result.append(
                (new StringBuilder("<error>Problem retrieving "))
                    .append(className)
                    .append(" information</error>")
                    .toString());
            System.out.println(e.getMessage());
          }
          result.append("</classDefinition>");
        }
      }
    }
    result.append("</classDefinitions>");
    return result.toString();
  }