Exemple #1
0
 // Serialize the bean using the specified namespace prefix & uri
 public String serialize(Object bean) throws IntrospectionException, IllegalAccessException {
   // Use the class name as the name of the root element
   String className = bean.getClass().getName();
   String rootElementName = null;
   if (bean.getClass().isAnnotationPresent(ObjectXmlAlias.class)) {
     AnnotatedElement annotatedElement = bean.getClass();
     ObjectXmlAlias aliasAnnotation = annotatedElement.getAnnotation(ObjectXmlAlias.class);
     rootElementName = aliasAnnotation.value();
   }
   // Use the package name as the namespace URI
   Package pkg = bean.getClass().getPackage();
   nsURI = pkg.getName();
   // Remove a trailing semi-colon (;) if present (i.e. if the bean is an array)
   className = StringUtils.deleteTrailingChar(className, ';');
   StringBuffer sb = new StringBuffer(className);
   String objectName = sb.delete(0, sb.lastIndexOf(".") + 1).toString();
   domDocument = createDomDocument(objectName);
   document = domDocument.getDocument();
   Element root = document.getDocumentElement();
   // Parse the bean elements
   getBeanElements(root, rootElementName, className, bean);
   StringBuffer xml = new StringBuffer();
   if (prettyPrint)
     xml.append(domDocument.serialize(lineSeperator, indentChars, includeXmlProlog));
   else xml.append(domDocument.serialize(includeXmlProlog));
   if (!includeTypeInfo) {
     int index = xml.indexOf(root.getNodeName());
     xml.delete(index - 1, index + root.getNodeName().length() + 2);
     xml.delete(xml.length() - root.getNodeName().length() - 4, xml.length());
   }
   return xml.toString();
 }
 public String getPublicMethodsForClasses(String classNames[]) {
   StringBuilder result = new StringBuilder();
   String className = null;
   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);
           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());
           Method methods[] = c.getMethods();
           Method method = null;
           result.append("<methods>");
           if (methods != null) {
             for (int j = 0; j < methods.length; j++) {
               method = methods[j];
               if (method != null && !methodsExclude.contains(method.getName())) {
                 result.append("<method>");
                 result.append(
                     (new StringBuilder("<methodSignature>"))
                         .append(method.toString())
                         .append("</methodSignature>")
                         .toString());
                 result.append(
                     (new StringBuilder("<methodName>"))
                         .append(method.getName())
                         .append("</methodName>")
                         .toString());
                 result.append(
                     (new StringBuilder("<returnType>"))
                         .append(method.getReturnType().getName())
                         .append("</returnType>")
                         .toString());
                 Class paramClasses[] = method.getParameterTypes();
                 result.append("<params>");
                 if (paramClasses != null) {
                   for (int l = 0; l < paramClasses.length; l++)
                     if (paramClasses[l] != null)
                       result.append(
                           (new StringBuilder("<param>"))
                               .append(paramClasses[l].getName())
                               .append("</param>")
                               .toString());
                 }
                 result.append("</params>");
                 result.append("</method>");
               }
             }
           }
           result.append("</methods>");
         } 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();
 }
  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();
  }