Java Code Examples for javax.xml.stream.events.XMLEvent#CHARACTERS

The following examples show how to use javax.xml.stream.events.XMLEvent#CHARACTERS . 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: TransientSecurityIndex.java    From powsybl-core with Mozilla Public License 2.0 6 votes vote down vote up
public static TransientSecurityIndex fromXml(String contingencyId, XMLStreamReader xmlsr) throws XMLStreamException {
    String text = null;
    while (xmlsr.hasNext()) {
        int eventType = xmlsr.next();
        switch (eventType) {
            case XMLEvent.CHARACTERS:
                text = xmlsr.getText();
                break;
            case XMLEvent.END_ELEMENT:
                if ("j".equals(xmlsr.getLocalName())) {
                    return new TransientSecurityIndex(contingencyId, Double.parseDouble(text));
                }
                break;
            default:
                break;
        }
    }
    throw new AssertionError("j element not found");
}
 
Example 2
Source File: XMLStreamReaderImpl.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
  * @return
  */
 public int getTextLength() {
     if( fEventType == XMLEvent.CHARACTERS || fEventType == XMLEvent.COMMENT
              || fEventType == XMLEvent.CDATA || fEventType == XMLEvent.SPACE){
          return fScanner.getCharacterData().length;
      } else{
          throw new IllegalStateException("Current state = " + getEventTypeString(fEventType)
          + " is not among the states " + getEventTypeString(XMLEvent.CHARACTERS) + " , "
                  + getEventTypeString(XMLEvent.COMMENT) + " , " + getEventTypeString(XMLEvent.CDATA)
                  + " , " + getEventTypeString(XMLEvent.SPACE) +" valid for getTextLength() " ) ;
      }

}
 
Example 3
Source File: XMLStreamReaderImpl.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @return
 */
public char[] getTextCharacters() {
    if( fEventType == XMLEvent.CHARACTERS || fEventType == XMLEvent.COMMENT
             || fEventType == XMLEvent.CDATA || fEventType == XMLEvent.SPACE){
         return fScanner.getCharacterData().ch;
     } else{
         throw new IllegalStateException("Current state = " + getEventTypeString(fEventType)
         + " is not among the states " + getEventTypeString(XMLEvent.CHARACTERS) + " , "
                 + getEventTypeString(XMLEvent.COMMENT) + " , " + getEventTypeString(XMLEvent.CDATA)
                 + " , " + getEventTypeString(XMLEvent.SPACE) +" valid for getTextCharacters() " ) ;
     }
}
 
Example 4
Source File: TsoOverloadSecurityIndex.java    From powsybl-core with Mozilla Public License 2.0 5 votes vote down vote up
public static TsoOverloadSecurityIndex fromXml(String contingencyId, XMLStreamReader xmlsr) throws XMLStreamException {
    String text = null;
    int overloadCount = -1;
    List<String> overloadedBranches = new ArrayList<>();
    boolean computationSucceed = true;
    while (xmlsr.hasNext()) {
        int eventType = xmlsr.next();
        switch (eventType) {
            case XMLEvent.CHARACTERS:
                text = xmlsr.getText();
                break;
            case XMLEvent.END_ELEMENT:
                switch (xmlsr.getLocalName()) {
                    case "overload-count":
                        overloadCount = Integer.parseInt(text);
                        break;

                    case "overloaded-branch":
                        overloadedBranches.add(text);
                        break;

                    case "computation-succeed":
                        computationSucceed = Boolean.parseBoolean(text);
                        break;

                    case "index":
                        return new TsoOverloadSecurityIndex(contingencyId, overloadCount, overloadedBranches, computationSucceed);

                    default:
                        break;
                }
                break;

            default:
                break;
        }
    }
    throw new AssertionError("index element not found");
}
 
Example 5
Source File: XMLStreamReaderImpl.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @return
 */
public String getValue() {
    if(fEventType == XMLEvent.PROCESSING_INSTRUCTION){
        return fScanner.getPIData().toString();
    } else if(fEventType == XMLEvent.COMMENT){
        return fScanner.getComment();
    } else if(fEventType == XMLEvent.START_ELEMENT || fEventType == XMLEvent.END_ELEMENT){
        return fScanner.getElementQName().localpart ;
    } else if(fEventType == XMLEvent.CHARACTERS){
        return fScanner.getCharacterData().toString();
    }
    return null;
}
 
Example 6
Source File: XMLStreamReaderImpl.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/** Return true if the current event has text, false otherwise
 * The following events have text:
 * CHARACTERS,DTD ,ENTITY_REFERENCE, COMMENT
 */
public boolean hasText() {
    if(DEBUG) pr("XMLReaderImpl#EVENT TYPE = " + fEventType ) ;
    if( fEventType == XMLEvent.CHARACTERS || fEventType == XMLEvent.COMMENT || fEventType == XMLEvent.CDATA) {
        return fScanner.getCharacterData().length > 0 ? true : false;
    } else if(fEventType == XMLEvent.ENTITY_REFERENCE) {
        String name = fScanner.getEntityName();
        if(name != null){
            if(fScanner.foundBuiltInRefs)
                return true;

            XMLEntityStorage entityStore = fEntityManager.getEntityStore();
            Entity en = entityStore.getEntity(name);
            if(en == null)
                return false;
            if(en.isExternal()){
                return ((Entity.ExternalEntity)en).entityLocation.getExpandedSystemId() != null ? true : false;
            } else{
                return ((Entity.InternalEntity)en).text != null ? true : false ;
            }
        }else
            return false;
    } else {
        if(fEventType == XMLEvent.DTD)
            return fScanner.fSeenDoctypeDecl;
    }
    return false;
}
 
Example 7
Source File: XMLStreamReaderImpl.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
/**
 * @return
 */
public boolean hasValue() {
    if(fEventType == XMLEvent.START_ELEMENT || fEventType == XMLEvent.END_ELEMENT
    || fEventType == XMLEvent.ENTITY_REFERENCE || fEventType == XMLEvent.PROCESSING_INSTRUCTION
    || fEventType == XMLEvent.COMMENT || fEventType == XMLEvent.CHARACTERS) {
        return true;
    } else {
        return false;
    }

}
 
Example 8
Source File: XMLStreamReaderImpl.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
/**
 * Return true if the current event has text, false otherwise The following
 * events have text: CHARACTERS,DTD ,ENTITY_REFERENCE, COMMENT
 */
public boolean hasText() {
    if (DEBUG) {
        pr("XMLReaderImpl#EVENT TYPE = " + fEventType);
    }
    if (fEventType == XMLEvent.CHARACTERS || fEventType == XMLEvent.COMMENT
            || fEventType == XMLEvent.CDATA) {
        return fScanner.getCharacterData().length > 0;
    } else if (fEventType == XMLEvent.ENTITY_REFERENCE) {
        String name = fScanner.getEntityName();
        if (name != null) {
            if (fScanner.foundBuiltInRefs) {
                return true;
            }

            XMLEntityStorage entityStore = fEntityManager.getEntityStore();
            Entity en = entityStore.getEntity(name);
            if (en == null) {
                return false;
            }
            if (en.isExternal()) {
                return ((Entity.ExternalEntity) en).entityLocation.getExpandedSystemId() != null;
            } else {
                return ((Entity.InternalEntity) en).text != null;
            }
        } else {
            return false;
        }
    } else if (fEventType == XMLEvent.DTD) {
        return fScanner.fSeenDoctypeDecl;
    }
    return false;
}
 
Example 9
Source File: DummyEvent.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
public boolean isCharacterData() {
    return fEventType == XMLEvent.CHARACTERS;
}
 
Example 10
Source File: StAXEventAllocatorBase.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
XMLEvent getXMLEvent(XMLStreamReader reader){
    XMLEvent event = null;
    //returns the current event
    int eventType = reader.getEventType();
    //this needs to be set before creating events
    factory.setLocation(reader.getLocation());
    switch(eventType){

        case XMLEvent.START_ELEMENT:
        {
            StartElementEvent startElement = (StartElementEvent)factory.createStartElement(reader.getPrefix(),
                                reader.getNamespaceURI(), reader.getLocalName());

            addAttributes(startElement,reader);
            addNamespaces(startElement, reader);
            //need to fix it along with the Reader
            //setNamespaceContext(startElement,reader);
            event = startElement;
            break;
        }
        case XMLEvent.END_ELEMENT:
        {
            EndElementEvent endElement = (EndElementEvent)factory.createEndElement(
                    reader.getPrefix(), reader.getNamespaceURI(), reader.getLocalName());
            addNamespaces(endElement,reader);
            event = endElement ;
            break;
        }
        case XMLEvent.PROCESSING_INSTRUCTION:
        {
            event = factory.createProcessingInstruction(reader.getPITarget(),reader.getPIData());
            break;
        }
        case XMLEvent.CHARACTERS:
        {
            if (reader.isWhiteSpace())
              event = factory.createSpace(reader.getText());
            else
              event = factory.createCharacters(reader.getText());

            break;
        }
        case XMLEvent.COMMENT:
        {
            event = factory.createComment(reader.getText());
            break;
        }
        case XMLEvent.START_DOCUMENT:
        {
            StartDocumentEvent docEvent = (StartDocumentEvent)factory.createStartDocument(
                    reader.getVersion(), reader.getEncoding(), reader.isStandalone());
            if(reader.getCharacterEncodingScheme() != null){
                docEvent.setDeclaredEncoding(true);
            }else{
                docEvent.setDeclaredEncoding(false);
            }
            event = docEvent ;
            break;
        }
        case XMLEvent.END_DOCUMENT:{
            EndDocumentEvent endDocumentEvent = new EndDocumentEvent() ;
            event = endDocumentEvent ;
            break;
        }
        case XMLEvent.ENTITY_REFERENCE:{
            event = factory.createEntityReference(reader.getLocalName(),
                    new EntityDeclarationImpl(reader.getLocalName(),reader.getText()));
            break;

        }
        case XMLEvent.ATTRIBUTE:{
            event = null ;
            break;
        }
        case XMLEvent.DTD:{
            event = factory.createDTD(reader.getText());
            break;
        }
        case XMLEvent.CDATA:{
            event = factory.createCData(reader.getText());
            break;
        }
        case XMLEvent.SPACE:{
            event = factory.createSpace(reader.getText());
            break;
        }
    }
    return event ;
}
 
Example 11
Source File: MultiCriteriaVoltageStabilityIndex.java    From powsybl-core with Mozilla Public License 2.0 4 votes vote down vote up
public static MultiCriteriaVoltageStabilityIndex fromXml(String contingencyId, XMLStreamReader xmlsr) throws XMLStreamException {
    String text = null;
    Map<String, Float> lockedTapChangerLoads = new LinkedHashMap<>();
    Map<String, Float> stoppedTapChangerLoads = new LinkedHashMap<>();
    Map<String, Float> underVoltageAutomatonGenerators = new LinkedHashMap<>();
    Set<String> underVoltageBuses = new LinkedHashSet<>();
    String id = null;
    while (xmlsr.hasNext()) {
        int eventType = xmlsr.next();
        switch (eventType) {
            case XMLEvent.CHARACTERS:
                text = xmlsr.getText();
                break;
            case XMLEvent.START_ELEMENT:
                switch (xmlsr.getLocalName()) {
                    case TAG_LOCKED_TAP_CHANGER_LOAD:
                    case TAG_STOPPED_TAP_CHANGER_LOAD:
                    case TAG_UNDER_VOLTAGE_AUTOMATON_GENERATOR:
                        id = xmlsr.getAttributeValue(null, TAG_ID);
                        break;
                    case TAG_UNDER_BUS_VOLTAGE:
                    case TAG_INDEX:
                        // nothing to do
                        break;
                    default:
                        throw new AssertionError("Unexpected element: " + xmlsr.getLocalName());
                }
                break;

            case XMLEvent.END_ELEMENT:
                switch (xmlsr.getLocalName()) {
                    case TAG_LOCKED_TAP_CHANGER_LOAD:
                        assertIdNonNull(id);
                        lockedTapChangerLoads.put(id, Float.parseFloat(text));
                        id = null;
                        break;
                    case TAG_STOPPED_TAP_CHANGER_LOAD:
                        assertIdNonNull(id);
                        stoppedTapChangerLoads.put(id, Float.parseFloat(text));
                        id = null;
                        break;
                    case TAG_UNDER_VOLTAGE_AUTOMATON_GENERATOR:
                        assertIdNonNull(id);
                        underVoltageAutomatonGenerators.put(id, Float.parseFloat(text));
                        id = null;
                        break;
                    case TAG_UNDER_BUS_VOLTAGE:
                        underVoltageBuses.add(text);
                        break;
                    case TAG_INDEX:
                        return new MultiCriteriaVoltageStabilityIndex(contingencyId, lockedTapChangerLoads, stoppedTapChangerLoads,
                                                                      underVoltageAutomatonGenerators, underVoltageBuses);
                    default:
                        throw new AssertionError("Unexpected element: " + xmlsr.getLocalName());
                }
                break;

            default:
                break;
        }
    }
    throw new AssertionError("Should not happen");
}
 
Example 12
Source File: XMLStreamReaderImpl.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
/** Returns true if the cursor points to a character data event
 * @return true if the cursor points to character data, false otherwise
 */
public boolean isCharacters() {
    return fEventType == XMLEvent.CHARACTERS ;
}
 
Example 13
Source File: XMLStreamReaderImpl.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
/** Returns the current value of the parse event as a string,
 * this returns the string value of a CHARACTERS event,
 * returns the value of a COMMENT, the replacement value
 * for an ENTITY_REFERENCE,
 * or the String value of the DTD
 * @return the current text or null
 * @throws java.lang.IllegalStateException if this state is not
 * a valid text state.
 */
public String getText() {
    if( fEventType == XMLEvent.CHARACTERS || fEventType == XMLEvent.COMMENT
            || fEventType == XMLEvent.CDATA || fEventType == XMLEvent.SPACE){
        //this requires creation of new string
        //fEventType == XMLEvent.ENTITY_REFERENCE
        return fScanner.getCharacterData().toString() ;
    } else if(fEventType == XMLEvent.ENTITY_REFERENCE){
        String name = fScanner.getEntityName();
        if(name != null){
            if(fScanner.foundBuiltInRefs)
                return fScanner.getCharacterData().toString();

            XMLEntityStorage entityStore = fEntityManager.getEntityStore();
            Entity en = entityStore.getEntity(name);
            if(en == null)
                return null;
            if(en.isExternal())
                return ((Entity.ExternalEntity)en).entityLocation.getExpandedSystemId();
            else
                return ((Entity.InternalEntity)en).text;
        }else
            return null;
    }
    else if(fEventType == XMLEvent.DTD){
            if(fDTDDecl != null){
                return fDTDDecl;
            }
            XMLStringBuffer tmpBuffer = fScanner.getDTDDecl();
            fDTDDecl = tmpBuffer.toString();
            return fDTDDecl;
    } else{
            throw new IllegalStateException("Current state " + getEventTypeString(fEventType)
                 + " is not among the states" + getEventTypeString(XMLEvent.CHARACTERS) + ", "
                 + getEventTypeString(XMLEvent.COMMENT) + ", "
                 + getEventTypeString(XMLEvent.CDATA) + ", "
                 + getEventTypeString(XMLEvent.SPACE) + ", "
                 + getEventTypeString(XMLEvent.ENTITY_REFERENCE) + ", "
                 + getEventTypeString(XMLEvent.DTD) + " valid for getText() " ) ;
    }
}
 
Example 14
Source File: XMLEventReaderImpl.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
/** 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 15
Source File: DummyEvent.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
/** A utility function to check if this event is Characters.
 * @see Characters
 */
public boolean isCharacters() {
    return fEventType == XMLEvent.CHARACTERS;
}
 
Example 16
Source File: DummyEvent.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
public boolean isCharacterData() {
    return fEventType == XMLEvent.CHARACTERS;
}
 
Example 17
Source File: XMLEventReaderImpl.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
/** 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: XMLStreamReaderImpl.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
/** Returns true if the cursor points to a character data event
 * @return true if the cursor points to character data, false otherwise
 */
public boolean isCharacters() {
    return fEventType == XMLEvent.CHARACTERS ;
}
 
Example 19
Source File: XMLStreamReaderImpl.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
/** Returns true if the cursor points to a character data event
 * @return true if the cursor points to character data, false otherwise
 */
public boolean isCharacters() {
    return fEventType == XMLEvent.CHARACTERS ;
}
 
Example 20
Source File: XmlUtils.java    From entity-fishing with Apache License 2.0 2 votes vote down vote up
/**
 * 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;

}