Search in sources :

Example 41 with Attribute

use of javax.xml.stream.events.Attribute in project contribution by checkstyle.

the class CheckstyleConfigurationsParser method processPropertyTag.

/**
 * Parses single "property" tag.
 *
 * @param startElement
 *        start element of the tag.
 * @param parent
 *        parent module instance.
 */
private static void processPropertyTag(StartElement startElement, ConfigurationModule parent) {
    String propertyName = null;
    String propertyValue = null;
    final Iterator<Attribute> attributes = startElement.getAttributes();
    while (attributes.hasNext()) {
        final Attribute attribute = attributes.next();
        final String attributeName = attribute.getName().toString();
        if (attributeName.equals(NAME_ATTR)) {
            propertyName = attribute.getValue();
        } else if (attributeName.equals(VALUE_ATTR)) {
            propertyValue = attribute.getValue();
        }
    }
    parent.addProperty(propertyName, propertyValue);
}
Also used : Attribute(javax.xml.stream.events.Attribute)

Example 42 with Attribute

use of javax.xml.stream.events.Attribute in project contribution by checkstyle.

the class CheckstyleReportsParser method parseErrorTag.

/**
 * Parses "error" XML tag.
 *
 * @param startElement
 *        cursor of StAX parser pointed on the tag.
 * @param statistics
 *        container accumulating statistics.
 * @param index
 *        internal index of the parsed file.
 * @param filename
 *        file name.
 * @return parsed data as CheckstyleRecord instance.
 */
private static CheckstyleRecord parseErrorTag(StartElement startElement, Statistics statistics, int index, String filename) {
    int line = -1;
    int column = -1;
    String source = null;
    String message = null;
    String severity = null;
    final Iterator<Attribute> attributes = startElement.getAttributes();
    while (attributes.hasNext()) {
        final Attribute attribute = attributes.next();
        final String attrName = attribute.getName().toString();
        switch(attrName) {
            case LINE_ATTR:
                line = Integer.parseInt(attribute.getValue());
                break;
            case COLUMN_ATTR:
                column = Integer.parseInt(attribute.getValue());
                break;
            case SEVERITY_ATTR:
                severity = attribute.getValue();
                statistics.addSeverityRecord(severity, index);
                break;
            case MESSAGE_ATTR:
                message = attribute.getValue();
                break;
            case SOURCE_ATTR:
                source = attribute.getValue();
                statistics.addModuleRecord(source, index);
                break;
            default:
                break;
        }
    }
    return new CheckstyleRecord(index, line, column, severity, source, message, filename);
}
Also used : CheckstyleRecord(com.github.checkstyle.data.CheckstyleRecord) Attribute(javax.xml.stream.events.Attribute)

Example 43 with Attribute

use of javax.xml.stream.events.Attribute in project jvarkit by lindenb.

the class WorldMapGenome method loadWorld.

private void loadWorld() throws IOException, XMLStreamException {
    Source source;
    LOG.warning("openingg " + svgMapUri);
    if (IOUtils.isRemoteURI(svgMapUri)) {
        source = new StreamSource(svgMapUri);
    } else {
        source = new StreamSource(new File(svgMapUri));
    }
    XMLInputFactory xif = XMLInputFactory.newFactory();
    XMLEventReader xef = xif.createXMLEventReader(source);
    while (xef.hasNext()) {
        XMLEvent evt = xef.nextEvent();
        if (!evt.isStartElement()) {
            continue;
        }
        StartElement E = evt.asStartElement();
        String localName = E.getName().getLocalPart();
        if (!localName.equals("path"))
            continue;
        Attribute att = E.getAttributeByName(new QName("id"));
        if (att == null)
            continue;
        String country = att.getValue().toLowerCase().replaceAll("[ ]+", "");
        att = E.getAttributeByName(new QName("d"));
        if (att == null)
            continue;
        GeneralPath path = null;
        char op = '\0';
        Scanner scanner = new Scanner(att.getValue().replaceAll("[ \t\n\r,]+", " "));
        path = new GeneralPath();
        while (scanner.hasNext()) {
            if (op == '\0') {
                op = scanner.next().charAt(0);
            }
            switch(op) {
                case 'M':
                    path.moveTo(scanner.nextDouble(), scanner.nextDouble());
                    break;
                case 'C':
                    path.curveTo(scanner.nextDouble(), scanner.nextDouble(), scanner.nextDouble(), scanner.nextDouble(), scanner.nextDouble(), scanner.nextDouble());
                    break;
                case 'Z':
                    {
                        path.closePath();
                        break;
                    }
                default:
                    throw new IOException("bad operator " + op);
            }
            if (scanner.hasNext("[MCZ]")) {
                op = scanner.next().charAt(0);
            }
        }
        scanner.close();
        this.country2shape.put(country, scaleWorld(path));
    }
    xef.close();
}
Also used : Scanner(java.util.Scanner) Attribute(javax.xml.stream.events.Attribute) QName(javax.xml.namespace.QName) StreamSource(javax.xml.transform.stream.StreamSource) IOException(java.io.IOException) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) StartElement(javax.xml.stream.events.StartElement) XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader) File(java.io.File) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 44 with Attribute

use of javax.xml.stream.events.Attribute in project jvarkit by lindenb.

the class NgsStage method loadSnippets.

protected List<SnippetCode> loadSnippets() {
    final String rsrc = getSnippetResourcePath();
    if (rsrc == null || rsrc.isEmpty())
        return Collections.emptyList();
    final List<SnippetCode> snippets = new ArrayList<>();
    InputStream in = null;
    XMLEventReader r = null;
    try {
        in = getClass().getResourceAsStream(rsrc);
        if (in != null) {
            final XMLInputFactory xif = XMLInputFactory.newFactory();
            xif.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);
            r = xif.createXMLEventReader(in);
            final QName isFunctionAtt = new QName("is-function");
            final QName scopeAtt = new QName("scope");
            final QName labelAtt = new QName("label");
            final QName nameAtt = new QName("name");
            while (r.hasNext()) {
                final XMLEvent evt = r.nextEvent();
                if (!evt.isStartElement())
                    continue;
                final StartElement start = evt.asStartElement();
                if (!start.getName().getLocalPart().equals("code"))
                    continue;
                final Attribute isFunction = start.getAttributeByName(isFunctionAtt);
                final Attribute scope = start.getAttributeByName(scopeAtt);
                Attribute attLabel = start.getAttributeByName(labelAtt);
                if (attLabel == null)
                    attLabel = start.getAttributeByName(nameAtt);
                if (attLabel != null && r.hasNext() && r.peek().isCharacters()) {
                    final SnippetCode snippet = new SnippetCode();
                    snippet.label = attLabel.getValue();
                    snippet.code = r.nextEvent().asCharacters().getData();
                    snippet.function = isFunction != null && isFunction.getValue().equals("true");
                    snippet.scope = (scope == null ? "" : scope.getValue());
                    snippets.add(snippet);
                }
            }
        } else {
            LOG.warning("Cannot read snippets " + rsrc);
        }
    } catch (Exception err) {
        LOG.warning(err.getMessage());
    } finally {
        CloserUtil.close(r);
        CloserUtil.close(in);
    }
    return snippets;
}
Also used : StartElement(javax.xml.stream.events.StartElement) Attribute(javax.xml.stream.events.Attribute) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList) XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader) XMLInputFactory(javax.xml.stream.XMLInputFactory) ScriptException(javax.script.ScriptException) IOException(java.io.IOException)

Example 45 with Attribute

use of javax.xml.stream.events.Attribute in project jvarkit by lindenb.

the class TreePackApp method parseNode.

private Node parseNode(XMLEventReader r, StartElement E, Node parent) throws XMLStreamException {
    Node node = new Node(parent);
    Attribute att = E.getAttributeByName(new QName("label"));
    node.label = (att != null ? att.getValue() : "");
    while (r.hasNext()) {
        XMLEvent evt = r.nextEvent();
        if (evt.isStartElement()) {
            QName qName = evt.asStartElement().getName();
            if (qName.getLocalPart().equals("node")) {
                Node child = parseNode(r, evt.asStartElement(), node);
                if (node.children == null)
                    node.children = new HashMap<String, Node>();
                node.children.put(node.label, child);
            } else {
                skip(r);
            }
        } else if (evt.isEndElement()) {
            if (node.children == null || node.children.isEmpty()) {
                att = E.getAttributeByName(new QName("weight"));
                if (att == null) {
                    node.weight = 1.0;
                } else {
                    node.weight = Double.parseDouble(att.getValue());
                    if (node.weight <= 0)
                        throw new XMLStreamException("bad @weight:" + node.weight, E.getLocation());
                }
            }
            return node;
        }
    }
    throw new IllegalStateException();
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) Attribute(javax.xml.stream.events.Attribute) HashMap(java.util.HashMap) QName(javax.xml.namespace.QName) XMLEvent(javax.xml.stream.events.XMLEvent)

Aggregations

Attribute (javax.xml.stream.events.Attribute)140 QName (javax.xml.namespace.QName)71 StartElement (javax.xml.stream.events.StartElement)62 XMLEvent (javax.xml.stream.events.XMLEvent)52 XMLEventReader (javax.xml.stream.XMLEventReader)30 Namespace (javax.xml.stream.events.Namespace)26 EndElement (javax.xml.stream.events.EndElement)25 ArrayList (java.util.ArrayList)23 XMLStreamException (javax.xml.stream.XMLStreamException)20 XMLInputFactory (javax.xml.stream.XMLInputFactory)18 InputStream (java.io.InputStream)14 IOException (java.io.IOException)12 Iterator (java.util.Iterator)11 ByteArrayInputStream (java.io.ByteArrayInputStream)7 FileInputStream (java.io.FileInputStream)7 HashMap (java.util.HashMap)7 Test (org.junit.Test)7 HashSet (java.util.HashSet)6 Characters (javax.xml.stream.events.Characters)5 QNm (org.brackit.xquery.atomic.QNm)5