Search in sources :

Example 11 with XMLInputFactory

use of javax.xml.stream.XMLInputFactory in project databus by linkedin.

the class StaxBuilderTest method processXml.

public void processXml() throws Exception {
    try {
        //wrap the stream with fake root
        String xmlStartTag = "<?xml version=\"" + DbusConstants.XML_VERSION + "\" encoding=\"" + DbusConstants.ISO_8859_1 + "\"?><root>";
        String xmlEndTag = "</root>";
        List xmlTagsList = Arrays.asList(new InputStream[] { new ByteArrayInputStream(xmlStartTag.getBytes(DbusConstants.ISO_8859_1)), _fileInputStream, new ByteArrayInputStream(xmlEndTag.getBytes(DbusConstants.ISO_8859_1)) });
        Enumeration<InputStream> streams = Collections.enumeration(xmlTagsList);
        SequenceInputStream seqStream = new SequenceInputStream(streams);
        XMLInputFactory factory = XMLInputFactory.newInstance();
        factory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);
        XMLStreamReader xmlStreamReader = factory.createXMLStreamReader(seqStream);
        //TODO replace _tableToNamespace this by physical sources config
        XmlParser parser = new XmlParser(xmlStreamReader, _schemaRegistry, _tableToNamespace, _tableToSourceId, _transactionSuccessCallBack, true, _staticReplicationConfig, seqStream);
        parser.start();
    } catch (XMLStreamException e) {
        LOG.error("Unable to parse the given xml stream", e);
    }
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) SequenceInputStream(java.io.SequenceInputStream) XMLStreamException(javax.xml.stream.XMLStreamException) ByteArrayInputStream(java.io.ByteArrayInputStream) SequenceInputStream(java.io.SequenceInputStream) FileInputStream(java.io.FileInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) List(java.util.List) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 12 with XMLInputFactory

use of javax.xml.stream.XMLInputFactory in project databus by linkedin.

the class XmlFormatTrailParser method createXmlInputFactory.

private XMLInputFactory createXmlInputFactory(boolean validating) throws FactoryConfigurationError {
    XMLInputFactory result = null;
    Throwable createError = null;
    try {
        @SuppressWarnings("unchecked") Class<XMLInputFactory> woodstoxFactory = (Class<XMLInputFactory>) Class.forName("com.ctc.wstx.stax.WstxInputFactory");
        result = woodstoxFactory.newInstance();
        if (validating) {
            _log.info("found woodstox library: DTD validation will be enabled");
            result.setProperty(XMLInputFactory.IS_VALIDATING, validating);
        }
    } catch (ClassNotFoundException e) {
        createError = e;
    } catch (InstantiationException e) {
        createError = e;
    } catch (IllegalAccessException e) {
        createError = e;
    } catch (RuntimeException e) {
        createError = e;
    }
    if (null != createError) {
        _log.info("unable to find woodstox library, defaulting to Java: " + createError);
        if (validating) {
            _log.warn("default implementation does not support DTD validation");
        }
        result = XMLInputFactory.newInstance();
    }
    return result;
}
Also used : XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 13 with XMLInputFactory

use of javax.xml.stream.XMLInputFactory in project modrun by nanosai.

the class ModuleDependencyReader method readDependencies.

public static List<Dependency> readDependencies(Reader pomReader) {
    List<Dependency> dependencies = new ArrayList<>();
    XMLInputFactory factory = XMLInputFactory.newInstance();
    try {
        XMLStreamReader streamReader = factory.createXMLStreamReader(pomReader);
        while (streamReader.hasNext()) {
            streamReader.next();
            if (streamReader.getEventType() == XMLStreamReader.START_ELEMENT) {
                String elementName = streamReader.getLocalName();
                if (elementName.equals("dependency")) {
                    Dependency dependency = parseDependency(streamReader);
                    dependencies.add(dependency);
                }
            }
        }
    } catch (XMLStreamException e) {
        e.printStackTrace();
    }
    return dependencies;
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) XMLStreamException(javax.xml.stream.XMLStreamException) ArrayList(java.util.ArrayList) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 14 with XMLInputFactory

use of javax.xml.stream.XMLInputFactory in project openhab1-addons by openhab.

the class DenonConnector method getDocument.

private <T> T getDocument(String uri, Class<T> response) {
    try {
        String result = doHttpRequest("GET", uri, null);
        logger.trace("result of getDocument for uri '{}':\r\n{}", uri, result);
        if (StringUtils.isNotBlank(result)) {
            JAXBContext jc = JAXBContext.newInstance(response);
            XMLInputFactory xif = XMLInputFactory.newInstance();
            XMLStreamReader xsr = xif.createXMLStreamReader(IOUtils.toInputStream(result));
            xsr = new PropertyRenamerDelegate(xsr);
            @SuppressWarnings("unchecked") T obj = (T) jc.createUnmarshaller().unmarshal(xsr);
            return obj;
        }
    } catch (UnmarshalException e) {
        logger.debug("Failed to unmarshal xml document: {}", e.getMessage());
    } catch (JAXBException e) {
        logger.debug("Unexpected error occurred during unmarshalling of document: {}", e.getMessage());
    } catch (XMLStreamException e) {
        logger.debug("Communication error: {}", e.getMessage());
    }
    return null;
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) XMLStreamException(javax.xml.stream.XMLStreamException) UnmarshalException(javax.xml.bind.UnmarshalException) JAXBException(javax.xml.bind.JAXBException) JAXBContext(javax.xml.bind.JAXBContext) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 15 with XMLInputFactory

use of javax.xml.stream.XMLInputFactory in project orientdb by orientechnologies.

the class OGraphMLReader method inputGraph.

/**
   * Input the GraphML stream data into the graph. More control over how data is streamed is provided by this method.
   *
   * @param inputGraph
   *          the graph to populate with the GraphML data
   * @param graphMLInputStream
   *          an InputStream of GraphML data
   * @param bufferSize
   *          the amount of elements to hold in memory before committing a transactions (only valid for TransactionalGraphs)
   * @param vertexIdKey
   *          if the id of a vertex is a &lt;data/&gt; property, fetch it from the data property.
   * @param edgeIdKey
   *          if the id of an edge is a &lt;data/&gt; property, fetch it from the data property.
   * @param edgeLabelKey
   *          if the label of an edge is a &lt;data/&gt; property, fetch it from the data property.
   * @throws IOException
   *           thrown when the GraphML data is not correctly formatted
   */
public OGraphMLReader inputGraph(final Graph inputGraph, final InputStream graphMLInputStream, int bufferSize, String vertexIdKey, String edgeIdKey, String edgeLabelKey) throws IOException {
    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    try {
        XMLStreamReader reader = inputFactory.createXMLStreamReader(graphMLInputStream);
        final OrientBaseGraph graph = (OrientBaseGraph) inputGraph;
        if (storeVertexIds)
            graph.setSaveOriginalIds(storeVertexIds);
        Map<String, String> keyIdMap = new HashMap<String, String>();
        Map<String, String> keyTypesMaps = new HashMap<String, String>();
        // <Mapped ID String, ID Object>
        // <Default ID String, Mapped ID String>
        Map<String, ORID> vertexMappedIdMap = new HashMap<String, ORID>();
        // Buffered Vertex Data
        String vertexId = null;
        Map<String, Object> vertexProps = null;
        boolean inVertex = false;
        // Buffered Edge Data
        String edgeId = null;
        String edgeLabel = null;
        String vertexLabel = null;
        // [0] = outVertex , [1] = inVertex
        Vertex[] edgeEndVertices = null;
        Map<String, Object> edgeProps = null;
        boolean inEdge = false;
        int bufferCounter = 0;
        long importedVertices = 0;
        long importedEdges = 0;
        while (reader.hasNext()) {
            Integer eventType = reader.next();
            if (eventType.equals(XMLEvent.START_ELEMENT)) {
                String elementName = reader.getName().getLocalPart();
                if (elementName.equals(GraphMLTokens.KEY)) {
                    String id = reader.getAttributeValue(null, GraphMLTokens.ID);
                    String attributeName = reader.getAttributeValue(null, GraphMLTokens.ATTR_NAME);
                    String attributeType = reader.getAttributeValue(null, GraphMLTokens.ATTR_TYPE);
                    keyIdMap.put(id, attributeName);
                    keyTypesMaps.put(id, attributeType);
                } else if (elementName.equals(GraphMLTokens.NODE)) {
                    vertexId = reader.getAttributeValue(null, GraphMLTokens.ID);
                    vertexLabel = reader.getAttributeValue(null, LABELS);
                    if (vertexLabel != null) {
                        if (vertexLabel.startsWith(":"))
                            // REMOVE : AS PREFIX
                            vertexLabel = vertexLabel.substring(1);
                        final String[] vertexLabels = vertexLabel.split(":");
                        // GET ONLY FIRST LABEL AS CLASS
                        vertexLabel = vertexId + ",class:" + vertexLabels[vertexLabelIndex];
                    } else
                        vertexLabel = vertexId;
                    inVertex = true;
                    vertexProps = new HashMap<String, Object>();
                } else if (elementName.equals(GraphMLTokens.EDGE)) {
                    edgeId = reader.getAttributeValue(null, GraphMLTokens.ID);
                    edgeLabel = reader.getAttributeValue(null, GraphMLTokens.LABEL);
                    edgeLabel = edgeLabel == null ? GraphMLTokens._DEFAULT : edgeLabel;
                    String[] vertexIds = new String[2];
                    vertexIds[0] = reader.getAttributeValue(null, GraphMLTokens.SOURCE);
                    vertexIds[1] = reader.getAttributeValue(null, GraphMLTokens.TARGET);
                    edgeEndVertices = new Vertex[2];
                    for (int i = 0; i < 2; i++) {
                        // i=0 => outVertex, i=1 => inVertex
                        if (vertexIdKey == null) {
                            edgeEndVertices[i] = null;
                        } else {
                            final Object vId = vertexMappedIdMap.get(vertexIds[i]);
                            edgeEndVertices[i] = vId != null ? graph.getVertex(vId) : null;
                        }
                        if (null == edgeEndVertices[i]) {
                            edgeEndVertices[i] = graph.addVertex(vertexLabel);
                            if (vertexIdKey != null) {
                                mapId(vertexMappedIdMap, vertexIds[i], (ORID) edgeEndVertices[i].getId());
                            }
                            bufferCounter++;
                            importedVertices++;
                            printStatus(reader, importedVertices, importedEdges);
                        }
                    }
                    inEdge = true;
                    vertexLabel = null;
                    edgeProps = new HashMap<String, Object>();
                } else if (elementName.equals(GraphMLTokens.DATA)) {
                    String key = reader.getAttributeValue(null, GraphMLTokens.KEY);
                    String attributeName = keyIdMap.get(key);
                    if (attributeName == null)
                        attributeName = key;
                    String value = reader.getElementText();
                    if (inVertex) {
                        if ((vertexIdKey != null) && (key.equals(vertexIdKey))) {
                            // Should occur at most once per Vertex
                            vertexId = value;
                        } else if (attributeName.equalsIgnoreCase(LABELS)) {
                        // IGNORE LABELS
                        } else {
                            final Object attrValue = typeCastValue(key, value, keyTypesMaps);
                            final OGraphMLImportStrategy strategy = vertexPropsStrategy.get(attributeName);
                            if (strategy != null) {
                                attributeName = strategy.transformAttribute(attributeName, attrValue);
                            }
                            if (attributeName != null)
                                vertexProps.put(attributeName, attrValue);
                        }
                    } else if (inEdge) {
                        if ((edgeLabelKey != null) && (key.equals(edgeLabelKey)))
                            edgeLabel = value;
                        else if ((edgeIdKey != null) && (key.equals(edgeIdKey)))
                            edgeId = value;
                        else {
                            final Object attrValue = typeCastValue(key, value, keyTypesMaps);
                            final OGraphMLImportStrategy strategy = edgePropsStrategy.get(attributeName);
                            if (strategy != null) {
                                attributeName = strategy.transformAttribute(attributeName, attrValue);
                            }
                            if (attributeName != null)
                                edgeProps.put(attributeName, attrValue);
                        }
                    }
                }
            } else if (eventType.equals(XMLEvent.END_ELEMENT)) {
                String elementName = reader.getName().getLocalPart();
                if (elementName.equals(GraphMLTokens.NODE)) {
                    ORID currentVertex = null;
                    if (vertexIdKey != null)
                        currentVertex = vertexMappedIdMap.get(vertexId);
                    if (currentVertex == null) {
                        final OrientVertex v = graph.addVertex(vertexLabel, vertexProps);
                        if (vertexIdKey != null)
                            mapId(vertexMappedIdMap, vertexId, v.getIdentity());
                        bufferCounter++;
                        importedVertices++;
                        printStatus(reader, importedVertices, importedEdges);
                    } else {
                        // UPDATE IT
                        final OrientVertex v = graph.getVertex(currentVertex);
                        v.setProperties(vertexProps);
                    }
                    vertexId = null;
                    vertexLabel = null;
                    vertexProps = null;
                    inVertex = false;
                } else if (elementName.equals(GraphMLTokens.EDGE)) {
                    Edge currentEdge = ((OrientVertex) edgeEndVertices[0]).addEdge(null, (OrientVertex) edgeEndVertices[1], edgeLabel, null, edgeProps);
                    bufferCounter++;
                    importedEdges++;
                    printStatus(reader, importedVertices, importedEdges);
                    edgeId = null;
                    edgeLabel = null;
                    edgeEndVertices = null;
                    edgeProps = null;
                    inEdge = false;
                }
            }
            if (bufferCounter > bufferSize) {
                graph.commit();
                bufferCounter = 0;
            }
        }
        reader.close();
        graph.commit();
    } catch (Exception xse) {
        throw OException.wrapException(new ODatabaseImportException("Error on importing GraphML"), xse);
    }
    return this;
}
Also used : Vertex(com.tinkerpop.blueprints.Vertex) OrientVertex(com.tinkerpop.blueprints.impls.orient.OrientVertex) XMLStreamReader(javax.xml.stream.XMLStreamReader) HashMap(java.util.HashMap) ODatabaseImportException(com.orientechnologies.orient.core.db.tool.ODatabaseImportException) OrientVertex(com.tinkerpop.blueprints.impls.orient.OrientVertex) OrientBaseGraph(com.tinkerpop.blueprints.impls.orient.OrientBaseGraph) ODatabaseImportException(com.orientechnologies.orient.core.db.tool.ODatabaseImportException) OException(com.orientechnologies.common.exception.OException) IOException(java.io.IOException) ORID(com.orientechnologies.orient.core.id.ORID) Edge(com.tinkerpop.blueprints.Edge) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Aggregations

XMLInputFactory (javax.xml.stream.XMLInputFactory)154 XMLStreamReader (javax.xml.stream.XMLStreamReader)98 XMLStreamException (javax.xml.stream.XMLStreamException)63 StringReader (java.io.StringReader)43 InputStream (java.io.InputStream)41 IOException (java.io.IOException)33 XMLEventReader (javax.xml.stream.XMLEventReader)30 Test (org.junit.Test)22 ByteArrayInputStream (java.io.ByteArrayInputStream)19 InputStreamReader (java.io.InputStreamReader)14 JAXBException (javax.xml.bind.JAXBException)14 StAXSource (javax.xml.transform.stax.StAXSource)14 StreamSource (javax.xml.transform.stream.StreamSource)14 Unmarshaller (javax.xml.bind.Unmarshaller)13 ArrayList (java.util.ArrayList)12 XMLEvent (javax.xml.stream.events.XMLEvent)12 DOMSource (javax.xml.transform.dom.DOMSource)11 DeploymentUnitProcessingException (org.jboss.as.server.deployment.DeploymentUnitProcessingException)10 JAXBContext (javax.xml.bind.JAXBContext)9 HashMap (java.util.HashMap)8