Search in sources :

Example 81 with Attribute

use of javax.xml.stream.events.Attribute in project ORCID-Source by ORCID.

the class LoadLEIData method execute.

/**
 * Stream the XML using Stax. Create orgs and process them as we go
 *
 * @throws XMLStreamException
 * @throws FileNotFoundException
 */
public void execute() throws XMLStreamException, FileNotFoundException {
    Instant start = Instant.now();
    XMLInputFactory factory = XMLInputFactory.newInstance();
    XMLEventReader r = null;
    try {
        r = factory.createXMLEventReader(new FileReader(fileToLoad));
        LEIOrganization org = null;
        while (r.hasNext()) {
            XMLEvent event = r.nextEvent();
            if (event.isStartElement()) {
                String startElement = event.asStartElement().getName().getLocalPart();
                if (startElement.equals("LEIRecord")) {
                    org = new LEIOrganization();
                } else if (startElement.equals("LEI")) {
                    org.id = r.getElementText();
                } else if (startElement.equals("LegalName")) {
                    org.name = r.getElementText();
                } else if (startElement.equals("OtherEntityName") || startElement.equals("TransliteratedOtherEntityName")) {
                    Attribute type = event.asStartElement().getAttributeByName(QNAME_TYPE);
                    if (type != null && type.getValue().equals(TRANSLITERATED_LEGAL_NAME_TYPE))
                        org.transliteratedLegalNames.add(r.getElementText());
                    else
                        org.otherNames.add(r.getElementText());
                } else if (startElement.equals("HeadquartersAddress")) {
                    org.hqAddres = extractAddress(r);
                } else if (startElement.equals("LegalAddress") || startElement.equals("OtherAddress") || startElement.equals("TransliteratedOtherAddress")) {
                    org.otherAddresses.add(extractAddress(r));
                } else if (startElement.equals("EntityStatus")) {
                    // ACTIVE || INACTIVE
                    org.status = r.getElementText();
                } else if (startElement.equals("SuccessorLEI")) {
                    // can be active
                    org.successorLEI = r.getElementText();
                // and have
                // successor
                } else if (startElement.equals("OtherLegalForm")) {
                    // see
                    org.type = r.getElementText();
                    // for vocab
                    if ("OTHER - please specify".equals(org.type))
                        org.type = "OTHER";
                }
            // we also have general purpose AssociatedLEI, for active
            // and inactive records.
            } else if (event.isEndElement()) {
                String endElement = event.asEndElement().getName().getLocalPart();
                if (endElement.equals("LEIRecord")) {
                    count++;
                    LOGGER.info("Processing [" + count + "] LEI:" + org.id);
                    processOrg(org);
                }
            }
        // we also have //LastUpdateDate
        }
    } finally {
        if (r != null)
            r.close();
    }
    LOGGER.info("Updated Disambiguated orgs: {}", updatedDisambiguatedOrgs);
    LOGGER.info("New Disambiguated orgs: {}", addedDisambiguatedOrgs);
    LOGGER.info("Updated orgs: {}", updatedOrgs);
    LOGGER.info("New orgs: {}", addedOrgs);
    LOGGER.info("Time taken to process the data: {}", Duration.between(start, Instant.now()).toString());
}
Also used : Attribute(javax.xml.stream.events.Attribute) Instant(java.time.Instant) XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader) FileReader(java.io.FileReader) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 82 with Attribute

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

the class StAXSerializerTest method emitElement.

/**
 * Emit an element.
 *
 * @param event {@link XMLEvent}, either a start tag or an end tag.
 * @param strBuilder String builder to build the string representation.
 */
@Ignore
private void emitElement(final XMLEvent event, final StringBuilder strBuilder) {
    emitQName(true, event, strBuilder);
    if (event.isStartElement()) {
        final StartElement elem = ((StartElement) event);
        // Parse namespaces.
        for (Iterator<?> it = elem.getNamespaces(); it.hasNext(); ) {
            final Namespace namespace = (Namespace) it.next();
            if ("".equals(namespace.getPrefix())) {
                strBuilder.append(" xmlns=\"").append(namespace.getNamespaceURI()).append("\"");
            } else {
                strBuilder.append(" xmlns:").append(namespace.getPrefix()).append("=\"").append(namespace.getNamespaceURI()).append("\"");
            }
        }
        // Parse attributes.
        for (Iterator<?> it = elem.getAttributes(); it.hasNext(); ) {
            final Attribute attribute = (Attribute) it.next();
            emitQName(false, attribute, strBuilder);
            strBuilder.append("=\"").append(attribute.getValue()).append("\"");
        }
    }
}
Also used : StartElement(javax.xml.stream.events.StartElement) Attribute(javax.xml.stream.events.Attribute) Namespace(javax.xml.stream.events.Namespace) Ignore(org.junit.Ignore)

Example 83 with Attribute

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

the class XMLUpdateShredder method addNewElement.

/**
 * Add a new element node.
 *
 * @param paramAdd determines wether node is added as first child or right sibling
 * @param paramStartElement the current {@link StartElement}
 * @throws SirixException if inserting node fails
 */
private void addNewElement(final EAdd paramAdd, final StartElement paramStartElement) throws SirixException {
    assert paramStartElement != null;
    final QName name = paramStartElement.getName();
    final QNm qName = new QNm(name.getNamespaceURI(), name.getPrefix(), name.getLocalPart());
    long key;
    if (mInsert == Insert.ASRIGHTSIBLING) {
        key = mWtx.insertElementAsRightSibling(qName).getNodeKey();
    } else {
        if (paramAdd == EAdd.ASFIRSTCHILD) {
            key = mWtx.insertElementAsFirstChild(qName).getNodeKey();
        } else {
            key = mWtx.insertElementAsRightSibling(qName).getNodeKey();
        }
    }
    // Parse namespaces.
    for (final Iterator<?> it = paramStartElement.getNamespaces(); it.hasNext(); ) {
        final Namespace namespace = (Namespace) it.next();
        mWtx.insertNamespace(new QNm(namespace.getNamespaceURI(), namespace.getPrefix(), ""));
        mWtx.moveTo(key);
    }
    // Parse attributes.
    for (final Iterator<?> it = paramStartElement.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.moveTo(key);
    }
}
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 84 with Attribute

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

the class TextView method processStartTag.

/**
 * Generate a String representation from a {@link StartElement}.
 *
 * @param pStartTag
 *          The {@link StartElement} to serialize.
 * @param pDoc
 *          The {@link StyledDocument} from the {@link JTextPane} instance.
 * @param pHasChild
 *          {@link Child}.
 */
private void processStartTag(final StartElement pStartTag, final StyledDocument pDoc, final Child pHasChild) {
    assert pStartTag != null;
    assert pDoc != null;
    assert pHasChild != null;
    try {
        final QName name = pStartTag.getName();
        final String qName = ViewUtilities.qNameToString(new QNm(name.getNamespaceURI(), name.getPrefix(), name.getLocalPart()));
        pDoc.insertString(pDoc.getLength(), new StringBuilder("<").append(qName).toString(), pDoc.getStyle("elements"));
        // Insert a space if namespaces or attributes follow.
        if (pStartTag.getAttributes().hasNext() || pStartTag.getNamespaces().hasNext()) {
            pDoc.insertString(pDoc.getLength(), " ", pDoc.getStyle("elements"));
        }
        // Process namespaces.
        for (final Iterator<?> namespaces = pStartTag.getNamespaces(); namespaces.hasNext(); ) {
            final Namespace ns = (Namespace) namespaces.next();
            if (ns.getPrefix().isEmpty()) {
                pDoc.insertString(pDoc.getLength(), new StringBuilder(" xmlns=\"").append(ns.getNamespaceURI()).append("\"").toString(), pDoc.getStyle("namespaces"));
            } else {
                pDoc.insertString(pDoc.getLength(), new StringBuilder(" xmlns:").append(ns.getPrefix()).append("=\"").append(ns.getNamespaceURI()).append("\"").toString(), pDoc.getStyle("namespaces"));
            }
            if (pStartTag.getAttributes().hasNext()) {
                pDoc.insertString(pDoc.getLength(), " ", pDoc.getStyle("elements"));
            }
        }
        // Process attributes.
        for (final Iterator<?> attributes = pStartTag.getAttributes(); attributes.hasNext(); ) {
            final Attribute att = (Attribute) attributes.next();
            final QName attName = att.getName();
            pDoc.insertString(pDoc.getLength(), new StringBuilder().append(ViewUtilities.qNameToString(new QNm(attName.getNamespaceURI(), attName.getPrefix(), attName.getLocalPart()))).append("=\"").append(att.getValue()).append("\"").toString(), pDoc.getStyle("attributes"));
            if (attributes.hasNext()) {
                pDoc.insertString(pDoc.getLength(), " ", pDoc.getStyle("elements"));
            }
        }
        switch(pHasChild) {
            case CHILD:
                pDoc.insertString(pDoc.getLength(), ">" + NEWLINE, pDoc.getStyle("elements"));
                break;
            case NOCHILD:
                pDoc.insertString(pDoc.getLength(), "/>" + NEWLINE, pDoc.getStyle("elements"));
                break;
            default:
                break;
        }
    } catch (final BadLocationException e) {
        LOGWRAPPER.error(e.getMessage(), e);
    }
}
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) BadLocationException(javax.swing.text.BadLocationException)

Example 85 with Attribute

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

the class SunburstItem method toString.

@Override
public String toString() {
    final StringBuilder builder = new StringBuilder().append("[Depth: ").append(mDepth);
    if (mOldText != null) {
        builder.append("\n").append("old Text: ");
        appendText(builder, mOldText);
        builder.append("\n");
    } else if (mOldQName != null) {
        builder.append("\n").append("old QName: ");
        appendText(builder, Utils.buildName(mOldQName));
        builder.append("\n");
    }
    if (mQName != null) {
        appendWhiteSpace(builder);
        builder.append("QName: ");
        appendText(builder, Utils.buildName(mQName));
        builder.append("\n");
    } else if (mText != null) {
        appendWhiteSpace(builder);
        builder.append("Text: ");
        appendText(builder, mText);
        builder.append("\n");
    }
    updated(builder);
    if (mGUI.mUseAttribute) {
        for (final Attribute attr : mAttributes) {
            if ("name".equalsIgnoreCase(attr.getName().getLocalPart())) {
                builder.append("Name: ").append(attr.getValue()).append("\n");
            }
        }
    }
    if (mModifications > 0) {
        builder.append("ModifcationCount: ").append(mModifications);
    }
    if (mModifications != 0) {
        builder.append(" ");
    }
    builder.append("DescendantCount: ").append((int) mValue);
    builder.append(" NodeKey: ").append(mNodeKey);
    if (mDiff != null) {
        builder.append(" Diff: ").append(mDiff.toString());
    }
    builder.append("]");
    return builder.toString();
}
Also used : Attribute(javax.xml.stream.events.Attribute)

Aggregations

Attribute (javax.xml.stream.events.Attribute)144 QName (javax.xml.namespace.QName)73 StartElement (javax.xml.stream.events.StartElement)63 XMLEvent (javax.xml.stream.events.XMLEvent)54 XMLEventReader (javax.xml.stream.XMLEventReader)30 EndElement (javax.xml.stream.events.EndElement)26 Namespace (javax.xml.stream.events.Namespace)26 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)6 QNm (org.brackit.xquery.atomic.QNm)5