List of usage examples for javax.xml.stream XMLStreamConstants SPACE
int SPACE
To view the source code for javax.xml.stream XMLStreamConstants SPACE.
Click Source Link
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 w w . jav a2s . co 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/>// ww w .j a v a 2 s . c o m * <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 .ja v a 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 w w 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.3/*from w ww.java2s . c om*/ */ 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 ww w . ja v a2s . 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; } } }
From source file:org.apache.synapse.commons.json.JsonDataSource.java
public void serialize(XMLStreamWriter xmlWriter) throws XMLStreamException { XMLStreamReader reader = getReader(); xmlWriter.writeStartDocument();//from w w w. ja v a 2 s. c o m while (reader.hasNext()) { int x = reader.next(); switch (x) { case XMLStreamConstants.START_ELEMENT: xmlWriter.writeStartElement(reader.getPrefix(), reader.getLocalName(), reader.getNamespaceURI()); int namespaceCount = reader.getNamespaceCount(); for (int i = namespaceCount - 1; i >= 0; i--) { xmlWriter.writeNamespace(reader.getNamespacePrefix(i), reader.getNamespaceURI(i)); } int attributeCount = reader.getAttributeCount(); for (int i = 0; i < attributeCount; i++) { xmlWriter.writeAttribute(reader.getAttributePrefix(i), reader.getAttributeNamespace(i), reader.getAttributeLocalName(i), reader.getAttributeValue(i)); } break; case XMLStreamConstants.START_DOCUMENT: break; case XMLStreamConstants.CHARACTERS: xmlWriter.writeCharacters(reader.getText()); break; case XMLStreamConstants.CDATA: xmlWriter.writeCData(reader.getText()); break; case XMLStreamConstants.END_ELEMENT: xmlWriter.writeEndElement(); break; case XMLStreamConstants.END_DOCUMENT: xmlWriter.writeEndDocument(); break; case XMLStreamConstants.SPACE: break; case XMLStreamConstants.COMMENT: xmlWriter.writeComment(reader.getText()); break; case XMLStreamConstants.DTD: xmlWriter.writeDTD(reader.getText()); break; case XMLStreamConstants.PROCESSING_INSTRUCTION: xmlWriter.writeProcessingInstruction(reader.getPITarget(), reader.getPIData()); break; case XMLStreamConstants.ENTITY_REFERENCE: xmlWriter.writeEntityRef(reader.getLocalName()); break; default: throw new OMException(); } } xmlWriter.writeEndDocument(); xmlWriter.flush(); xmlWriter.close(); }
From source file:org.apereo.portal.xml.stream.IndentingXMLEventWriter.java
@Override public void add(XMLEvent event) throws XMLStreamException { switch (event.getEventType()) { case XMLStreamConstants.CHARACTERS: case XMLStreamConstants.CDATA: case XMLStreamConstants.SPACE: { wrappedWriter.add(event);/* w ww . ja v a 2 s .c o m*/ afterData(); return; } case XMLStreamConstants.START_ELEMENT: { beforeStartElement(); wrappedWriter.add(event); afterStartElement(); return; } case XMLStreamConstants.END_ELEMENT: { beforeEndElement(); wrappedWriter.add(event); afterEndElement(); return; } case XMLStreamConstants.START_DOCUMENT: case XMLStreamConstants.PROCESSING_INSTRUCTION: case XMLStreamConstants.COMMENT: case XMLStreamConstants.DTD: { beforeMarkup(); wrappedWriter.add(event); afterMarkup(); return; } case XMLStreamConstants.END_DOCUMENT: { wrappedWriter.add(event); afterEndDocument(); break; } default: { wrappedWriter.add(event); return; } } }
From source file:org.auraframework.impl.factory.SVGParser.java
@Override public SVGDef getDefinition(DefDescriptor<SVGDef> descriptor, TextSource<SVGDef> source) throws SVGParserException, QuickFixException { if (descriptor.getDefType() == DefType.SVG) { XMLStreamReader reader = null; String contents = source.getContents(); //If the file is too big throw before we parse the whole thing. SVGDef ret = new SVGDefHandler<>(descriptor, source).createDefinition(); try (StringReader stringReader = new StringReader(contents)) { reader = xmlInputFactory.createXMLStreamReader(stringReader); if (reader != null) { LOOP: while (reader.hasNext()) { int type = reader.next(); switch (type) { case XMLStreamConstants.END_DOCUMENT: break LOOP; //This is plain text inside the file case XMLStreamConstants.CHARACTERS: if (DISSALOWED_LIST.matcher(reader.getText()).matches()) { throw new InvalidDefinitionException( String.format("Text contains disallowed symbols: %s", reader.getText()), XMLParser.getLocation(reader, source)); }// w w w . j a v a2s . c om break; case XMLStreamConstants.START_ELEMENT: String name = reader.getName().toString().toLowerCase(); if (!SVG_TAG_WHITELIST.contains(name)) { throw new InvalidDefinitionException( String.format("Invalid SVG tag specified: %s", name), XMLParser.getLocation(reader, source)); } for (int i = 0; i < reader.getAttributeCount(); i++) { QName qAttr = reader.getAttributeName(i); String attr = qAttr.getLocalPart(); if (SVG_ATTR_BLACKLIST.contains(attr)) { throw new InvalidDefinitionException( String.format("Invalid SVG attribute specified: %s", attr), XMLParser.getLocation(reader, source)); } } break; case XMLStreamConstants.END_ELEMENT: case XMLStreamConstants.COMMENT: case XMLStreamConstants.DTD: case XMLStreamConstants.SPACE: continue; default: throw new InvalidDefinitionException(String.format("Found unexpected element in xml."), XMLParser.getLocation(reader, source)); } } } } catch (XMLStreamException e) { throw new SVGParserException(StringEscapeUtils.escapeHtml4(e.getMessage())); } finally { if (reader != null) { try { reader.close(); } catch (XMLStreamException e) { //Well I tried to play nicely } } } return ret; } return null; }
From source file:org.auraframework.impl.svg.parser.SVGParser.java
@Override public SVGDef parse(DefDescriptor<SVGDef> descriptor, Source<SVGDef> source) throws SVGParserException, QuickFixException { if (descriptor.getDefType() == DefType.SVG) { XMLStreamReader reader = null; String contents = source.getContents(); //If the file is too big throw before we parse the whole thing. SVGDef ret = new SVGDefHandler<>(descriptor, source).createDefinition(); try (StringReader stringReader = new StringReader(contents)) { reader = xmlInputFactory.createXMLStreamReader(stringReader); if (reader != null) { LOOP: while (reader.hasNext()) { int type = reader.next(); switch (type) { case XMLStreamConstants.END_DOCUMENT: break LOOP; //This is plain text inside the file case XMLStreamConstants.CHARACTERS: if (DISSALOWED_LIST.matcher(reader.getText()).matches()) { throw new InvalidDefinitionException( String.format("Text contains disallowed symbols: %s", reader.getText()), XMLParser.getLocation(reader, source)); }/* w w w . j a va 2 s .co m*/ break; case XMLStreamConstants.START_ELEMENT: String name = reader.getName().toString().toLowerCase(); if (!SVG_TAG_WHITELIST.contains(name)) { throw new InvalidDefinitionException( String.format("Invalid SVG tag specified: %s", name), XMLParser.getLocation(reader, source)); } for (int i = 0; i < reader.getAttributeCount(); i++) { QName qAttr = reader.getAttributeName(i); String attr = qAttr.getLocalPart(); if (SVG_ATTR_BLACKLIST.contains(attr)) { throw new InvalidDefinitionException( String.format("Invalid SVG attribute specified: %s", attr), XMLParser.getLocation(reader, source)); } } break; case XMLStreamConstants.END_ELEMENT: case XMLStreamConstants.COMMENT: case XMLStreamConstants.DTD: case XMLStreamConstants.SPACE: continue; default: throw new InvalidDefinitionException(String.format("Found unexpected element in xml."), XMLParser.getLocation(reader, source)); } } } } catch (XMLStreamException e) { throw new SVGParserException(StringEscapeUtils.escapeHtml4(e.getMessage())); } finally { if (reader != null) { try { reader.close(); } catch (XMLStreamException e) { //Well I tried to play nicely } } } return ret; } return null; }