@Override
  public void writeTo(
      T obj,
      Class<?> type,
      Type genericType,
      Annotation[] anns,
      MediaType m,
      MultivaluedMap<String, Object> headers,
      OutputStream os)
      throws IOException {
    if (type == null) {
      type = obj.getClass();
    }
    if (genericType == null) {
      genericType = type;
    }
    AegisContext context = getAegisContext(type, genericType);
    AegisType aegisType = context.getTypeMapping().getType(genericType);
    AegisWriter<XMLStreamWriter> aegisWriter = context.createXMLStreamWriter();
    try {
      W3CDOMStreamWriter w3cStreamWriter = new W3CDOMStreamWriter();
      XMLStreamWriter spyingWriter =
          new PrefixCollectingXMLStreamWriter(w3cStreamWriter, namespaceMap);
      spyingWriter.writeStartDocument();
      // use type qname as element qname?
      aegisWriter.write(obj, aegisType.getSchemaType(), false, spyingWriter, aegisType);
      spyingWriter.writeEndDocument();
      spyingWriter.close();
      Document dom = w3cStreamWriter.getDocument();
      // ok, now the namespace map has all the prefixes.

      XMLStreamWriter xmlStreamWriter = createStreamWriter(aegisType.getSchemaType(), os);
      xmlStreamWriter.writeStartDocument();
      StaxUtils.copy(dom, xmlStreamWriter);
      // Jettison needs, and StaxUtils.copy doesn't do it.
      xmlStreamWriter.writeEndDocument();
      xmlStreamWriter.flush();
      xmlStreamWriter.close();
    } catch (Exception e) {
      throw new WebApplicationException(e);
    }
  }
Beispiel #2
0
  private void go() throws Exception {
    AegisContext context;

    context = new AegisContext();
    context.setWriteXsiTypes(true);
    Set<java.lang.reflect.Type> rootClasses = new HashSet<java.lang.reflect.Type>();
    rootClasses.add(Zoo.class);
    context.setRootClasses(rootClasses);
    context.initialize();
    AegisWriter<XMLStreamWriter> writer = context.createXMLStreamWriter();
    FileOutputStream output = new FileOutputStream(outputPathname);
    XMLStreamWriter xmlWriter = outputFactory.createXMLStreamWriter(output);
    IndentingXMLStreamWriter indentWriter = new IndentingXMLStreamWriter(xmlWriter);

    Zoo zoo = populateZoo();
    AegisType aegisType = context.getTypeMapping().getType(zoo.getClass());
    writer.write(zoo, new QName("urn:aegis:demo", "zoo"), false, indentWriter, aegisType);
    xmlWriter.close();
    output.close();
  }