Example usage for javax.xml.stream XMLStreamReader next

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

Introduction

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

Prototype

public int next() throws XMLStreamException;

Source Link

Document

Get next parsing event - a processor may return all contiguous character data in a single chunk, or it may split it into several chunks.

Usage

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);
    }// w w w.j a  va2s  .  c om

    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());
            }//  w  ww . j a va 2 s.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();// w  ww  .  ja v  a2s.c  o 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;
        }
        }
    }
}

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

SolrInputDocument readDoc(XMLStreamReader parser) throws XMLStreamException {
    SolrInputDocument doc = new SolrInputDocument();

    StringBuilder text = new StringBuilder();
    String name = null;//from   w ww. j  ava  2 s  .  com
    String attrName = "";
    float boost = 1.0f;
    boolean isNull = false;
    while (true) {
        int event = parser.next();
        switch (event) {
        // Add everything to the text
        case XMLStreamConstants.SPACE:
        case XMLStreamConstants.CDATA:
        case XMLStreamConstants.CHARACTERS:
            text.append(parser.getText());
            break;

        case XMLStreamConstants.END_ELEMENT:
            if ("doc".equals(parser.getLocalName())) {
                return doc;
            } else if ("field".equals(parser.getLocalName())) {
                if (!isNull) {
                    doc.addField(name, text.toString(), boost);
                    boost = 1.0f;
                }
            }
            break;

        case XMLStreamConstants.START_ELEMENT:
            text.setLength(0);
            String localName = parser.getLocalName();
            if (!"field".equals(localName)) {
                log.warn("unexpected XML tag doc/" + localName);
                throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
                        "unexpected XML tag doc/" + localName);
            }

            String attrVal = "";
            for (int i = 0; i < parser.getAttributeCount(); i++) {
                attrName = parser.getAttributeLocalName(i);
                attrVal = parser.getAttributeValue(i);
                if ("name".equals(attrName)) {
                    name = attrVal;
                }
            }
            break;
        }
    }
}

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

/**
 * Resolves the {@link DocumentAnalysisRequest} from the given solr request.
 *
 * @param req The solr request./*from w  w w . j a  va 2  s.c  o  m*/
 *
 * @return The resolved document analysis request.
 *
 * @throws IOException        Thrown when reading/parsing the content stream of the request fails.
 * @throws XMLStreamException Thrown when reading/parsing the content stream of the request fails.
 */
DocumentAnalysisRequest resolveAnalysisRequest(SolrQueryRequest req) throws IOException, XMLStreamException {

    DocumentAnalysisRequest request = new DocumentAnalysisRequest();

    SolrParams params = req.getParams();

    String query = params.get(AnalysisParams.QUERY, params.get(CommonParams.Q, null));
    request.setQuery(query);

    boolean showMatch = params.getBool(AnalysisParams.SHOW_MATCH, false);
    request.setShowMatch(showMatch);

    ContentStream stream = extractSingleContentStream(req);
    InputStream is = null;
    XMLStreamReader parser = null;

    try {
        is = stream.getStream();
        final String charset = ContentStreamBase.getCharsetFromContentType(stream.getContentType());
        parser = (charset == null) ? inputFactory.createXMLStreamReader(is)
                : inputFactory.createXMLStreamReader(is, charset);

        while (true) {
            int event = parser.next();
            switch (event) {
            case XMLStreamConstants.END_DOCUMENT: {
                parser.close();
                return request;
            }
            case XMLStreamConstants.START_ELEMENT: {
                String currTag = parser.getLocalName();
                if ("doc".equals(currTag)) {
                    log.trace("Reading doc...");
                    SolrInputDocument document = readDocument(parser, req.getSchema());
                    request.addDocument(document);
                }
                break;
            }
            }
        }

    } finally {
        if (parser != null)
            parser.close();
        IOUtils.closeQuietly(is);
    }
}

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

/**
 * Reads the document from the given xml stream reader. The following document format is expected:
 * <p/>/* w  ww .j  a  v a 2s.  c o m*/
 * <pre><code>
 * &lt;doc&gt;
 *    &lt;field name="id"&gt;1&lt;/field&gt;
 *    &lt;field name="name"&gt;The Name&lt;/field&gt;
 *    &lt;field name="text"&gt;The Text Value&lt;/field&gt;
 * &lt;/doc&gt;
 * </code></pre>
 * <p/>
 * <p/>
 * <em>NOTE: each read document is expected to have at least one field which serves as the unique key.</em>
 *
 * @param reader The {@link XMLStreamReader} from which the document will be read.
 * @param schema The index schema. The schema is used to validate that the read document has a unique key field.
 *
 * @return The read document.
 *
 * @throws XMLStreamException When reading of the document fails.
 */
SolrInputDocument readDocument(XMLStreamReader reader, IndexSchema schema) throws XMLStreamException {
    SolrInputDocument doc = new SolrInputDocument();

    String uniqueKeyField = schema.getUniqueKeyField().getName();

    StringBuilder text = new StringBuilder();
    String fieldName = null;
    boolean hasId = false;

    while (true) {
        int event = reader.next();
        switch (event) {
        // Add everything to the text
        case XMLStreamConstants.SPACE:
        case XMLStreamConstants.CDATA:
        case XMLStreamConstants.CHARACTERS:
            text.append(reader.getText());
            break;

        case XMLStreamConstants.END_ELEMENT:
            if ("doc".equals(reader.getLocalName())) {
                if (!hasId) {
                    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
                            "All documents must contain a unique key value: '" + doc.toString() + "'");
                }
                return doc;
            } else if ("field".equals(reader.getLocalName())) {
                doc.addField(fieldName, text.toString(), DEFAULT_BOOST);
                if (uniqueKeyField.equals(fieldName)) {
                    hasId = true;
                }
            }
            break;

        case XMLStreamConstants.START_ELEMENT:
            text.setLength(0);
            String localName = reader.getLocalName();
            if (!"field".equals(localName)) {
                log.warn("unexpected XML tag doc/" + localName);
                throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
                        "unexpected XML tag doc/" + localName);
            }

            for (int i = 0; i < reader.getAttributeCount(); i++) {
                String attrName = reader.getAttributeLocalName(i);
                if ("name".equals(attrName)) {
                    fieldName = reader.getAttributeValue(i);
                }
            }
            break;
        }
    }
}

From source file:org.apache.solr.handler.loader.XMLLoader.java

/**
 * @since solr 1.2//  w  w w .ja  v a2  s .c  o m
 */
void processUpdate(SolrQueryRequest req, UpdateRequestProcessor processor, XMLStreamReader parser)
        throws XMLStreamException, IOException, FactoryConfigurationError {
    AddUpdateCommand addCmd = null;
    SolrParams params = req.getParams();
    while (true) {
        int event = parser.next();
        switch (event) {
        case XMLStreamConstants.END_DOCUMENT:
            parser.close();
            return;

        case XMLStreamConstants.START_ELEMENT:
            String currTag = parser.getLocalName();
            if (currTag.equals(UpdateRequestHandler.ADD)) {
                log.trace("SolrCore.update(add)");

                addCmd = new AddUpdateCommand(req);

                // First look for commitWithin parameter on the request, will be overwritten for individual <add>'s
                addCmd.commitWithin = params.getInt(UpdateParams.COMMIT_WITHIN, -1);
                addCmd.overwrite = params.getBool(UpdateParams.OVERWRITE, true);

                for (int i = 0; i < parser.getAttributeCount(); i++) {
                    String attrName = parser.getAttributeLocalName(i);
                    String attrVal = parser.getAttributeValue(i);
                    if (UpdateRequestHandler.OVERWRITE.equals(attrName)) {
                        addCmd.overwrite = StrUtils.parseBoolean(attrVal);
                    } else if (UpdateRequestHandler.COMMIT_WITHIN.equals(attrName)) {
                        addCmd.commitWithin = Integer.parseInt(attrVal);
                    } else {
                        log.warn("XML element <add> has invalid XML attr: " + attrName);
                    }
                }

            } else if ("doc".equals(currTag)) {
                if (addCmd != null) {
                    log.trace("adding doc...");
                    addCmd.clear();
                    addCmd.solrDoc = readDoc(parser);
                    processor.processAdd(addCmd);
                } else {
                    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
                            "Unexpected <doc> tag without an <add> tag surrounding it.");
                }
            } else if (UpdateRequestHandler.COMMIT.equals(currTag)
                    || UpdateRequestHandler.OPTIMIZE.equals(currTag)) {
                log.trace("parsing " + currTag);

                CommitUpdateCommand cmd = new CommitUpdateCommand(req,
                        UpdateRequestHandler.OPTIMIZE.equals(currTag));
                ModifiableSolrParams mp = new ModifiableSolrParams();

                for (int i = 0; i < parser.getAttributeCount(); i++) {
                    String attrName = parser.getAttributeLocalName(i);
                    String attrVal = parser.getAttributeValue(i);
                    mp.set(attrName, attrVal);
                }

                RequestHandlerUtils.validateCommitParams(mp);
                SolrParams p = SolrParams.wrapDefaults(mp, req.getParams()); // default to the normal request params for commit options
                RequestHandlerUtils.updateCommit(cmd, p);

                processor.processCommit(cmd);
            } // end commit
            else if (UpdateRequestHandler.ROLLBACK.equals(currTag)) {
                log.trace("parsing rollback");

                RollbackUpdateCommand cmd = new RollbackUpdateCommand(req);

                processor.processRollback(cmd);
            } // end rollback
            else if (UpdateRequestHandler.DELETE.equals(currTag)) {
                log.trace("parsing delete");
                processDelete(req, processor, parser);
            } // end delete
            break;
        }
    }
}

From source file:org.apache.solr.handler.loader.XMLLoader.java

/**
 * @since solr 1.3/*w  ww .  java 2 s.co  m*/
 */
void processDelete(SolrQueryRequest req, UpdateRequestProcessor processor, XMLStreamReader parser)
        throws XMLStreamException, IOException {
    // Parse the command
    DeleteUpdateCommand deleteCmd = new DeleteUpdateCommand(req);

    // First look for commitWithin parameter on the request, will be overwritten for individual <delete>'s
    SolrParams params = req.getParams();
    deleteCmd.commitWithin = params.getInt(UpdateParams.COMMIT_WITHIN, -1);

    for (int i = 0; i < parser.getAttributeCount(); i++) {
        String attrName = parser.getAttributeLocalName(i);
        String attrVal = parser.getAttributeValue(i);
        if ("fromPending".equals(attrName)) {
            // deprecated
        } else if ("fromCommitted".equals(attrName)) {
            // deprecated
        } else if (UpdateRequestHandler.COMMIT_WITHIN.equals(attrName)) {
            deleteCmd.commitWithin = Integer.parseInt(attrVal);
        } else {
            log.warn("XML element <delete> has invalid XML attr: " + attrName);
        }
    }

    StringBuilder text = new StringBuilder();
    while (true) {
        int event = parser.next();
        switch (event) {
        case XMLStreamConstants.START_ELEMENT:
            String mode = parser.getLocalName();
            if (!("id".equals(mode) || "query".equals(mode))) {
                String msg = "XML element <delete> has invalid XML child element: " + mode;
                log.warn(msg);
                throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, msg);
            }
            text.setLength(0);

            if ("id".equals(mode)) {
                for (int i = 0; i < parser.getAttributeCount(); i++) {
                    String attrName = parser.getAttributeLocalName(i);
                    String attrVal = parser.getAttributeValue(i);
                    if (UpdateRequestHandler.VERSION.equals(attrName)) {
                        deleteCmd.setVersion(Long.parseLong(attrVal));
                    }
                    if (UpdateRequest.ROUTE.equals(attrName)) {
                        deleteCmd.setRoute(attrVal);
                    }
                }
            }
            break;

        case XMLStreamConstants.END_ELEMENT:
            String currTag = parser.getLocalName();
            if ("id".equals(currTag)) {
                deleteCmd.setId(text.toString());
            } else if ("query".equals(currTag)) {
                deleteCmd.setQuery(text.toString());
            } else if ("delete".equals(currTag)) {
                return;
            } else {
                String msg = "XML element <delete> has invalid XML (closing) child element: " + currTag;
                log.warn(msg);
                throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, msg);
            }
            processor.processDelete(deleteCmd);
            deleteCmd.clear();
            break;

        // Add everything to the text
        case XMLStreamConstants.SPACE:
        case XMLStreamConstants.CDATA:
        case XMLStreamConstants.CHARACTERS:
            text.append(parser.getText());
            break;
        }
    }
}

From source file:org.apache.solr.handler.loader.XMLLoader.java

/**
 * Given the input stream, read a document
 *
 * @since solr 1.3/*from ww  w . j a  v  a 2 s . co  m*/
 */
public SolrInputDocument readDoc(XMLStreamReader parser) throws XMLStreamException {
    SolrInputDocument doc = new SolrInputDocument();

    String attrName = "";
    for (int i = 0; i < parser.getAttributeCount(); i++) {
        attrName = parser.getAttributeLocalName(i);
        if ("boost".equals(attrName)) {
            doc.setDocumentBoost(Float.parseFloat(parser.getAttributeValue(i)));
        } else {
            log.warn("XML element <doc> has invalid XML attr:" + attrName);
        }
    }

    StringBuilder text = new StringBuilder();
    String name = null;
    float boost = 1.0f;
    boolean isNull = false;
    String update = null;
    Collection<SolrInputDocument> subDocs = null;
    Map<String, Map<String, Object>> updateMap = null;
    boolean complete = false;
    while (!complete) {
        int event = parser.next();
        switch (event) {
        // Add everything to the text
        case XMLStreamConstants.SPACE:
        case XMLStreamConstants.CDATA:
        case XMLStreamConstants.CHARACTERS:
            text.append(parser.getText());
            break;

        case XMLStreamConstants.END_ELEMENT:
            if ("doc".equals(parser.getLocalName())) {
                if (subDocs != null && !subDocs.isEmpty()) {
                    doc.addChildDocuments(subDocs);
                    subDocs = null;
                }
                complete = true;
                break;
            } else if ("field".equals(parser.getLocalName())) {
                // should I warn in some text has been found too
                Object v = isNull ? null : text.toString();
                if (update != null) {
                    if (updateMap == null)
                        updateMap = new HashMap<>();
                    Map<String, Object> extendedValues = updateMap.get(name);
                    if (extendedValues == null) {
                        extendedValues = new HashMap<>(1);
                        updateMap.put(name, extendedValues);
                    }
                    Object val = extendedValues.get(update);
                    if (val == null) {
                        extendedValues.put(update, v);
                    } else {
                        // multiple val are present
                        if (val instanceof List) {
                            List list = (List) val;
                            list.add(v);
                        } else {
                            List<Object> values = new ArrayList<>();
                            values.add(val);
                            values.add(v);
                            extendedValues.put(update, values);
                        }
                    }
                    break;
                }
                doc.addField(name, v, boost);
                boost = 1.0f;
                // field is over
                name = null;
            }
            break;

        case XMLStreamConstants.START_ELEMENT:
            text.setLength(0);
            String localName = parser.getLocalName();
            if ("doc".equals(localName)) {
                if (subDocs == null)
                    subDocs = Lists.newArrayList();
                subDocs.add(readDoc(parser));
            } else {
                if (!"field".equals(localName)) {
                    String msg = "XML element <doc> has invalid XML child element: " + localName;
                    log.warn(msg);
                    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, msg);
                }
                boost = 1.0f;
                update = null;
                isNull = false;
                String attrVal = "";
                for (int i = 0; i < parser.getAttributeCount(); i++) {
                    attrName = parser.getAttributeLocalName(i);
                    attrVal = parser.getAttributeValue(i);
                    if (NAME.equals(attrName)) {
                        name = attrVal;
                    } else if ("boost".equals(attrName)) {
                        boost = Float.parseFloat(attrVal);
                    } else if ("null".equals(attrName)) {
                        isNull = StrUtils.parseBoolean(attrVal);
                    } else if ("update".equals(attrName)) {
                        update = attrVal;
                    } else {
                        log.warn("XML element <field> has invalid XML attr: " + attrName);
                    }
                }
            }
            break;
        }
    }

    if (updateMap != null) {
        for (Map.Entry<String, Map<String, Object>> entry : updateMap.entrySet()) {
            name = entry.getKey();
            Map<String, Object> value = entry.getValue();
            doc.addField(name, value, 1.0f);
        }
    }

    return doc;
}

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

/**
 * @since solr 1.2/*from  www .j  a va  2s  .com*/
 */
void processUpdate(SolrQueryRequest req, UpdateRequestProcessor processor, XMLStreamReader parser)
        throws XMLStreamException, IOException, FactoryConfigurationError, InstantiationException,
        IllegalAccessException, TransformerConfigurationException {
    AddUpdateCommand addCmd = null;
    // Need to instansiate a SolrParams, even if req is null, for backward compat with legacyUpdate
    SolrParams params = (req != null) ? req.getParams() : new ModifiableSolrParams();
    while (true) {
        int event = parser.next();
        switch (event) {
        case XMLStreamConstants.END_DOCUMENT:
            parser.close();
            return;

        case XMLStreamConstants.START_ELEMENT:
            String currTag = parser.getLocalName();
            if (currTag.equals(XmlUpdateRequestHandler.ADD)) {
                XmlUpdateRequestHandler.log.trace("SolrCore.update(add)");

                addCmd = new AddUpdateCommand();

                // First look for commitWithin parameter on the request, will be overwritten for individual <add>'s
                addCmd.commitWithin = params.getInt(UpdateParams.COMMIT_WITHIN, -1);

                boolean overwrite = true; // the default

                Boolean overwritePending = null;
                Boolean overwriteCommitted = null;
                for (int i = 0; i < parser.getAttributeCount(); i++) {
                    String attrName = parser.getAttributeLocalName(i);
                    String attrVal = parser.getAttributeValue(i);
                    if (XmlUpdateRequestHandler.OVERWRITE.equals(attrName)) {
                        overwrite = StrUtils.parseBoolean(attrVal);
                    } else if (XmlUpdateRequestHandler.ALLOW_DUPS.equals(attrName)) {
                        overwrite = !StrUtils.parseBoolean(attrVal);
                    } else if (XmlUpdateRequestHandler.COMMIT_WITHIN.equals(attrName)) {
                        addCmd.commitWithin = Integer.parseInt(attrVal);
                    } else if (XmlUpdateRequestHandler.OVERWRITE_PENDING.equals(attrName)) {
                        overwritePending = StrUtils.parseBoolean(attrVal);
                    } else if (XmlUpdateRequestHandler.OVERWRITE_COMMITTED.equals(attrName)) {
                        overwriteCommitted = StrUtils.parseBoolean(attrVal);
                    } else {
                        XmlUpdateRequestHandler.log.warn("Unknown attribute id in add:" + attrName);
                    }
                }

                // check if these flags are set
                if (overwritePending != null && overwriteCommitted != null) {
                    if (overwritePending != overwriteCommitted) {
                        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
                                "can't have different values for 'overwritePending' and 'overwriteCommitted'");
                    }
                    overwrite = overwritePending;
                }
                addCmd.overwriteCommitted = overwrite;
                addCmd.overwritePending = overwrite;
                addCmd.allowDups = !overwrite;
            } else if ("doc".equals(currTag)) {
                //            if(addCmd != null) {
                XmlUpdateRequestHandler.log.trace("adding doc...");
                addCmd.clear();
                addCmd.solrDoc = readDoc(parser);
                processor.processAdd(addCmd);
                //            } else {
                //              throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Unexpected <doc> tag without an <add> tag surrounding it.");
                //            }
            } else if (XmlUpdateRequestHandler.COMMIT.equals(currTag)
                    || XmlUpdateRequestHandler.OPTIMIZE.equals(currTag)) {
                XmlUpdateRequestHandler.log.trace("parsing " + currTag);

                CommitUpdateCommand cmd = new CommitUpdateCommand(
                        XmlUpdateRequestHandler.OPTIMIZE.equals(currTag));

                boolean sawWaitSearcher = false, sawWaitFlush = false;
                for (int i = 0; i < parser.getAttributeCount(); i++) {
                    String attrName = parser.getAttributeLocalName(i);
                    String attrVal = parser.getAttributeValue(i);
                    if (XmlUpdateRequestHandler.WAIT_FLUSH.equals(attrName)) {
                        cmd.waitFlush = StrUtils.parseBoolean(attrVal);
                        sawWaitFlush = true;
                    } else if (XmlUpdateRequestHandler.WAIT_SEARCHER.equals(attrName)) {
                        cmd.waitSearcher = StrUtils.parseBoolean(attrVal);
                        sawWaitSearcher = true;
                    } else if (UpdateParams.MAX_OPTIMIZE_SEGMENTS.equals(attrName)) {
                        cmd.maxOptimizeSegments = Integer.parseInt(attrVal);
                    } else if (UpdateParams.EXPUNGE_DELETES.equals(attrName)) {
                        cmd.expungeDeletes = StrUtils.parseBoolean(attrVal);
                    } else {
                        XmlUpdateRequestHandler.log.warn("unexpected attribute commit/@" + attrName);
                    }
                }

                // If waitFlush is specified and waitSearcher wasn't, then
                // clear waitSearcher.
                if (sawWaitFlush && !sawWaitSearcher) {
                    cmd.waitSearcher = false;
                }
                processor.processCommit(cmd);
            } // end commit
            else if (XmlUpdateRequestHandler.ROLLBACK.equals(currTag)) {
                XmlUpdateRequestHandler.log.trace("parsing " + currTag);

                RollbackUpdateCommand cmd = new RollbackUpdateCommand();

                processor.processRollback(cmd);
            } // end rollback
            else if (XmlUpdateRequestHandler.DELETE.equals(currTag)) {
                XmlUpdateRequestHandler.log.trace("parsing delete");
                processDelete(processor, parser);
            } // end delete
            break;
        }
    }
}