javax.xml.stream.XMLStreamConstants Java Examples
The following examples show how to use
javax.xml.stream.XMLStreamConstants.
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: ApackGitManifestDeserializer.java From ADT_Frontend with MIT License | 6 votes |
public IApackManifest deserializeApackManifest(XMLStreamReader xmlReader) throws XMLStreamException { ApackManifest apackManifest = new ApackManifest(); while (xmlReader.hasNext()) { int next = xmlReader.next(); switch (next) { case XMLStreamConstants.START_ELEMENT: switch (xmlReader.getLocalName()) { case "descriptor": //$NON-NLS-1$ apackManifest.setDescriptor(deserializeDescriptor(xmlReader)); break; default: break; } break; default: break; } } return apackManifest; }
Example #2
Source File: FeaturePackXmlParser20.java From galleon with Apache License 2.0 | 6 votes |
private static void readTransitive(XMLExtendedStreamReader reader, FeaturePackDepsConfigBuilder<?> builder) throws XMLStreamException { ParsingUtils.parseNoAttributes(reader); while (reader.hasNext()) { switch (reader.nextTag()) { case XMLStreamConstants.END_ELEMENT: { return; } case XMLStreamConstants.START_ELEMENT: { final Element element = Element.of(reader.getLocalName()); switch (element) { case DEPENDENCY: ProvisioningXmlParser30.readTransitiveFeaturePackDep(reader, builder); break; default: throw ParsingUtils.unexpectedContent(reader); } break; } default: { throw ParsingUtils.unexpectedContent(reader); } } } throw ParsingUtils.endOfDocument(reader.getLocation()); }
Example #3
Source File: FeaturePackXmlParser20.java From galleon with Apache License 2.0 | 6 votes |
private static void parsePlugins(XMLExtendedStreamReader reader, FeaturePackSpec.Builder fpBuilder) throws XMLStreamException { ParsingUtils.parseNoAttributes(reader); while (reader.hasNext()) { switch (reader.nextTag()) { case XMLStreamConstants.END_ELEMENT: { return; } case XMLStreamConstants.START_ELEMENT: { final Element element = Element.of(reader.getName().getLocalPart()); switch (element) { case PLUGIN: parsePlugin(reader, fpBuilder); break; default: throw ParsingUtils.unexpectedContent(reader); } break; } default: { throw ParsingUtils.unexpectedContent(reader); } } } throw ParsingUtils.endOfDocument(reader.getLocation()); }
Example #4
Source File: BaseXMLEventReader.java From lams with GNU General Public License v2.0 | 6 votes |
@Override public final XMLEvent nextTag() throws XMLStreamException { XMLEvent event = this.nextEvent(); while ((event.isCharacters() && event.asCharacters().isWhiteSpace()) || event.isProcessingInstruction() || event.getEventType() == XMLStreamConstants.COMMENT) { event = this.nextEvent(); } if (!event.isStartElement() && event.isEndElement()) { throw new XMLStreamException("Unexpected event type '" + XMLStreamConstantsUtils.getEventName(event.getEventType()) + "' encountered. Found event: " + event, event.getLocation()); } return event; }
Example #5
Source File: ConfigXml.java From galleon with Apache License 2.0 | 6 votes |
private static void readProps(XMLExtendedStreamReader reader, ConfigModel.Builder builder) throws XMLStreamException { ParsingUtils.parseNoAttributes(reader); while (reader.hasNext()) { switch (reader.nextTag()) { case XMLStreamConstants.END_ELEMENT: { return; } case XMLStreamConstants.START_ELEMENT: { final Element element = Element.of(reader.getName().getLocalPart()); switch (element) { case PROP: readProp(reader, builder); break; default: throw ParsingUtils.unexpectedContent(reader); } break; } default: { throw ParsingUtils.unexpectedContent(reader); } } } throw ParsingUtils.endOfDocument(reader.getLocation()); }
Example #6
Source File: NamespaceTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
@Test public void testRootElementNamespace() { try { XMLInputFactory xif = XMLInputFactory.newInstance(); xif.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.TRUE); InputStream is = new java.io.ByteArrayInputStream(getXML().getBytes()); XMLStreamReader sr = xif.createXMLStreamReader(is); while (sr.hasNext()) { int eventType = sr.next(); if (eventType == XMLStreamConstants.START_ELEMENT) { if (sr.getLocalName().equals(rootElement)) { Assert.assertTrue(sr.getNamespacePrefix(0).equals(prefix) && sr.getNamespaceURI(0).equals(namespaceURI)); } } } } catch (Exception ex) { ex.printStackTrace(); } }
Example #7
Source File: XMLStreamReaderImpl.java From TencentKona-8 with GNU General Public License v2.0 | 6 votes |
/** Skips any insignificant events (COMMENT and PROCESSING_INSTRUCTION) * until a START_ELEMENT or * END_ELEMENT is reached. If other than space characters are * encountered, an exception is thrown. This method should * be used when processing element-only content because * the parser is not able to recognize ignorable whitespace if * then DTD is missing or not interpreted. * @return the event type of the element read * @throws XMLStreamException if the current event is not white space */ public int nextTag() throws XMLStreamException { int eventType = next(); while((eventType == XMLStreamConstants.CHARACTERS && isWhiteSpace()) // skip whitespace || (eventType == XMLStreamConstants.CDATA && isWhiteSpace()) // skip whitespace || eventType == XMLStreamConstants.SPACE || eventType == XMLStreamConstants.PROCESSING_INSTRUCTION || eventType == XMLStreamConstants.COMMENT ) { eventType = next(); } if (eventType != XMLStreamConstants.START_ELEMENT && eventType != XMLStreamConstants.END_ELEMENT) { throw new XMLStreamException( "found: " + getEventTypeString(eventType) + ", expected " + getEventTypeString(XMLStreamConstants.START_ELEMENT) + " or " + getEventTypeString(XMLStreamConstants.END_ELEMENT), getLocation()); } return eventType; }
Example #8
Source File: XMLStreamReaderImpl.java From jdk8u60 with GNU General Public License v2.0 | 6 votes |
protected List getNotationDecls(){ if(fEventType == XMLStreamConstants.DTD){ if(fScanner.fDTDScanner == null) return null; DTDGrammar grammar = ((XMLDTDScannerImpl)(fScanner.fDTDScanner)).getGrammar(); if(grammar == null) return null; List notations = grammar.getNotationDecls(); Iterator it = notations.iterator(); ArrayList list = new ArrayList(); while(it.hasNext()){ XMLNotationDecl ni = (XMLNotationDecl)it.next(); if(ni!= null){ list.add(new NotationDeclarationImpl(ni)); } } return list; } return null; }
Example #9
Source File: XMLStreamReaderImpl.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
/** * Skips any insignificant events (COMMENT and PROCESSING_INSTRUCTION) until * a START_ELEMENT or END_ELEMENT is reached. If other than space characters * are encountered, an exception is thrown. This method should be used when * processing element-only content because the parser is not able to * recognize ignorable whitespace if then DTD is missing or not interpreted. * * @return the event type of the element read * @throws XMLStreamException if the current event is not white space */ public int nextTag() throws XMLStreamException { int eventType = next(); while ((eventType == XMLStreamConstants.CHARACTERS && isWhiteSpace()) // skip whitespace || (eventType == XMLStreamConstants.CDATA && isWhiteSpace()) // skip whitespace || eventType == XMLStreamConstants.SPACE || eventType == XMLStreamConstants.PROCESSING_INSTRUCTION || eventType == XMLStreamConstants.COMMENT) { eventType = next(); } if (eventType != XMLStreamConstants.START_ELEMENT && eventType != XMLStreamConstants.END_ELEMENT) { throw new XMLStreamException( "found: " + getEventTypeString(eventType) + ", expected " + getEventTypeString(XMLStreamConstants.START_ELEMENT) + " or " + getEventTypeString(XMLStreamConstants.END_ELEMENT), getLocation()); } return eventType; }
Example #10
Source File: MavenProducerXmlParser10.java From galleon with Apache License 2.0 | 6 votes |
private void readFrequencies(XMLExtendedStreamReader reader, MavenParsedProducerCallbackHandler builder) throws XMLStreamException { ParsingUtils.parseNoAttributes(reader); while (reader.hasNext()) { switch (reader.nextTag()) { case XMLStreamConstants.END_ELEMENT: { return; } case XMLStreamConstants.START_ELEMENT: { final Element element = Element.of(reader.getName()); switch (element) { case FREQUENCY: readFrequency(reader, builder); break; default: throw ParsingUtils.unexpectedContent(reader); } break; } default: { throw ParsingUtils.unexpectedContent(reader); } } } throw ParsingUtils.endOfDocument(reader.getLocation()); }
Example #11
Source File: Bug6481678.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
@Test public void testReadingNamespace() { is = new java.io.ByteArrayInputStream(getXML().getBytes()); try { XMLStreamReader sr = factory.createFilteredReader(factory.createXMLStreamReader(is), (StreamFilter) filter); while (sr.hasNext()) { int eventType = sr.getEventType(); if (eventType == XMLStreamConstants.START_ELEMENT) { if (sr.getLocalName().equals(rootElement)) { Assert.assertTrue(sr.getNamespacePrefix(0).equals(prefixApple) && sr.getNamespaceURI(0).equals(namespaceURIApple)); } } eventType = sr.next(); } } catch (Exception ex) { Assert.fail("Exception: " + ex.getMessage()); } }
Example #12
Source File: XMLStreamReaderImpl.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
protected List<NotationDeclaration> getNotationDecls() { if (fEventType == XMLStreamConstants.DTD) { if (fScanner.fDTDScanner == null) { return null; } DTDGrammar grammar = ((XMLDTDScannerImpl) (fScanner.fDTDScanner)).getGrammar(); if (grammar == null) { return null; } List<XMLNotationDecl> notations = grammar.getNotationDecls(); ArrayList<NotationDeclaration> list = new ArrayList<>(); for (XMLNotationDecl notation : notations) { if (notation != null) { list.add(new NotationDeclarationImpl(notation)); } } return list; } return null; }
Example #13
Source File: StAXSource.java From jdk8u60 with GNU General Public License v2.0 | 6 votes |
/** * Creates a new {@link javax.xml.transform.Source} for the given * {@link XMLStreamReader}. * * The XMLStreamReader must be pointing at either a * {@link javax.xml.stream.XMLStreamConstants#START_DOCUMENT} or * {@link javax.xml.stream.XMLStreamConstants#START_ELEMENT} event. * * @param reader XMLStreamReader that will be exposed as a Source * @param eagerQuit if true, when the conversion is completed, leave the cursor to the last * event that was fired (such as end element) * @param inscope inscope Namespaces * array of the even length of the form { prefix0, uri0, prefix1, uri1, ... } * @throws IllegalArgumentException iff the reader is null * @throws IllegalStateException iff the reader is not pointing at either a * START_DOCUMENT or START_ELEMENT event */ public StAXSource(XMLStreamReader reader, boolean eagerQuit, @NotNull String[] inscope) { if( reader == null ) throw new IllegalArgumentException(); this.staxReader = reader; int eventType = reader.getEventType(); if (!(eventType == XMLStreamConstants.START_DOCUMENT) && !(eventType == XMLStreamConstants.START_ELEMENT)) { throw new IllegalStateException(); } this.reader = new XMLStreamReaderToContentHandler(reader,repeater,eagerQuit,false,inscope); super.setXMLReader(pseudoParser); // pass a dummy InputSource. We don't care super.setInputSource(new InputSource()); }
Example #14
Source File: ProcessingInstructionTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
@Test public void testPITargetAndData() { try { XMLInputFactory xif = XMLInputFactory.newInstance(); String PITarget = "soffice"; String PIData = "WebservicesArchitecture"; String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<?" + PITarget + " " + PIData + "?>" + "<foo></foo>"; // System.out.println("XML = " + xml) ; InputStream is = new java.io.ByteArrayInputStream(xml.getBytes()); XMLStreamReader sr = xif.createXMLStreamReader(is); while (sr.hasNext()) { int eventType = sr.next(); if (eventType == XMLStreamConstants.PROCESSING_INSTRUCTION) { String target = sr.getPITarget(); String data = sr.getPIData(); Assert.assertTrue(target.equals(PITarget) && data.equals(PIData)); } } } catch (Exception ex) { ex.printStackTrace(); } }
Example #15
Source File: Bug6481678.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
@Test public void testNamespaceContext() { is = new java.io.ByteArrayInputStream(getXML().getBytes()); try { XMLStreamReader sr = factory.createFilteredReader(factory.createXMLStreamReader(is), (StreamFilter) filter); while (sr.hasNext()) { int eventType = sr.next(); if (eventType == XMLStreamConstants.START_ELEMENT) { if (sr.getLocalName().equals(childElement)) { NamespaceContext context = sr.getNamespaceContext(); Assert.assertTrue(context.getPrefix(namespaceURIApple).equals(prefixApple)); } } } } catch (Exception ex) { Assert.fail("Exception: " + ex.getMessage()); } }
Example #16
Source File: StreamReaderTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
/** * CR 6631264 / sjsxp Issue 45: * https://sjsxp.dev.java.net/issues/show_bug.cgi?id=45 * XMLStreamReader.hasName() should return false for ENTITY_REFERENCE */ @Test public void testHasNameOnEntityEvent() throws Exception { XMLInputFactory xif = XMLInputFactory.newInstance(); xif.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, false); XMLStreamReader r = xif.createXMLStreamReader( this.getClass().getResourceAsStream("ExternalDTD.xml")); while (r.next() != XMLStreamConstants.ENTITY_REFERENCE) { System.out.println("event type: " + r.getEventType()); continue; } if (r.hasName()) { System.out.println("hasName returned true on ENTITY_REFERENCE event."); } Assert.assertFalse(r.hasName()); // fails }
Example #17
Source File: CatalogSupportBase.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
/** * Returns the accumulated text of an event type. * * @param streamReader the XMLStreamReader * @param type the type of event requested * @return the text of the accumulated text for the request type * @throws XMLStreamException */ String getText(XMLStreamReader streamReader, int type) throws XMLStreamException { StringBuilder text = new StringBuilder(); StringBuilder entityRef = new StringBuilder(); while(streamReader.hasNext()){ int eventType = streamReader.next(); switch (eventType) { case XMLStreamConstants.START_ELEMENT: break; case XMLStreamConstants.CHARACTERS: text.append(streamReader.getText()); break; case XMLStreamConstants.ENTITY_REFERENCE: entityRef.append(streamReader.getText()); break; } } if (type == XMLStreamConstants.CHARACTERS) { return text.toString(); } else { return entityRef.toString(); } }
Example #18
Source File: SchemaReaderBase.java From staedi with Apache License 2.0 | 6 votes |
List<Integer> readSyntaxPositions(XMLStreamReader reader) { final List<Integer> positions = new ArrayList<>(5); boolean endOfSyntax = false; while (!endOfSyntax) { final int event = nextTag(reader, "reading syntax positions"); final QName element = reader.getName(); if (event == XMLStreamConstants.START_ELEMENT) { if (element.equals(qnPosition)) { final String position = getElementText(reader, "syntax position"); try { positions.add(Integer.parseInt(position)); } catch (@SuppressWarnings("unused") NumberFormatException e) { throw schemaException("invalid position [" + position + ']', reader); } } } else { endOfSyntax = true; } } return positions; }
Example #19
Source File: AbstractXMLStreamReader.java From lams with GNU General Public License v2.0 | 5 votes |
@Override public String getPrefix() { int eventType = getEventType(); if (eventType == XMLStreamConstants.START_ELEMENT || eventType == XMLStreamConstants.END_ELEMENT) { return getName().getPrefix(); } else { throw new IllegalStateException("parser must be on START_ELEMENT or END_ELEMENT state"); } }
Example #20
Source File: StaEDIXMLStreamReaderTest.java From staedi with Apache License 2.0 | 5 votes |
@Test void testRequire() throws Exception { XMLStreamReader xmlReader = getXmlReader(DUMMY_X12); XMLStreamException thrown; assertEquals(XMLStreamConstants.START_DOCUMENT, xmlReader.getEventType()); thrown = assertThrows(XMLStreamException.class, () -> xmlReader.require(XMLStreamConstants.START_DOCUMENT, EDINamespaces.LOOPS, "INTERCHANGE")); assertTrue(thrown.getMessage().endsWith("does not have a corresponding name")); thrown = assertThrows(XMLStreamException.class, () -> xmlReader.require(XMLStreamConstants.START_ELEMENT, EDINamespaces.LOOPS, "INTERCHANGE")); assertTrue(thrown.getMessage().contains("does not match required type")); xmlReader.next(); // Happy Path xmlReader.require(XMLStreamConstants.START_ELEMENT, EDINamespaces.LOOPS, "INTERCHANGE"); xmlReader.require(XMLStreamConstants.START_ELEMENT, EDINamespaces.LOOPS, null); xmlReader.require(XMLStreamConstants.START_ELEMENT, null, "INTERCHANGE"); xmlReader.require(XMLStreamConstants.START_ELEMENT, null, null); thrown = assertThrows(XMLStreamException.class, () -> xmlReader.require(XMLStreamConstants.START_ELEMENT, EDINamespaces.LOOPS, "GROUP")); assertTrue(thrown.getMessage().contains("does not match required localName")); thrown = assertThrows(XMLStreamException.class, () -> xmlReader.require(XMLStreamConstants.START_ELEMENT, EDINamespaces.SEGMENTS, "INTERCHANGE")); assertTrue(thrown.getMessage().contains("does not match required namespaceURI")); }
Example #21
Source File: StartDocumentEvent.java From jdk8u60 with GNU General Public License v2.0 | 5 votes |
protected void init(String encoding, String version, boolean standalone,Location loc) { setEventType(XMLStreamConstants.START_DOCUMENT); this.fEncodingScheam = encoding; this.fVersion = version; this.fStandalone = standalone; if (encoding != null && !encoding.equals("")) this.fEncodingSchemeSet = true; else { this.fEncodingSchemeSet = false; this.fEncodingScheam = "UTF-8"; } this.fLocation = loc; }
Example #22
Source File: XMLStreamReaderImpl.java From TencentKona-8 with GNU General Public License v2.0 | 5 votes |
/** * Returns true if the cursor points to a character data event that consists of all whitespace * Application calling this method needs to cache the value and avoid calling this method again * for the same event. * @return */ public boolean isWhiteSpace() { if(isCharacters() || (fEventType == XMLStreamConstants.CDATA)){ char [] ch = this.getTextCharacters(); final int start = this.getTextStart(); final int end = start + this.getTextLength(); for (int i = start; i < end; i++){ if(!XMLChar.isSpace(ch[i])){ return false; } } return true; } return false; }
Example #23
Source File: XmlPolicyModelUnmarshaller.java From TencentKona-8 with GNU General Public License v2.0 | 5 votes |
/** * See {@link PolicyModelUnmarshaller#unmarshalModel(Object) base method documentation}. */ public PolicySourceModel unmarshalModel(final Object storage) throws PolicyException { final XMLEventReader reader = createXMLEventReader(storage); PolicySourceModel model = null; loop: while (reader.hasNext()) { try { final XMLEvent event = reader.peek(); switch (event.getEventType()) { case XMLStreamConstants.START_DOCUMENT: case XMLStreamConstants.COMMENT: reader.nextEvent(); break; // skipping the comments and start document events case XMLStreamConstants.CHARACTERS: processCharacters(ModelNode.Type.POLICY, event.asCharacters(), null); // we advance the reader only if there is no exception thrown from // the processCharacters(...) call. Otherwise we don't modify the stream reader.nextEvent(); break; case XMLStreamConstants.START_ELEMENT: if (NamespaceVersion.resolveAsToken(event.asStartElement().getName()) == XmlToken.Policy) { StartElement rootElement = reader.nextEvent().asStartElement(); model = initializeNewModel(rootElement); unmarshalNodeContent(model.getNamespaceVersion(), model.getRootNode(), rootElement.getName(), reader); break loop; } else { throw LOGGER.logSevereException(new PolicyException(LocalizationMessages.WSP_0048_POLICY_ELEMENT_EXPECTED_FIRST())); } default: throw LOGGER.logSevereException(new PolicyException(LocalizationMessages.WSP_0048_POLICY_ELEMENT_EXPECTED_FIRST())); } } catch (XMLStreamException e) { throw LOGGER.logSevereException(new PolicyException(LocalizationMessages.WSP_0068_FAILED_TO_UNMARSHALL_POLICY_EXPRESSION(), e)); } } return model; }
Example #24
Source File: EPRHeader.java From TencentKona-8 with GNU General Public License v2.0 | 5 votes |
@Nullable public String getAttribute(@NotNull String nsUri, @NotNull String localName) { try { XMLStreamReader sr = epr.read("EndpointReference"/*doesn't matter*/); while(sr.getEventType()!= XMLStreamConstants.START_ELEMENT) sr.next(); return sr.getAttributeValue(nsUri,localName); } catch (XMLStreamException e) { // since we are reading from buffer, this can't happen. throw new AssertionError(e); } }
Example #25
Source File: XMLEventReaderImpl.java From TencentKona-8 with GNU General Public License v2.0 | 5 votes |
/** Skips any insignificant space events until a START_ELEMENT or * END_ELEMENT is reached. If anything other than space characters are * encountered, an exception is thrown. This method should * be used when processing element-only content because * the parser is not able to recognize ignorable whitespace if * the DTD is missing or not interpreted. * @throws XMLStreamException if anything other than space characters are encountered */ public XMLEvent nextTag() throws XMLStreamException { //its really a pain if there is peeked event before calling nextTag() if(fPeekedEvent != null){ //check the peeked event first. XMLEvent event = fPeekedEvent; fPeekedEvent = null ; int eventType = event.getEventType(); //if peeked event is whitespace move to the next event //if peeked event is PI or COMMENT move to the next event if( (event.isCharacters() && event.asCharacters().isWhiteSpace()) || eventType == XMLStreamConstants.PROCESSING_INSTRUCTION || eventType == XMLStreamConstants.COMMENT || eventType == XMLStreamConstants.START_DOCUMENT){ event = nextEvent(); eventType = event.getEventType(); } //we have to have the while loop because there can be many PI or comment event in sucession while((event.isCharacters() && event.asCharacters().isWhiteSpace()) || eventType == XMLStreamConstants.PROCESSING_INSTRUCTION || eventType == XMLStreamConstants.COMMENT){ event = nextEvent(); eventType = event.getEventType(); } if (eventType != XMLStreamConstants.START_ELEMENT && eventType != XMLStreamConstants.END_ELEMENT) { throw new XMLStreamException("expected start or end tag", event.getLocation()); } return event; } //if there is no peeked event -- delegate the work of getting next event to fXMLReader fXMLReader.nextTag(); return (fLastEvent = fXMLEventAllocator.allocate(fXMLReader)); }
Example #26
Source File: XMLStreamReaderUtil.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
public static void readRest(XMLStreamReader reader) { try { while(reader.getEventType() != XMLStreamConstants.END_DOCUMENT) { reader.next(); } } catch (XMLStreamException e) { throw wrapException(e); } }
Example #27
Source File: AbstractXMLStreamReader.java From spring-analysis-note with MIT License | 5 votes |
@Override public String getElementText() throws XMLStreamException { if (getEventType() != XMLStreamConstants.START_ELEMENT) { throw new XMLStreamException("Parser must be on START_ELEMENT to read next text", getLocation()); } int eventType = next(); StringBuilder builder = new StringBuilder(); while (eventType != XMLStreamConstants.END_ELEMENT) { if (eventType == XMLStreamConstants.CHARACTERS || eventType == XMLStreamConstants.CDATA || eventType == XMLStreamConstants.SPACE || eventType == XMLStreamConstants.ENTITY_REFERENCE) { builder.append(getText()); } else if (eventType == XMLStreamConstants.PROCESSING_INSTRUCTION || eventType == XMLStreamConstants.COMMENT) { // skipping } else if (eventType == XMLStreamConstants.END_DOCUMENT) { throw new XMLStreamException("Unexpected end of document when reading element text content", getLocation()); } else if (eventType == XMLStreamConstants.START_ELEMENT) { throw new XMLStreamException("Element text content may not contain START_ELEMENT", getLocation()); } else { throw new XMLStreamException("Unexpected event type " + eventType, getLocation()); } eventType = next(); } return builder.toString(); }
Example #28
Source File: XMLEventReaderImpl.java From jdk8u60 with GNU General Public License v2.0 | 5 votes |
/** Skips any insignificant space events until a START_ELEMENT or * END_ELEMENT is reached. If anything other than space characters are * encountered, an exception is thrown. This method should * be used when processing element-only content because * the parser is not able to recognize ignorable whitespace if * the DTD is missing or not interpreted. * @throws XMLStreamException if anything other than space characters are encountered */ public XMLEvent nextTag() throws XMLStreamException { //its really a pain if there is peeked event before calling nextTag() if(fPeekedEvent != null){ //check the peeked event first. XMLEvent event = fPeekedEvent; fPeekedEvent = null ; int eventType = event.getEventType(); //if peeked event is whitespace move to the next event //if peeked event is PI or COMMENT move to the next event if( (event.isCharacters() && event.asCharacters().isWhiteSpace()) || eventType == XMLStreamConstants.PROCESSING_INSTRUCTION || eventType == XMLStreamConstants.COMMENT || eventType == XMLStreamConstants.START_DOCUMENT){ event = nextEvent(); eventType = event.getEventType(); } //we have to have the while loop because there can be many PI or comment event in sucession while((event.isCharacters() && event.asCharacters().isWhiteSpace()) || eventType == XMLStreamConstants.PROCESSING_INSTRUCTION || eventType == XMLStreamConstants.COMMENT){ event = nextEvent(); eventType = event.getEventType(); } if (eventType != XMLStreamConstants.START_ELEMENT && eventType != XMLStreamConstants.END_ELEMENT) { throw new XMLStreamException("expected start or end tag", event.getLocation()); } return event; } //if there is no peeked event -- delegate the work of getting next event to fXMLReader fXMLReader.nextTag(); return (fLastEvent = fXMLEventAllocator.allocate(fXMLReader)); }
Example #29
Source File: XmlPolicyModelUnmarshaller.java From jdk8u60 with GNU General Public License v2.0 | 5 votes |
private String unmarshalNodeContent(final NamespaceVersion nsVersion, final ModelNode node, final QName nodeElementName, final XMLEventReader reader) throws PolicyException { StringBuilder valueBuffer = null; loop: while (reader.hasNext()) { try { final XMLEvent xmlParserEvent = reader.nextEvent(); switch (xmlParserEvent.getEventType()) { case XMLStreamConstants.COMMENT: break; // skipping the comments case XMLStreamConstants.CHARACTERS: valueBuffer = processCharacters(node.getType(), xmlParserEvent.asCharacters(), valueBuffer); break; case XMLStreamConstants.END_ELEMENT: checkEndTagName(nodeElementName, xmlParserEvent.asEndElement()); break loop; // data exctraction for currently processed policy node is done case XMLStreamConstants.START_ELEMENT: final StartElement childElement = xmlParserEvent.asStartElement(); ModelNode childNode = addNewChildNode(nsVersion, node, childElement); String value = unmarshalNodeContent(nsVersion, childNode, childElement.getName(), reader); if (childNode.isDomainSpecific()) { parseAssertionData(nsVersion, value, childNode, childElement); } break; default: throw LOGGER.logSevereException(new PolicyException(LocalizationMessages.WSP_0011_UNABLE_TO_UNMARSHALL_POLICY_XML_ELEM_EXPECTED())); } } catch (XMLStreamException e) { throw LOGGER.logSevereException(new PolicyException(LocalizationMessages.WSP_0068_FAILED_TO_UNMARSHALL_POLICY_EXPRESSION(), e)); } } return (valueBuffer == null) ? null : valueBuffer.toString().trim(); }
Example #30
Source File: XMLStreamReaderImpl.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
/** * Reads the content of a text-only element. Precondition: the current event * is START_ELEMENT. Postcondition: The current event is the corresponding * END_ELEMENT. * * @throws XMLStreamException if the current event is not a START_ELEMENT or * if a non text element is encountered */ public String getElementText() throws XMLStreamException { if (getEventType() != XMLStreamConstants.START_ELEMENT) { throw new XMLStreamException( "parser must be on START_ELEMENT to read next text", getLocation()); } int eventType = next(); StringBuilder content = new StringBuilder(); while (eventType != XMLStreamConstants.END_ELEMENT) { if (eventType == XMLStreamConstants.CHARACTERS || eventType == XMLStreamConstants.CDATA || eventType == XMLStreamConstants.SPACE || eventType == XMLStreamConstants.ENTITY_REFERENCE) { content.append(getText()); } else if (eventType == XMLStreamConstants.PROCESSING_INSTRUCTION || eventType == XMLStreamConstants.COMMENT) { // skipping } else if (eventType == XMLStreamConstants.END_DOCUMENT) { throw new XMLStreamException( "unexpected end of document when reading element text content"); } else if (eventType == XMLStreamConstants.START_ELEMENT) { throw new XMLStreamException("elementGetText() function expects text " + "only elment but START_ELEMENT was encountered.", getLocation()); } else { throw new XMLStreamException( "Unexpected event type " + eventType, getLocation()); } eventType = next(); } return content.toString(); }