Example usage for javax.xml.stream.events XMLEvent isAttribute

List of usage examples for javax.xml.stream.events XMLEvent isAttribute

Introduction

In this page you can find the example usage for javax.xml.stream.events XMLEvent isAttribute.

Prototype

public boolean isAttribute();

Source Link

Document

A utility function to check if this event is an Attribute.

Usage

From source file:org.eclipse.swordfish.core.configuration.xml.SaxParsingPrototype.java

/**
 * @param args// ww w .ja  va2  s  . c  o  m
 */
public static void main(String[] args) throws Exception {
    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    LinkedList<String> currentElements = new LinkedList<String>();
    InputStream in = SaxParsingPrototype.class.getResource("ComplexPidXmlProperties.xml").openStream();
    XMLEventReader eventReader = inputFactory.createXMLEventReader(in);
    Map<String, List<String>> props = new HashMap<String, List<String>>();
    // Read the XML document
    while (eventReader.hasNext()) {
        XMLEvent event = eventReader.nextEvent();
        if (event.isCharacters() && !event.asCharacters().isWhiteSpace()) {
            putElement(props, getQualifiedName(currentElements), event.asCharacters().getData());

        } else if (event.isStartElement()) {
            System.out.println(event.asStartElement().getName());
            currentElements.add(event.asStartElement().getName().getLocalPart());
            for (Iterator attrIt = event.asStartElement().getAttributes(); attrIt.hasNext();) {
                Attribute attribute = (Attribute) attrIt.next();
                putElement(props, getQualifiedName(currentElements) + "[@" + attribute.getName() + "]",
                        attribute.getValue());

            }
        } else if (event.isAttribute()) {
        } else if (event.isEndElement()) {
            String lastElem = event.asEndElement().getName().getLocalPart();
            if (!currentElements.getLast().equals(lastElem)) {
                throw new UnsupportedOperationException(lastElem + "," + currentElements.getLast());
            }
            currentElements.removeLast();
        }
    }

    eventReader.close();
}

From source file:com.google.code.activetemplates.impl.TemplateCompilerImpl.java

private void doCompile(String name, CompileContext cc) throws XMLStreamException {

    while (cc.hasNextEvent()) {

        XMLEvent e = cc.nextEvent();

        //Location loc = e.getLocation();

        if (e.isAttribute()) {
            //System.out.println("Adding " + e);

            // attributes added during tag processing and under the same tag
            // get handled here, outcome is always PROCESS_ALL

            Attribute a = (Attribute) e;
            if (h.isAttributeHandled(a.getName())) {
                h.processAttribute(cc, a);
            } else {
                String value = a.getValue();
                String nvalue = processText(cc, value);
                if (nvalue != null) {
                    a = cc.getElementFactory().createAttribute(a.getName(), nvalue);
                }//  www  .  ja  v a  2 s.c om
                //System.out.println("Adding " + e);
                cc.getWriter().add(a);
            }

        } else if (e.isStartElement()) {

            StartElement se = e.asStartElement();

            Processing processing = Processing.DEFAULT;

            // collect namespaces
            @SuppressWarnings("unchecked")
            Iterator<Namespace> nsit = se.getNamespaces();
            List<Namespace> namespaces = new ArrayList<Namespace>();

            while (nsit.hasNext()) {
                Namespace ns = nsit.next();
                if (excludedNamespaces.contains(ns.getNamespaceURI())) {
                    processing = Processing.REPLACE;
                } else {
                    namespaces.add(ns);
                }
            }

            // collect attributes
            @SuppressWarnings("unchecked")
            Iterator<Attribute> it = se.getAttributes();
            List<Attribute> attributes = new LinkedList<Attribute>();
            while (it.hasNext()) {
                attributes.add(it.next());
            }

            // collect any separate attribute and namespace xml events
            while (cc.hasNextEvent()) {
                if (cc.peekEvent().isNamespace()) {
                    namespaces.add((Namespace) cc.nextEvent());
                    processing = Processing.REPLACE;
                } else if (cc.peekEvent().isAttribute()) {
                    attributes.add((Attribute) cc.nextEvent());
                    processing = Processing.REPLACE;
                } else {
                    break;
                }
            }

            // preprocess attributes
            it = attributes.iterator();
            attributes = new ArrayList<Attribute>();

            while (it.hasNext() && processing != Processing.SKIP) {
                Attribute a = it.next();

                if (h.isAttributeHandled(a.getName())) {
                    processing = Processing.REPLACE;

                    AttributeHandler.Outcome o = h.processAttribute(cc, a);
                    if (o == Outcome.PROCESS_NONE) {
                        processing = Processing.SKIP;
                    }

                } else {
                    String value = a.getValue();
                    String nvalue = processText(cc, value);
                    if (nvalue != null) {
                        a = cc.getElementFactory().createAttribute(a.getName(), nvalue);
                        processing = Processing.REPLACE;
                    }

                    attributes.add(a);
                }
            }

            if (processing == Processing.SKIP) {

                skipChildren(cc, false);

            } else {

                if (processing == Processing.REPLACE) {
                    // replace element with new one
                    se = cc.getElementFactory().createStartElement(se.getName(), attributes.iterator(),
                            namespaces.iterator());
                }

                // handle start element
                if (h.isElementHandled(se.getName())) {
                    ElementHandler.Outcome o = h.processStartElement(cc, se);
                    cc.flushEventQueue();
                    switch (o) {
                    case PROCESS_SIBLINGS:
                        skipChildren(cc, true);
                        break;
                    }
                } else {
                    //System.out.println("Adding " + se);
                    cc.getWriter().add(se);
                    cc.flushEventQueue(); // flush events added by any attribute handlers
                }
            }

        } else if (e.isEndElement()) {

            // handle end element
            if (h.isElementHandled(e.asEndElement().getName())) {
                h.processEndElement(cc, e.asEndElement());
                cc.flushEventQueue();
            } else {
                //System.out.println("Adding " + e);
                cc.getWriter().add(e);
            }

        } else if (e.isCharacters()) {

            // process text
            Characters ce = e.asCharacters();
            String s = ce.getData();
            String ns = processText(cc, s);
            if (ns != null) {
                ce = cc.getElementFactory().createCharacters(ns);
            }
            //System.out.println("Adding " + e);
            cc.getWriter().add(ce);

        }

    }

}

From source file:org.eclipse.swordfish.core.configuration.xml.XmlToPropertiesTransformerImpl.java

public void loadConfiguration(URL path) {
    Assert.notNull(path);/*from  w  ww. j a v  a 2s  .  co m*/
    InputStream inputStream = null;
    try {
        inputStream = path.openStream();
        XMLInputFactory inputFactory = XMLInputFactory.newInstance();
        LinkedList<String> currentElements = new LinkedList<String>();
        XMLEventReader eventReader = inputFactory.createXMLEventReader(inputStream);
        Map<String, List<String>> props = new HashMap<String, List<String>>();
        // Read the XML document
        while (eventReader.hasNext()) {
            XMLEvent event = eventReader.nextEvent();
            if (event.isCharacters() && !event.asCharacters().isWhiteSpace()) {
                putElement(props, getQualifiedName(currentElements), event.asCharacters().getData());

            } else if (event.isStartElement()) {
                currentElements.add(event.asStartElement().getName().getLocalPart());
                for (Iterator attrIt = event.asStartElement().getAttributes(); attrIt.hasNext();) {
                    Attribute attribute = (Attribute) attrIt.next();
                    putElement(props, getQualifiedName(currentElements) + "[@" + attribute.getName() + "]",
                            attribute.getValue());

                }
            } else if (event.isAttribute()) {
            } else if (event.isEndElement()) {
                String lastElem = event.asEndElement().getName().getLocalPart();
                if (!currentElements.getLast().equals(lastElem)) {
                    throw new UnsupportedOperationException(lastElem + "," + currentElements.getLast());
                }
                currentElements.removeLast();
            }
        }
        properties = flattenProperties(props);
    } catch (Exception ex) {
        throw new SwordfishException(ex);
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException ex) {
            }
        }
    }
}

From source file:org.apache.hadoop.util.ConfTest.java

public static List<String> checkConf(InputStream in) {
    List<NodeInfo> nodes = null;
    List<String> errors = new ArrayList<String>();

    try {/*from  w  w w.  jav a  2s.  co m*/
        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;
}