Example usage for javax.xml.stream XMLStreamReader getLocalName

List of usage examples for javax.xml.stream XMLStreamReader getLocalName

Introduction

In this page you can find the example usage for javax.xml.stream XMLStreamReader getLocalName.

Prototype

public String getLocalName();

Source Link

Document

Returns the (local) name of the current event.

Usage

From source file:org.activiti.designer.eclipse.bpmn.BpmnParser.java

private ThrowEvent parseIntermediateThrowEvent(XMLStreamReader xtr) {
    ThrowEvent throwEvent = new ThrowEvent();
    try {//  w  w w . ja  v a  2 s. com
        while (xtr.hasNext()) {
            xtr.next();
            if (xtr.isStartElement() && "signalEventDefinition".equalsIgnoreCase(xtr.getLocalName())) {
                throwEvent.getEventDefinitions().add(parseSignalEventDefinition(xtr));
                break;

            } else if (xtr.isEndElement() && "intermediateThrowEvent".equalsIgnoreCase(xtr.getLocalName())) {
                break;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return throwEvent;
}

From source file:org.activiti.designer.eclipse.bpmn.BpmnParser.java

private TimerEventDefinition parseTimerEventDefinition(XMLStreamReader xtr) {
    TimerEventDefinition eventDefinition = new TimerEventDefinition();
    try {/*w  w w.  j  av a 2 s.  c  om*/
        while (xtr.hasNext()) {
            xtr.next();
            if (xtr.isStartElement() && "timeDuration".equalsIgnoreCase(xtr.getLocalName())) {
                eventDefinition.setTimeDuration(xtr.getElementText());
                break;

            } else if (xtr.isStartElement() && "timeDate".equalsIgnoreCase(xtr.getLocalName())) {
                eventDefinition.setTimeDate(xtr.getElementText());
                break;

            } else if (xtr.isStartElement() && "timeCycle".equalsIgnoreCase(xtr.getLocalName())) {
                eventDefinition.setTimeCycle(xtr.getElementText());
                break;

            } else if (xtr.isEndElement() && "timerEventDefinition".equalsIgnoreCase(xtr.getLocalName())) {
                break;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return eventDefinition;
}

From source file:org.activiti.dmn.converter.util.DmnXMLUtil.java

public static void parseChildElements(String elementName, DmnElement parentElement, XMLStreamReader xtr,
        Map<String, BaseChildElementParser> childParsers, DmnDefinition model) throws Exception {

    Map<String, BaseChildElementParser> localParserMap = new HashMap<String, BaseChildElementParser>(
            genericChildParserMap);//from   ww w . j a v  a2 s. c  o  m
    if (childParsers != null) {
        localParserMap.putAll(childParsers);
    }

    boolean inExtensionElements = false;
    boolean readyWithChildElements = false;
    while (readyWithChildElements == false && xtr.hasNext()) {
        xtr.next();

        if (xtr.isStartElement()) {

            if (ELEMENT_EXTENSIONS.equals(xtr.getLocalName())) {
                inExtensionElements = true;
            } else if (localParserMap.containsKey(xtr.getLocalName())) {
                BaseChildElementParser childParser = localParserMap.get(xtr.getLocalName());
                //if we're into an extension element but the current element is not accepted by this parentElement then is read as a custom extension element
                if (inExtensionElements && !childParser.accepts(parentElement)) {
                    DmnExtensionElement extensionElement = parseExtensionElement(xtr);
                    parentElement.addExtensionElement(extensionElement);
                    continue;
                }
                localParserMap.get(xtr.getLocalName()).parseChildElement(xtr, parentElement, model);
            } else if (inExtensionElements) {
                DmnExtensionElement extensionElement = parseExtensionElement(xtr);
                parentElement.addExtensionElement(extensionElement);
            }

        } else if (xtr.isEndElement()) {
            if (ELEMENT_EXTENSIONS.equals(xtr.getLocalName())) {
                inExtensionElements = false;
            } else if (elementName.equalsIgnoreCase(xtr.getLocalName())) {
                readyWithChildElements = true;
            }
        }
    }
}

From source file:org.activiti.dmn.converter.util.DmnXMLUtil.java

public static DmnExtensionElement parseExtensionElement(XMLStreamReader xtr) throws Exception {
    DmnExtensionElement extensionElement = new DmnExtensionElement();
    extensionElement.setName(xtr.getLocalName());
    if (StringUtils.isNotEmpty(xtr.getNamespaceURI())) {
        extensionElement.setNamespace(xtr.getNamespaceURI());
    }/*from   w ww  . j  a v a2  s  .  c om*/
    if (StringUtils.isNotEmpty(xtr.getPrefix())) {
        extensionElement.setNamespacePrefix(xtr.getPrefix());
    }

    for (int i = 0; i < xtr.getAttributeCount(); i++) {
        DmnExtensionAttribute extensionAttribute = new DmnExtensionAttribute();
        extensionAttribute.setName(xtr.getAttributeLocalName(i));
        extensionAttribute.setValue(xtr.getAttributeValue(i));
        if (StringUtils.isNotEmpty(xtr.getAttributeNamespace(i))) {
            extensionAttribute.setNamespace(xtr.getAttributeNamespace(i));
        }
        if (StringUtils.isNotEmpty(xtr.getAttributePrefix(i))) {
            extensionAttribute.setNamespacePrefix(xtr.getAttributePrefix(i));
        }
        extensionElement.addAttribute(extensionAttribute);
    }

    boolean readyWithExtensionElement = false;
    while (readyWithExtensionElement == false && xtr.hasNext()) {
        xtr.next();
        if (xtr.isCharacters() || XMLStreamReader.CDATA == xtr.getEventType()) {
            if (StringUtils.isNotEmpty(xtr.getText().trim())) {
                extensionElement.setElementText(xtr.getText().trim());
            }
        } else if (xtr.isStartElement()) {
            DmnExtensionElement childExtensionElement = parseExtensionElement(xtr);
            extensionElement.addChildElement(childExtensionElement);
        } else if (xtr.isEndElement() && extensionElement.getName().equalsIgnoreCase(xtr.getLocalName())) {
            readyWithExtensionElement = true;
        }
    }
    return extensionElement;
}

From source file:org.activiti.dmn.xml.converter.DmnXMLConverter.java

public DmnDefinition convertToDmnModel(XMLStreamReader xtr) {
    DmnDefinition model = new DmnDefinition();
    DmnElement parentElement = null;/* ww w.  ja v  a2s. c om*/

    try {
        while (xtr.hasNext()) {
            try {
                xtr.next();
            } catch (Exception e) {
                LOGGER.debug("Error reading XML document", e);
                throw new DmnXMLException("Error reading XML", e);
            }

            if (xtr.isStartElement() == false) {
                continue;
            }

            if (ELEMENT_DEFINITIONS.equals(xtr.getLocalName())) {
                model.setId(xtr.getAttributeValue(null, ATTRIBUTE_ID));
                model.setName(xtr.getAttributeValue(null, ATTRIBUTE_NAME));
                model.setNamespace(MODEL_NAMESPACE);
                parentElement = model;
            } else if (ELEMENT_DECISION.equals(xtr.getLocalName())) {
                Decision decision = new Decision();
                model.addDrgElement(decision);
                decision.setId(xtr.getAttributeValue(null, ATTRIBUTE_ID));
                decision.setName(xtr.getAttributeValue(null, ATTRIBUTE_NAME));
                parentElement = decision;
            } else if (ELEMENT_DECISION_TABLE.equals(xtr.getLocalName())) {
                DecisionTable currentDecisionTable = new DecisionTable();
                currentDecisionTable.setId(xtr.getAttributeValue(null, ATTRIBUTE_ID));

                if (xtr.getAttributeValue(null, ATTRIBUTE_HIT_POLICY) != null) {
                    currentDecisionTable
                            .setHitPolicy(HitPolicy.valueOf(xtr.getAttributeValue(null, ATTRIBUTE_HIT_POLICY)));
                } else {
                    currentDecisionTable.setHitPolicy(HitPolicy.FIRST);
                }

                model.getDrgElements().get(model.getDrgElements().size() - 1)
                        .setDecisionTable(currentDecisionTable);
                model.setCurrentDecisionTable(currentDecisionTable);
                parentElement = currentDecisionTable;
            } else if (ELEMENT_OUTPUT_CLAUSE.equals(xtr.getLocalName())) {
                OutputClause outputClause = new OutputClause();
                model.getCurrentDecisionTable().addOutput(outputClause);
                outputClause.setId(xtr.getAttributeValue(null, ATTRIBUTE_ID));
                outputClause.setLabel(xtr.getAttributeValue(null, ATTRIBUTE_LABEL));
                outputClause.setName(xtr.getAttributeValue(null, ATTRIBUTE_NAME));
                outputClause.setTypeRef(xtr.getAttributeValue(null, ATTRIBUTE_TYPE_REF));
                parentElement = outputClause;
            } else if (ELEMENT_DESCRIPTION.equals(xtr.getLocalName())) {
                parentElement.setDescription(xtr.getElementText());
            } else if (ELEMENT_EXTENSIONS.equals(xtr.getLocalName())) {
                while (xtr.hasNext()) {
                    xtr.next();
                    if (xtr.isStartElement()) {
                        DmnExtensionElement extensionElement = DmnXMLUtil.parseExtensionElement(xtr);
                        parentElement.addExtensionElement(extensionElement);
                    } else if (xtr.isEndElement()) {
                        if (ELEMENT_EXTENSIONS.equals(xtr.getLocalName())) {
                            break;
                        }
                    }
                }

            } else if (convertersToDmnMap.containsKey(xtr.getLocalName())) {
                BaseDmnXMLConverter converter = convertersToDmnMap.get(xtr.getLocalName());
                converter.convertToDmnModel(xtr, model);
            }
        }

        processDmnElements(model.getCurrentDecisionTable());

    } catch (DmnXMLException e) {
        throw e;

    } catch (Exception e) {
        LOGGER.error("Error processing DMN document", e);
        throw new DmnXMLException("Error processing DMN document", e);
    }
    return model;
}

From source file:org.apache.axiom.om.impl.builder.StAXBuilder.java

/**
 * Method discard./*from  ww w  .  j  a v  a 2  s  . c  o m*/
 *
 * @param element
 * @throws OMException
 */
public void discard(OMElement element) throws OMException {

    if (element.isComplete() || !cache) {
        throw new OMException();
    }
    try {

        // We simply cannot use the parser instance from the builder for this case
        // it is not safe to assume that the parser inside the builder will be in
        // sync with the parser of the element in question
        // Note 1 - however  calling getXMLStreamReaderWithoutCaching sets off two flags
        // the cache flag for this builder and the parserAccessed flag. These flags will be
        // reset later in this procedure

        int event = 0;
        XMLStreamReader elementParser = element.getXMLStreamReaderWithoutCaching();
        do {
            event = elementParser.next();
        } while (!(event == XMLStreamConstants.END_ELEMENT
                && element.getLocalName().equals(elementParser.getLocalName())));

        //at this point we are safely at the end_element event of the element we discarded
        lastNode = element.getPreviousOMSibling();

        // resetting the flags - see Note 1 above
        cache = true;
        parserAccessed = false;

        if (lastNode != null) {
            // if the last node is not an element, we are in trouble because leaf nodes
            // (such as text) cannot build themselves. worst the lastchild of the
            // currentparent is still the removed node! we have to correct it
            OMContainerEx ex = ((OMContainerEx) lastNode.getParent());
            ex.setLastChild(lastNode);
            if (!(lastNode instanceof OMContainerEx)) {
                ex.buildNext();
            } else {
                ((OMNodeEx) lastNode).setNextOMSibling(null);
            }

        } else {
            OMElement parent = (OMElement) element.getParent();
            if (parent == null) {
                throw new OMException();
            }
            ((OMContainerEx) parent).setFirstChild(null);
            lastNode = parent;
        }

    } catch (OMException e) {
        throw e;
    } catch (Exception e) {
        throw new OMException(e);
    }
    // when an element is discarded the element index that was incremented
    //at creation needs to be decremented !
    elementLevel--;
}

From source file:org.apache.axiom.om.impl.serialize.StreamingOMSerializer.java

/**
 * @param reader//from   ww w . j a  v a  2s. co  m
 * @param writer
 * @throws XMLStreamException
 */
protected void serializeElement(XMLStreamReader reader, XMLStreamWriter writer) throws XMLStreamException {

    // Note: To serialize the start tag, we must follow the order dictated by the JSR-173 (StAX) specification.
    // Please keep this code in sync with the code in OMSerializerUtil.serializeStartpart

    // The algorithm is:
    // ... generate writeStartElement
    //
    // ... generate setPrefix/setDefaultNamespace for each namespace declaration if the prefix is unassociated.
    // ... generate setPrefix/setDefaultNamespace if the prefix of the element is unassociated
    // ... generate setPrefix/setDefaultNamespace for each unassociated prefix of the attributes.
    //
    // ... generate writeNamespace/writerDefaultNamespace for the new namespace declarations determine during the "set" processing
    // ... generate writeAttribute for each attribute

    ArrayList writePrefixList = null;
    ArrayList writeNSList = null;

    // Get the prefix and namespace of the element.  "" and null are identical.
    String ePrefix = reader.getPrefix();
    ePrefix = (ePrefix != null && ePrefix.length() == 0) ? null : ePrefix;
    String eNamespace = reader.getNamespaceURI();
    eNamespace = (eNamespace != null && eNamespace.length() == 0) ? null : eNamespace;

    // Write the startElement if required
    if (eNamespace != null) {
        if (ePrefix == null) {
            if (!OMSerializerUtil.isAssociated("", eNamespace, writer)) {

                if (writePrefixList == null) {
                    writePrefixList = new ArrayList();
                    writeNSList = new ArrayList();
                }
                writePrefixList.add("");
                writeNSList.add(eNamespace);
            }

            writer.writeStartElement("", reader.getLocalName(), eNamespace);
        } else {

            if (!OMSerializerUtil.isAssociated(ePrefix, eNamespace, writer)) {
                if (writePrefixList == null) {
                    writePrefixList = new ArrayList();
                    writeNSList = new ArrayList();
                }
                writePrefixList.add(ePrefix);
                writeNSList.add(eNamespace);
            }

            writer.writeStartElement(ePrefix, reader.getLocalName(), eNamespace);
        }
    } else {
        writer.writeStartElement(reader.getLocalName());
    }

    // Generate setPrefix for the namespace declarations
    int count = reader.getNamespaceCount();
    for (int i = 0; i < count; i++) {
        String prefix = reader.getNamespacePrefix(i);
        prefix = (prefix != null && prefix.length() == 0) ? null : prefix;
        String namespace = reader.getNamespaceURI(i);
        namespace = (namespace != null && namespace.length() == 0) ? null : namespace;

        String newPrefix = OMSerializerUtil.generateSetPrefix(prefix, namespace, writer, false);
        // If this is a new association, remember it so that it can written out later
        if (newPrefix != null) {
            if (writePrefixList == null) {
                writePrefixList = new ArrayList();
                writeNSList = new ArrayList();
            }
            if (!writePrefixList.contains(newPrefix)) {
                writePrefixList.add(newPrefix);
                writeNSList.add(namespace);
            }
        }
    }

    // Generate setPrefix for the element
    // If the prefix is not associated with a namespace yet, remember it so that we can
    // write out a namespace declaration
    String newPrefix = OMSerializerUtil.generateSetPrefix(ePrefix, eNamespace, writer, false);
    // If this is a new association, remember it so that it can written out later
    if (newPrefix != null) {
        if (writePrefixList == null) {
            writePrefixList = new ArrayList();
            writeNSList = new ArrayList();
        }
        if (!writePrefixList.contains(newPrefix)) {
            writePrefixList.add(newPrefix);
            writeNSList.add(eNamespace);
        }
    }

    // Now Generate setPrefix for each attribute
    count = reader.getAttributeCount();
    for (int i = 0; i < count; i++) {
        String prefix = reader.getAttributePrefix(i);
        prefix = (prefix != null && prefix.length() == 0) ? null : prefix;
        String namespace = reader.getAttributeNamespace(i);
        namespace = (namespace != null && namespace.length() == 0) ? null : namespace;

        // Default prefix referencing is not allowed on an attribute
        if (prefix == null && namespace != null) {
            String writerPrefix = writer.getPrefix(namespace);
            writerPrefix = (writerPrefix != null && writerPrefix.length() == 0) ? null : writerPrefix;
            prefix = (writerPrefix != null) ? writerPrefix : generateUniquePrefix(writer.getNamespaceContext());
        }
        newPrefix = OMSerializerUtil.generateSetPrefix(prefix, namespace, writer, true);
        // If the prefix is not associated with a namespace yet, remember it so that we can
        // write out a namespace declaration
        if (newPrefix != null) {
            if (writePrefixList == null) {
                writePrefixList = new ArrayList();
                writeNSList = new ArrayList();
            }
            if (!writePrefixList.contains(newPrefix)) {
                writePrefixList.add(newPrefix);
                writeNSList.add(namespace);
            }
        }
    }

    // Now Generate setPrefix for each prefix referenced in an xsi:type
    // For example xsi:type="p:dataType"
    // The following code will make sure that setPrefix is called for "p".
    count = reader.getAttributeCount();
    for (int i = 0; i < count; i++) {
        String prefix = reader.getAttributePrefix(i);
        prefix = (prefix != null && prefix.length() == 0) ? null : prefix;
        String namespace = reader.getAttributeNamespace(i);
        namespace = (namespace != null && namespace.length() == 0) ? null : namespace;
        String localName = reader.getAttributeLocalName(i);

        if (XSI_URI.equals(namespace) && XSI_LOCAL_NAME.equals(localName)) {
            String value = reader.getAttributeValue(i);
            if (DEBUG_ENABLED) {
                log.debug("The value of xsi:type is " + value);
            }
            if (value != null) {
                value = value.trim();
                if (value.indexOf(":") > 0) {
                    String refPrefix = value.substring(0, value.indexOf(":"));
                    String refNamespace = reader.getNamespaceURI(refPrefix);
                    if (refNamespace != null && refNamespace.length() > 0) {

                        newPrefix = OMSerializerUtil.generateSetPrefix(refPrefix, refNamespace, writer, true);
                        // If the prefix is not associated with a namespace yet, remember it so that we can
                        // write out a namespace declaration
                        if (newPrefix != null) {
                            if (DEBUG_ENABLED) {
                                log.debug(
                                        "An xmlns:" + newPrefix + "=\"" + refNamespace + "\" will be written");
                            }
                            if (writePrefixList == null) {
                                writePrefixList = new ArrayList();
                                writeNSList = new ArrayList();
                            }
                            if (!writePrefixList.contains(newPrefix)) {
                                writePrefixList.add(newPrefix);
                                writeNSList.add(refNamespace);
                            }
                        }
                    }

                }
            }
        }
    }

    // Now write out the list of namespace declarations in this list that we constructed
    // while doing the "set" processing.
    if (writePrefixList != null) {
        for (int i = 0; i < writePrefixList.size(); i++) {
            String prefix = (String) writePrefixList.get(i);
            String namespace = (String) writeNSList.get(i);
            if (prefix != null) {
                if (namespace == null) {
                    writer.writeNamespace(prefix, "");
                } else {
                    writer.writeNamespace(prefix, namespace);
                }
            } else {
                writer.writeDefaultNamespace(namespace);
            }
        }
    }

    // Now write the attributes
    count = reader.getAttributeCount();
    for (int i = 0; i < count; i++) {
        String prefix = reader.getAttributePrefix(i);
        prefix = (prefix != null && prefix.length() == 0) ? null : prefix;
        String namespace = reader.getAttributeNamespace(i);
        namespace = (namespace != null && namespace.length() == 0) ? null : namespace;

        if (prefix == null && namespace != null) {
            // Default namespaces are not allowed on an attribute reference.
            // Earlier in this code, a unique prefix was added for this case...now obtain and use it
            prefix = writer.getPrefix(namespace);
            //XMLStreamWriter doesn't allow for getPrefix to know whether you're asking for the prefix
            //for an attribute or an element. So if the namespace matches the default namespace getPrefix will return
            //the empty string, as if it were an element, in all cases (even for attributes, and even if 
            //there was a prefix specifically set up for this), which is not the desired behavior.
            //Since the interface is base java, we can't fix it where we need to (by adding an attr boolean to 
            //XMLStreamWriter.getPrefix), so we hack it in here...
            if (prefix == null || "".equals(prefix)) {
                for (int j = 0; j < writePrefixList.size(); j++) {
                    if (namespace.equals((String) writeNSList.get(j))) {
                        prefix = (String) writePrefixList.get(j);
                    }
                }
            }
        } else if (namespace != null && !prefix.equals("xml")) {
            // Use the writer's prefix if it is different, but if the writers 
            // prefix is empty then do not replace because attributes do not
            // default to the default namespace like elements do.
            String writerPrefix = writer.getPrefix(namespace);
            if (!prefix.equals(writerPrefix) && !"".equals(writerPrefix)) {
                prefix = writerPrefix;
            }
        }
        if (namespace != null) {
            // Qualified attribute
            writer.writeAttribute(prefix, namespace, reader.getAttributeLocalName(i),
                    reader.getAttributeValue(i));
        } else {
            // Unqualified attribute
            writer.writeAttribute(reader.getAttributeLocalName(i), reader.getAttributeValue(i));
        }
    }
}

From source file:org.apache.jackrabbit.oak.benchmark.wikipedia.WikipediaImport.java

public int importWikipedia(Session session) throws Exception {
    long start = System.currentTimeMillis();
    int count = 0;
    int code = 0;

    if (doReport) {
        System.out.format("Importing %s...%n", dump);
    }/*from ww  w .j a  v  a2  s . c  o m*/

    String type = "nt:unstructured";
    if (session.getWorkspace().getNodeTypeManager().hasNodeType("oak:Unstructured")) {
        type = "oak:Unstructured";
    }
    Node wikipedia = session.getRootNode().addNode("wikipedia", type);

    int levels = 0;
    if (!flat) {
        // calculate the number of levels needed, based on the rough
        // estimate that the average XML size of a page is about 1kB
        for (long pages = dump.length() / 1024; pages > 256; pages /= 256) {
            levels++;
        }
    }

    String title = null;
    String text = null;
    XMLInputFactory factory = XMLInputFactory.newInstance();
    StreamSource source;
    if (dump.getName().endsWith(".xml")) {
        source = new StreamSource(dump);
    } else {
        CompressorStreamFactory csf = new CompressorStreamFactory();
        source = new StreamSource(
                csf.createCompressorInputStream(new BufferedInputStream(new FileInputStream(dump))));
    }
    haltImport = false;
    XMLStreamReader reader = factory.createXMLStreamReader(source);
    while (reader.hasNext() && !haltImport) {
        switch (reader.next()) {
        case XMLStreamConstants.START_ELEMENT:
            if ("title".equals(reader.getLocalName())) {
                title = reader.getElementText();
            } else if ("text".equals(reader.getLocalName())) {
                text = reader.getElementText();
            }
            break;
        case XMLStreamConstants.END_ELEMENT:
            if ("page".equals(reader.getLocalName())) {
                String name = Text.escapeIllegalJcrChars(title);
                Node parent = wikipedia;
                if (levels > 0) {
                    int n = name.length();
                    for (int i = 0; i < levels; i++) {
                        int hash = name.substring(min(i, n)).hashCode();
                        parent = JcrUtils.getOrAddNode(parent, String.format("%02x", hash & 0xff));
                    }
                }
                Node page = parent.addNode(name);
                page.setProperty("title", title);
                page.setProperty("text", text);
                code += title.hashCode();
                code += text.hashCode();
                count++;
                if (count % 1000 == 0) {
                    batchDone(session, start, count);
                }

                pageAdded(title, text);
            }
            break;
        }
    }

    session.save();

    if (doReport) {
        long millis = System.currentTimeMillis() - start;
        System.out.format("Imported %d pages in %d seconds (%.2fms/page)%n", count, millis / 1000,
                (double) millis / count);
    }

    return code;
}

From source file:org.apache.ode.utils.DOMUtils.java

private static void parse(XMLStreamReader reader, Document doc, Node parent) throws XMLStreamException {
    int event = reader.getEventType();

    while (reader.hasNext()) {
        switch (event) {
        case XMLStreamConstants.START_ELEMENT:
            // create element
            Element e = doc.createElementNS(reader.getNamespaceURI(), reader.getLocalName());
            if (reader.getPrefix() != null && reader.getPrefix() != "") {
                e.setPrefix(reader.getPrefix());
            }/*from  ww  w .j  av  a2s .  c  o m*/
            parent.appendChild(e);

            // copy namespaces
            for (int ns = 0; ns < reader.getNamespaceCount(); ns++) {
                String uri = reader.getNamespaceURI(ns);
                String prefix = reader.getNamespacePrefix(ns);
                declare(e, uri, prefix);
            }

            // copy attributes
            for (int att = 0; att < reader.getAttributeCount(); att++) {
                String name = reader.getAttributeLocalName(att);
                String prefix = reader.getAttributePrefix(att);
                if (prefix != null && prefix.length() > 0) {
                    name = prefix + ":" + name;
                }
                Attr attr = doc.createAttributeNS(reader.getAttributeNamespace(att), name);
                attr.setValue(reader.getAttributeValue(att));
                e.setAttributeNode(attr);
            }
            // sub-nodes
            if (reader.hasNext()) {
                reader.next();
                parse(reader, doc, e);
            }
            if (parent instanceof Document) {
                while (reader.hasNext())
                    reader.next();
                return;
            }
            break;
        case XMLStreamConstants.END_ELEMENT:
            return;
        case XMLStreamConstants.CHARACTERS:
            if (parent != null) {
                parent.appendChild(doc.createTextNode(reader.getText()));
            }
            break;
        case XMLStreamConstants.COMMENT:
            if (parent != null) {
                parent.appendChild(doc.createComment(reader.getText()));
            }
            break;
        case XMLStreamConstants.CDATA:
            parent.appendChild(doc.createCDATASection(reader.getText()));
            break;
        case XMLStreamConstants.PROCESSING_INSTRUCTION:
            parent.appendChild(doc.createProcessingInstruction(reader.getPITarget(), reader.getPIData()));
            break;
        case XMLStreamConstants.ENTITY_REFERENCE:
            parent.appendChild(doc.createProcessingInstruction(reader.getPITarget(), reader.getPIData()));
            break;
        case XMLStreamConstants.NAMESPACE:
        case XMLStreamConstants.ATTRIBUTE:
            break;
        default:
            break;
        }

        if (reader.hasNext()) {
            event = reader.next();
        }
    }
}

From source file:org.apache.solr.handler.AnalysisRequestHandler.java

NamedList<Object> processContent(XMLStreamReader parser, IndexSchema schema)
        throws XMLStreamException, IOException {
    NamedList<Object> result = new SimpleOrderedMap<Object>();
    while (true) {
        int event = parser.next();
        switch (event) {
        case XMLStreamConstants.END_DOCUMENT: {
            parser.close();/*from ww  w.ja  va  2 s. co m*/
            return result;
        }
        case XMLStreamConstants.START_ELEMENT: {
            String currTag = parser.getLocalName();
            if ("doc".equals(currTag)) {
                log.trace("Tokenizing doc...");

                SolrInputDocument doc = readDoc(parser);
                SchemaField uniq = schema.getUniqueKeyField();
                NamedList<NamedList<NamedList<Object>>> theTokens = new SimpleOrderedMap<NamedList<NamedList<Object>>>();
                result.add(doc.getFieldValue(uniq.getName()).toString(), theTokens);
                for (String name : doc.getFieldNames()) {
                    FieldType ft = schema.getFieldType(name);
                    Analyzer analyzer = ft.getAnalyzer();
                    Collection<Object> vals = doc.getFieldValues(name);
                    for (Object val : vals) {
                        Reader reader = new StringReader(val.toString());
                        TokenStream tstream = analyzer.tokenStream(name, reader);
                        NamedList<NamedList<Object>> tokens = getTokens(tstream);
                        theTokens.add(name, tokens);
                    }
                }
            }
            break;
        }
        }
    }
}