List of usage examples for javax.xml.stream XMLStreamConstants END_ELEMENT
int END_ELEMENT
To view the source code for javax.xml.stream XMLStreamConstants END_ELEMENT.
Click Source Link
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 w w 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()); }//w w w .ja 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.olingo.fit.utils.AbstractXMLUtilities.java
private void skipElement(final StartElement start, final XMLEventReader reader, final XMLEventWriter writer, final boolean excludeStart) throws Exception { if (!excludeStart) { writeEvent(start, writer);/*from ww w . j a va 2s .c om*/ } int depth = 1; boolean found = false; while (reader.hasNext() && !found) { final XMLEvent event = reader.nextEvent(); writeEvent(event, writer); if (event.getEventType() == XMLStreamConstants.START_ELEMENT) { depth++; } else if (event.getEventType() == XMLStreamConstants.END_ELEMENT) { depth--; found = depth == 0 && start.getName().equals(event.asEndElement().getName()); } } }
From source file:org.apache.olingo.fit.utils.AbstractXMLUtilities.java
public XmlElement getXmlElement(final StartElement start, final XMLEventReader reader) throws Exception { final XmlElement res = new XmlElement(); res.setStart(start);//from w w w . ja va2 s . c o m StringWriter content = new StringWriter(); int depth = 1; while (reader.hasNext() && depth > 0) { final XMLEvent event = reader.nextEvent(); if (event.getEventType() == XMLStreamConstants.START_ELEMENT) { depth++; } else if (event.getEventType() == XMLStreamConstants.END_ELEMENT) { depth--; } if (depth == 0) { res.setEnd(event.asEndElement()); } else { event.writeAsEncodedUnicode(content); } } content.flush(); content.close(); res.setContent(new ByteArrayInputStream(content.toString().getBytes())); return res; }
From source file:org.apache.olingo.fit.utils.AbstractXMLUtilities.java
public Map.Entry<Integer, XmlElement> extractElement(final XMLEventReader reader, final XMLEventWriter writer, final List<String> path, final Collection<Map.Entry<String, String>> filter, final boolean filterInOr, final int startPathPos, final int minPathPos, final int maxPathPos) throws Exception { StartElement start = null;//from w w w.j a va 2 s. c om int searchFor = 0; int depth = startPathPos; // Current inspected element String current = null; // set defaults final List<String> pathElementNames = path == null ? Collections.<String>emptyList() : path; final Collection<Map.Entry<String, String>> filterAttrs = filter == null ? Collections.<Map.Entry<String, String>>emptySet() : filter; while (reader.hasNext() && start == null) { final XMLEvent event = reader.nextEvent(); if (event.getEventType() == XMLStreamConstants.START_ELEMENT) { depth++; if (current != null || ((minPathPos < 0 || minPathPos <= depth) && (maxPathPos < 0 || depth <= maxPathPos))) { if (pathElementNames.isEmpty() || pathElementNames.get(searchFor).trim() .equals(event.asStartElement().getName().getLocalPart())) { if (searchFor < pathElementNames.size() - 1) { // path exploring not completed writeEvent(event, writer); current = pathElementNames.get(searchFor).trim(); searchFor++; } else { // path exploring completed ... evaluate filter about path element name attribute boolean match = filterAttrs.isEmpty() || !filterInOr; for (Map.Entry<String, String> filterAttr : filterAttrs) { final Attribute attr = event.asStartElement() .getAttributeByName(new QName(filterAttr.getKey().trim())); if (attr == null || !filterAttr.getValue().trim().equals(attr.getValue())) { match = filterInOr ? match : false; } else { match = filterInOr ? true : match; } } if (match) { // found searched element start = event.asStartElement(); } else { skipElement(event.asStartElement(), reader, writer, false); depth--; } } } else if (current == null) { writeEvent(event, writer); } else { // skip element skipElement(event.asStartElement(), reader, writer, false); depth--; } } else { writeEvent(event, writer); } } else if (event.getEventType() == XMLStreamConstants.END_ELEMENT) { depth--; writeEvent(event, writer); if (event.asEndElement().getName().getLocalPart().equals(current)) { // back step .... searchFor--; current = searchFor > 0 ? pathElementNames.get(searchFor - 1).trim() : null; } } else { writeEvent(event, writer); } } if (start == null) { throw new NotFoundException(); } return new SimpleEntry<Integer, XmlElement>(Integer.valueOf(depth - 1), getXmlElement(start, reader)); }
From source file:org.apache.olingo.fit.utils.AbstractXMLUtilities.java
@Override public InputStream selectEntity(final InputStream entity, final String[] propertyNames) throws Exception { final XMLEventReader reader = getEventReader(entity); final ByteArrayOutputStream bos = new ByteArrayOutputStream(); final XMLEventWriter writer = getEventWriter(bos); final List<String> found = new ArrayList<String>(Arrays.asList(propertyNames)); boolean inProperties = false; boolean writeCurrent = true; Boolean writeNext = null;//from w ww. j a v a2 s . c o m String currentName = null; final List<String> fieldToBeSaved = new ArrayList<String>(Arrays.asList(propertyNames)); while (reader.hasNext()) { final XMLEvent event = reader.nextEvent(); if (event.getEventType() == XMLStreamConstants.START_ELEMENT && LINK.equals(event.asStartElement().getName().getLocalPart()) && !fieldToBeSaved .contains(event.asStartElement().getAttributeByName(new QName("title")).getValue()) && !"edit".equals(event.asStartElement().getAttributeByName(new QName("rel")).getValue())) { writeCurrent = false; } else if (event.getEventType() == XMLStreamConstants.END_ELEMENT && LINK.equals(event.asEndElement().getName().getLocalPart())) { writeNext = true; } else if (event.getEventType() == XMLStreamConstants.START_ELEMENT && (PROPERTIES).equals(event.asStartElement().getName().getLocalPart())) { writeCurrent = true; writeNext = false; inProperties = true; } else if (event.getEventType() == XMLStreamConstants.END_ELEMENT && (PROPERTIES).equals(event.asEndElement().getName().getLocalPart())) { writeCurrent = true; } else if (inProperties) { if (event.getEventType() == XMLStreamConstants.START_ELEMENT) { final String elementName = event.asStartElement().getName().getLocalPart(); for (String propertyName : propertyNames) { if ((ATOM_PROPERTY_PREFIX + propertyName.trim()).equals(elementName)) { writeCurrent = true; found.remove(propertyName); currentName = propertyName; } } } else if (event.getEventType() == XMLStreamConstants.END_ELEMENT && StringUtils.isNotBlank(currentName) && (ATOM_PROPERTY_PREFIX + currentName.trim()) .equals(event.asEndElement().getName().getLocalPart())) { writeNext = false; currentName = null; } } if (writeCurrent) { writer.add(event); } if (writeNext != null) { writeCurrent = writeNext; writeNext = null; } } writer.flush(); writer.close(); reader.close(); IOUtils.closeQuietly(entity); // Do not raise any exception in order to support FC properties as well // if (!found.isEmpty()) { // throw new Exception(String.format("Could not find a properties '%s'", found)); // } return new ByteArrayInputStream(bos.toByteArray()); }
From source file:org.apache.olingo.fit.utils.XMLUtilities.java
public XMLElement getXmlElement(final StartElement start, final XMLEventReader reader) throws Exception { final XMLElement res = new XMLElement(); res.setStart(start);/* www. j a v a 2 s. c o m*/ final Charset encoding = Charset.forName(org.apache.olingo.commons.api.Constants.UTF8); final ByteArrayOutputStream content = new ByteArrayOutputStream(); final OutputStreamWriter writer = new OutputStreamWriter(content, encoding); int depth = 1; while (reader.hasNext() && depth > 0) { final XMLEvent event = reader.nextEvent(); if (event.getEventType() == XMLStreamConstants.START_ELEMENT) { depth++; } else if (event.getEventType() == XMLStreamConstants.END_ELEMENT) { depth--; } if (depth == 0) { res.setEnd(event.asEndElement()); } else { event.writeAsEncodedUnicode(writer); } } writer.flush(); writer.close(); res.setContent(new ByteArrayInputStream(content.toByteArray())); return res; }
From source file:org.apache.olingo.fit.utils.XMLUtilities.java
public Map.Entry<Integer, XMLElement> extractElement(final XMLEventReader reader, final XMLEventWriter writer, final List<String> path, final Collection<Map.Entry<String, String>> filter, final boolean filterInOr, final int startPathPos, final int minPathPos, final int maxPathPos) throws Exception { StartElement start = null;/*from w w w . j a v a2 s .c o m*/ int searchFor = 0; int depth = startPathPos; // Current inspected element String current = null; // set defaults final List<String> pathElementNames = path == null ? Collections.<String>emptyList() : path; final Collection<Map.Entry<String, String>> filterAttrs = filter == null ? Collections.<Map.Entry<String, String>>emptySet() : filter; while (reader.hasNext() && start == null) { final XMLEvent event = reader.nextEvent(); if (event.getEventType() == XMLStreamConstants.START_ELEMENT) { depth++; if (current != null || ((minPathPos < 0 || minPathPos <= depth) && (maxPathPos < 0 || depth <= maxPathPos))) { if (pathElementNames.isEmpty() || pathElementNames.get(searchFor).trim() .equals(event.asStartElement().getName().getLocalPart())) { if (searchFor < pathElementNames.size() - 1) { // path exploring not completed writeEvent(event, writer); current = pathElementNames.get(searchFor).trim(); searchFor++; } else { // path exploring completed ... evaluate filter about path element name attribute boolean match = filterAttrs.isEmpty() || !filterInOr; for (Map.Entry<String, String> filterAttr : filterAttrs) { final Attribute attr = event.asStartElement() .getAttributeByName(new QName(filterAttr.getKey().trim())); if (attr == null || !filterAttr.getValue().trim().equals(attr.getValue())) { match = filterInOr ? match : false; } else { match = filterInOr ? true : match; } } if (match) { // found searched element start = event.asStartElement(); } else { skipElement(event.asStartElement(), reader, writer, false); depth--; } } } else if (current == null) { writeEvent(event, writer); } else { // skip element skipElement(event.asStartElement(), reader, writer, false); depth--; } } else { writeEvent(event, writer); } } else if (event.getEventType() == XMLStreamConstants.END_ELEMENT) { depth--; writeEvent(event, writer); if (event.asEndElement().getName().getLocalPart().equals(current)) { // back step .... searchFor--; current = searchFor > 0 ? pathElementNames.get(searchFor - 1).trim() : null; } } else { writeEvent(event, writer); } } if (start == null) { throw new NotFoundException(); } return new SimpleEntry<Integer, XMLElement>(Integer.valueOf(depth - 1), getXmlElement(start, reader)); }
From source file:org.apache.olingo.fit.utils.XMLUtilities.java
@Override public InputStream selectEntity(final InputStream entity, final String[] propertyNames) throws Exception { final XMLEventReader reader = getEventReader(entity); final ByteArrayOutputStream bos = new ByteArrayOutputStream(); final XMLEventWriter writer = getEventWriter(bos); final List<String> found = new ArrayList<String>(Arrays.asList(propertyNames)); boolean inProperties = false; boolean writeCurrent = true; Boolean writeNext = null;/*ww w . jav a2 s.c om*/ String currentName = null; final List<String> fieldToBeSaved = new ArrayList<String>(Arrays.asList(propertyNames)); while (reader.hasNext()) { final XMLEvent event = reader.nextEvent(); if (event.getEventType() == XMLStreamConstants.START_ELEMENT && Constants.get(ConstantKey.LINK).equals(event.asStartElement().getName().getLocalPart()) && !fieldToBeSaved .contains(event.asStartElement().getAttributeByName(new QName("title")).getValue()) && !"edit".equals(event.asStartElement().getAttributeByName(new QName("rel")).getValue())) { writeCurrent = false; } else if (event.getEventType() == XMLStreamConstants.END_ELEMENT && Constants.get(ConstantKey.LINK).equals(event.asEndElement().getName().getLocalPart())) { writeNext = true; } else if (event.getEventType() == XMLStreamConstants.START_ELEMENT && (Constants.get(ConstantKey.PROPERTIES)) .equals(event.asStartElement().getName().getLocalPart())) { writeCurrent = true; writeNext = false; inProperties = true; } else if (event.getEventType() == XMLStreamConstants.END_ELEMENT && (Constants.get(ConstantKey.PROPERTIES)) .equals(event.asEndElement().getName().getLocalPart())) { writeCurrent = true; } else if (inProperties) { if (event.getEventType() == XMLStreamConstants.START_ELEMENT) { final String elementName = event.asStartElement().getName().getLocalPart(); for (String propertyName : propertyNames) { if ((Constants.get(ConstantKey.ATOM_PROPERTY_PREFIX) + propertyName.trim()) .equals(elementName)) { writeCurrent = true; found.remove(propertyName); currentName = propertyName; } } } else if (event.getEventType() == XMLStreamConstants.END_ELEMENT && StringUtils.isNotBlank(currentName) && (Constants.get(ConstantKey.ATOM_PROPERTY_PREFIX) + currentName.trim()) .equals(event.asEndElement().getName().getLocalPart())) { writeNext = false; currentName = null; } } if (writeCurrent) { writer.add(event); } if (writeNext != null) { writeCurrent = writeNext; writeNext = null; } } writer.flush(); writer.close(); reader.close(); IOUtils.closeQuietly(entity); // Do not raise any exception in order to support FC properties as well // if (!found.isEmpty()) { // throw new Exception(String.format("Could not find a properties '%s'", found)); // } return new ByteArrayInputStream(bos.toByteArray()); }
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 www .j a v a 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; } } }