Пример #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();
  }
Пример #2
0
  // -------------------------------------------------------------------------
  protected void unmarshall(
      Class<?> p_class, SerializableDO p_parent, Element p_elem, XMLParser p_parser)
      throws Exception {

    Node l_node;
    NodeList l_list;
    int l_nbr = 0;
    Object l_dto;
    int l_occ;
    String[] l_args;
    HashSet<String> l_xml_fields = new HashSet<>();
    Field[] l_flds;
    String l_fld_node_name;
    SerializeXML l_xmlann;

    l_list = p_elem.getChildNodes();
    if ((l_list == null) || ((l_nbr = l_list.getLength()) == 0)) {
      return;
    }
    for (int l_i = 0; l_i < l_nbr; l_i++) {
      l_node = l_list.item(l_i);
      if (!(l_node instanceof Element)) {
        continue;
      }
      l_xml_fields.add(getNodeNameNS(l_node));
    }
    //        System.out.println ("ArrayList Start *** " + l_xml_fields);

    l_flds = p_class.getDeclaredFields();
    for (Field l_fld : l_flds) {
      if (Modifier.isTransient(l_fld.getModifiers())) {
        continue;
      }
      l_xmlann = l_fld.getAnnotation(SerializeXML.class);
      l_fld_node_name = getNodeNameFromField(l_fld);
      l_occ = 0;
      l_node = p_parser.getDirectElement(l_fld_node_name, 0, p_elem);
      //            System.out.println ("Finding Node [" + l_fld_node_name + "] " + l_node + " " +
      // p_elem);
      //            if (l_node == null) {
      //                return;
      //            }
      l_xml_fields.remove(l_fld_node_name);
      if (l_fld.getType().isArray()) {
        if (SerializableDO.class.isAssignableFrom(l_fld.getType().getComponentType())) {
          l_occ =
              p_parser.getDirectNbrElements(
                  CoreUtils.truncateClassName(l_fld.getType().getComponentType().getName()),
                  (Element) l_node);
          if (l_occ > 0) {
            l_dto = Array.newInstance(l_fld.getType().getComponentType(), l_occ);
            CoreUtils.setFieldValue(l_fld, p_parent, l_dto);
            for (int l_j = 0; l_j < l_occ; l_j++) {
              Array.set(l_dto, l_j, l_fld.getType().getComponentType().newInstance());
              System.out.println("Locating... " + l_fld);
              unmarshall(
                  l_fld.getType().getComponentType(),
                  (SerializableDO) Array.get(l_dto, l_j),
                  p_parser.getDirectElement(
                      CoreUtils.truncateClassName(l_fld.getType().getComponentType().getName()),
                      l_j,
                      (Element) l_node),
                  p_parser);
            }
          }
        } else if (CoreUtils.isTypeSupported(l_fld.getType().getComponentType().getName())) {
          l_occ = p_parser.getDirectNbrElements(l_fld_node_name, p_elem);
          if (l_occ > 0) {
            l_dto = Array.newInstance(l_fld.getType().getComponentType(), l_occ);
            CoreUtils.setFieldValue(l_fld, p_parent, l_dto);
            for (int l_j = 0; l_j < l_occ; l_j++) {
              //                        	System.out.println("*** " + l_fld);
              Array.set(
                  l_dto,
                  l_j,
                  CoreUtils.getObjectValue(
                      l_fld.getType().getComponentType(),
                      getElementText(p_parser.getDirectElement(l_fld_node_name, l_j, p_elem))));
            }
          }
        } else {
          l_args = new String[1];
          l_args[0] = l_fld.getType().getName();
          throw new CoreException("");
        }
      } else {
        if (SerializableDO.class.isAssignableFrom(l_fld.getType())) {
          if (l_node != null) {
            if (l_xmlann != null) {
              if (l_xmlann.substitute()) {
                l_dto = getNodeClassFromChild(l_node).newInstance();
                CoreUtils.setFieldValue(l_fld, p_parent, l_dto);
                unmarshall(
                    l_dto.getClass() // l_fld.getType ()
                    ,
                    (SerializableDO) l_dto,
                    (Element) (l_node = getNodeFromChild(l_node)),
                    p_parser);
              } else {
                l_dto = l_fld.getType().newInstance();
                CoreUtils.setFieldValue(l_fld, p_parent, l_dto);
                unmarshall(l_fld.getType(), (SerializableDO) l_dto, (Element) l_node, p_parser);
              }
            } else {
              l_dto = l_fld.getType().newInstance();
              CoreUtils.setFieldValue(l_fld, p_parent, l_dto);
              unmarshall(l_fld.getType(), (SerializableDO) l_dto, (Element) l_node, p_parser);
            }
          }
        } else if (CoreUtils.isTypeSupported(l_fld.getType().getName())) {
          //                	System.out.println ("A " + l_fld + " " + l_node + " " + p_parent);
          if (l_node != null) {
            CoreUtils.setFieldValue(
                l_fld,
                p_parent,
                CoreUtils.getObjectValue(l_fld.getType(), getElementText((Element) l_node)));
          }
        } else {
          l_args = new String[1];
          l_args[0] = l_fld.getType().getName();
          throw new CoreException(MessageCodes.ECOD_FATAL_ERROR);
        }
      }
    }
    if (l_xml_fields.size() > 0) {
      System.out.println("WARNING *** Unable to process fields in XML [" + l_xml_fields + "]");
    }
  }
Пример #3
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);
      }
    }
  }