List of usage examples for javax.xml.stream XMLStreamConstants CHARACTERS
int CHARACTERS
To view the source code for javax.xml.stream XMLStreamConstants CHARACTERS.
Click Source Link
From source file:org.apache.axiom.util.stax.debug.XMLStreamReaderValidator.java
/** * Dump the current event of the delegate. *///from w w w .j av a 2 s. c o m protected void logParserState() { if (IS_ADV_DEBUG_ENABLED) { int currentEvent = super.getEventType(); switch (currentEvent) { case XMLStreamConstants.START_ELEMENT: log.trace("START_ELEMENT: "); log.trace(" QName: " + super.getName()); break; case XMLStreamConstants.START_DOCUMENT: log.trace("START_DOCUMENT: "); break; case XMLStreamConstants.CHARACTERS: log.trace("CHARACTERS: "); log.trace("[" + super.getText() + "]"); break; case XMLStreamConstants.CDATA: log.trace("CDATA: "); log.trace("[" + super.getText() + "]"); break; case XMLStreamConstants.END_ELEMENT: log.trace("END_ELEMENT: "); log.trace(" QName: " + super.getName()); break; case XMLStreamConstants.END_DOCUMENT: log.trace("END_DOCUMENT: "); break; case XMLStreamConstants.SPACE: log.trace("SPACE: "); log.trace("[" + super.getText() + "]"); break; case XMLStreamConstants.COMMENT: log.trace("COMMENT: "); log.trace("[" + super.getText() + "]"); break; case XMLStreamConstants.DTD: log.trace("DTD: "); log.trace("[" + super.getText() + "]"); break; case XMLStreamConstants.PROCESSING_INSTRUCTION: log.trace("PROCESSING_INSTRUCTION: "); log.trace(" [" + super.getPITarget() + "][" + super.getPIData() + "]"); break; case XMLStreamConstants.ENTITY_REFERENCE: log.trace("ENTITY_REFERENCE: "); log.trace(" " + super.getLocalName() + "[" + super.getText() + "]"); break; default: log.trace("UNKNOWN_STATE: " + currentEvent); } } }
From source file:org.apache.axiom.util.stax.XMLStreamReaderUtils.java
/** * Get a {@link DataHandler} for the binary data encoded in an element. The method supports * base64 encoded character data as well as optimized binary data through the * {@link DataHandlerReader} extension./* w ww . java2 s . c o m*/ * <p> * <em>Precondition</em>: the reader is on a {@link XMLStreamConstants#START_ELEMENT} * <p> * <em>Postcondition</em>: the reader is on the corresponding * {@link XMLStreamConstants#END_ELEMENT} * * @param reader the stream to read the data from * @return the binary data from the element */ public static DataHandler getDataHandlerFromElement(XMLStreamReader reader) throws XMLStreamException { int event = reader.next(); if (event == XMLStreamConstants.END_ELEMENT) { // This means that the element is actually empty -> return empty DataHandler return new DataHandler(new EmptyDataSource("application/octet-stream")); } else if (event != XMLStreamConstants.CHARACTERS) { throw new XMLStreamException("Expected a CHARACTER event"); } DataHandlerReader dhr = getDataHandlerReader(reader); if (dhr != null && dhr.isBinary()) { DataHandler dh = dhr.getDataHandler(); reader.next(); return dh; } else { WritableBlob blob = new MemoryBlob(); Writer out = new Base64DecodingOutputStreamWriter(blob.getOutputStream()); try { writeTextTo(reader, out); // Take into account that in non coalescing mode, there may be additional // CHARACTERS events loop: while (true) { switch (reader.next()) { case XMLStreamConstants.CHARACTERS: writeTextTo(reader, out); break; case XMLStreamConstants.END_ELEMENT: break loop; default: throw new XMLStreamException("Expected a CHARACTER event"); } } out.close(); } catch (IOException ex) { throw new XMLStreamException("Error during base64 decoding", ex); } return new DataHandler(new BlobDataSource(blob, "application/octet-string")); } }
From source file:org.apache.ode.il.OMUtils.java
public static OMElement toOM(Element src, OMFactory omf, OMContainer parent) { OMElement omElement = parent == null ? omf.createOMElement(src.getLocalName(), null) : omf.createOMElement(src.getLocalName(), null, parent); if (src.getNamespaceURI() != null) { if (src.getPrefix() != null) omElement.setNamespace(omf.createOMNamespace(src.getNamespaceURI(), src.getPrefix())); else/*w ww . j av a 2 s . com*/ omElement.declareDefaultNamespace(src.getNamespaceURI()); } if (parent == null) { NSContext nscontext = DOMUtils.getMyNSContext(src); injectNamespaces(omElement, nscontext.toMap()); } else { Map<String, String> nss = DOMUtils.getMyNamespaces(src); injectNamespaces(omElement, nss); } NamedNodeMap attrs = src.getAttributes(); for (int i = 0; i < attrs.getLength(); ++i) { Attr attr = (Attr) attrs.item(i); if (attr.getLocalName().equals("xmlns") || (attr.getNamespaceURI() != null && attr.getNamespaceURI().equals(DOMUtils.NS_URI_XMLNS))) continue; OMNamespace attrOmNs = null; String attrNs = attr.getNamespaceURI(); String attrPrefix = attr.getPrefix(); if (attrNs != null) attrOmNs = omElement.findNamespace(attrNs, null); if (attrOmNs == null && attrPrefix != null) attrOmNs = omElement.findNamespace(null, attrPrefix); omElement.addAttribute(attr.getLocalName(), attr.getValue(), attrOmNs); } NodeList children = src.getChildNodes(); for (int i = 0; i < children.getLength(); ++i) { Node n = children.item(i); switch (n.getNodeType()) { case Node.CDATA_SECTION_NODE: omElement.addChild(omf.createOMText(((CDATASection) n).getTextContent(), XMLStreamConstants.CDATA)); break; case Node.TEXT_NODE: omElement.addChild(omf.createOMText(((Text) n).getTextContent(), XMLStreamConstants.CHARACTERS)); break; case Node.ELEMENT_NODE: toOM((Element) n, omf, omElement); break; } } return omElement; }
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 v a 2s . 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
SolrInputDocument readDoc(XMLStreamReader parser) throws XMLStreamException { SolrInputDocument doc = new SolrInputDocument(); StringBuilder text = new StringBuilder(); String name = null;/* w w w. j a va 2 s .c o m*/ 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
/** * Reads the document from the given xml stream reader. The following document format is expected: * <p/>/*from ww w . ja v a 2 s . c om*/ * <pre><code> * <doc> * <field name="id">1</field> * <field name="name">The Name</field> * <field name="text">The Text Value</field> * </doc> * </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.3//from w w w . j a v a2 s . c o 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/* w w w. j ava 2s. c o 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.3// ww w . ja v a 2s . c o m */ void processDelete(UpdateRequestProcessor processor, XMLStreamReader parser) throws XMLStreamException, IOException { // Parse the command DeleteUpdateCommand deleteCmd = new DeleteUpdateCommand(); deleteCmd.fromPending = true; deleteCmd.fromCommitted = true; for (int i = 0; i < parser.getAttributeCount(); i++) { String attrName = parser.getAttributeLocalName(i); String attrVal = parser.getAttributeValue(i); if ("fromPending".equals(attrName)) { deleteCmd.fromPending = StrUtils.parseBoolean(attrVal); } else if ("fromCommitted".equals(attrName)) { deleteCmd.fromCommitted = StrUtils.parseBoolean(attrVal); } else { XmlUpdateRequestHandler.log.warn("unexpected attribute delete/@" + 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))) { XmlUpdateRequestHandler.log.warn("unexpected XML tag /delete/" + mode); throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "unexpected XML tag /delete/" + mode); } text.setLength(0); break; case XMLStreamConstants.END_ELEMENT: String currTag = parser.getLocalName(); if ("id".equals(currTag)) { deleteCmd.id = text.toString(); } else if ("query".equals(currTag)) { deleteCmd.query = text.toString(); } else if ("delete".equals(currTag)) { return; } else { XmlUpdateRequestHandler.log.warn("unexpected XML tag /delete/" + currTag); throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "unexpected XML tag /delete/" + currTag); } processor.processDelete(deleteCmd); deleteCmd.id = null; deleteCmd.query = null; 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.XMLLoader.java
/** * Given the input stream, read a document * * @since solr 1.3//from w ww . j a v a 2s . c o m */ 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 { XmlUpdateRequestHandler.log.warn("Unknown attribute doc/@" + attrName); } } StringBuilder text = new StringBuilder(); String name = null; 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)) { XmlUpdateRequestHandler.log.warn("unexpected XML tag doc/" + localName); throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "unexpected XML tag doc/" + localName); } boost = 1.0f; 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 { XmlUpdateRequestHandler.log.warn("Unknown attribute doc/field/@" + attrName); } } break; } } }