Java Code Examples for javax.xml.stream.events.XMLEvent#isStartElement()
The following examples show how to use
javax.xml.stream.events.XMLEvent#isStartElement() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StAXFilteredEvent.java From openjdk-8-source with GNU General Public License v2.0 | 6 votes |
public String getElementText() throws XMLStreamException { StringBuffer buffer = new StringBuffer(); XMLEvent e = nextEvent(); if (!e.isStartElement()) throw new XMLStreamException( CommonResourceBundle.getInstance().getString("message.mustBeOnSTART_ELEMENT")); while(hasNext()) { e = nextEvent(); if(e.isStartElement()) throw new XMLStreamException( CommonResourceBundle.getInstance().getString("message.getElementTextExpectTextOnly")); if(e.isCharacters()) buffer.append(((Characters) e).getData()); if(e.isEndElement()) return buffer.toString(); } throw new XMLStreamException(CommonResourceBundle.getInstance().getString("message.END_ELEMENTnotFound")); }
Example 2
Source File: StAXFilteredEvent.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 6 votes |
public String getElementText() throws XMLStreamException { StringBuffer buffer = new StringBuffer(); XMLEvent e = nextEvent(); if (!e.isStartElement()) throw new XMLStreamException( CommonResourceBundle.getInstance().getString("message.mustBeOnSTART_ELEMENT")); while(hasNext()) { e = nextEvent(); if(e.isStartElement()) throw new XMLStreamException( CommonResourceBundle.getInstance().getString("message.getElementTextExpectTextOnly")); if(e.isCharacters()) buffer.append(((Characters) e).getData()); if(e.isEndElement()) return buffer.toString(); } throw new XMLStreamException(CommonResourceBundle.getInstance().getString("message.END_ELEMENTnotFound")); }
Example 3
Source File: XmlDomParser.java From aws-sdk-java-v2 with Apache License 2.0 | 6 votes |
/** * Parse an XML elemnt and any nested elements by recursively calling this method. * * @param startElement Start element object containing element name. * @param reader XML reader to get more events. * @return Parsed {@link XmlElement}. */ private static XmlElement parseElement(StartElement startElement, XMLEventReader reader) throws XMLStreamException { XmlElement.Builder elementBuilder = XmlElement.builder() .elementName(startElement.getName().getLocalPart()); if (startElement.getAttributes().hasNext()) { parseAttributes(startElement, elementBuilder); } XMLEvent nextEvent; do { nextEvent = reader.nextEvent(); if (nextEvent.isStartElement()) { elementBuilder.addChildElement(parseElement(nextEvent.asStartElement(), reader)); } else if (nextEvent.isCharacters()) { elementBuilder.textContent(readText(reader, nextEvent.asCharacters().getData())); } } while (!nextEvent.isEndElement()); return elementBuilder.build(); }
Example 4
Source File: StAXAntPathIterator.java From secure-data-service with Apache License 2.0 | 6 votes |
protected boolean retrieveContent(ContentRetriever retriever) throws XMLStreamException { int count = groupBy(); while (reader.hasNext()) { XMLEvent tmp = reader.peek(); if (tmp.isStartElement() && isTargeted(tmp)) { if (count-- > 0) { retriever.retrieve(); } else { break; } } else { if (tmp.isStartElement()) { parents.add(tmp.asStartElement()); } else if (tmp.isEndElement()) { parents.pop(); } reader.nextEvent(); } } return count != groupBy(); }
Example 5
Source File: EwsXmlReader.java From ews-java-api with MIT License | 6 votes |
public boolean readToDescendant(String localName, String namespaceURI) throws XMLStreamException { if (!this.isStartElement()) { return false; } XMLEvent startEvent = this.presentEvent; XMLEvent event = this.presentEvent; do { if (event.isStartElement()) { QName qEName = event.asStartElement().getName(); if (qEName.getLocalPart().equals(localName) && qEName.getNamespaceURI().equals(namespaceURI)) { return true; } } event = this.xmlReader.nextEvent(); } while (!checkEndElement(startEvent, event)); return false; }
Example 6
Source File: StAXFilteredEvent.java From jdk8u60 with GNU General Public License v2.0 | 6 votes |
public String getElementText() throws XMLStreamException { StringBuffer buffer = new StringBuffer(); XMLEvent e = nextEvent(); if (!e.isStartElement()) throw new XMLStreamException( CommonResourceBundle.getInstance().getString("message.mustBeOnSTART_ELEMENT")); while(hasNext()) { e = nextEvent(); if(e.isStartElement()) throw new XMLStreamException( CommonResourceBundle.getInstance().getString("message.getElementTextExpectTextOnly")); if(e.isCharacters()) buffer.append(((Characters) e).getData()); if(e.isEndElement()) return buffer.toString(); } throw new XMLStreamException(CommonResourceBundle.getInstance().getString("message.END_ELEMENTnotFound")); }
Example 7
Source File: UploadActionListener.java From zap-extensions with Apache License 2.0 | 6 votes |
public static Boolean reportIsEmpty(File reportFile) throws IOException, XMLStreamException { BufferedReader br = Files.newBufferedReader(reportFile.toPath()); try { XMLEventReader reader = xmlInputFactory.createXMLEventReader(br); while(reader.hasNext()) { XMLEvent event = reader.nextEvent(); if(event.isStartElement() && !event.asStartElement().getName().getLocalPart().equals("OWASPZAPReport")) { return false; } } } finally { br.close(); } return true; }
Example 8
Source File: CimAnonymizer.java From powsybl-core with Mozilla Public License 2.0 | 5 votes |
@Override public void add(XMLEvent event) throws XMLStreamException { XMLEvent newEvent = null; if (event.isStartElement()) { StartElement startElement = event.asStartElement(); newEvent = anonymizeStartElement(startElement); } else if (event.isCharacters()) { Characters characters = event.asCharacters(); newEvent = anonymizeCharacters(characters); } super.add(newEvent != null ? newEvent : event); }
Example 9
Source File: JaxbCfgProcessor.java From lams with GNU General Public License v2.0 | 5 votes |
@Override public XMLEvent peek() throws XMLStreamException { XMLEvent event = super.peek(); if ( event.isStartElement() ) { return withNamespace( event.asStartElement() ); } else { return event; } }
Example 10
Source File: UnmarshallerImpl.java From jdk8u60 with GNU General Public License v2.0 | 5 votes |
private Object unmarshal0(XMLEventReader reader,JaxBeanInfo expectedType) throws JAXBException { if (reader == null) { throw new IllegalArgumentException( Messages.format(Messages.NULL_READER)); } try { XMLEvent event = reader.peek(); if (!event.isStartElement() && !event.isStartDocument()) { // TODO: convert event into event name throw new IllegalStateException( Messages.format( Messages.ILLEGAL_READER_STATE,event.getEventType())); } // Quick hack until SJSXP fixes 6270116 boolean isZephyr = reader.getClass().getName().equals("com.sun.xml.internal.stream.XMLReaderImpl"); XmlVisitor h = createUnmarshallerHandler(null,false,expectedType); if(!isZephyr) { h = new InterningXmlVisitor(h); } new StAXEventConnector(reader,h).bridge(); return h.getContext().getResult(); } catch (XMLStreamException e) { throw handleStreamException(e); } }
Example 11
Source File: EdfiRecordParserImpl.java From secure-data-service with Apache License 2.0 | 5 votes |
private static String extractTagName(XMLEvent e) { String result = ""; if (e.isEndElement()) { result = e.asEndElement().getName().getLocalPart(); } else if (e.isStartElement()) { result = e.asStartElement().getName().getLocalPart(); } return result; }
Example 12
Source File: AndroidManifest.java From bazel with Apache License 2.0 | 5 votes |
/** Parses the manifest from the path. */ public static AndroidManifest parseFrom(Path manifest) { try (InputStream input = Files.newInputStream(manifest)) { final XMLEventReader xmlEventReader = XmlResourceValues.getXmlInputFactory().createXMLEventReader(input); String packageName = ""; String minSdk = ""; while (xmlEventReader.hasNext()) { final XMLEvent xmlEvent = xmlEventReader.nextEvent(); if (xmlEvent.isStartElement()) { final StartElement element = xmlEvent.asStartElement(); if (MANIFEST.equals(element.getName())) { packageName = getAttributeValue(xmlEvent, PACKAGE_NAME).orElse(packageName); } if (USES_SDK.equals(element.getName())) { minSdk = getAttributeValue(xmlEvent, MIN_SDK).orElse(minSdk); } } } if (minSdk.isEmpty()) { logger.warning( String.format( "\n\u001B[31mCONFIGURATION:\u001B[0m" + " %s has no minSdkVersion. Using 1.", manifest)); } return new AndroidManifest(packageName, minSdk.isEmpty() ? "1" : minSdk); } catch (IOException | XMLStreamException e) { throw new ManifestProcessingException(e); } }
Example 13
Source File: ODataXmlDeserializer.java From olingo-odata4 with Apache License 2.0 | 5 votes |
private Object complex(final XMLEventReader reader, final StartElement start, final EdmComplexType edmComplex) throws XMLStreamException, EdmPrimitiveTypeException, DeserializerException { ComplexValue value = new ComplexValue(); EdmType resolvedType = edmComplex; boolean foundEndProperty = false; while (reader.hasNext() && !foundEndProperty) { final XMLEvent event = reader.nextEvent(); if (event.isStartElement()) { //Get the derived type from the element tag final Attribute attrType = start.getAttributeByName(typeQName); if (attrType != null ) { String type = new EdmTypeInfo.Builder().setTypeExpression(attrType.getValue()).build().internal(); if (type.startsWith("Collection(") && type.endsWith(")")) { type = type.substring(11, type.length()-1); } resolvedType = getDerivedType(edmComplex, type); } StartElement se = event.asStartElement(); EdmProperty p = (EdmProperty) ((EdmComplexType)resolvedType).getProperty(se.getName().getLocalPart()); value.getValue().add(property(reader, se, p.getType(), p.isNullable(), p.getMaxLength(), p.getPrecision(), p.getScale(), p.isUnicode(), p.isCollection())); value.setTypeName(resolvedType.getFullQualifiedName().getFullQualifiedNameAsString()); } if (event.isEndElement() && start.getName().equals(event.asEndElement().getName())) { foundEndProperty = true; } } return value; }
Example 14
Source File: XmlRecordSource.java From nifi with Apache License 2.0 | 5 votes |
private StartElement readStartElement() throws XMLStreamException { while (xmlEventReader.hasNext()) { final XMLEvent xmlEvent = xmlEventReader.nextEvent(); if (xmlEvent.isStartElement()) { final StartElement startElement = xmlEvent.asStartElement(); return startElement; } } return null; }
Example 15
Source File: SpeedTestUtils.java From drftpd with GNU General Public License v2.0 | 4 votes |
private static HashSet<SpeedTestServer> parseXML(String xmlString) throws UnsupportedEncodingException, XMLStreamException { HashSet<SpeedTestServer> serverList = new HashSet<>(); XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance(); XMLEventReader xmlEventReader = xmlInputFactory.createXMLEventReader(new ByteArrayInputStream(xmlString.getBytes(StandardCharsets.UTF_8))); while (xmlEventReader.hasNext()) { //Get next event. XMLEvent xmlEvent = xmlEventReader.nextEvent(); //Check if event is the start element. if (xmlEvent.isStartElement()) { //Get event as start element. StartElement startElement = xmlEvent.asStartElement(); if (startElement.getName().getLocalPart().equals("server")) { SpeedTestServer server = new SpeedTestServer(); //Iterate and process attributes. Iterator iterator = startElement.getAttributes(); while (iterator.hasNext()) { Attribute attribute = (Attribute) iterator.next(); String name = attribute.getName().getLocalPart(); String value = attribute.getValue(); switch (name) { case "url": server.setUrl(value); break; case "url2": server.setUrl2(value); break; case "lat": server.setLatitude(Double.parseDouble(value)); break; case "lon": server.setLongitude(Double.parseDouble(value)); break; case "name": server.setName(value); break; case "country": server.setCountry(value); break; case "cc": server.setCc(value); break; case "sponsor": server.setSponsor(value); break; case "id": server.setId(Integer.parseInt(value)); break; case "host": server.setHost(value); break; } } serverList.add(server); } } } return serverList; }
Example 16
Source File: CheckstyleReportsParser.java From contribution with GNU Lesser General Public License v2.1 | 4 votes |
/** * Parses portion of the XML report. * * @param diffReport * container for parsed data. * @param reader * StAX parser interface. * @param numOfFilenames * number of "file" tags to parse. * @param index * internal index of the parsed file. * @throws XMLStreamException * on internal parser error. */ private static void parseXmlPortion(DiffReport diffReport, XMLEventReader reader, int numOfFilenames, int index) throws XMLStreamException { int counter = numOfFilenames; String filename = null; List<CheckstyleRecord> records = null; while (reader.hasNext()) { final XMLEvent event = reader.nextEvent(); if (event.isStartElement()) { final StartElement startElement = event.asStartElement(); final String startElementName = startElement.getName() .getLocalPart(); // file tag encounter if (startElementName.equals(FILE_TAG)) { counter--; diffReport.getStatistics().incrementFileCount(index); final Iterator<Attribute> attributes = startElement .getAttributes(); while (attributes.hasNext()) { final Attribute attribute = attributes.next(); if (attribute.getName().toString() .equals(FILENAME_ATTR)) { filename = attribute.getValue(); } } records = new ArrayList<>(); } // error tag encounter else if (startElementName.equals(ERROR_TAG)) { records.add(parseErrorTag(startElement, diffReport.getStatistics(), index, filename)); } } if (event.isEndElement()) { final EndElement endElement = event.asEndElement(); if (endElement.getName().getLocalPart().equals(FILE_TAG)) { diffReport.addRecords(records, filename); if (counter == 0) { break; } } } } }
Example 17
Source File: XmlResourceValues.java From bazel with Apache License 2.0 | 4 votes |
public static boolean isStartTag(XMLEvent event, QName subTagType) { if (event.isStartElement()) { return subTagType.equals(event.asStartElement().getName()); } return false; }
Example 18
Source File: XmlMerger.java From aion-germany with GNU General Public License v3.0 | 4 votes |
/** * Read all {@link javax.xml.stream.events.XMLEvent}'s from specified file and write them onto the {@link javax.xml.stream.XMLEventWriter} * * @param file * File to import * @param skipRoot * Skip-root flag * @param writer * Destenation writer * @throws XMLStreamException * On event reading/writing error. * @throws FileNotFoundException * if the reading file does not exist, is a directory rather than a regular file, or for some other reason cannot be opened for reading. */ private void importFile(File file, boolean skipRoot, XMLEventWriter writer, Properties metadata) throws XMLStreamException, IOException { logger.debug("Appending file " + file); metadata.setProperty(file.getPath(), makeHash(file)); XMLEventReader reader = null; try { reader = inputFactory.createXMLEventReader(new FileReader(file)); QName firstTagQName = null; while (reader.hasNext()) { XMLEvent event = reader.nextEvent(); // skip start and end of document. if (event.isStartDocument() || event.isEndDocument()) { continue; } // skip all comments. if (event instanceof Comment) { continue; } // skip white-spaces and all ignoreable white-spaces. if (event.isCharacters()) { if (event.asCharacters().isWhiteSpace() || event.asCharacters().isIgnorableWhiteSpace()) { continue; } } // modify root-tag of imported file. if (firstTagQName == null && event.isStartElement()) { firstTagQName = event.asStartElement().getName(); if (skipRoot) { continue; } else { StartElement old = event.asStartElement(); event = eventFactory.createStartElement(old.getName(), old.getAttributes(), null); } } // if root was skipped - skip root end too. if (event.isEndElement() && skipRoot && event.asEndElement().getName().equals(firstTagQName)) { continue; } // finally - write tag writer.add(event); } } finally { if (reader != null) { try { reader.close(); } catch (Exception ignored) { } } } }
Example 19
Source File: AtomDeserializer.java From olingo-odata4 with Apache License 2.0 | 4 votes |
private void fromCollection(final Valuable valuable, final XMLEventReader reader, final StartElement start, final EdmTypeInfo typeInfo) throws XMLStreamException, EdmPrimitiveTypeException { List<Object> values = new ArrayList<>(); ValueType valueType = ValueType.COLLECTION_PRIMITIVE; final EdmTypeInfo type = typeInfo == null ? null : new EdmTypeInfo.Builder().setTypeExpression(typeInfo.getFullQualifiedName().toString()).build(); boolean foundEndProperty = false; while (reader.hasNext() && !foundEndProperty) { final XMLEvent event = reader.nextEvent(); if (event.isStartElement()) { switch (guessPropertyType(reader, typeInfo)) { case COMPLEX: final Object complexValue = fromComplexOrEnum(reader, event.asStartElement()); valueType = ValueType.COLLECTION_COMPLEX; final Attribute typeAttr = event.asStartElement().getAttributeByName(typeQName); final String typeAttrValue = typeAttr == null ? null : typeAttr.getValue(); final EdmTypeInfo typeInfoEle = StringUtils.isBlank(typeAttrValue) ? null : new EdmTypeInfo.Builder().setTypeExpression(typeAttrValue).build(); if (typeInfoEle != null) { ((ComplexValue)complexValue).setTypeName(typeInfoEle.external()); } values.add(complexValue); break; case ENUM: valueType = ValueType.COLLECTION_ENUM; values.add(fromComplexOrEnum(reader, event.asStartElement())); break; case PRIMITIVE: final Object value = fromPrimitive(reader, event.asStartElement(), type); valueType = value instanceof Geospatial ? ValueType.COLLECTION_GEOSPATIAL : ValueType.COLLECTION_PRIMITIVE; values.add(value); break; default: // do not add null or empty values } } if (event.isEndElement() && start.getName().equals(event.asEndElement().getName())) { foundEndProperty = true; } } valuable.setValue(valueType, values); }
Example 20
Source File: DrawSimpleShape.java From lams with GNU General Public License v2.0 | 4 votes |
protected static CustomGeometry getCustomGeometry(String name, Graphics2D graphics) { @SuppressWarnings("unchecked") Map<String, CustomGeometry> presets = (graphics == null) ? null : (Map<String, CustomGeometry>)graphics.getRenderingHint(Drawable.PRESET_GEOMETRY_CACHE); if (presets == null) { presets = new HashMap<String,CustomGeometry>(); if (graphics != null) { graphics.setRenderingHint(Drawable.PRESET_GEOMETRY_CACHE, presets); } String packageName = "org.apache.poi.sl.draw.binding"; InputStream presetIS = Drawable.class.getResourceAsStream("presetShapeDefinitions.xml"); // StAX: EventFilter startElementFilter = new EventFilter() { @Override public boolean accept(XMLEvent event) { return event.isStartElement(); } }; try { XMLInputFactory staxFactory = StaxHelper.newXMLInputFactory(); XMLEventReader staxReader = staxFactory.createXMLEventReader(presetIS); XMLEventReader staxFiltRd = staxFactory.createFilteredReader(staxReader, startElementFilter); // Ignore StartElement: staxFiltRd.nextEvent(); // JAXB: JAXBContext jaxbContext = JAXBContext.newInstance(packageName); Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); while (staxFiltRd.peek() != null) { StartElement evRoot = (StartElement)staxFiltRd.peek(); String cusName = evRoot.getName().getLocalPart(); // XMLEvent ev = staxReader.nextEvent(); JAXBElement<org.apache.poi.sl.draw.binding.CTCustomGeometry2D> el = unmarshaller.unmarshal(staxReader, CTCustomGeometry2D.class); CTCustomGeometry2D cusGeom = el.getValue(); presets.put(cusName, new CustomGeometry(cusGeom)); } staxFiltRd.close(); staxReader.close(); } catch (Exception e) { throw new RuntimeException("Unable to load preset geometries.", e); } finally { IOUtils.closeQuietly(presetIS); } } return presets.get(name); }