Exemplo n.º 1
0
  // -----------------------------------------------------------------------------
  @Override
  public String serializeXML(SerializableDO p_object, String p_namespace) throws CoreException {

    StringBuilder l_buf = new StringBuilder();
    DOFactory._DOMetaData l_do_md = null;
    //		SerializeXML			l_xml	= null;
    String l_namespace = null;

    try {
      if (p_object != null) {
        l_namespace =
            ((p_namespace == null) || (p_namespace.isEmpty()))
                ? ("http://www.inventecsolutions.com/"
                    + CoreUtils.truncateClassName(p_object.getClass().getName())
                    + "/")
                : p_namespace;

        l_do_md = DOFactory.getDOMetaData(p_object);

        addStartNode(l_buf, l_do_md.shortName, l_do_md.classRef, l_namespace);
        //				l_buf.append ("<").append (l_do_md.shortName);
        //				l_xml	= p_object.getClass().getAnnotation(SerializeXML.class);
        //				if (l_xml != null) {
        //					if (l_xml.cName()) {
        //						l_buf.append (" name=\"");
        //						l_buf.append (l_do_md.className);
        //						l_buf.append ("\" ");
        //					}
        //				}
        //				l_buf.append (">");
        for (int l_i = 0; l_i < l_do_md.fields.length; l_i++) {
          // System.out.println ("S[" + l_do_md.fields [l_i] + "]");
          serializeObject(
              l_buf,
              l_do_md.fields[l_i],
              CoreUtils.getFieldValue(
                  l_do_md.fields[l_i], p_object) // l_do_md.allFields[l_i].get (p_object)
              );
        }
        addEndNode(l_buf, l_do_md.shortName);
        //				l_buf.append ("</").append (l_do_md.shortName).append (">");
      }
    } catch (Throwable e) {
      e.printStackTrace();
      throw new CoreException("", e);
    }
    return l_buf.toString();
  }
Exemplo n.º 2
0
  // -------------------------------------------------------------------------
  private void serializeObject(StringBuilder p_buf, Field p_field, Object p_obj) throws Exception {

    String l_name = null;
    Field[] l_flds = null;
    int l_mod = 0;
    String[] l_args = null;
    SerializeXML l_ann = null;
    boolean l_substitute = false;

    if (p_field == null) {
      return;
    }
    l_ann = p_field.getAnnotation(SerializeXML.class);
    //            System.out.println ("Field [" + p_field.getName() + "] Ann [" + l_ann + "] ");

    l_mod = p_field.getModifiers();
    if (Modifier.isTransient(l_mod)) {
      return;
    }
    l_name = CoreUtils.truncateClassName(p_field.getName());
    if (l_ann != null) {
      if ((l_ann.xmlTag() != null) && (l_ann.xmlTag().length() > 0)) {
        l_name = l_ann.xmlTag();
      }
      //                l_substitute    = l_ann.substitute();
    }

    if (p_field.getType().isArray()) {
      if (SerializableDO.class.isAssignableFrom(p_field.getType().getComponentType())) {
        if (p_obj != null) {
          addStartNode(p_buf, l_name);
          for (int l_i = 0; l_i < Array.getLength(p_obj); l_i++) {
            if (l_substitute) {
              //                                                        System.out.println ("*****"
              // + p_field.getType ().getComponentType ().getName() + " " + Array.get(p_obj,
              // l_i).getClass().getName());
              addStartNode(
                  p_buf,
                  CoreUtils.truncateClassName(Array.get(p_obj, l_i).getClass().getName()),
                  Array.get(p_obj, l_i).getClass(),
                  null);
            } else {
              addStartNode(
                  p_buf,
                  CoreUtils.truncateClassName(p_field.getType().getComponentType().getName()),
                  p_field.getType().getComponentType(),
                  null);
            }
            l_flds = p_field.getType().getComponentType().getDeclaredFields();
            for (int l_j = 0; l_j < l_flds.length; l_j++) {
              //                                                        System.err.println ("FIELD "
              // + l_flds [l_j]);
              if (Array.get(p_obj, l_i) != null) {
                serializeObject(
                    p_buf,
                    l_flds[l_j],
                    CoreUtils.getFieldValue(
                        l_flds[l_j],
                        (SerializableDO)
                            Array.get(p_obj, l_i)) // l_flds [l_j].get (Array.get(p_obj, l_i))
                    );
              }
            }
            if (l_substitute) {
              addEndNode(
                  p_buf, CoreUtils.truncateClassName(Array.get(p_obj, l_i).getClass().getName()));

            } else {
              addEndNode(
                  p_buf,
                  CoreUtils.truncateClassName(p_field.getType().getComponentType().getName()));
            }
          }
          addEndNode(p_buf, l_name);
        }
      } else if (CoreUtils.isTypeSupported(p_field.getType().getComponentType().getName())) {
        if (p_obj != null) {
          if (CoreUtils.isTypeByteArray(p_field.getType())) {
            addStartNode(p_buf, l_name);
            p_buf.append(CoreUtils.getStringValue(p_obj));
            addEndNode(p_buf, l_name);

          } else {
            for (int l_i = 0; l_i < Array.getLength(p_obj); l_i++) {
              addStartNode(p_buf, l_name);
              p_buf.append(CoreUtils.getStringValue(Array.get(p_obj, l_i)));
              addEndNode(p_buf, l_name);
            }
          }
        }
      } else {
        l_args = new String[1];
        l_args[0] = p_field.getType().getComponentType().getName();
        throw new CoreException(
            "Exception for [" + p_field.getType().getComponentType().getName() + "]");
      }
    } else {
      if (SerializableDO.class.isAssignableFrom(p_field.getType())) {
        if (p_obj != null) {
          //					if (! l_substitute) {
          //                                            addStartNode(p_buf, l_name);
          //                                        }
          addStartNode(
              p_buf,
              l_name // CoreUtils.truncateClassName(p_obj.getClass ().getName())
              ,
              p_obj.getClass(),
              null);
          l_flds = p_obj.getClass().getDeclaredFields();
          for (int l_i = 0; l_i < l_flds.length; l_i++) {
            serializeObject(
                p_buf, l_flds[l_i], CoreUtils.getFieldValue(l_flds[l_i], (SerializableDO) p_obj));
          }
          addEndNode(
              p_buf, l_name // CoreUtils.truncateClassName(p_obj.getClass ().getName())
              );
          //					if (! l_substitute) {
          //                                            addEndNode(p_buf, l_name);
          //                                        }
        }
      } else if (CoreUtils.isTypeSupported(p_field.getType().getName())) {

        //                                        l_ann   =
        // p_field.getAnnotation(SerializeXML.class);
        //                                        System.out.println ("Field 4 [" +
        // p_field.getName() + "] Ann [" + l_ann + "] ");
        addStartNode(p_buf, l_name);
        p_buf.append(CoreUtils.getStringValue(p_obj, !Modifier.isVolatile(l_mod)));
        addEndNode(p_buf, l_name);
      } else {
        l_args = new String[1];
        l_args[0] = p_field.getType().getName();
        System.out.println(l_args[0]);
        throw new CoreException(MessageCodes.ECOD_CANNOT_SERIALIZE_FIELD, l_args);
      }
    }
  }