下面列出了javax.xml.stream.events.ProcessingInstruction#javax.xml.stream.events.EntityReference 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void handleEntityReference(EntityReference reference) throws SAXException {
if (getLexicalHandler() != null) {
getLexicalHandler().startEntity(reference.getName());
}
if (getLexicalHandler() != null) {
getLexicalHandler().endEntity(reference.getName());
}
}
private void handleEntityReference(EntityReference reference) throws SAXException {
if (getLexicalHandler() != null) {
getLexicalHandler().startEntity(reference.getName());
}
if (getLexicalHandler() != null) {
getLexicalHandler().endEntity(reference.getName());
}
}
private void handleEntityReference(EntityReference reference) throws SAXException {
if (getLexicalHandler() != null) {
getLexicalHandler().startEntity(reference.getName());
}
if (getLexicalHandler() != null) {
getLexicalHandler().endEntity(reference.getName());
}
}
@Test
public void testReplacing() throws Exception {
JAXPTestUtilities.tryRunWithTmpPermission(() -> {
XMLInputFactory factory = XMLInputFactory.newInstance();
factory.setProperty("javax.xml.stream.isReplacingEntityReferences", true);
StringReader sr = new StringReader(XML);
XMLEventReader reader = factory.createXMLEventReader(sr);
boolean sawUndef = false;
boolean sawDef = false;
while (reader.hasNext()) {
XMLEvent event = reader.nextEvent();
// System.out.println("Event: " + event);
if (event.isEntityReference()) {
EntityReference ref = (EntityReference) event;
if ("def".equals(ref.getName())) {
sawDef = true;
} else if ("undef".equals(ref.getName())) {
sawUndef = true;
} else {
throw new IllegalArgumentException("Unexpected entity name");
}
}
}
Assert.assertEquals(false, sawDef);
Assert.assertEquals(true, sawUndef);
reader.close();
}, new FilePermission("/tmp/this/does/not/exist/but/that/is/ok", "read"));
}
@Test
public void testNotReplacing() throws Exception {
JAXPTestUtilities.tryRunWithTmpPermission(() -> {
XMLInputFactory factory = XMLInputFactory.newInstance();
factory.setProperty("javax.xml.stream.isReplacingEntityReferences", false);
StringReader sr = new StringReader(XML);
XMLEventReader reader = factory.createXMLEventReader(sr);
boolean sawUndef = false;
boolean sawDef = false;
while (reader.hasNext()) {
XMLEvent event = reader.nextEvent();
// System.out.println("Event: " + event);
if (event.isEntityReference()) {
EntityReference ref = (EntityReference) event;
if ("def".equals(ref.getName())) {
sawDef = true;
} else if ("undef".equals(ref.getName())) {
sawUndef = true;
} else {
throw new IllegalArgumentException("Unexpected entity name");
}
}
}
Assert.assertEquals(true, sawDef);
Assert.assertEquals(true, sawUndef);
reader.close();
}, new FilePermission("/tmp/this/does/not/exist/but/that/is/ok", "read"));
}
private void handleEntityReference(EntityReference reference) throws SAXException {
if (getLexicalHandler() != null) {
getLexicalHandler().startEntity(reference.getName());
}
if (getLexicalHandler() != null) {
getLexicalHandler().endEntity(reference.getName());
}
}
@Override
protected void parseInternal() throws SAXException, XMLStreamException {
boolean documentStarted = false;
boolean documentEnded = false;
int elementDepth = 0;
while (this.reader.hasNext() && elementDepth >= 0) {
XMLEvent event = this.reader.nextEvent();
if (!event.isStartDocument() && !event.isEndDocument() && !documentStarted) {
handleStartDocument(event);
documentStarted = true;
}
switch (event.getEventType()) {
case XMLStreamConstants.START_DOCUMENT:
handleStartDocument(event);
documentStarted = true;
break;
case XMLStreamConstants.START_ELEMENT:
elementDepth++;
handleStartElement(event.asStartElement());
break;
case XMLStreamConstants.END_ELEMENT:
elementDepth--;
if (elementDepth >= 0) {
handleEndElement(event.asEndElement());
}
break;
case XMLStreamConstants.PROCESSING_INSTRUCTION:
handleProcessingInstruction((ProcessingInstruction) event);
break;
case XMLStreamConstants.CHARACTERS:
case XMLStreamConstants.SPACE:
case XMLStreamConstants.CDATA:
handleCharacters(event.asCharacters());
break;
case XMLStreamConstants.END_DOCUMENT:
handleEndDocument();
documentEnded = true;
break;
case XMLStreamConstants.NOTATION_DECLARATION:
handleNotationDeclaration((NotationDeclaration) event);
break;
case XMLStreamConstants.ENTITY_DECLARATION:
handleEntityDeclaration((EntityDeclaration) event);
break;
case XMLStreamConstants.COMMENT:
handleComment((Comment) event);
break;
case XMLStreamConstants.DTD:
handleDtd((DTD) event);
break;
case XMLStreamConstants.ENTITY_REFERENCE:
handleEntityReference((EntityReference) event);
break;
}
}
if (documentStarted && !documentEnded) {
handleEndDocument();
}
}
/** 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;
}
@Override
protected void parseInternal() throws SAXException, XMLStreamException {
boolean documentStarted = false;
boolean documentEnded = false;
int elementDepth = 0;
while (this.reader.hasNext() && elementDepth >= 0) {
XMLEvent event = this.reader.nextEvent();
if (!event.isStartDocument() && !event.isEndDocument() && !documentStarted) {
handleStartDocument(event);
documentStarted = true;
}
switch (event.getEventType()) {
case XMLStreamConstants.START_DOCUMENT:
handleStartDocument(event);
documentStarted = true;
break;
case XMLStreamConstants.START_ELEMENT:
elementDepth++;
handleStartElement(event.asStartElement());
break;
case XMLStreamConstants.END_ELEMENT:
elementDepth--;
if (elementDepth >= 0) {
handleEndElement(event.asEndElement());
}
break;
case XMLStreamConstants.PROCESSING_INSTRUCTION:
handleProcessingInstruction((ProcessingInstruction) event);
break;
case XMLStreamConstants.CHARACTERS:
case XMLStreamConstants.SPACE:
case XMLStreamConstants.CDATA:
handleCharacters(event.asCharacters());
break;
case XMLStreamConstants.END_DOCUMENT:
handleEndDocument();
documentEnded = true;
break;
case XMLStreamConstants.NOTATION_DECLARATION:
handleNotationDeclaration((NotationDeclaration) event);
break;
case XMLStreamConstants.ENTITY_DECLARATION:
handleEntityDeclaration((EntityDeclaration) event);
break;
case XMLStreamConstants.COMMENT:
handleComment((Comment) event);
break;
case XMLStreamConstants.DTD:
handleDtd((DTD) event);
break;
case XMLStreamConstants.ENTITY_REFERENCE:
handleEntityReference((EntityReference) event);
break;
}
}
if (documentStarted && !documentEnded) {
handleEndDocument();
}
}
/** 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;
}
/** 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;
}
@Override
protected void parseInternal() throws SAXException, XMLStreamException {
boolean documentStarted = false;
boolean documentEnded = false;
int elementDepth = 0;
while (this.reader.hasNext() && elementDepth >= 0) {
XMLEvent event = this.reader.nextEvent();
if (!event.isStartDocument() && !event.isEndDocument() && !documentStarted) {
handleStartDocument(event);
documentStarted = true;
}
switch (event.getEventType()) {
case XMLStreamConstants.START_DOCUMENT:
handleStartDocument(event);
documentStarted = true;
break;
case XMLStreamConstants.START_ELEMENT:
elementDepth++;
handleStartElement(event.asStartElement());
break;
case XMLStreamConstants.END_ELEMENT:
elementDepth--;
if (elementDepth >= 0) {
handleEndElement(event.asEndElement());
}
break;
case XMLStreamConstants.PROCESSING_INSTRUCTION:
handleProcessingInstruction((ProcessingInstruction) event);
break;
case XMLStreamConstants.CHARACTERS:
case XMLStreamConstants.SPACE:
case XMLStreamConstants.CDATA:
handleCharacters(event.asCharacters());
break;
case XMLStreamConstants.END_DOCUMENT:
handleEndDocument();
documentEnded = true;
break;
case XMLStreamConstants.NOTATION_DECLARATION:
handleNotationDeclaration((NotationDeclaration) event);
break;
case XMLStreamConstants.ENTITY_DECLARATION:
handleEntityDeclaration((EntityDeclaration) event);
break;
case XMLStreamConstants.COMMENT:
handleComment((Comment) event);
break;
case XMLStreamConstants.DTD:
handleDtd((DTD) event);
break;
case XMLStreamConstants.ENTITY_REFERENCE:
handleEntityReference((EntityReference) event);
break;
}
}
if (documentStarted && !documentEnded) {
handleEndDocument();
}
}
@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();
}
@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();
}
/** 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;
}
/** 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 ((type = 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
data = null;
} 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;
}
@Override
public EntityReference createEntityReference(String name, EntityDeclaration entityDeclaration) {
EntityReferenceEvent event = new EntityReferenceEvent(name, entityDeclaration);
if(location != null)event.setLocation(location);
return event;
}
/** 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;
}
@Override
public EntityReference createEntityReference(String name, EntityDeclaration entityDeclaration) {
EntityReferenceEvent event = new EntityReferenceEvent(name, entityDeclaration);
if(location != null)event.setLocation(location);
return event;
}
@Override
public EntityReference createEntityReference(String name, EntityDeclaration declaration) {
return defaultImpl.createEntityReference(name, declaration);
}
@Override
public EntityReference createEntityReference(String name, EntityDeclaration declaration) {
return null;
}
/** 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;
}
/** 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;
}
@Override
protected void parseInternal() throws SAXException, XMLStreamException {
boolean documentStarted = false;
boolean documentEnded = false;
int elementDepth = 0;
while (this.reader.hasNext() && elementDepth >= 0) {
XMLEvent event = this.reader.nextEvent();
if (!event.isStartDocument() && !event.isEndDocument() && !documentStarted) {
handleStartDocument(event);
documentStarted = true;
}
switch (event.getEventType()) {
case XMLStreamConstants.START_DOCUMENT:
handleStartDocument(event);
documentStarted = true;
break;
case XMLStreamConstants.START_ELEMENT:
elementDepth++;
handleStartElement(event.asStartElement());
break;
case XMLStreamConstants.END_ELEMENT:
elementDepth--;
if (elementDepth >= 0) {
handleEndElement(event.asEndElement());
}
break;
case XMLStreamConstants.PROCESSING_INSTRUCTION:
handleProcessingInstruction((ProcessingInstruction) event);
break;
case XMLStreamConstants.CHARACTERS:
case XMLStreamConstants.SPACE:
case XMLStreamConstants.CDATA:
handleCharacters(event.asCharacters());
break;
case XMLStreamConstants.END_DOCUMENT:
handleEndDocument();
documentEnded = true;
break;
case XMLStreamConstants.NOTATION_DECLARATION:
handleNotationDeclaration((NotationDeclaration) event);
break;
case XMLStreamConstants.ENTITY_DECLARATION:
handleEntityDeclaration((EntityDeclaration) event);
break;
case XMLStreamConstants.COMMENT:
handleComment((Comment) event);
break;
case XMLStreamConstants.DTD:
handleDtd((DTD) event);
break;
case XMLStreamConstants.ENTITY_REFERENCE:
handleEntityReference((EntityReference) event);
break;
}
}
if (documentStarted && !documentEnded) {
handleEndDocument();
}
}
/** 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;
}
/**
* Returns the String representation of the given {@link EntityReference}.
*
* @param reference
* The {@link EntityReference} for which the String representation is requested.
* @return The String representation for the given {@link EntityReference}.
*/
String getEntityReferenceValue(EntityReference reference);
/**
* Compares two {@link XMLEvent} instances. This method delegates actual
* matching to the appropriate overloaded method.
*
* @param a
* The first event.
* @param b
* The second event.
* @return <code>true</code> if the events match, <code>false</code>
* otherwise.
*/
public static boolean eventsMatch(XMLEvent a, XMLEvent b) {
if (a == b) {
return true;
} else if (a == null || b == null) {
return false;
} else if (a.getEventType() == b.getEventType()) {
switch (a.getEventType()) {
case XMLEvent.START_ELEMENT:
return eventsMatch(a.asStartElement(), b.asStartElement());
case XMLEvent.END_ELEMENT:
return eventsMatch(a.asEndElement(), b.asEndElement());
case XMLEvent.CDATA:
case XMLEvent.SPACE:
case XMLEvent.CHARACTERS:
return eventsMatch(a.asCharacters(), b.asCharacters());
case XMLEvent.COMMENT:
return eventsMatch((Comment) a, (Comment) b);
case XMLEvent.ENTITY_REFERENCE:
return eventsMatch((EntityReference) a, (EntityReference) b);
case XMLEvent.ATTRIBUTE:
return eventsMatch((Attribute) a, (Attribute) b);
case XMLEvent.NAMESPACE:
return eventsMatch((Namespace) a, (Namespace) b);
case XMLEvent.START_DOCUMENT:
return eventsMatch((StartDocument) a, (StartDocument) b);
case XMLEvent.END_DOCUMENT:
return eventsMatch((EndDocument) a, (EndDocument) b);
case XMLEvent.PROCESSING_INSTRUCTION:
return eventsMatch((ProcessingInstruction) a, (ProcessingInstruction) b);
case XMLEvent.DTD:
return eventsMatch((DTD) a, (DTD) b);
case XMLEvent.ENTITY_DECLARATION:
return eventsMatch((EntityDeclaration) a, (EntityDeclaration) b);
case XMLEvent.NOTATION_DECLARATION:
return eventsMatch((NotationDeclaration) a, (NotationDeclaration) b);
}
}
return false;
}