List of usage examples for javax.xml.stream XMLStreamConstants END_DOCUMENT
int END_DOCUMENT
To view the source code for javax.xml.stream XMLStreamConstants END_DOCUMENT.
Click Source Link
From source file:Main.java
/** * Gets the version of the XML./*from w w w . j a va2 s. c om*/ * * @param path the XML file. * @return the corresponding version of the XML. */ public static String getXMLVersion(final Path path) { try (InputStream inputStream = Files.newInputStream(path)) { XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(inputStream); for (int event; (event = reader.next()) != XMLStreamConstants.END_DOCUMENT;) { if (event == XMLStreamConstants.START_ELEMENT) { String tmp = reader.getLocalName(); if ("persistence".equals(tmp)) { for (int i = 0; i < reader.getAttributeCount(); i++) { if ("version".equals(reader.getAttributeName(i).toString())) { return reader.getAttributeValue(i); } } } } } } catch (Exception ex) { throw new RuntimeException("Error reading the persistence.xml version.", ex); } return null; }
From source file:Main.java
/** Get the human readable event name for the numeric event id */ public static String getEventName(int eventId) { switch (eventId) { case XMLStreamConstants.START_ELEMENT: return "StartElementEvent"; case XMLStreamConstants.END_ELEMENT: return "EndElementEvent"; case XMLStreamConstants.PROCESSING_INSTRUCTION: return "ProcessingInstructionEvent"; case XMLStreamConstants.CHARACTERS: return "CharacterEvent"; case XMLStreamConstants.COMMENT: return "CommentEvent"; case XMLStreamConstants.START_DOCUMENT: return "StartDocumentEvent"; case XMLStreamConstants.END_DOCUMENT: return "EndDocumentEvent"; case XMLStreamConstants.ENTITY_REFERENCE: return "EntityReferenceEvent"; case XMLStreamConstants.ATTRIBUTE: return "AttributeBase"; case XMLStreamConstants.DTD: return "DTDEvent"; case XMLStreamConstants.CDATA: return "CDATA"; }//from w w w . jav a2 s . com return "UNKNOWN_EVENT_TYPE"; }
From source file:Main.java
/** * /*from ww w. jav a2s. c om*/ * @param is * @param os * @param elementNames * @throws XMLStreamException * @throws FactoryConfigurationError * @throws UnsupportedEncodingException */ public static void stripElements(final InputStream is, final OutputStream os, final Collection<String> elementNames) throws XMLStreamException, UnsupportedEncodingException, FactoryConfigurationError { final XMLEventReader xmlEventReader = XMLInputFactory.newInstance() .createXMLEventReader(new InputStreamReader(is, Charset.defaultCharset().name())); final XMLEventWriter xmlEventWriter = XMLOutputFactory.newInstance().createXMLEventWriter(os); String elementName = null; while (xmlEventReader.peek() != null) { final XMLEvent event = (XMLEvent) xmlEventReader.next(); switch (event.getEventType()) { case XMLStreamConstants.START_DOCUMENT: case XMLStreamConstants.END_DOCUMENT: { // Ignore. break; } case XMLStreamConstants.START_ELEMENT: { final StartElement startElement = event.asStartElement(); final QName name = startElement.getName(); if (elementNames.contains(name.getLocalPart())) { elementName = name.getLocalPart(); } if (elementName == null) { xmlEventWriter.add(event); } break; } case XMLStreamConstants.END_ELEMENT: { final EndElement endElement = event.asEndElement(); final QName name = endElement.getName(); if (elementName == null) { xmlEventWriter.add(event); } else if (elementName.equals(name.getLocalPart())) { elementName = null; } break; } default: { if (elementName == null) { xmlEventWriter.add(event); } } } } xmlEventWriter.flush(); }
From source file:Main.java
/** * /* w w w .j a va 2 s . c om*/ * @param elementName * @param attributeValue * @param is * @return Collection * @throws XMLStreamException * @throws FactoryConfigurationError * @throws UnsupportedEncodingException */ public static Collection<String> getElementValues(final String elementName, final String attributeValue, final InputStream is) throws XMLStreamException, UnsupportedEncodingException, FactoryConfigurationError { final Collection<String> elementValues = new ArrayList<>(); final XMLEventReader xmlEventReader = XMLInputFactory.newInstance() .createXMLEventReader(new InputStreamReader(is, Charset.defaultCharset().name())); final StringBuffer characters = new StringBuffer(); boolean read = false; while (xmlEventReader.peek() != null) { final XMLEvent event = (XMLEvent) xmlEventReader.next(); switch (event.getEventType()) { case XMLStreamConstants.START_DOCUMENT: case XMLStreamConstants.END_DOCUMENT: { // Ignore. break; } case XMLStreamConstants.START_ELEMENT: { read = elementName.equals(event.asStartElement().getName().getLocalPart()); if (read && attributeValue != null) { read = false; for (Iterator<Attribute> iterator = event.asStartElement().getAttributes(); iterator .hasNext();) { Attribute attribute = iterator.next(); if (attribute.getValue().equals(attributeValue)) { read = true; break; } } } break; } case XMLStreamConstants.CHARACTERS: { if (read) { characters.append(event.asCharacters().getData()); } break; } case XMLStreamConstants.END_ELEMENT: { if (read) { elementValues.add(characters.toString()); characters.setLength(0); } read = false; break; } default: { // Ignore break; } } } return elementValues; }
From source file:Main.java
/** * Get the string representation of a given StAX event type. The returned * value is the name of the constant in {@link XMLStreamConstants} * corresponding to the event type.//ww w . java 2s . c o m * * @param event * the event type as returned by * {@link javax.xml.stream.events.XMLEvent#getEventType()}, * {@link javax.xml.stream.XMLStreamReader#getEventType()} or * {@link javax.xml.stream.XMLStreamReader#next()} * @return a string representation of the event type */ public static String getEventTypeString(int event) { String state = null; switch (event) { case XMLStreamConstants.START_ELEMENT: state = "START_ELEMENT"; break; case XMLStreamConstants.START_DOCUMENT: state = "START_DOCUMENT"; break; case XMLStreamConstants.CHARACTERS: state = "CHARACTERS"; break; case XMLStreamConstants.CDATA: state = "CDATA"; break; case XMLStreamConstants.END_ELEMENT: state = "END_ELEMENT"; break; case XMLStreamConstants.END_DOCUMENT: state = "END_DOCUMENT"; break; case XMLStreamConstants.SPACE: state = "SPACE"; break; case XMLStreamConstants.COMMENT: state = "COMMENT"; break; case XMLStreamConstants.DTD: state = "DTD"; break; case XMLStreamConstants.PROCESSING_INSTRUCTION: state = "PROCESSING_INSTRUCTION"; break; case XMLStreamConstants.ENTITY_REFERENCE: state = "ENTITY_REFERENCE"; break; default: state = "UNKNOWN_STATE: " + event; } return state; }
From source file:Main.java
/** * // w w w. j av a 2 s . co m * @param elementName * @param is * @param onlyValues * @return Collection * @throws XMLStreamException * @throws UnsupportedEncodingException */ public static Collection<String> getElements(final String elementName, final InputStream is, final boolean onlyValues) throws XMLStreamException, UnsupportedEncodingException { final Collection<String> elements = new ArrayList<>(); final ByteArrayOutputStream os = new ByteArrayOutputStream(); final XMLEventReader reader = XMLInputFactory.newInstance() .createXMLEventReader(new InputStreamReader(is, Charset.defaultCharset().name())); final XMLEventWriter writer = XMLOutputFactory.newInstance() .createXMLEventWriter(new OutputStreamWriter(os, Charset.defaultCharset().name())); boolean read = false; String characters = null; while (reader.peek() != null) { final XMLEvent event = (XMLEvent) reader.next(); switch (event.getEventType()) { case XMLStreamConstants.START_DOCUMENT: case XMLStreamConstants.END_DOCUMENT: { // Ignore. break; } case XMLStreamConstants.START_ELEMENT: { read = read || elementName.equals(event.asStartElement().getName().getLocalPart()); if (read && !onlyValues) { writer.add(event); } break; } case XMLStreamConstants.ATTRIBUTE: { if (read && !onlyValues) { writer.add(event); } break; } case XMLStreamConstants.CHARACTERS: { if (read && !onlyValues) { writer.add(event); } characters = event.asCharacters().getData(); break; } case XMLStreamConstants.END_ELEMENT: { if (read && !onlyValues) { writer.add(event); } if (elementName.equals(event.asEndElement().getName().getLocalPart())) { writer.flush(); if (characters != null) { elements.add(characters); } os.reset(); read = false; } break; } default: { // Ignore break; } } } return elements; }
From source file:Main.java
/** * Copy an XML event stream.//from w w w . j a v a 2 s . co m * @param reader The event reader. * @param writer The event writer. * @param omitDoc if true, ignore start/end document events. * @throws XMLStreamException For errors writing to the XML event writer. */ public static void copyXMLEventStream(final XMLEventReader reader, final XMLEventWriter writer, final boolean omitDoc) throws XMLStreamException { if (omitDoc) { while (reader.hasNext()) { final XMLEvent event = reader.nextEvent(); final int type = event.getEventType(); if ((type != XMLStreamConstants.START_DOCUMENT) && (type != XMLStreamConstants.END_DOCUMENT)) { writer.add(event); } } } else { writer.add(reader); } writer.flush(); }
From source file:jodtemplate.pptx.io.xml.SlideXmlRelsReader.java
@Override public Slide read(final String path, final Resources resources, final XMLInputFactory xmlInputFactory, final Slide slide) throws XMLStreamException, IOException { final Resource slideXmlRelsRes = resources.getResource(Utils.removePrefixSeparator(path)); try (final InputStream is = slideXmlRelsRes.getInputStream()) { final XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(is); int event = xmlStreamReader.next(); while (event != XMLStreamConstants.END_DOCUMENT) { if (xmlStreamReader.getEventType() == XMLStreamConstants.START_ELEMENT) { final String elementNS = xmlStreamReader.getName().getNamespaceURI(); final String elementName = xmlStreamReader.getName().getLocalPart(); if (OOXMLDocument.RELATIONSHIP_ELEMENT.equals(elementName) && OOXMLDocument.RELATIONSHIPS_RELS_NAMESPACE.equals(elementNS)) { final Relationship relationship = createRelationshipElement(xmlStreamReader); slide.addOtherRelationship(relationship); }// w w w. jav a2s . com } event = xmlStreamReader.next(); } } return slide; }
From source file:jodtemplate.io.xml.ContentTypesReader.java
@Override public ContentTypes read(final String path, final Resources resources, final XMLInputFactory xmlInputFactory, final ContentTypes contentTypes) throws XMLStreamException, IOException { final Resource contentTypesRes = resources.getResource(Utils.removePrefixSeparator(path)); try (final InputStream is = contentTypesRes.getInputStream()) { final XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(is); int event = xmlStreamReader.next(); while (event != XMLStreamConstants.END_DOCUMENT) { if (xmlStreamReader.getEventType() == XMLStreamConstants.START_ELEMENT) { final String elementNS = xmlStreamReader.getName().getNamespaceURI(); final String elementName = xmlStreamReader.getName().getLocalPart(); if (OOXMLDocument.DEFAULT_ELEMENT.equals(elementName) && OOXMLDocument.CONTENT_TYPES_NAMESPACE.equals(elementNS)) { contentTypes.addDefaultElement(createDefaultElement(xmlStreamReader)); }//from www . jav a2s .c om if (OOXMLDocument.OVERRIDE_ELEMENT.equals(elementName) && OOXMLDocument.CONTENT_TYPES_NAMESPACE.equals(elementNS)) { contentTypes.addOverrideElement(createOverrideElement(xmlStreamReader)); } } event = xmlStreamReader.next(); } } return contentTypes; }
From source file:jodtemplate.pptx.io.xml.PresentationXmlRelsReader.java
@Override public Presentation read(final String path, final Resources resources, final XMLInputFactory xmlInputFactory, final Presentation presentation) throws XMLStreamException, IOException { final Resource presentationXmlRelsRes = resources.getResource(Utils.removePrefixSeparator(path)); try (final InputStream is = presentationXmlRelsRes.getInputStream()) { final XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(is); int event = xmlStreamReader.next(); while (event != XMLStreamConstants.END_DOCUMENT) { if (xmlStreamReader.getEventType() == XMLStreamConstants.START_ELEMENT) { final String elementNS = xmlStreamReader.getName().getNamespaceURI(); final String elementName = xmlStreamReader.getName().getLocalPart(); if (OOXMLDocument.RELATIONSHIP_ELEMENT.equals(elementName) && OOXMLDocument.RELATIONSHIPS_RELS_NAMESPACE.equals(elementNS)) { final Relationship relationship = createRelationshipElement(xmlStreamReader); if (Relationship.SLIDE_TYPE.equals(relationship.getType())) { final Slide slide = new Slide(); slide.setRelationship(relationship); slide.setPresentation(presentation); presentation.addSlide(slide); } else { presentation.addOtherRelationship(relationship); }// w w w . j a v a 2s . co m } } event = xmlStreamReader.next(); } } return presentation; }