Example usage for javax.xml.stream XMLOutputFactory newInstance

List of usage examples for javax.xml.stream XMLOutputFactory newInstance

Introduction

In this page you can find the example usage for javax.xml.stream XMLOutputFactory newInstance.

Prototype

public static XMLOutputFactory newInstance() throws FactoryConfigurationError 

Source Link

Document

Creates a new instance of the factory in exactly the same manner as the #newFactory() method.

Usage

From source file:org.intermine.api.profile.ProfileManagerTest.java

public void testXMLWrite() throws Exception {
    setUpUserProfiles();// www  .  ja  v a2s.c om
    XMLUnit.setIgnoreWhitespace(true);
    StringWriter sw = new StringWriter();
    XMLOutputFactory factory = XMLOutputFactory.newInstance();
    TagManager tagManager = im.getTagManager();

    tagManager.addTag("test-tag", "Department.company", "reference", "bob");
    tagManager.addTag("test-tag2", "Department.name", "attribute", "bob");
    tagManager.addTag("test-tag2", "Department.company", "reference", "bob");
    tagManager.addTag("test-tag2", "Department.employees", "collection", "bob");

    tagManager.addTag("test-tag", "Department.company", "reference", "sally");

    try {
        XMLStreamWriter writer = factory.createXMLStreamWriter(sw);
        writer.writeStartElement("userprofiles");
        ProfileBinding.marshal(bobProfile, os, writer, PathQuery.USERPROFILE_VERSION, classKeys);
        ProfileBinding.marshal(sallyProfile, os, writer, PathQuery.USERPROFILE_VERSION, classKeys);
        writer.writeEndElement();
    } catch (XMLStreamException e) {
        throw new RuntimeException(e);
    }

    InputStream is = getClass().getClassLoader().getResourceAsStream("ProfileManagerBindingTest.xml");
    String expectedXml = IOUtils.toString(is);

    String actualXml = sw.toString().trim();

    assertEquals(normalise(expectedXml), normalise(actualXml));
}

From source file:org.intermine.api.xml.TagBindingTest.java

public void testMarshal() throws Exception {
    XMLUnit.setIgnoreWhitespace(true);/* ww w.  ja v  a 2s  . co m*/
    StringWriter sw = new StringWriter();
    XMLOutputFactory factory = XMLOutputFactory.newInstance();
    try {
        XMLStreamWriter writer = factory.createXMLStreamWriter(sw);
        writer.writeStartElement("tags");
        List<Tag> tags = getTags();
        for (Tag tag : tags) {
            TagBinding.marshal(tag, writer);
        }
        writer.writeEndElement();
        writer.close();
    } catch (XMLStreamException e) {
        throw new RuntimeException(e);
    }

    InputStream is = getClass().getClassLoader().getResourceAsStream("TagBindingTest.xml");
    String expectedXml = IOUtils.toString(is);

    String actualXml = sw.toString().trim();
    System.out.println(normalise(actualXml));
    assertEquals("actual and expected XML should be the same", normalise(expectedXml), normalise(actualXml));
}

From source file:org.intermine.pathquery.PathQuery.java

/**
 * Convert a PathQuery to XML./*from   w  w w  .  j av  a  2 s. com*/
 *
 * @param version the version number of the XML format
 * @return this template query as XML.
 */
public synchronized String toXml(int version) {
    StringWriter sw = new StringWriter();
    XMLOutputFactory factory = XMLOutputFactory.newInstance();

    try {
        XMLStreamWriter writer = factory.createXMLStreamWriter(sw);
        PathQueryBinding.marshal(this, "query", model.getName(), writer, version);
    } catch (XMLStreamException e) {
        throw new RuntimeException(e);
    }

    return sw.toString();
}

From source file:org.intermine.pathquery.PathQueryBinding.java

/**
 * Convert a PathQuery to XML.//from   ww w  .  j  a  v  a  2 s .com
 *
 * @param query the PathQuery
 * @param queryName the name of the query
 * @param modelName the model name
 * @param version the version number of the xml format, an attribute of the ProfileManager
 * @return the corresponding XML String
 */
public static String marshal(PathQuery query, String queryName, String modelName, int version) {
    StringWriter sw = new StringWriter();
    XMLOutputFactory factory = XMLOutputFactory.newInstance();

    try {
        XMLStreamWriter writer = factory.createXMLStreamWriter(sw);
        marshal(query, queryName, modelName, writer, version);
    } catch (XMLStreamException e) {
        throw new RuntimeException(e);
    }

    return sw.toString();
}

From source file:org.intermine.template.TemplateQuery.java

/**
 * Convert a template query to XML.//from  w ww .j  a  v  a 2s  . c o m
 *
 * @param version the version number of the XML format
 * @return this template query as XML.
 */
@Override
public synchronized String toXml(int version) {
    StringWriter sw = new StringWriter();
    XMLOutputFactory factory = XMLOutputFactory.newInstance();

    try {
        XMLStreamWriter writer = factory.createXMLStreamWriter(sw);
        TemplateQueryBinding.marshal(this, writer, version);
    } catch (XMLStreamException e) {
        throw new RuntimeException(e);
    }

    return sw.toString();
}

From source file:org.intermine.template.xml.TemplateQueryBinding.java

/**
 * Convert a TemplateQuery to XML/*from ww  w  .  java 2 s  .  c o m*/
 *
 * @param template the TemplateQuery
 * @return the corresponding XML String
 * @param version the version number of the XML format
 */
public static String marshal(TemplateQuery template, int version) {
    StringWriter sw = new StringWriter();
    XMLOutputFactory factory = XMLOutputFactory.newInstance();
    try {
        XMLStreamWriter writer = factory.createXMLStreamWriter(sw);
        marshal(template, writer, version);
    } catch (XMLStreamException e) {
        throw new RuntimeException(e);
    }

    return sw.toString();
}

From source file:org.intermine.web.logic.template.TemplateHelper.java

/**
 * Given a Map of TemplateQueries (mapping from template name to TemplateQuery)
 * return a string containing each template seriaised as XML. The root element
 * will be a <code>template-queries</code> element.
 *
 * @param templates  map from template name to TemplateQuery
 * @param version the version number of the XML format
 * @return  all template queries serialised as XML
 * @see  TemplateQuery/*from   w  w  w.j  av a  2  s .com*/
 */
public static String templateMapToXml(Map<String, TemplateQuery> templates, int version) {
    StringWriter sw = new StringWriter();
    XMLOutputFactory factory = XMLOutputFactory.newInstance();

    try {
        XMLStreamWriter writer = factory.createXMLStreamWriter(sw);
        writer.writeStartElement("template-queries");
        for (TemplateQuery template : templates.values()) {
            TemplateQueryBinding.marshal(template, writer, version);
        }
        writer.writeEndElement();
    } catch (XMLStreamException e) {
        throw new RuntimeException(e);
    }
    return sw.toString();
}

From source file:org.mcisb.subliminal.SubliminalUtils.java

/**
 * //from  w  ww . ja va 2s  .c  o  m
 * @param elementName
 * @param is
 * @param onlyValues
 * @return Collection
 * @throws XMLStreamException
 * @throws UnsupportedEncodingException
 */
private static Collection<String> getElements(final String elementName, final InputStream is,
        final boolean onlyValues) throws XMLStreamException, UnsupportedEncodingException {
    final Collection<String> elements = new ArrayList<>();
    final ByteArrayOutputStream os = new ByteArrayOutputStream();
    final XMLEventReader reader = XMLInputFactory.newInstance()
            .createXMLEventReader(new InputStreamReader(is, Charset.defaultCharset().name()));
    final XMLEventWriter writer = XMLOutputFactory.newInstance()
            .createXMLEventWriter(new OutputStreamWriter(os, Charset.defaultCharset().name()));
    boolean read = false;
    String characters = null;

    while (reader.peek() != null) {
        final XMLEvent event = (XMLEvent) reader.next();

        switch (event.getEventType()) {
        case XMLStreamConstants.START_DOCUMENT:
        case XMLStreamConstants.END_DOCUMENT: {
            // Ignore.
            break;
        }
        case XMLStreamConstants.START_ELEMENT: {
            read = read || elementName.equals(event.asStartElement().getName().getLocalPart());

            if (read && !onlyValues) {
                writer.add(event);
            }

            break;
        }
        case XMLStreamConstants.ATTRIBUTE: {
            if (read && !onlyValues) {
                writer.add(event);
            }
            break;
        }
        case XMLStreamConstants.CHARACTERS: {
            if (read && !onlyValues) {
                writer.add(event);
            }
            characters = event.asCharacters().getData();
            break;
        }
        case XMLStreamConstants.END_ELEMENT: {
            if (read && !onlyValues) {
                writer.add(event);
            }
            if (elementName.equals(event.asEndElement().getName().getLocalPart())) {
                writer.flush();

                if (characters != null) {
                    elements.add(characters);
                }

                os.reset();
                read = false;
            }
            break;
        }
        default: {
            // Ignore
            break;
        }
        }
    }

    return elements;
}

From source file:org.mule.module.xml.transformer.AbstractXmlTransformer.java

public AbstractXmlTransformer() {
    registerSourceType(DataTypeFactory.STRING);
    registerSourceType(DataTypeFactory.BYTE_ARRAY);
    registerSourceType(DataTypeFactory.create(javax.xml.transform.Source.class));
    registerSourceType(DataTypeFactory.create(org.xml.sax.InputSource.class));
    registerSourceType(DataTypeFactory.create(org.dom4j.Node.class));
    registerSourceType(DataTypeFactory.create(org.dom4j.Document.class));
    registerSourceType(DataTypeFactory.create(org.w3c.dom.Document.class));
    registerSourceType(DataTypeFactory.create(org.w3c.dom.Element.class));
    registerSourceType(DataTypeFactory.create(java.io.InputStream.class));
    registerSourceType(DataTypeFactory.create(org.mule.api.transport.OutputHandler.class));
    registerSourceType(DataTypeFactory.create(javax.xml.stream.XMLStreamReader.class));
    registerSourceType(DataTypeFactory.create(org.mule.module.xml.transformer.DelayedResult.class));
    setReturnDataType(DataTypeFactory.BYTE_ARRAY);

    xmlInputFactory = XMLInputFactory.newInstance();
    xmlOutputFactory = XMLOutputFactory.newInstance();
}

From source file:org.mule.transport.sap.transformer.JcoFunctionToXmlTransformer.java

/**
 * Describe <code>transform</code> method here.
 *
 * @param obj an <code>Object</code> value
 * @return an <code>Object</code> value
 * @exception XMLStreamException if an error occurs
 *//*from  ww  w  .ja va  2s  .  co  m*/
public Object transform(Object obj, String encoding) throws XMLStreamException {
    // getting JCoFunction object
    JCoFunction function = (JCoFunction) obj;

    factory = XMLOutputFactory.newInstance();
    eventFactory = XMLEventFactory.newInstance();

    StringWriter stringWriter = new StringWriter();

    String result = null;

    try {
        // getting instance of writer
        writer = factory.createXMLEventWriter(stringWriter);

        // writing start document

        writeStartDocument(writer);

        // writing JCO start element

        writeCharacters(writer, "\n");
        writeStartElement(writer, MessageConstants.JCO);

        // writing NAME
        writeAttribute(writer, MessageConstants.JCO_ATTR_NAME, function.getName());

        // writing TIMESTAMP
        writeAttribute(writer, MessageConstants.JCO_ATTR_TIMESTAMP, String.valueOf(new Date().getTime()));

        // writing VERSION
        writeAttribute(writer, MessageConstants.JCO_ATTR_VERSION, MessageConstants.JCO_ATTR_VERSION_VALUE);

        // writing Import

        writeCharacters(writer, "\n");
        writeImportElement(writer, function);

        // writing Export
        writeCharacters(writer, "\n  ");
        writeExportElement(writer, function);

        // writing Tables

        writeCharacters(writer, "\n");
        writeTablesElement(writer, function);

        // writing Errors

        writeCharacters(writer, "\n  ");

        writeErrorElement(writer, function.getExceptionList());

        // writing JCO end element
        writeEndElement(writer, MessageConstants.JCO);

        // close document

        writeEndDocument(writer);
        writer.flush();

        result = stringWriter.toString();

    } catch (XMLStreamException ex) {
        throw ex;
    } finally {
        // close
        if (writer != null) {
            try {
                writer.close();
            } catch (XMLStreamException ex) {
            }
        }
        if (stringWriter != null) {
            try {
                stringWriter.close();
            } catch (IOException ex) {
            }
        }
    }
    return result;
}