Ejemplo n.º 1
1
  /**
   * Serialize object to XML and output it to given OutputStream
   *
   * @param object objec to marshal
   * @param outputStream stream to put the resulting XML to
   */
  public void marshal(Object object, OutputStream outputStream) {
    try {
      XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
      outputFactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true);
      ByteArrayOutputStream tempOutput = null;
      XMLStreamWriter writer = null;

      if (autoIndent) {
        tempOutput = new ByteArrayOutputStream();
        writer = outputFactory.createXMLStreamWriter(tempOutput);
      } else {
        writer = outputFactory.createXMLStreamWriter(outputStream);
      }

      //            if (autoIndent) {
      //                writer = new IndentingXMLStreamWriterDecorator(writer);
      //            }

      writerContext.writer = writer;

      writer.writeStartDocument();

      marshalObject(writer, object);

      writer.writeEndDocument();
      writer.close();

      if (autoIndent) {
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        transformer.setOutputProperty("doctype-public", "yes");
        transformer.transform(
            new StreamSource(new ByteArrayInputStream(tempOutput.toByteArray())),
            new StreamResult(outputStream));
      }

    } catch (TransformerException ex) {
      throw new XmlizerException("Error whule indenting XML", ex);
    } catch (XMLStreamException ex) {
      throw new XmlizerException("Error occured while writting", ex);
    } catch (IllegalAccessException ex) {
      throw new XmlizerException("Error while trying to read a property", ex);
    } catch (IllegalArgumentException ex) {
      throw new XmlizerException("Error while trying to read a property", ex);
    } catch (InvocationTargetException ex) {
      throw new XmlizerException("Error while trying to read a property", ex);
    }
  }
Ejemplo n.º 2
0
 private void marshalObject(XMLStreamWriter writer, Object object)
     throws IllegalAccessException, IllegalArgumentException, InvocationTargetException,
         XMLStreamException {
   Class<?> type = object.getClass();
   String name = writerContext.typeNamingStrategy.getTypeName(type);
   writer.writeStartElement(name);
   writerContext.writeXSDPath(type);
   writerContext.writeObject(new Property(name, type), object);
   writer.writeEndElement();
 }
Ejemplo n.º 3
0
  @Override
  public XMLNode write(Identified object, WriterContext context) {
    if (object == null) {
      return null;
    }

    XMLNode node = new XMLNode(DOMTags.ELEMENT_TAG);

    if (object.getId() != null && context.containsId(object.getId())) {
      node.setText(object.getId());
    } else {
      // Set id if necessary
      if (object.getId() == null) {
        object.setId(context.generateNewId());
      }

      object = (Identified) context.process(object, node);
      // If it's a reference
      if (object.getClass() == BasicElement.class) {
        node.setText(object.getId());
      } else {
        node.setAttribute(DOMTags.ID_AT, object.getId());
        ReflectionClass<?> clazz = ReflectionClassLoader.getReflectionClass(object.getClass());
        node.setAttribute(DOMTags.CLASS_AT, context.translateClass(clazz.getType()));
        while (clazz != null) {
          for (ReflectionField f : clazz.getFields()) {
            // Only store fields annotated with param
            if (f.getAnnotation(Param.class) != null) {
              Object value = f.getFieldValue(object);
              if (value != null) {
                modelVisitor.writeElement(
                    value,
                    object,
                    new ObjectWriterListener(context.translateField(f.getName()), node));
              }
            }
          }
          clazz = clazz.getSuperclass();
        }
      }
    }
    // Detect duplicated ids
    Object o = pointers.get(object.getId());
    if (o != null && o != object) {
      if (o.getClass() != BasicElement.class && object.getClass() != BasicElement.class) {
        logger.error("Duplicated id {}", object.getId());
      }
    } else {
      // We don't add references to the pointers list
      if (object.getClass() != BasicElement.class) {
        pointers.put(object.getId(), object);
      }
    }
    return node;
  }
Ejemplo n.º 4
0
 public void setXsdPathRegistry(XSDPathRegistry xsdPathRegistry) {
   writerContext.xsdPathRegistry = xsdPathRegistry;
 }
Ejemplo n.º 5
0
 public void setNamingStrategy(TypeNamingStrategy namingStrategy) {
   writerContext.typeNamingStrategy = namingStrategy;
 }