List of usage examples for javax.xml.stream XMLStreamConstants START_ELEMENT
int START_ELEMENT
To view the source code for javax.xml.stream XMLStreamConstants START_ELEMENT.
Click Source Link
From source file:Main.java
/** * Skips a tag or subtree./*from w ww. java 2 s . c o m*/ */ public static void skip(XMLStreamReader parser) throws XMLStreamException { assert parser != null; int level = 1; while (next(parser)) { int event = parser.getEventType(); if (event == XMLStreamConstants.START_ELEMENT) { level++; } else if (event == XMLStreamConstants.END_ELEMENT) { level--; if (level == 0) { break; } } } next(parser); }
From source file:Main.java
/** * Test if reader is on a start tag named tagLocalName. * * @param reader xml stream reader * @param tagLocalName tag local name//from w w w.j av a 2 s .co m * @return true if reader is on a start tag named tagLocalName */ public static boolean isStartTag(XMLStreamReader reader, String tagLocalName) { return (reader.getEventType() == XMLStreamConstants.START_ELEMENT) && (reader.getLocalName().equals(tagLocalName)); }
From source file:Main.java
/** * Gets the version of the XML.//from w w w .java2s .co m * * @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
/** * Moves the parser to the next start element. * //from w w w . j ava 2 s .c o m * @return {@code true} if another start element has been found, * {@code false} otherwise */ public static boolean findNextStartElemenet(XMLStreamReader parser) throws XMLStreamException { assert parser != null; while (true) { int event = parser.getEventType(); if (event == XMLStreamConstants.START_ELEMENT) { return true; } if (parser.hasNext()) { parser.next(); } else { return false; } } }
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./* w w w.j a v a2 s. 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
/** * Test if reader is on a start tag.//from w w w . ja v a 2 s . c om * * @param reader xml stream reader * @return true if reader is on a start tag */ public static boolean isStartTag(XMLStreamReader reader) { return (reader.getEventType() == XMLStreamConstants.START_ELEMENT); }
From source file:Main.java
/** * /*from w w w . j a v a 2 s . co m*/ * @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
/** * //from ww w . ja v a2 s.c o m * @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
/** * /*w w w . java 2s .c om*/ * Checks indentation (over a single line - multipline text nodes is not supported) * * @param out * @param indentSize * @return * @throws Exception */ public static boolean isIndented(String out, int indentSize) throws Exception { BufferedReader reader = new BufferedReader(new StringReader(out)); boolean indentated = false; int level = 0; int line = 0; String string = reader.readLine(); while (string != null) { int newLevel = 0; while (newLevel < string.length()) { if (!Character.isWhitespace(string.charAt(newLevel))) { break; } newLevel++; } if ((newLevel % indentSize) != 0) { throw new IllegalArgumentException("Unexpected " + newLevel + " whitespace chars at line " + line); } if (Math.abs(level - newLevel) > indentSize) { throw new IllegalArgumentException("Unexpected jump from " + level + " to " + newLevel + " whitespace chars at line " + line + " for indenting with " + indentSize + " chars"); } level = newLevel; string = reader.readLine(); line++; if (level > 0) { indentated = true; } } if (!indentated) { // see if a simple xml piece XMLInputFactory inputFactory = XMLInputFactory.newInstance(); XMLStreamReader parser = inputFactory.createXMLStreamReader(new StringReader(out)); int elementMaxLevel = -1; int elementLevel = 0; do { int event = parser.next(); if (event == XMLStreamConstants.START_ELEMENT) { elementLevel++; if (elementMaxLevel < elementLevel) { elementMaxLevel = elementLevel; } } else if (event == XMLStreamConstants.END_ELEMENT) { elementLevel--; } } while (parser.hasNext()); if (elementMaxLevel > 1) { // should be indentated return false; } return true; } return indentated; }
From source file:MDRevealer.ResistanceTests.java
private static ArrayList<HashMap<String, String>> parse_test(File rt_input) throws XMLStreamException, FileNotFoundException { XMLInputFactory xif = XMLInputFactory.newInstance(); XMLStreamReader xsr = xif.createXMLStreamReader(new FileInputStream(rt_input)); ArrayList<HashMap<String, String>> test = new ArrayList<HashMap<String, String>>(); HashMap<String, String> hm = new HashMap<String, String>(); String key = "null"; while (xsr.hasNext() == true) { int constant = xsr.next(); switch (constant) { case XMLStreamConstants.START_ELEMENT: key = xsr.getLocalName();//w w w.j a v a 2 s .c om if (xsr.getAttributeCount() > 0) { for (int i = 0; i < xsr.getAttributeCount(); i++) { hm.put(xsr.getAttributeLocalName(i), xsr.getAttributeValue(i)); } test.add(hm); hm = new HashMap<String, String>(); } break; case XMLStreamConstants.CHARACTERS: if (!xsr.isWhiteSpace()) { hm.put(key, xsr.getText()); test.add(hm); } hm = new HashMap<String, String>(); break; case XMLStreamConstants.END_ELEMENT: if (xsr.getLocalName().equals("condition")) { hm.put("condition_over", "true"); test.add(hm); hm = new HashMap<String, String>(); } break; } } return test; }