Search in sources :

Example 96 with XMLEvent

use of javax.xml.stream.events.XMLEvent in project service-proxy by membrane.

the class WSDLUtil method getXSDs.

public static List<String> getXSDs(String wsdl) throws XMLStreamException {
    List<String> result = new ArrayList<String>();
    XMLEventReader parser;
    synchronized (xmlInputFactory) {
        parser = xmlInputFactory.createXMLEventReader(new StringReader(wsdl));
    }
    while (parser.hasNext()) {
        XMLEvent event = parser.nextEvent();
        if (event.isStartElement()) {
            String name = event.asStartElement().getName().getLocalPart();
            if (name.equals("import") || name.equals("include")) {
                Attribute a = event.asStartElement().getAttributeByName(new QName("schemaLocation"));
                if (a != null)
                    result.add(a.getValue());
            }
        }
    }
    return result;
}
Also used : Attribute(javax.xml.stream.events.Attribute) QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList) StringReader(java.io.StringReader) XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader)

Example 97 with XMLEvent

use of javax.xml.stream.events.XMLEvent in project service-proxy by membrane.

the class XMLProtector method protect.

public boolean protect(InputStreamReader isr) {
    try {
        XMLEventReader parser;
        synchronized (xmlInputFactory) {
            parser = xmlInputFactory.createXMLEventReader(isr);
        }
        while (parser.hasNext()) {
            XMLEvent event = parser.nextEvent();
            if (event.isStartElement()) {
                StartElement startElement = (StartElement) event;
                if (maxElementNameLength != -1)
                    if (startElement.getName().getLocalPart().length() > maxElementNameLength) {
                        log.warn("Element name length: Limit exceeded.");
                        return false;
                    }
                if (maxAttibuteCount != -1) {
                    @SuppressWarnings("rawtypes") Iterator i = startElement.getAttributes();
                    for (int attributeCount = 0; i.hasNext(); i.next()) if (++attributeCount == maxAttibuteCount) {
                        log.warn("Number of attributes per element: Limit exceeded.");
                        return false;
                    }
                }
            }
            if (event instanceof javax.xml.stream.events.DTD) {
                if (removeDTD) {
                    log.debug("removed DTD.");
                    continue;
                }
            }
            writer.add(event);
        }
        writer.flush();
    } catch (XMLStreamException e) {
        log.warn("Received not-wellformed XML.");
        return false;
    }
    return true;
}
Also used : StartElement(javax.xml.stream.events.StartElement) XMLStreamException(javax.xml.stream.XMLStreamException) XMLEvent(javax.xml.stream.events.XMLEvent) Iterator(java.util.Iterator) XMLEventReader(javax.xml.stream.XMLEventReader)

Example 98 with XMLEvent

use of javax.xml.stream.events.XMLEvent in project vespa by vespa-engine.

the class VespaRecordWriter method findDocIdFromXml.

private String findDocIdFromXml(String xml) {
    try {
        XMLEventReader eventReader = XMLInputFactory.newInstance().createXMLEventReader(new StringReader(xml));
        while (eventReader.hasNext()) {
            XMLEvent event = eventReader.nextEvent();
            if (event.getEventType() == XMLEvent.START_ELEMENT) {
                StartElement element = event.asStartElement();
                String elementName = element.getName().getLocalPart();
                if (VespaDocumentOperation.Operation.valid(elementName)) {
                    return element.getAttributeByName(QName.valueOf("documentid")).getValue();
                }
            }
        }
    } catch (XMLStreamException | FactoryConfigurationError e) {
        // as json dude does
        return null;
    }
    return null;
}
Also used : StartElement(javax.xml.stream.events.StartElement) XMLStreamException(javax.xml.stream.XMLStreamException) StringReader(java.io.StringReader) XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader) FactoryConfigurationError(javax.xml.stream.FactoryConfigurationError)

Example 99 with XMLEvent

use of javax.xml.stream.events.XMLEvent in project neo4j-apoc-procedures by neo4j-contrib.

the class XmlGraphMLReader method parseXML.

public long parseXML(Reader input) throws XMLStreamException {
    Map<String, Long> cache = new HashMap<>(1024 * 32);
    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    inputFactory.setProperty("javax.xml.stream.isCoalescing", true);
    XMLEventReader reader = inputFactory.createXMLEventReader(input);
    PropertyContainer last = null;
    Map<String, Key> nodeKeys = new HashMap<>();
    Map<String, Key> relKeys = new HashMap<>();
    int count = 0;
    try (BatchTransaction tx = new BatchTransaction(gdb, batchSize * 10, reporter)) {
        while (reader.hasNext()) {
            XMLEvent event = (XMLEvent) reader.next();
            if (event.isStartElement()) {
                StartElement element = event.asStartElement();
                String name = element.getName().getLocalPart();
                if (name.equals("graphml") || name.equals("graph"))
                    continue;
                if (name.equals("key")) {
                    String id = getAttribute(element, ID);
                    Key key = new Key(id, getAttribute(element, NAME), getAttribute(element, TYPE), getAttribute(element, LIST), getAttribute(element, FOR));
                    XMLEvent next = peek(reader);
                    if (next.isStartElement() && next.asStartElement().getName().getLocalPart().equals("default")) {
                        reader.nextEvent().asStartElement();
                        key.setDefault(reader.nextEvent().asCharacters().getData());
                    }
                    if (key.forNode)
                        nodeKeys.put(id, key);
                    else
                        relKeys.put(id, key);
                    continue;
                }
                if (name.equals("data")) {
                    if (last == null)
                        continue;
                    String id = getAttribute(element, KEY);
                    boolean isNode = last instanceof Node;
                    Key key = isNode ? nodeKeys.get(id) : relKeys.get(id);
                    if (key == null)
                        key = Key.defaultKey(id, isNode);
                    Object value = key.defaultValue;
                    XMLEvent next = peek(reader);
                    if (next.isCharacters()) {
                        value = key.parseValue(reader.nextEvent().asCharacters().getData());
                    }
                    if (value != null) {
                        if (this.labels && isNode && id.equals("labels")) {
                            addLabels((Node) last, value.toString());
                        } else if (!this.labels || isNode || !id.equals("label")) {
                            last.setProperty(key.name, value);
                            if (reporter != null)
                                reporter.update(0, 0, 1);
                        }
                    }
                    continue;
                }
                if (name.equals("node")) {
                    tx.increment();
                    String id = getAttribute(element, ID);
                    Node node = gdb.createNode();
                    if (this.labels) {
                        String labels = getAttribute(element, LABELS);
                        addLabels(node, labels);
                    }
                    if (storeNodeIds)
                        node.setProperty("id", id);
                    setDefaults(nodeKeys, node);
                    last = node;
                    cache.put(id, node.getId());
                    if (reporter != null)
                        reporter.update(1, 0, 0);
                    count++;
                    continue;
                }
                if (name.equals("edge")) {
                    tx.increment();
                    String source = getAttribute(element, SOURCE);
                    String target = getAttribute(element, TARGET);
                    String label = getAttribute(element, LABEL);
                    Node from = gdb.getNodeById(cache.get(source));
                    Node to = gdb.getNodeById(cache.get(target));
                    RelationshipType type = label != null ? RelationshipType.withName(label) : defaultRelType;
                    Relationship relationship = from.createRelationshipTo(to, type);
                    setDefaults(relKeys, relationship);
                    if (reporter != null)
                        reporter.update(0, 1, 0);
                    count++;
                    last = relationship;
                }
            }
        }
    }
    return count;
}
Also used : HashMap(java.util.HashMap) StartElement(javax.xml.stream.events.StartElement) XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader) BatchTransaction(apoc.export.util.BatchTransaction) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 100 with XMLEvent

use of javax.xml.stream.events.XMLEvent in project xsimilarity by iamxiatian.

the class DictStatistic method testFromXml.

/**
 * 从指定的xml文件加载词典文件
 * @param xmlFile
 * @param gzCompressed 是否再用gz格式对词典进行了压缩
 * @return
 */
public void testFromXml(String xmlFile, boolean gzCompressed) {
    File file = new File(xmlFile);
    if (!file.canRead()) {
        System.out.println("无法读取文件:" + xmlFile);
        // fail while opening the file
        return;
    }
    int count = 0, conceptCount = 0;
    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    InputStream input = null;
    try {
        if (gzCompressed) {
            input = new GZIPInputStream(new FileInputStream(file));
        } else {
            input = new FileInputStream(file);
        }
        XMLEventReader xmlEventReader = inputFactory.createXMLEventReader(input);
        while (xmlEventReader.hasNext()) {
            XMLEvent event = xmlEventReader.nextEvent();
            if (event.isStartElement()) {
                StartElement startElement = event.asStartElement();
                if (startElement.getName().toString().equals("table")) {
                    String head = startElement.getAttributeByName(QName.valueOf("head")).getValue();
                    while (xmlEventReader.hasNext()) {
                        XMLEvent itemEvent = xmlEventReader.nextEvent();
                        if (itemEvent.isStartElement()) {
                            StartElement itemStartElement = itemEvent.asStartElement();
                            if (!itemStartElement.getName().toString().equals("item"))
                                continue;
                            String word = itemStartElement.getAttributeByName(QName.valueOf("word")).getValue();
                            word = head + word;
                            if (XiaConceptParser.getInstance().isConcept(word)) {
                                conceptCount++;
                            }
                            count++;
                            if (count % 1000 == 0) {
                                System.out.println("process words " + count + "...");
                            }
                        }
                    }
                }
            }
        }
        input.close();
        System.out.println(count + "\t" + conceptCount);
        return;
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) StartElement(javax.xml.stream.events.StartElement) GZIPInputStream(java.util.zip.GZIPInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader) File(java.io.File) XMLInputFactory(javax.xml.stream.XMLInputFactory) FileInputStream(java.io.FileInputStream)

Aggregations

XMLEvent (javax.xml.stream.events.XMLEvent)242 XMLEventReader (javax.xml.stream.XMLEventReader)111 XMLStreamException (javax.xml.stream.XMLStreamException)91 StartElement (javax.xml.stream.events.StartElement)86 XMLInputFactory (javax.xml.stream.XMLInputFactory)63 QName (javax.xml.namespace.QName)47 IOException (java.io.IOException)40 Attribute (javax.xml.stream.events.Attribute)40 EndElement (javax.xml.stream.events.EndElement)33 ArrayList (java.util.ArrayList)31 InputStream (java.io.InputStream)28 XMLEventWriter (javax.xml.stream.XMLEventWriter)23 Characters (javax.xml.stream.events.Characters)21 Test (org.junit.Test)17 ByteArrayInputStream (java.io.ByteArrayInputStream)16 StringWriter (java.io.StringWriter)16 StringReader (java.io.StringReader)14 XMLOutputFactory (javax.xml.stream.XMLOutputFactory)14 HashMap (java.util.HashMap)13 XMLEventFactory (javax.xml.stream.XMLEventFactory)13