Search in sources :

Example 1 with StartElement

use of javax.xml.stream.events.StartElement in project hadoop by apache.

the class NodeInfo method parseConf.

private static List<NodeInfo> parseConf(InputStream in) throws XMLStreamException {
    QName configuration = new QName("configuration");
    QName property = new QName("property");
    List<NodeInfo> nodes = new ArrayList<NodeInfo>();
    Stack<NodeInfo> parsed = new Stack<NodeInfo>();
    XMLInputFactory factory = XMLInputFactory.newInstance();
    XMLEventReader reader = factory.createXMLEventReader(in);
    while (reader.hasNext()) {
        XMLEvent event = reader.nextEvent();
        if (event.isStartElement()) {
            StartElement currentElement = event.asStartElement();
            NodeInfo currentNode = new NodeInfo(currentElement);
            if (parsed.isEmpty()) {
                if (!currentElement.getName().equals(configuration)) {
                    return null;
                }
            } else {
                NodeInfo parentNode = parsed.peek();
                QName parentName = parentNode.getStartElement().getName();
                if (parentName.equals(configuration) && currentNode.getStartElement().getName().equals(property)) {
                    @SuppressWarnings("unchecked") Iterator<Attribute> it = currentElement.getAttributes();
                    while (it.hasNext()) {
                        currentNode.addAttribute(it.next());
                    }
                } else if (parentName.equals(property)) {
                    parentNode.addElement(currentElement);
                }
            }
            parsed.push(currentNode);
        } else if (event.isEndElement()) {
            NodeInfo node = parsed.pop();
            if (parsed.size() == 1) {
                nodes.add(node);
            }
        } else if (event.isCharacters()) {
            if (2 < parsed.size()) {
                NodeInfo parentNode = parsed.pop();
                StartElement parentElement = parentNode.getStartElement();
                NodeInfo grandparentNode = parsed.peek();
                if (grandparentNode.getElement(parentElement) == null) {
                    grandparentNode.setElement(parentElement, event.asCharacters());
                }
                parsed.push(parentNode);
            }
        }
    }
    return nodes;
}
Also used : Attribute(javax.xml.stream.events.Attribute) QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList) Stack(java.util.Stack) StartElement(javax.xml.stream.events.StartElement) XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 2 with StartElement

use of javax.xml.stream.events.StartElement in project hadoop by apache.

the class NodeInfo method checkConf.

public static List<String> checkConf(InputStream in) {
    List<NodeInfo> nodes = null;
    List<String> errors = new ArrayList<String>();
    try {
        nodes = parseConf(in);
        if (nodes == null) {
            errors.add("bad conf file: top-level element not <configuration>");
        }
    } catch (XMLStreamException e) {
        errors.add("bad conf file: " + e.getMessage());
    }
    if (!errors.isEmpty()) {
        return errors;
    }
    Map<String, List<Integer>> duplicatedProperties = new HashMap<String, List<Integer>>();
    for (NodeInfo node : nodes) {
        StartElement element = node.getStartElement();
        int line = element.getLocation().getLineNumber();
        if (!element.getName().equals(new QName("property"))) {
            errors.add(String.format("Line %d: element not <property>", line));
            continue;
        }
        List<XMLEvent> events = node.getXMLEventsForQName(new QName("name"));
        if (events == null) {
            errors.add(String.format("Line %d: <property> has no <name>", line));
        } else {
            String v = null;
            for (XMLEvent event : events) {
                if (event.isAttribute()) {
                    v = ((Attribute) event).getValue();
                } else {
                    Characters c = node.getElement(event.asStartElement());
                    if (c != null) {
                        v = c.getData();
                    }
                }
                if (v == null || v.isEmpty()) {
                    errors.add(String.format("Line %d: <property> has an empty <name>", line));
                }
            }
            if (v != null && !v.isEmpty()) {
                List<Integer> lines = duplicatedProperties.get(v);
                if (lines == null) {
                    lines = new ArrayList<Integer>();
                    duplicatedProperties.put(v, lines);
                }
                lines.add(node.getStartElement().getLocation().getLineNumber());
            }
        }
        events = node.getXMLEventsForQName(new QName("value"));
        if (events == null) {
            errors.add(String.format("Line %d: <property> has no <value>", line));
        }
        for (QName qName : node.getDuplicatedQNames()) {
            if (!qName.equals(new QName("source"))) {
                errors.add(String.format("Line %d: <property> has duplicated <%s>s", line, qName));
            }
        }
    }
    for (Entry<String, List<Integer>> e : duplicatedProperties.entrySet()) {
        List<Integer> lines = e.getValue();
        if (1 < lines.size()) {
            errors.add(String.format("Line %s: duplicated <property>s for %s", StringUtils.join(", ", lines), e.getKey()));
        }
    }
    return errors;
}
Also used : HashMap(java.util.HashMap) QName(javax.xml.namespace.QName) Characters(javax.xml.stream.events.Characters) ArrayList(java.util.ArrayList) StartElement(javax.xml.stream.events.StartElement) XMLStreamException(javax.xml.stream.XMLStreamException) XMLEvent(javax.xml.stream.events.XMLEvent) ArrayList(java.util.ArrayList) List(java.util.List)

Example 3 with StartElement

use of javax.xml.stream.events.StartElement in project GCViewer by chewiebug.

the class DataReaderIBM_J9_R28 method read.

@Override
public GCModel read() throws IOException {
    GCModel model = new GCModel();
    try {
        XMLInputFactory factory = XMLInputFactory.newInstance();
        XMLEventReader eventReader = factory.createXMLEventReader(in);
        GCEvent currentGcEvent = null;
        String eventNameStart = null;
        while (eventReader.hasNext()) {
            try {
                XMLEvent event = eventReader.nextEvent();
                if (event.isStartElement()) {
                    StartElement startElement = event.asStartElement();
                    switch(startElement.getName().getLocalPart()) {
                        case VERBOSEGC:
                            handleVerboseGC(startElement);
                            break;
                        case INITIALIZED:
                            handleInitialized(eventReader);
                            break;
                        case EXCLUSIVE_START:
                            currentGcEvent = handleExclusiveStart(startElement);
                            break;
                        case SYS_START:
                            assert eventNameStart == null : "eventNameStart was expected to be null, but was " + eventNameStart;
                            eventNameStart = handleSysStart(eventReader, startElement);
                            break;
                        case AF_START:
                            assert eventNameStart == null : "eventNameStart was expected to be null, but was " + eventNameStart;
                            eventNameStart = handleAfStart(eventReader, startElement);
                            break;
                        case GC_START:
                            handleGcStart(eventReader, startElement, currentGcEvent, eventNameStart);
                            break;
                        case GC_END:
                            if (currentGcEvent.getTypeAsString() != null) {
                                handleGcEnd(eventReader, currentGcEvent);
                            }
                            break;
                        case EXCLUSIVE_END:
                            handleExclusiveEnd(startElement, currentGcEvent);
                            if (currentGcEvent.getExtendedType() == null) {
                                if (getLogger().isLoggable(Level.FINE))
                                    getLogger().fine("event at " + in.getLineNumber() + " doesn't contain any information, the parser can handle");
                            } else {
                                model.add(currentGcEvent);
                            }
                            currentGcEvent = null;
                            eventNameStart = null;
                            break;
                    }
                }
            } catch (Exception e) {
                if (e instanceof XMLStreamException) {
                    throw e;
                }
                if (getLogger().isLoggable(Level.WARNING))
                    getLogger().warning("line " + in.getLineNumber() + ": " + e.toString());
                if (getLogger().isLoggable(Level.FINE))
                    getLogger().log(Level.FINE, "line " + in.getLineNumber() + ": " + e.getMessage(), e);
            }
        }
    } catch (XMLStreamException e) {
        if (getLogger().isLoggable(Level.WARNING))
            getLogger().warning("line " + in.getLineNumber() + ": " + e.toString());
        if (getLogger().isLoggable(Level.FINE))
            getLogger().log(Level.FINE, "line " + in.getLineNumber() + ": " + e.getMessage(), e);
    } finally {
        if (getLogger().isLoggable(Level.INFO))
            getLogger().info("Done reading.");
    }
    return model;
}
Also used : StartElement(javax.xml.stream.events.StartElement) XMLStreamException(javax.xml.stream.XMLStreamException) XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader) AbstractGCEvent(com.tagtraum.perf.gcviewer.model.AbstractGCEvent) GCEvent(com.tagtraum.perf.gcviewer.model.GCEvent) GCModel(com.tagtraum.perf.gcviewer.model.GCModel) XMLInputFactory(javax.xml.stream.XMLInputFactory) IOException(java.io.IOException) DateTimeParseException(java.time.format.DateTimeParseException) XMLStreamException(javax.xml.stream.XMLStreamException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 4 with StartElement

use of javax.xml.stream.events.StartElement in project GCViewer by chewiebug.

the class DataReaderIBM_J9_R28 method handleGcStart.

private void handleGcStart(XMLEventReader eventReader, StartElement startElement, GCEvent event, String eventNameStart) throws XMLStreamException, UnknownGcTypeException {
    String typeName = eventNameStart + getAttributeValue(startElement, "type");
    AbstractGCEvent.Type type = Type.lookup(typeName);
    if (type == null) {
        throw new UnknownGcTypeException(typeName, startElement.toString());
    }
    event.setType(type);
    String currentElementName = "";
    while (eventReader.hasNext() && !currentElementName.equals(GC_START)) {
        XMLEvent xmlEvent = eventReader.nextEvent();
        if (xmlEvent.isStartElement()) {
            StartElement startEl = xmlEvent.asStartElement();
            if (startEl.getName().getLocalPart().equals("mem-info")) {
                setTotalAndPreUsed(event, startEl);
            } else if (startEl.getName().getLocalPart().equals("mem")) {
                switch(getAttributeValue(startEl, "type")) {
                    case "nursery":
                        GCEvent young = new GCEvent();
                        young.setType(Type.lookup("nursery"));
                        setTotalAndPreUsed(young, startEl);
                        event.add(young);
                        break;
                    case "tenure":
                        GCEvent tenured = new GCEvent();
                        tenured.setType(Type.lookup("tenure"));
                        setTotalAndPreUsed(tenured, startEl);
                        event.add(tenured);
                        break;
                }
            }
        } else if (xmlEvent.isEndElement()) {
            EndElement endElement = xmlEvent.asEndElement();
            currentElementName = endElement.getName().getLocalPart();
        }
    }
}
Also used : StartElement(javax.xml.stream.events.StartElement) EndElement(javax.xml.stream.events.EndElement) Type(com.tagtraum.perf.gcviewer.model.AbstractGCEvent.Type) XMLEvent(javax.xml.stream.events.XMLEvent) AbstractGCEvent(com.tagtraum.perf.gcviewer.model.AbstractGCEvent) AbstractGCEvent(com.tagtraum.perf.gcviewer.model.AbstractGCEvent) GCEvent(com.tagtraum.perf.gcviewer.model.GCEvent)

Example 5 with StartElement

use of javax.xml.stream.events.StartElement in project bazel by bazelbuild.

the class XmlResourceValues method parseDeclareStyleable.

static void parseDeclareStyleable(FullyQualifiedName.Factory fqnFactory, Path path, KeyValueConsumer<DataKey, DataResource> overwritingConsumer, KeyValueConsumer<DataKey, DataResource> combiningConsumer, XMLEventReader eventReader, StartElement start) throws XMLStreamException {
    Map<FullyQualifiedName, Boolean> members = new LinkedHashMap<>();
    for (XMLEvent element = nextTag(eventReader); !isEndTag(element, TAG_DECLARE_STYLEABLE); element = nextTag(eventReader)) {
        if (isStartTag(element, TAG_ATTR)) {
            StartElement attr = element.asStartElement();
            FullyQualifiedName attrName = fqnFactory.create(ResourceType.ATTR, getElementName(attr));
            // Without those, it will be an attr reference.
            if (XmlResourceValues.getElementAttributeByName(attr, ATTR_FORMAT) != null || (XmlResourceValues.peekNextTag(eventReader) != null && XmlResourceValues.peekNextTag(eventReader).isStartElement())) {
                overwritingConsumer.consume(attrName, DataResourceXml.createWithNoNamespace(path, parseAttr(eventReader, attr)));
                members.put(attrName, Boolean.TRUE);
            } else {
                members.put(attrName, Boolean.FALSE);
            }
        }
    }
    combiningConsumer.consume(fqnFactory.create(ResourceType.STYLEABLE, getElementName(start)), DataResourceXml.createWithNoNamespace(path, StyleableXmlResourceValue.of(members)));
}
Also used : StartElement(javax.xml.stream.events.StartElement) XMLEvent(javax.xml.stream.events.XMLEvent) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

StartElement (javax.xml.stream.events.StartElement)30 XMLEvent (javax.xml.stream.events.XMLEvent)20 QName (javax.xml.namespace.QName)11 XMLEventReader (javax.xml.stream.XMLEventReader)10 XMLStreamException (javax.xml.stream.XMLStreamException)9 Attribute (javax.xml.stream.events.Attribute)8 EndElement (javax.xml.stream.events.EndElement)8 XMLInputFactory (javax.xml.stream.XMLInputFactory)7 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 InputStream (java.io.InputStream)3 LinkedList (java.util.LinkedList)3 JAXBContext (javax.xml.bind.JAXBContext)3 Unmarshaller (javax.xml.bind.Unmarshaller)3 EventFilter (javax.xml.stream.EventFilter)3 Characters (javax.xml.stream.events.Characters)3 IPortletWindow (org.apereo.portal.portlet.om.IPortletWindow)3 AbstractGCEvent (com.tagtraum.perf.gcviewer.model.AbstractGCEvent)2 GCEvent (com.tagtraum.perf.gcviewer.model.GCEvent)2 BufferedInputStream (java.io.BufferedInputStream)2