Search in sources :

Example 31 with Attribute

use of javax.xml.stream.events.Attribute in project sirix by sirixdb.

the class WikipediaImport method checkStAXStartElement.

/**
 * Check if start element of two StAX parsers match.
 *
 * @param startTag StartTag of the StAX parser, where it is currently (the "real" StAX parser over
 *        the whole document)
 * @param elem StartTag to check against
 * @return {@code true} if start elements match, {@code false} otherwise
 * @throws XMLStreamException handling XML Stream Exception
 */
private static boolean checkStAXStartElement(final StartElement startTag, final StartElement elem) throws XMLStreamException {
    assert startTag != null && elem != null;
    boolean retVal = false;
    if (startTag.getEventType() == XMLStreamConstants.START_ELEMENT && startTag.getName().equals(elem.getName())) {
        // Check attributes.
        boolean foundAtts = false;
        boolean hasAtts = false;
        for (final Iterator<?> itStartTag = startTag.getAttributes(); itStartTag.hasNext(); ) {
            hasAtts = true;
            final Attribute attStartTag = (Attribute) itStartTag.next();
            for (final Iterator<?> itElem = elem.getAttributes(); itElem.hasNext(); ) {
                final Attribute attElem = (Attribute) itElem.next();
                if (attStartTag.getName().equals(attElem.getName()) && attStartTag.getName().equals(attElem.getName())) {
                    foundAtts = true;
                    break;
                }
            }
            if (!foundAtts) {
                break;
            }
        }
        if (!hasAtts) {
            foundAtts = true;
        }
        // Check namespaces.
        boolean foundNamesps = false;
        boolean hasNamesps = false;
        for (final Iterator<?> itStartTag = startTag.getNamespaces(); itStartTag.hasNext(); ) {
            hasNamesps = true;
            final Namespace nsStartTag = (Namespace) itStartTag.next();
            for (final Iterator<?> itElem = elem.getNamespaces(); itElem.hasNext(); ) {
                final Namespace nsElem = (Namespace) itElem.next();
                if (nsStartTag.getName().equals(nsElem.getName()) && nsStartTag.getName().equals(nsElem.getName())) {
                    foundNamesps = true;
                    break;
                }
            }
            if (!foundNamesps) {
                break;
            }
        }
        if (!hasNamesps) {
            foundNamesps = true;
        }
        // Check if qname, atts and namespaces are the same.
        if (foundAtts && foundNamesps) {
            retVal = true;
        } else {
            retVal = false;
        }
    }
    return retVal;
}
Also used : Attribute(javax.xml.stream.events.Attribute) Namespace(javax.xml.stream.events.Namespace)

Example 32 with Attribute

use of javax.xml.stream.events.Attribute in project sirix by sirixdb.

the class XMLShredder method addNewElement.

/**
 * Add a new element node.
 *
 * @param pLeftSiblingKeyStack stack used to determine if the new element has to be inserted as a
 *        right sibling or as a new child (in the latter case is NULL on top of the stack)
 * @param event the current event from the StAX parser
 * @return the modified stack
 * @throws SirixException if adding {@link ElementNode} fails
 */
private void addNewElement(final StartElement event) throws SirixException {
    assert event != null;
    final QName qName = event.getName();
    final QNm name = new QNm(qName.getNamespaceURI(), qName.getPrefix(), qName.getLocalPart());
    processStartTag(name);
    // Parse namespaces.
    for (final Iterator<?> it = event.getNamespaces(); it.hasNext(); ) {
        final Namespace namespace = (Namespace) it.next();
        mWtx.insertNamespace(new QNm(namespace.getNamespaceURI(), namespace.getPrefix(), ""));
        mWtx.moveToParent();
    }
    // Parse attributes.
    for (final Iterator<?> it = event.getAttributes(); it.hasNext(); ) {
        final Attribute attribute = (Attribute) it.next();
        final QName attName = attribute.getName();
        mWtx.insertAttribute(new QNm(attName.getNamespaceURI(), attName.getPrefix(), attName.getLocalPart()), attribute.getValue());
        mWtx.moveToParent();
    }
}
Also used : QNm(org.brackit.xquery.atomic.QNm) Attribute(javax.xml.stream.events.Attribute) QName(javax.xml.namespace.QName) Namespace(javax.xml.stream.events.Namespace)

Example 33 with Attribute

use of javax.xml.stream.events.Attribute in project sirix by sirixdb.

the class XMLUpdateShredder method checkElement.

/**
 * Check if current element matches the element in the shreddered file.
 *
 * @param mEvent StartElement event, from the XML file to shredder.
 * @return true if they are equal, false otherwise.
 */
private boolean checkElement(final StartElement mEvent) {
    assert mEvent != null;
    boolean retVal = false;
    // Matching element names?
    final QName name = mEvent.getName();
    final QNm currName = mWtx.getName();
    if (mWtx.getKind() == Kind.ELEMENT && currName.getNamespaceURI().equals(name.getNamespaceURI()) && currName.getLocalName().equals(name.getLocalPart())) {
        // Check if atts and namespaces are the same.
        final long nodeKey = mWtx.getNodeKey();
        // Check attributes.
        boolean foundAtts = false;
        boolean hasAtts = false;
        for (final Iterator<?> it = mEvent.getAttributes(); it.hasNext(); ) {
            hasAtts = true;
            final Attribute attribute = (Attribute) it.next();
            for (int i = 0, attCount = mWtx.getAttributeCount(); i < attCount; i++) {
                mWtx.moveToAttribute(i);
                final QName attName = attribute.getName();
                final QNm currAttName = mWtx.getName();
                if (attName.getNamespaceURI().equals(currAttName.getNamespaceURI()) && attName.getLocalPart().equals(currAttName.getLocalName()) && attribute.getValue().equals(mWtx.getValue())) {
                    foundAtts = true;
                    mWtx.moveTo(nodeKey);
                    break;
                }
                mWtx.moveTo(nodeKey);
            }
            if (!foundAtts) {
                break;
            }
        }
        if (!hasAtts && mWtx.getAttributeCount() == 0) {
            foundAtts = true;
        }
        // Check namespaces.
        boolean foundNamesps = false;
        boolean hasNamesps = false;
        for (final Iterator<?> namespIt = mEvent.getNamespaces(); namespIt.hasNext(); ) {
            hasNamesps = true;
            final Namespace namespace = (Namespace) namespIt.next();
            for (int i = 0, namespCount = mWtx.getNamespaceCount(); i < namespCount; i++) {
                mWtx.moveToNamespace(i);
                if (namespace.getNamespaceURI().equals(mWtx.nameForKey(mWtx.getURIKey()))) {
                    final String prefix = namespace.getPrefix();
                    if (prefix.isEmpty()) {
                        foundNamesps = true;
                        mWtx.moveTo(nodeKey);
                        break;
                    } else if (prefix.equals(mWtx.nameForKey(mWtx.getPrefixKey()))) {
                        foundNamesps = true;
                        mWtx.moveTo(nodeKey);
                        break;
                    }
                }
                mWtx.moveTo(nodeKey);
            }
            if (!foundNamesps) {
                break;
            }
        }
        if (!hasNamesps && mWtx.getNamespaceCount() == 0) {
            foundNamesps = true;
        }
        // Check if atts and namespaces are the same.
        if (foundAtts && foundNamesps) {
            retVal = true;
        } else {
            retVal = false;
        }
    }
    return retVal;
}
Also used : QNm(org.brackit.xquery.atomic.QNm) Attribute(javax.xml.stream.events.Attribute) QName(javax.xml.namespace.QName) Namespace(javax.xml.stream.events.Namespace)

Example 34 with Attribute

use of javax.xml.stream.events.Attribute in project bw-calendar-engine by Bedework.

the class DmozStructure method processStartElement.

void processStartElement(XMLEventReader reader, StartElement element) {
    QName elname = element.getName();
    if (skipEls.containsKey(elname)) {
        skippingElement = elname;
        return;
    }
    if (elname.equals(qnTopic)) {
        processTopicStart(element);
        return;
    }
    if (elname.equals(qnAltlang)) {
        processAltLang(element);
        return;
    }
    if (elname.equals(qnAltlang1)) {
        processAltLang(element);
        return;
    }
    if (elname.equals(qnDescription)) {
        curText = new StringBuilder();
        return;
    }
    if (elname.equals(qnTitle)) {
        curText = new StringBuilder();
        return;
    }
    if (elname.equals(qnNarrow)) {
        if (curTopic != null) {
            curTopic.narrows.add(getResource(element));
        }
        return;
    }
    if (elname.equals(qnNarrow1)) {
        if (curTopic != null) {
            curTopic.narrow1s.add(getResource(element));
        }
        return;
    }
    if (elname.equals(qnNarrow2)) {
        if (curTopic != null) {
            curTopic.narrow2s.add(getResource(element));
        }
        return;
    }
    info("Start Element: " + elname);
    Iterator iterator = element.getAttributes();
    while (iterator.hasNext()) {
        Attribute attribute = (Attribute) iterator.next();
        QName name = attribute.getName();
        String value = attribute.getValue();
        System.out.println("Attribute name/value: " + name + "/" + value);
    }
}
Also used : Attribute(javax.xml.stream.events.Attribute) QName(javax.xml.namespace.QName) Iterator(java.util.Iterator)

Example 35 with Attribute

use of javax.xml.stream.events.Attribute in project tutorials by eugenp.

the class StaxParser method getAllTutorial.

public List<Tutorial> getAllTutorial() {
    boolean bTitle = false;
    boolean bDescription = false;
    boolean bDate = false;
    boolean bAuthor = false;
    List<Tutorial> tutorials = new ArrayList<Tutorial>();
    try {
        XMLInputFactory factory = XMLInputFactory.newInstance();
        XMLEventReader eventReader = factory.createXMLEventReader(new FileReader(this.getFile()));
        Tutorial current = null;
        while (eventReader.hasNext()) {
            XMLEvent event = eventReader.nextEvent();
            switch(event.getEventType()) {
                case XMLStreamConstants.START_ELEMENT:
                    StartElement startElement = event.asStartElement();
                    String qName = startElement.getName().getLocalPart();
                    if (qName.equalsIgnoreCase("tutorial")) {
                        current = new Tutorial();
                        Iterator<Attribute> attributes = startElement.getAttributes();
                        while (attributes.hasNext()) {
                            Attribute currentAt = attributes.next();
                            if (currentAt.getName().toString().equalsIgnoreCase("tutId")) {
                                current.setTutId(currentAt.getValue());
                            } else if (currentAt.getName().toString().equalsIgnoreCase("type")) {
                                current.setType(currentAt.getValue());
                            }
                        }
                    } else if (qName.equalsIgnoreCase("title")) {
                        bTitle = true;
                    } else if (qName.equalsIgnoreCase("description")) {
                        bDescription = true;
                    } else if (qName.equalsIgnoreCase("date")) {
                        bDate = true;
                    } else if (qName.equalsIgnoreCase("author")) {
                        bAuthor = true;
                    }
                    break;
                case XMLStreamConstants.CHARACTERS:
                    Characters characters = event.asCharacters();
                    if (bTitle) {
                        if (current != null) {
                            current.setTitle(characters.getData());
                        }
                        bTitle = false;
                    }
                    if (bDescription) {
                        if (current != null) {
                            current.setDescription(characters.getData());
                        }
                        bDescription = false;
                    }
                    if (bDate) {
                        if (current != null) {
                            current.setDate(characters.getData());
                        }
                        bDate = false;
                    }
                    if (bAuthor) {
                        if (current != null) {
                            current.setAuthor(characters.getData());
                        }
                        bAuthor = false;
                    }
                    break;
                case XMLStreamConstants.END_ELEMENT:
                    EndElement endElement = event.asEndElement();
                    if (endElement.getName().getLocalPart().equalsIgnoreCase("tutorial")) {
                        if (current != null) {
                            tutorials.add(current);
                        }
                    }
                    break;
            }
        }
    } catch (FileNotFoundException | XMLStreamException e) {
        e.printStackTrace();
    }
    return tutorials;
}
Also used : Tutorial(com.baeldung.xml.binding.Tutorial) Attribute(javax.xml.stream.events.Attribute) EndElement(javax.xml.stream.events.EndElement) Characters(javax.xml.stream.events.Characters) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) StartElement(javax.xml.stream.events.StartElement) XMLStreamException(javax.xml.stream.XMLStreamException) XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader) FileReader(java.io.FileReader) XMLInputFactory(javax.xml.stream.XMLInputFactory)

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