Search in sources :

Example 6 with XMLOutputFactory

use of javax.xml.stream.XMLOutputFactory in project spring-framework by spring-projects.

the class StaxUtilsTests method isStaxResultJaxp14.

@Test
public void isStaxResultJaxp14() throws Exception {
    XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
    XMLStreamWriter streamWriter = outputFactory.createXMLStreamWriter(new StringWriter());
    StAXResult result = new StAXResult(streamWriter);
    assertTrue("Not a StAX Result", StaxUtils.isStaxResult(result));
}
Also used : StAXResult(javax.xml.transform.stax.StAXResult) XMLOutputFactory(javax.xml.stream.XMLOutputFactory) StringWriter(java.io.StringWriter) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) Test(org.junit.Test)

Example 7 with XMLOutputFactory

use of javax.xml.stream.XMLOutputFactory in project spring-framework by spring-projects.

the class StaxEventHandlerTests method createStaxHandler.

@Override
protected AbstractStaxHandler createStaxHandler(Result result) throws XMLStreamException {
    XMLOutputFactory outputFactory = XMLOutputFactory.newFactory();
    XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(result);
    return new StaxEventHandler(eventWriter);
}
Also used : XMLOutputFactory(javax.xml.stream.XMLOutputFactory) XMLEventWriter(javax.xml.stream.XMLEventWriter)

Example 8 with XMLOutputFactory

use of javax.xml.stream.XMLOutputFactory in project spring-framework by spring-projects.

the class AbstractMarshallerTests method marshalStaxResultStreamWriter.

@Test
public void marshalStaxResultStreamWriter() throws Exception {
    XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
    StringWriter writer = new StringWriter();
    XMLStreamWriter streamWriter = outputFactory.createXMLStreamWriter(writer);
    Result result = StaxUtils.createStaxResult(streamWriter);
    marshaller.marshal(flights, result);
    assertThat("Marshaller writes invalid StreamResult", writer.toString(), isSimilarTo(EXPECTED_STRING));
}
Also used : XMLOutputFactory(javax.xml.stream.XMLOutputFactory) StringWriter(java.io.StringWriter) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result) StAXResult(javax.xml.transform.stax.StAXResult) DOMResult(javax.xml.transform.dom.DOMResult) Test(org.junit.Test)

Example 9 with XMLOutputFactory

use of javax.xml.stream.XMLOutputFactory in project spring-framework by spring-projects.

the class XStreamMarshallerTests method marshalStaxResultXMLEventWriter.

@Test
public void marshalStaxResultXMLEventWriter() throws Exception {
    XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
    StringWriter writer = new StringWriter();
    XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(writer);
    Result result = StaxUtils.createStaxResult(eventWriter);
    marshaller.marshal(flight, result);
    assertThat("Marshaller writes invalid StreamResult", writer.toString(), isSimilarTo(EXPECTED_STRING));
}
Also used : XMLOutputFactory(javax.xml.stream.XMLOutputFactory) StringWriter(java.io.StringWriter) XMLEventWriter(javax.xml.stream.XMLEventWriter) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result) SAXResult(javax.xml.transform.sax.SAXResult) DOMResult(javax.xml.transform.dom.DOMResult) Test(org.junit.Test)

Example 10 with XMLOutputFactory

use of javax.xml.stream.XMLOutputFactory in project blueprints by tinkerpop.

the class GraphMLWriter method outputGraph.

/**
     * Write the data in a Graph to a GraphML OutputStream.
     *
     * @param graphMLOutputStream the GraphML OutputStream to write the Graph data to
     * @throws IOException thrown if there is an error generating the GraphML data
     */
public void outputGraph(final OutputStream graphMLOutputStream) throws IOException {
    if (null == vertexKeyTypes || null == edgeKeyTypes) {
        Map<String, String> vertexKeyTypes = new HashMap<String, String>();
        Map<String, String> edgeKeyTypes = new HashMap<String, String>();
        for (Vertex vertex : graph.getVertices()) {
            for (String key : vertex.getPropertyKeys()) {
                if (!vertexKeyTypes.containsKey(key)) {
                    vertexKeyTypes.put(key, GraphMLWriter.getStringType(vertex.getProperty(key)));
                }
            }
            for (Edge edge : vertex.getEdges(Direction.OUT)) {
                for (String key : edge.getPropertyKeys()) {
                    if (!edgeKeyTypes.containsKey(key)) {
                        edgeKeyTypes.put(key, GraphMLWriter.getStringType(edge.getProperty(key)));
                    }
                }
            }
        }
        if (null == this.vertexKeyTypes) {
            this.vertexKeyTypes = vertexKeyTypes;
        }
        if (null == this.edgeKeyTypes) {
            this.edgeKeyTypes = edgeKeyTypes;
        }
    }
    // will live with the edge data itself (which won't validate against the graphml schema)
    if (null != this.edgeLabelKey && null != this.edgeKeyTypes && null == this.edgeKeyTypes.get(this.edgeLabelKey))
        this.edgeKeyTypes.put(this.edgeLabelKey, GraphMLTokens.STRING);
    final XMLOutputFactory inputFactory = XMLOutputFactory.newInstance();
    try {
        XMLStreamWriter writer = inputFactory.createXMLStreamWriter(graphMLOutputStream, "UTF8");
        if (normalize) {
            writer = new GraphMLWriterHelper.IndentingXMLStreamWriter(writer);
            ((GraphMLWriterHelper.IndentingXMLStreamWriter) writer).setIndentStep("    ");
        }
        writer.writeStartDocument();
        writer.writeStartElement(GraphMLTokens.GRAPHML);
        writer.writeAttribute(GraphMLTokens.XMLNS, GraphMLTokens.GRAPHML_XMLNS);
        //XML Schema instance namespace definition (xsi)
        writer.writeAttribute(XMLConstants.XMLNS_ATTRIBUTE + ":" + GraphMLTokens.XML_SCHEMA_NAMESPACE_TAG, XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI);
        //XML Schema location
        writer.writeAttribute(GraphMLTokens.XML_SCHEMA_NAMESPACE_TAG + ":" + GraphMLTokens.XML_SCHEMA_LOCATION_ATTRIBUTE, GraphMLTokens.GRAPHML_XMLNS + " " + (this.xmlSchemaLocation == null ? GraphMLTokens.DEFAULT_GRAPHML_SCHEMA_LOCATION : this.xmlSchemaLocation));
        // <key id="weight" for="edge" attr.name="weight" attr.type="float"/>
        Collection<String> keyset;
        if (normalize) {
            keyset = new ArrayList<String>();
            keyset.addAll(vertexKeyTypes.keySet());
            Collections.sort((List<String>) keyset);
        } else {
            keyset = vertexKeyTypes.keySet();
        }
        for (String key : keyset) {
            writer.writeStartElement(GraphMLTokens.KEY);
            writer.writeAttribute(GraphMLTokens.ID, key);
            writer.writeAttribute(GraphMLTokens.FOR, GraphMLTokens.NODE);
            writer.writeAttribute(GraphMLTokens.ATTR_NAME, key);
            writer.writeAttribute(GraphMLTokens.ATTR_TYPE, vertexKeyTypes.get(key));
            writer.writeEndElement();
        }
        if (normalize) {
            keyset = new ArrayList<String>();
            keyset.addAll(edgeKeyTypes.keySet());
            Collections.sort((List<String>) keyset);
        } else {
            keyset = edgeKeyTypes.keySet();
        }
        for (String key : keyset) {
            writer.writeStartElement(GraphMLTokens.KEY);
            writer.writeAttribute(GraphMLTokens.ID, key);
            writer.writeAttribute(GraphMLTokens.FOR, GraphMLTokens.EDGE);
            writer.writeAttribute(GraphMLTokens.ATTR_NAME, key);
            writer.writeAttribute(GraphMLTokens.ATTR_TYPE, edgeKeyTypes.get(key));
            writer.writeEndElement();
        }
        writer.writeStartElement(GraphMLTokens.GRAPH);
        writer.writeAttribute(GraphMLTokens.ID, GraphMLTokens.G);
        writer.writeAttribute(GraphMLTokens.EDGEDEFAULT, GraphMLTokens.DIRECTED);
        Iterable<Vertex> vertices;
        if (normalize) {
            vertices = new ArrayList<Vertex>();
            for (Vertex v : graph.getVertices()) {
                ((Collection<Vertex>) vertices).add(v);
            }
            Collections.sort((List<Vertex>) vertices, new LexicographicalElementComparator());
        } else {
            vertices = graph.getVertices();
        }
        for (Vertex vertex : vertices) {
            writer.writeStartElement(GraphMLTokens.NODE);
            writer.writeAttribute(GraphMLTokens.ID, vertex.getId().toString());
            Collection<String> keys;
            if (normalize) {
                keys = new ArrayList<String>();
                keys.addAll(vertex.getPropertyKeys());
                Collections.sort((List<String>) keys);
            } else {
                keys = vertex.getPropertyKeys();
            }
            for (String key : keys) {
                writer.writeStartElement(GraphMLTokens.DATA);
                writer.writeAttribute(GraphMLTokens.KEY, key);
                Object value = vertex.getProperty(key);
                if (null != value) {
                    writer.writeCharacters(value.toString());
                }
                writer.writeEndElement();
            }
            writer.writeEndElement();
        }
        if (normalize) {
            List<Edge> edges = new ArrayList<Edge>();
            for (Vertex vertex : graph.getVertices()) {
                for (Edge edge : vertex.getEdges(Direction.OUT)) {
                    edges.add(edge);
                }
            }
            Collections.sort(edges, new LexicographicalElementComparator());
            for (Edge edge : edges) {
                writer.writeStartElement(GraphMLTokens.EDGE);
                writer.writeAttribute(GraphMLTokens.ID, edge.getId().toString());
                writer.writeAttribute(GraphMLTokens.SOURCE, edge.getVertex(Direction.OUT).getId().toString());
                writer.writeAttribute(GraphMLTokens.TARGET, edge.getVertex(Direction.IN).getId().toString());
                if (this.edgeLabelKey == null) {
                    // this will not comply with the graphml schema but is here so that the label is not
                    // mixed up with properties.
                    writer.writeAttribute(GraphMLTokens.LABEL, edge.getLabel());
                } else {
                    writer.writeStartElement(GraphMLTokens.DATA);
                    writer.writeAttribute(GraphMLTokens.KEY, this.edgeLabelKey);
                    writer.writeCharacters(edge.getLabel());
                    writer.writeEndElement();
                }
                final List<String> keys = new ArrayList<String>();
                keys.addAll(edge.getPropertyKeys());
                Collections.sort(keys);
                for (String key : keys) {
                    writer.writeStartElement(GraphMLTokens.DATA);
                    writer.writeAttribute(GraphMLTokens.KEY, key);
                    Object value = edge.getProperty(key);
                    if (null != value) {
                        writer.writeCharacters(value.toString());
                    }
                    writer.writeEndElement();
                }
                writer.writeEndElement();
            }
        } else {
            for (Vertex vertex : graph.getVertices()) {
                for (Edge edge : vertex.getEdges(Direction.OUT)) {
                    writer.writeStartElement(GraphMLTokens.EDGE);
                    writer.writeAttribute(GraphMLTokens.ID, edge.getId().toString());
                    writer.writeAttribute(GraphMLTokens.SOURCE, edge.getVertex(Direction.OUT).getId().toString());
                    writer.writeAttribute(GraphMLTokens.TARGET, edge.getVertex(Direction.IN).getId().toString());
                    writer.writeAttribute(GraphMLTokens.LABEL, edge.getLabel());
                    for (String key : edge.getPropertyKeys()) {
                        writer.writeStartElement(GraphMLTokens.DATA);
                        writer.writeAttribute(GraphMLTokens.KEY, key);
                        Object value = edge.getProperty(key);
                        if (null != value) {
                            writer.writeCharacters(value.toString());
                        }
                        writer.writeEndElement();
                    }
                    writer.writeEndElement();
                }
            }
        }
        // graph
        writer.writeEndElement();
        // graphml
        writer.writeEndElement();
        writer.writeEndDocument();
        writer.flush();
        writer.close();
    } catch (XMLStreamException xse) {
        throw new IOException(xse);
    }
}
Also used : Vertex(com.tinkerpop.blueprints.Vertex) XMLOutputFactory(javax.xml.stream.XMLOutputFactory) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) IOException(java.io.IOException) XMLStreamException(javax.xml.stream.XMLStreamException) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) Collection(java.util.Collection) LexicographicalElementComparator(com.tinkerpop.blueprints.util.io.LexicographicalElementComparator) Edge(com.tinkerpop.blueprints.Edge)

Aggregations

XMLOutputFactory (javax.xml.stream.XMLOutputFactory)42 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)28 StringWriter (java.io.StringWriter)18 XMLStreamException (javax.xml.stream.XMLStreamException)15 XMLEventWriter (javax.xml.stream.XMLEventWriter)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)8 Test (org.junit.Test)8 StAXResult (javax.xml.transform.stax.StAXResult)7 IOException (java.io.IOException)6 DOMResult (javax.xml.transform.dom.DOMResult)6 StreamResult (javax.xml.transform.stream.StreamResult)6 Result (javax.xml.transform.Result)5 PcrManifest (com.intel.mountwilson.manifest.data.PcrManifest)3 OutputStream (java.io.OutputStream)3 SAXResult (javax.xml.transform.sax.SAXResult)3 IManifest (com.intel.mountwilson.manifest.data.IManifest)2 Headers (com.sun.net.httpserver.Headers)2 OutputStreamWriter (java.io.OutputStreamWriter)2 InetSocketAddress (java.net.InetSocketAddress)2 Path (java.nio.file.Path)2