Java Code Examples for javax.xml.stream.events.XMLEvent#getLocation()
The following examples show how to use
javax.xml.stream.events.XMLEvent#getLocation() .
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: AttrXmlResourceValue.java From bazel with Apache License 2.0 | 6 votes |
private static Map<String, String> readSubValues(XMLEventReader reader, QName subTagType) throws XMLStreamException { ImmutableMap.Builder<String, String> builder = ImmutableMap.builder(); while (reader.hasNext() && XmlResourceValues.isTag(XmlResourceValues.peekNextTag(reader), subTagType)) { StartElement element = reader.nextEvent().asStartElement(); builder.put( XmlResourceValues.getElementName(element), XmlResourceValues.getElementValue(element)); XMLEvent endTag = reader.nextEvent(); if (!XmlResourceValues.isEndTag(endTag, subTagType)) { throw new XMLStreamException( String.format("Unexpected [%s]; Expected %s", endTag, "</enum>"), endTag.getLocation()); } } return builder.build(); }
Example 2
Source File: Issue58Test.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
@Test public void testLocation() { String XML = "<?xml version='1.0' ?>" + "<!DOCTYPE root [\n" + "<!ENTITY intEnt 'internal'>\n" + "<!ENTITY extParsedEnt SYSTEM 'url:dummy'>\n" + "<!NOTATION notation PUBLIC 'notation-public-id'>\n" + "<!NOTATION notation2 SYSTEM 'url:dummy'>\n" + "<!ENTITY extUnparsedEnt SYSTEM 'url:dummy2' NDATA notation>\n" + "]>\n" + "<root />"; try { XMLEventReader er = getReader(XML); XMLEvent evt = er.nextEvent(); // StartDocument Location loc1 = evt.getLocation(); System.out.println("Location 1: " + loc1.getLineNumber() + "," + loc1.getColumnNumber()); evt = er.nextEvent(); // DTD // loc1 should not change so its line number should still be 1 Assert.assertTrue(loc1.getLineNumber() == 1); Location loc2 = evt.getLocation(); System.out.println("Location 2: " + loc2.getLineNumber() + "," + loc2.getColumnNumber()); evt = er.nextEvent(); // root System.out.println("Location 1: " + loc1.getLineNumber() + "," + loc1.getColumnNumber()); Assert.assertTrue(loc1.getLineNumber() == 1); Assert.assertTrue(loc2.getLineNumber() == 7); } catch (Exception e) { Assert.fail(e.getMessage()); } }
Example 3
Source File: ExternalAttachmentsUnmarshaller.java From TencentKona-8 with GNU General Public License v2.0 | 5 votes |
private Map<URI, Policy> unmarshal(final XMLEventReader reader, final StartElement parentElement) throws PolicyException { XMLEvent event = null; while (reader.hasNext()) { try { event = reader.peek(); switch (event.getEventType()) { case XMLStreamConstants.START_DOCUMENT: case XMLStreamConstants.COMMENT: reader.nextEvent(); break; case XMLStreamConstants.CHARACTERS: processCharacters(event.asCharacters(), parentElement, map); reader.nextEvent(); break; case XMLStreamConstants.END_ELEMENT: processEndTag(event.asEndElement(), parentElement); reader.nextEvent(); return map; case XMLStreamConstants.START_ELEMENT: final StartElement element = event.asStartElement(); processStartTag(element, parentElement, reader, map); break; case XMLStreamConstants.END_DOCUMENT: return map; default: throw LOGGER.logSevereException(new PolicyException(LocalizationMessages.WSP_0087_UNKNOWN_EVENT(event))); } } catch (XMLStreamException e) { final Location location = event == null ? null : event.getLocation(); throw LOGGER.logSevereException(new PolicyException(LocalizationMessages.WSP_0088_FAILED_PARSE(location)), e); } } return map; }
Example 4
Source File: ExternalAttachmentsUnmarshaller.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
private Map<URI, Policy> unmarshal(final XMLEventReader reader, final StartElement parentElement) throws PolicyException { XMLEvent event = null; while (reader.hasNext()) { try { event = reader.peek(); switch (event.getEventType()) { case XMLStreamConstants.START_DOCUMENT: case XMLStreamConstants.COMMENT: reader.nextEvent(); break; case XMLStreamConstants.CHARACTERS: processCharacters(event.asCharacters(), parentElement, map); reader.nextEvent(); break; case XMLStreamConstants.END_ELEMENT: processEndTag(event.asEndElement(), parentElement); reader.nextEvent(); return map; case XMLStreamConstants.START_ELEMENT: final StartElement element = event.asStartElement(); processStartTag(element, parentElement, reader, map); break; case XMLStreamConstants.END_DOCUMENT: return map; default: throw LOGGER.logSevereException(new PolicyException(LocalizationMessages.WSP_0087_UNKNOWN_EVENT(event))); } } catch (XMLStreamException e) { final Location location = event == null ? null : event.getLocation(); throw LOGGER.logSevereException(new PolicyException(LocalizationMessages.WSP_0088_FAILED_PARSE(location)), e); } } return map; }
Example 5
Source File: ExternalAttachmentsUnmarshaller.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 5 votes |
private Map<URI, Policy> unmarshal(final XMLEventReader reader, final StartElement parentElement) throws PolicyException { XMLEvent event = null; while (reader.hasNext()) { try { event = reader.peek(); switch (event.getEventType()) { case XMLStreamConstants.START_DOCUMENT: case XMLStreamConstants.COMMENT: reader.nextEvent(); break; case XMLStreamConstants.CHARACTERS: processCharacters(event.asCharacters(), parentElement, map); reader.nextEvent(); break; case XMLStreamConstants.END_ELEMENT: processEndTag(event.asEndElement(), parentElement); reader.nextEvent(); return map; case XMLStreamConstants.START_ELEMENT: final StartElement element = event.asStartElement(); processStartTag(element, parentElement, reader, map); break; case XMLStreamConstants.END_DOCUMENT: return map; default: throw LOGGER.logSevereException(new PolicyException(LocalizationMessages.WSP_0087_UNKNOWN_EVENT(event))); } } catch (XMLStreamException e) { final Location location = event == null ? null : event.getLocation(); throw LOGGER.logSevereException(new PolicyException(LocalizationMessages.WSP_0088_FAILED_PARSE(location)), e); } } return map; }
Example 6
Source File: XMLEventReaderImpl.java From hottub 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 7
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 8
Source File: FileSourceXML.java From gama with GNU General Public License v3.0 | 5 votes |
/** * Generate a new parse exception. * * @param e event producing an error * @param critical if true, will always produce an exception, else if strict mode is disable, will only produce a warning * @param msg message to put in the exception * @param args arguments of the message */ protected void newParseError(XMLEvent e, boolean critical, String msg, Object... args) throws XMLStreamException { if (!critical && !strictMode) { LOGGER.warning(String.format(msg, args)); } else { throw new XMLStreamException(String.format(msg, args), e.getLocation()); } }
Example 9
Source File: ExternalAttachmentsUnmarshaller.java From openjdk-8 with GNU General Public License v2.0 | 5 votes |
private Map<URI, Policy> unmarshal(final XMLEventReader reader, final StartElement parentElement) throws PolicyException { XMLEvent event = null; while (reader.hasNext()) { try { event = reader.peek(); switch (event.getEventType()) { case XMLStreamConstants.START_DOCUMENT: case XMLStreamConstants.COMMENT: reader.nextEvent(); break; case XMLStreamConstants.CHARACTERS: processCharacters(event.asCharacters(), parentElement, map); reader.nextEvent(); break; case XMLStreamConstants.END_ELEMENT: processEndTag(event.asEndElement(), parentElement); reader.nextEvent(); return map; case XMLStreamConstants.START_ELEMENT: final StartElement element = event.asStartElement(); processStartTag(element, parentElement, reader, map); break; case XMLStreamConstants.END_DOCUMENT: return map; default: throw LOGGER.logSevereException(new PolicyException(LocalizationMessages.WSP_0087_UNKNOWN_EVENT(event))); } } catch (XMLStreamException e) { final Location location = event == null ? null : event.getLocation(); throw LOGGER.logSevereException(new PolicyException(LocalizationMessages.WSP_0088_FAILED_PARSE(location)), e); } } return map; }
Example 10
Source File: StaxEventXMLReader.java From spring4-understanding with Apache License 2.0 | 4 votes |
private void handleStartDocument(final XMLEvent event) throws SAXException { if (event.isStartDocument()) { StartDocument startDocument = (StartDocument) event; String xmlVersion = startDocument.getVersion(); if (StringUtils.hasLength(xmlVersion)) { this.xmlVersion = xmlVersion; } if (startDocument.encodingSet()) { this.encoding = startDocument.getCharacterEncodingScheme(); } } if (getContentHandler() != null) { final Location location = event.getLocation(); getContentHandler().setDocumentLocator(new Locator2() { @Override public int getColumnNumber() { return (location != null ? location.getColumnNumber() : -1); } @Override public int getLineNumber() { return (location != null ? location.getLineNumber() : -1); } @Override public String getPublicId() { return (location != null ? location.getPublicId() : null); } @Override public String getSystemId() { return (location != null ? location.getSystemId() : null); } @Override public String getXMLVersion() { return xmlVersion; } @Override public String getEncoding() { return encoding; } }); getContentHandler().startDocument(); } }
Example 11
Source File: XMLEventReaderImpl.java From openjdk-jdk8u with GNU General Public License v2.0 | 4 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 { //we have to keep reference to the 'last event' of the stream to be able //to make this check - is there another way ? - nb. if(fLastEvent.getEventType() != XMLEvent.START_ELEMENT){ throw new XMLStreamException( "parser must be on START_ELEMENT to read next text", fLastEvent.getLocation()); } // STag content ETag //[43] content ::= CharData? ((element | Reference | CDSect | PI | Comment) CharData?)* //<foo>....some long text say in KB and underlying parser reports multiple character // but getElementText() events....</foo> String data = null; //having a peeked event makes things really worse -- we have to test the first event if(fPeekedEvent != null){ XMLEvent event = fPeekedEvent ; fPeekedEvent = null; int type = event.getEventType(); if( type == XMLEvent.CHARACTERS || type == XMLEvent.SPACE || type == XMLEvent.CDATA){ data = event.asCharacters().getData(); } else if(type == XMLEvent.ENTITY_REFERENCE){ data = ((EntityReference)event).getDeclaration().getReplacementText(); } else if(type == XMLEvent.COMMENT || type == XMLEvent.PROCESSING_INSTRUCTION){ //ignore } else if(type == XMLEvent.START_ELEMENT) { throw new XMLStreamException( "elementGetText() function expects text only elment but START_ELEMENT was encountered.", event.getLocation()); }else if(type == XMLEvent.END_ELEMENT){ return ""; } //create the string buffer and add initial data StringBuffer buffer = new StringBuffer(); if(data != null && data.length() > 0 ) { buffer.append(data); } //get the next event -- we should stop at END_ELEMENT but it can be any thing //things are worse when implementing this function in XMLEventReader because //there isn't any function called getText() which can get values for //space, cdata, characters and entity reference //nextEvent() would also set the last event. event = nextEvent(); while(event.getEventType() != XMLEvent.END_ELEMENT){ if( type == XMLEvent.CHARACTERS || type == XMLEvent.SPACE || type == XMLEvent.CDATA){ data = event.asCharacters().getData(); } else if(type == XMLEvent.ENTITY_REFERENCE){ data = ((EntityReference)event).getDeclaration().getReplacementText(); } else if(type == XMLEvent.COMMENT || type == XMLEvent.PROCESSING_INSTRUCTION){ //ignore } else if(type == XMLEvent.END_DOCUMENT) { throw new XMLStreamException("unexpected end of document when reading element text content"); } else if(type == XMLEvent.START_ELEMENT) { throw new XMLStreamException( "elementGetText() function expects text only elment but START_ELEMENT was encountered.", event.getLocation()); } else { throw new XMLStreamException( "Unexpected event type "+ type, event.getLocation()); } //add the data to the buffer if(data != null && data.length() > 0 ) { buffer.append(data); } event = nextEvent(); } return buffer.toString(); }//if (fPeekedEvent != null) //if there was no peeked, delegate everything to fXMLReader //update the last event before returning the text data = fXMLReader.getElementText(); fLastEvent = fXMLEventAllocator.allocate(fXMLReader); return data; }
Example 12
Source File: StaxEventXMLReader.java From java-technology-stack with MIT License | 4 votes |
private void handleStartDocument(final XMLEvent event) throws SAXException { if (event.isStartDocument()) { StartDocument startDocument = (StartDocument) event; String xmlVersion = startDocument.getVersion(); if (StringUtils.hasLength(xmlVersion)) { this.xmlVersion = xmlVersion; } if (startDocument.encodingSet()) { this.encoding = startDocument.getCharacterEncodingScheme(); } } if (getContentHandler() != null) { final Location location = event.getLocation(); getContentHandler().setDocumentLocator(new Locator2() { @Override public int getColumnNumber() { return (location != null ? location.getColumnNumber() : -1); } @Override public int getLineNumber() { return (location != null ? location.getLineNumber() : -1); } @Override @Nullable public String getPublicId() { return (location != null ? location.getPublicId() : null); } @Override @Nullable public String getSystemId() { return (location != null ? location.getSystemId() : null); } @Override public String getXMLVersion() { return xmlVersion; } @Override @Nullable public String getEncoding() { return encoding; } }); getContentHandler().startDocument(); } }
Example 13
Source File: StaxEventXMLReader.java From lams with GNU General Public License v2.0 | 4 votes |
private void handleStartDocument(final XMLEvent event) throws SAXException { if (event.isStartDocument()) { StartDocument startDocument = (StartDocument) event; String xmlVersion = startDocument.getVersion(); if (StringUtils.hasLength(xmlVersion)) { this.xmlVersion = xmlVersion; } if (startDocument.encodingSet()) { this.encoding = startDocument.getCharacterEncodingScheme(); } } if (getContentHandler() != null) { final Location location = event.getLocation(); getContentHandler().setDocumentLocator(new Locator2() { @Override public int getColumnNumber() { return (location != null ? location.getColumnNumber() : -1); } @Override public int getLineNumber() { return (location != null ? location.getLineNumber() : -1); } @Override public String getPublicId() { return (location != null ? location.getPublicId() : null); } @Override public String getSystemId() { return (location != null ? location.getSystemId() : null); } @Override public String getXMLVersion() { return xmlVersion; } @Override public String getEncoding() { return encoding; } }); getContentHandler().startDocument(); } }
Example 14
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 15
Source File: XMLEventReaderImpl.java From openjdk-8-source with GNU General Public License v2.0 | 4 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 { //we have to keep reference to the 'last event' of the stream to be able //to make this check - is there another way ? - nb. if(fLastEvent.getEventType() != XMLEvent.START_ELEMENT){ throw new XMLStreamException( "parser must be on START_ELEMENT to read next text", fLastEvent.getLocation()); } // STag content ETag //[43] content ::= CharData? ((element | Reference | CDSect | PI | Comment) CharData?)* //<foo>....some long text say in KB and underlying parser reports multiple character // but getElementText() events....</foo> String data = null; //having a peeked event makes things really worse -- we have to test the first event if(fPeekedEvent != null){ XMLEvent event = fPeekedEvent ; fPeekedEvent = null; int type = event.getEventType(); if( type == XMLEvent.CHARACTERS || type == XMLEvent.SPACE || type == XMLEvent.CDATA){ data = event.asCharacters().getData(); } else if(type == XMLEvent.ENTITY_REFERENCE){ data = ((EntityReference)event).getDeclaration().getReplacementText(); } else if(type == XMLEvent.COMMENT || type == XMLEvent.PROCESSING_INSTRUCTION){ //ignore } else if(type == XMLEvent.START_ELEMENT) { throw new XMLStreamException( "elementGetText() function expects text only elment but START_ELEMENT was encountered.", event.getLocation()); }else if(type == XMLEvent.END_ELEMENT){ return ""; } //create the string buffer and add initial data StringBuffer buffer = new StringBuffer(); if(data != null && data.length() > 0 ) { buffer.append(data); } //get the next event -- we should stop at END_ELEMENT but it can be any thing //things are worse when implementing this function in XMLEventReader because //there isn't any function called getText() which can get values for //space, cdata, characters and entity reference //nextEvent() would also set the last event. event = nextEvent(); while(event.getEventType() != XMLEvent.END_ELEMENT){ if( type == XMLEvent.CHARACTERS || type == XMLEvent.SPACE || type == XMLEvent.CDATA){ data = event.asCharacters().getData(); } else if(type == XMLEvent.ENTITY_REFERENCE){ data = ((EntityReference)event).getDeclaration().getReplacementText(); } else if(type == XMLEvent.COMMENT || type == XMLEvent.PROCESSING_INSTRUCTION){ //ignore } else if(type == XMLEvent.END_DOCUMENT) { throw new XMLStreamException("unexpected end of document when reading element text content"); } else if(type == XMLEvent.START_ELEMENT) { throw new XMLStreamException( "elementGetText() function expects text only elment but START_ELEMENT was encountered.", event.getLocation()); } else { throw new XMLStreamException( "Unexpected event type "+ type, event.getLocation()); } //add the data to the buffer if(data != null && data.length() > 0 ) { buffer.append(data); } event = nextEvent(); } return buffer.toString(); }//if (fPeekedEvent != null) //if there was no peeked, delegate everything to fXMLReader //update the last event before returning the text data = fXMLReader.getElementText(); fLastEvent = fXMLEventAllocator.allocate(fXMLReader); return data; }
Example 16
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 17
Source File: XMLEventReaderImpl.java From openjdk-8 with GNU General Public License v2.0 | 4 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 { //we have to keep reference to the 'last event' of the stream to be able //to make this check - is there another way ? - nb. if(fLastEvent.getEventType() != XMLEvent.START_ELEMENT){ throw new XMLStreamException( "parser must be on START_ELEMENT to read next text", fLastEvent.getLocation()); } // STag content ETag //[43] content ::= CharData? ((element | Reference | CDSect | PI | Comment) CharData?)* //<foo>....some long text say in KB and underlying parser reports multiple character // but getElementText() events....</foo> String data = null; //having a peeked event makes things really worse -- we have to test the first event if(fPeekedEvent != null){ XMLEvent event = fPeekedEvent ; fPeekedEvent = null; int type = event.getEventType(); if( type == XMLEvent.CHARACTERS || type == XMLEvent.SPACE || type == XMLEvent.CDATA){ data = event.asCharacters().getData(); } else if(type == XMLEvent.ENTITY_REFERENCE){ data = ((EntityReference)event).getDeclaration().getReplacementText(); } else if(type == XMLEvent.COMMENT || type == XMLEvent.PROCESSING_INSTRUCTION){ //ignore } else if(type == XMLEvent.START_ELEMENT) { throw new XMLStreamException( "elementGetText() function expects text only elment but START_ELEMENT was encountered.", event.getLocation()); }else if(type == XMLEvent.END_ELEMENT){ return ""; } //create the string buffer and add initial data StringBuffer buffer = new StringBuffer(); if(data != null && data.length() > 0 ) { buffer.append(data); } //get the next event -- we should stop at END_ELEMENT but it can be any thing //things are worse when implementing this function in XMLEventReader because //there isn't any function called getText() which can get values for //space, cdata, characters and entity reference //nextEvent() would also set the last event. event = nextEvent(); while(event.getEventType() != XMLEvent.END_ELEMENT){ if( type == XMLEvent.CHARACTERS || type == XMLEvent.SPACE || type == XMLEvent.CDATA){ data = event.asCharacters().getData(); } else if(type == XMLEvent.ENTITY_REFERENCE){ data = ((EntityReference)event).getDeclaration().getReplacementText(); } else if(type == XMLEvent.COMMENT || type == XMLEvent.PROCESSING_INSTRUCTION){ //ignore } else if(type == XMLEvent.END_DOCUMENT) { throw new XMLStreamException("unexpected end of document when reading element text content"); } else if(type == XMLEvent.START_ELEMENT) { throw new XMLStreamException( "elementGetText() function expects text only elment but START_ELEMENT was encountered.", event.getLocation()); } else { throw new XMLStreamException( "Unexpected event type "+ type, event.getLocation()); } //add the data to the buffer if(data != null && data.length() > 0 ) { buffer.append(data); } event = nextEvent(); } return buffer.toString(); }//if (fPeekedEvent != null) //if there was no peeked, delegate everything to fXMLReader //update the last event before returning the text data = fXMLReader.getElementText(); fLastEvent = fXMLEventAllocator.allocate(fXMLReader); return data; }
Example 18
Source File: XMLEventReaderImpl.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 4 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 { //we have to keep reference to the 'last event' of the stream to be able //to make this check - is there another way ? - nb. if(fLastEvent.getEventType() != XMLEvent.START_ELEMENT){ throw new XMLStreamException( "parser must be on START_ELEMENT to read next text", fLastEvent.getLocation()); } // STag content ETag //[43] content ::= CharData? ((element | Reference | CDSect | PI | Comment) CharData?)* //<foo>....some long text say in KB and underlying parser reports multiple character // but getElementText() events....</foo> String data = null; //having a peeked event makes things really worse -- we have to test the first event if(fPeekedEvent != null){ XMLEvent event = fPeekedEvent ; fPeekedEvent = null; int type = event.getEventType(); if( type == XMLEvent.CHARACTERS || type == XMLEvent.SPACE || type == XMLEvent.CDATA){ data = event.asCharacters().getData(); } else if(type == XMLEvent.ENTITY_REFERENCE){ data = ((EntityReference)event).getDeclaration().getReplacementText(); } else if(type == XMLEvent.COMMENT || type == XMLEvent.PROCESSING_INSTRUCTION){ //ignore } else if(type == XMLEvent.START_ELEMENT) { throw new XMLStreamException( "elementGetText() function expects text only elment but START_ELEMENT was encountered.", event.getLocation()); }else if(type == XMLEvent.END_ELEMENT){ return ""; } //create the string buffer and add initial data StringBuffer buffer = new StringBuffer(); if(data != null && data.length() > 0 ) { buffer.append(data); } //get the next event -- we should stop at END_ELEMENT but it can be any thing //things are worse when implementing this function in XMLEventReader because //there isn't any function called getText() which can get values for //space, cdata, characters and entity reference //nextEvent() would also set the last event. event = nextEvent(); while(event.getEventType() != XMLEvent.END_ELEMENT){ if( type == XMLEvent.CHARACTERS || type == XMLEvent.SPACE || type == XMLEvent.CDATA){ data = event.asCharacters().getData(); } else if(type == XMLEvent.ENTITY_REFERENCE){ data = ((EntityReference)event).getDeclaration().getReplacementText(); } else if(type == XMLEvent.COMMENT || type == XMLEvent.PROCESSING_INSTRUCTION){ //ignore } else if(type == XMLEvent.END_DOCUMENT) { throw new XMLStreamException("unexpected end of document when reading element text content"); } else if(type == XMLEvent.START_ELEMENT) { throw new XMLStreamException( "elementGetText() function expects text only elment but START_ELEMENT was encountered.", event.getLocation()); } else { throw new XMLStreamException( "Unexpected event type "+ type, event.getLocation()); } //add the data to the buffer if(data != null && data.length() > 0 ) { buffer.append(data); } event = nextEvent(); } return buffer.toString(); }//if (fPeekedEvent != null) //if there was no peeked, delegate everything to fXMLReader //update the last event before returning the text data = fXMLReader.getElementText(); fLastEvent = fXMLEventAllocator.allocate(fXMLReader); return data; }
Example 19
Source File: AttrXmlResourceValue.java From bazel with Apache License 2.0 | 4 votes |
private static void endAttrElement(XMLEventReader reader) throws XMLStreamException { XMLEvent endTag = reader.nextTag(); if (!endTag.isEndElement() || !QName.valueOf("attr").equals(endTag.asEndElement().getName())) { throw new XMLStreamException("Unexpected ParentTag:" + endTag, endTag.getLocation()); } }
Example 20
Source File: StreamReader.java From simplexml with Apache License 2.0 | 2 votes |
/** * Constructor for the <code>Start</code> object. This will * wrap the provided node and expose the required details such * as the name, namespace prefix and namespace reference. The * provided element node can be acquired for debugging purposes. * * @param event this is the element being wrapped by this */ public Start(XMLEvent event) { this.element = event.asStartElement(); this.location = event.getLocation(); }