Java Code Examples for javax.xml.stream.events.XMLEvent#asCharacters()
The following examples show how to use
javax.xml.stream.events.XMLEvent#asCharacters() .
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: ListBasedXMLEventReader.java From spring-analysis-note with MIT License | 6 votes |
@Override public String getElementText() throws XMLStreamException { checkIfClosed(); if (this.currentEvent == null || !this.currentEvent.isStartElement()) { throw new XMLStreamException("Not at START_ELEMENT: " + this.currentEvent); } StringBuilder builder = new StringBuilder(); while (true) { XMLEvent event = nextEvent(); if (event.isEndElement()) { break; } else if (!event.isCharacters()) { throw new XMLStreamException("Unexpected non-text event: " + event); } Characters characters = event.asCharacters(); if (!characters.isIgnorableWhiteSpace()) { builder.append(event.asCharacters().getData()); } } return builder.toString(); }
Example 2
Source File: ListBasedXMLEventReader.java From java-technology-stack with MIT License | 6 votes |
@Override public String getElementText() throws XMLStreamException { checkIfClosed(); if (this.currentEvent == null || !this.currentEvent.isStartElement()) { throw new XMLStreamException("Not at START_ELEMENT: " + this.currentEvent); } StringBuilder builder = new StringBuilder(); while (true) { XMLEvent event = nextEvent(); if (event.isEndElement()) { break; } else if (!event.isCharacters()) { throw new XMLStreamException("Unexpected non-text event: " + event); } Characters characters = event.asCharacters(); if (!characters.isIgnorableWhiteSpace()) { builder.append(event.asCharacters().getData()); } } return builder.toString(); }
Example 3
Source File: XmlResourceValues.java From bazel with Apache License 2.0 | 6 votes |
public static XMLEvent nextTag(XMLEventReader eventReader) throws XMLStreamException { while (eventReader.hasNext() && !(eventReader.peek().isEndElement() || eventReader.peek().isStartElement())) { XMLEvent nextEvent = eventReader.nextEvent(); if (nextEvent.isCharacters() && !nextEvent.asCharacters().isIgnorableWhiteSpace()) { Characters characters = nextEvent.asCharacters(); // TODO(corysmith): Turn into a warning with the Path is available to add to it. // This case is when unexpected characters are thrown into the xml. Best case, it's a // incorrect comment type... logger.fine( String.format( "Invalid characters [%s] found at %s", characters.getData(), characters.getLocation().getLineNumber())); } } return eventReader.nextEvent(); }
Example 4
Source File: StaxParserUtil.java From keycloak with Apache License 2.0 | 6 votes |
/** * Advances reader if character whitespace encountered * * @param xmlEventReader * @param xmlEvent * @return */ public static boolean wasWhitespacePeeked(XMLEventReader xmlEventReader, XMLEvent xmlEvent) { if (xmlEvent.isCharacters()) { Characters chars = xmlEvent.asCharacters(); String data = chars.getData(); if (data == null || data.trim().equals("")) { try { xmlEventReader.nextEvent(); return true; } catch (XMLStreamException e) { throw new RuntimeException(e); } } } return false; }
Example 5
Source File: WindowSuffixTransformer.java From recheck with GNU Affero General Public License v3.0 | 5 votes |
protected boolean updatePath( final XMLEvent event, final XMLEventWriter eventWriter ) throws XMLStreamException { if ( isInPath && event.isCharacters() ) { final Characters characters = event.asCharacters(); final String window = characters.getData(); isWindowPath = "Window".equals( window ); final String replace = window.replaceFirst( "Window", "Window_0" ); eventWriter.add( characters( replace ) ); return true; } return false; }
Example 6
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 7
Source File: BaseXMLEventReader.java From lams with GNU General Public License v2.0 | 4 votes |
@Override public final String getElementText() throws XMLStreamException { XMLEvent event = this.previousEvent; if (event == null) { throw new XMLStreamException("Must be on START_ELEMENT to read next text, element was null"); } if (!event.isStartElement()) { throw new XMLStreamException("Must be on START_ELEMENT to read next text", event.getLocation()); } final StringBuilder text = new StringBuilder(); while (!event.isEndDocument()) { switch (event.getEventType()) { case XMLStreamConstants.CHARACTERS: case XMLStreamConstants.SPACE: case XMLStreamConstants.CDATA: { final Characters characters = event.asCharacters(); text.append(characters.getData()); break; } case XMLStreamConstants.ENTITY_REFERENCE: { final EntityReference entityReference = (EntityReference)event; final EntityDeclaration declaration = entityReference.getDeclaration(); text.append(declaration.getReplacementText()); break; } case XMLStreamConstants.COMMENT: case XMLStreamConstants.PROCESSING_INSTRUCTION: { //Ignore break; } default: { throw new XMLStreamException("Unexpected event type '" + XMLStreamConstantsUtils.getEventName(event.getEventType()) + "' encountered. Found event: " + event, event.getLocation()); } } event = this.nextEvent(); } return text.toString(); }
Example 8
Source File: BaseXMLEventReader.java From lams with GNU General Public License v2.0 | 4 votes |
@Override public final String getElementText() throws XMLStreamException { XMLEvent event = this.previousEvent; if (event == null) { throw new XMLStreamException("Must be on START_ELEMENT to read next text, element was null"); } if (!event.isStartElement()) { throw new XMLStreamException("Must be on START_ELEMENT to read next text", event.getLocation()); } final StringBuilder text = new StringBuilder(); while (!event.isEndDocument()) { switch (event.getEventType()) { case XMLStreamConstants.CHARACTERS: case XMLStreamConstants.SPACE: case XMLStreamConstants.CDATA: { final Characters characters = event.asCharacters(); text.append(characters.getData()); break; } case XMLStreamConstants.ENTITY_REFERENCE: { final EntityReference entityReference = (EntityReference)event; final EntityDeclaration declaration = entityReference.getDeclaration(); text.append(declaration.getReplacementText()); break; } case XMLStreamConstants.COMMENT: case XMLStreamConstants.PROCESSING_INSTRUCTION: { //Ignore break; } default: { throw new XMLStreamException("Unexpected event type '" + XMLStreamConstantsUtils.getEventName(event.getEventType()) + "' encountered. Found event: " + event, event.getLocation()); } } event = this.nextEvent(); } return text.toString(); }
Example 9
Source File: EppXmlSanitizer.java From nomulus with Apache License 2.0 | 4 votes |
private static String sanitizeAndEncode(byte[] inputXmlBytes) throws XMLStreamException, UnsupportedEncodingException { XMLEventReader xmlEventReader = XML_INPUT_FACTORY.createXMLEventReader(new ByteArrayInputStream(inputXmlBytes)); if (!xmlEventReader.hasNext()) { return ""; } XMLEvent firstEvent = xmlEventReader.nextEvent(); checkState(firstEvent.isStartDocument(), "Missing StartDocument"); // Get input encoding for use in XMLEventWriter creation, so that sanitized XML preserves the // encoding declaration. According to XML spec, UTF-8 is to be used unless input declares // otherwise. Epp officially allows UTF-8 and UTF-16. String inputEncoding = Optional.ofNullable(((StartDocument) firstEvent).getCharacterEncodingScheme()) .orElse(StandardCharsets.UTF_8.name()); ByteArrayOutputStream outputXmlBytes = new ByteArrayOutputStream(); XMLEventWriter xmlEventWriter = XML_OUTPUT_FACTORY.createXMLEventWriter(outputXmlBytes, inputEncoding); xmlEventWriter.add(firstEvent); while (xmlEventReader.hasNext()) { XMLEvent xmlEvent = xmlEventReader.nextEvent(); xmlEventWriter.add(xmlEvent); if (isStartEventForSensitiveData(xmlEvent)) { QName startEventName = xmlEvent.asStartElement().getName(); while (xmlEventReader.hasNext()) { XMLEvent event = xmlEventReader.nextEvent(); if (event.isCharacters()) { Characters characters = event.asCharacters(); event = XML_EVENT_FACTORY.createCharacters(maskSensitiveData(characters.getData())); } xmlEventWriter.add(event); if (isMatchingEndEvent(event, startEventName)) { // The inner while-loop is guaranteed to break here for any valid XML. // If matching event is missing, xmlEventReader will throw XMLStreamException. break; } } } } xmlEventWriter.flush(); return outputXmlBytes.toString(inputEncoding); }
Example 10
Source File: AbstractXMLEventWriter.java From jettison with Apache License 2.0 | 4 votes |
public void add(XMLEvent event) throws XMLStreamException { if (event.isStartDocument()) { streamWriter.writeStartDocument(); } else if (event.isStartElement()) { StartElement element = event.asStartElement(); QName elQName = element.getName(); if (elQName.getPrefix().length() > 0 && elQName.getNamespaceURI().length() > 0) streamWriter.writeStartElement(elQName.getPrefix(), elQName .getLocalPart(), elQName.getNamespaceURI()); else if (elQName.getNamespaceURI().length() > 0) streamWriter.writeStartElement(elQName.getNamespaceURI(), elQName.getLocalPart()); else streamWriter.writeStartElement(elQName.getLocalPart()); // Add element namespaces Iterator namespaces = element.getNamespaces(); while (namespaces.hasNext()) { Namespace ns = (Namespace) namespaces.next(); String prefix = ns.getPrefix(); String nsURI = ns.getNamespaceURI(); streamWriter.writeNamespace(prefix, nsURI); } // Add element attributes Iterator attris = element.getAttributes(); while (attris.hasNext()) { Attribute attr = (Attribute) attris.next(); QName atQName = attr.getName(); String value = attr.getValue(); if (atQName.getPrefix().length() > 0 && atQName.getNamespaceURI().length() > 0) streamWriter.writeAttribute(atQName.getPrefix(), atQName .getNamespaceURI(), atQName.getLocalPart(), value); else if (atQName.getNamespaceURI().length() > 0) streamWriter.writeAttribute(atQName.getNamespaceURI(), atQName.getLocalPart(), value); else streamWriter.writeAttribute(atQName.getLocalPart(), value); } } else if (event.isCharacters()) { Characters chars = event.asCharacters(); streamWriter.writeCharacters(chars.getData()); } else if (event.isEndElement()) { streamWriter.writeEndElement(); } else if (event.isEndDocument()) { streamWriter.writeEndDocument(); } else { throw new XMLStreamException("Unsupported event type: " + event); } }
Example 11
Source File: XmlRecordSource.java From nifi with Apache License 2.0 | 4 votes |
private XmlNode readNext(final StartElement startElement) throws XMLStreamException, IOException { // Parse everything until we encounter the end element final StringBuilder content = new StringBuilder(); final Map<String, XmlNode> childNodes = new LinkedHashMap<>(); while (xmlEventReader.hasNext()) { final XMLEvent xmlEvent = xmlEventReader.nextEvent(); if (xmlEvent.isEndDocument()) { throw new EOFException("Expected to encounter End-of-Element tag for start tag '" + startElement.getName() + "'"); } if (xmlEvent.isEndElement()) { break; } if (xmlEvent.isCharacters()) { final Characters characters = xmlEvent.asCharacters(); if (!characters.isWhiteSpace()) { content.append(characters.getData()); } } if (xmlEvent.isStartElement()) { final StartElement childStartElement = xmlEvent.asStartElement(); final XmlNode childNode = readNext(childStartElement); final String childName = childStartElement.getName().getLocalPart(); final XmlNode existingNode = childNodes.get(childName); if (existingNode == null) { childNodes.put(childName, childNode); } else if (existingNode.getNodeType() == XmlNodeType.ARRAY) { ((XmlArrayNode) existingNode).addElement(childNode); } else { final XmlArrayNode arrayNode = new XmlArrayNode(childStartElement.getName().getLocalPart()); arrayNode.addElement(existingNode); arrayNode.addElement(childNode); childNodes.put(childName, arrayNode); } } } final String nodeName = startElement.getName().getLocalPart(); if (childNodes.isEmpty()) { return new XmlTextNode(nodeName, content.toString().trim()); } else { return new XmlContainerNode(nodeName, childNodes); } }
Example 12
Source File: StaxParser.java From tutorials with MIT License | 4 votes |
public List<Tutorial> getAllTutorial() { boolean bTitle = false; boolean bDescription = false; boolean bDate = false; boolean bAuthor = false; List<Tutorial> tutorials = new ArrayList<Tutorial>(); try { XMLInputFactory factory = XMLInputFactory.newInstance(); XMLEventReader eventReader = factory.createXMLEventReader(new FileReader(this.getFile())); Tutorial current = null; while (eventReader.hasNext()) { XMLEvent event = eventReader.nextEvent(); switch (event.getEventType()) { case XMLStreamConstants.START_ELEMENT: StartElement startElement = event.asStartElement(); String qName = startElement.getName().getLocalPart(); if (qName.equalsIgnoreCase("tutorial")) { current = new Tutorial(); Iterator<Attribute> attributes = startElement.getAttributes(); while (attributes.hasNext()) { Attribute currentAt = attributes.next(); if (currentAt.getName().toString().equalsIgnoreCase("tutId")) { current.setTutId(currentAt.getValue()); } else if (currentAt.getName().toString().equalsIgnoreCase("type")) { current.setType(currentAt.getValue()); } } } else if (qName.equalsIgnoreCase("title")) { bTitle = true; } else if (qName.equalsIgnoreCase("description")) { bDescription = true; } else if (qName.equalsIgnoreCase("date")) { bDate = true; } else if (qName.equalsIgnoreCase("author")) { bAuthor = true; } break; case XMLStreamConstants.CHARACTERS: Characters characters = event.asCharacters(); if (bTitle) { if (current != null) { current.setTitle(characters.getData()); } bTitle = false; } if (bDescription) { if (current != null) { current.setDescription(characters.getData()); } bDescription = false; } if (bDate) { if (current != null) { current.setDate(characters.getData()); } bDate = false; } if (bAuthor) { if (current != null) { current.setAuthor(characters.getData()); } bAuthor = false; } break; case XMLStreamConstants.END_ELEMENT: EndElement endElement = event.asEndElement(); if (endElement.getName().getLocalPart().equalsIgnoreCase("tutorial")) { if (current != null) { tutorials.add(current); } } break; } } } catch (FileNotFoundException | XMLStreamException e) { e.printStackTrace(); } return tutorials; }
Example 13
Source File: StreamReader.java From simplexml with Apache License 2.0 | 2 votes |
/** * Constructor for the <code>Text</code> object. This creates * an event that provides text to the core reader. Text can be * in the form of a CDATA section or a normal text entry. * * @param event this is the node that represents the text value */ public Text(XMLEvent event) { this.text = event.asCharacters(); }