Java Code Examples for javax.xml.stream.XMLStreamConstants#SPACE

The following examples show how to use javax.xml.stream.XMLStreamConstants#SPACE . 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: XMLStreamReaderImpl.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
/** Skips any insignificant events (COMMENT and PROCESSING_INSTRUCTION)
 * until a START_ELEMENT or
 * END_ELEMENT is reached. If other than space characters are
 * encountered, an exception is thrown. This method should
 * be used when processing element-only content because
 * the parser is not able to recognize ignorable whitespace if
 * then DTD is missing or not interpreted.
 * @return the event type of the element read
 * @throws XMLStreamException if the current event is not white space
 */
public int nextTag() throws XMLStreamException {

    int eventType = next();
    while((eventType == XMLStreamConstants.CHARACTERS && isWhiteSpace()) // skip whitespace
    || (eventType == XMLStreamConstants.CDATA && isWhiteSpace())
    // skip whitespace
    || eventType == XMLStreamConstants.SPACE
    || eventType == XMLStreamConstants.PROCESSING_INSTRUCTION
    || eventType == XMLStreamConstants.COMMENT
    ) {
        eventType = next();
    }

    if (eventType != XMLStreamConstants.START_ELEMENT && eventType != XMLStreamConstants.END_ELEMENT) {
        throw new XMLStreamException(
                "found: " + getEventTypeString(eventType)
                + ", expected " + getEventTypeString(XMLStreamConstants.START_ELEMENT)
                + " or " + getEventTypeString(XMLStreamConstants.END_ELEMENT),
                getLocation());
    }

    return eventType;
}
 
Example 2
Source File: XMLStreamReaderImpl.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
/**
 * Skips any insignificant events (COMMENT and PROCESSING_INSTRUCTION) until
 * a START_ELEMENT or END_ELEMENT is reached. If other than space characters
 * are encountered, an exception is thrown. This method should be used when
 * processing element-only content because the parser is not able to
 * recognize ignorable whitespace if then DTD is missing or not interpreted.
 *
 * @return the event type of the element read
 * @throws XMLStreamException if the current event is not white space
 */
public int nextTag() throws XMLStreamException {

    int eventType = next();
    while ((eventType == XMLStreamConstants.CHARACTERS && isWhiteSpace()) // skip whitespace
            || (eventType == XMLStreamConstants.CDATA && isWhiteSpace())
            // skip whitespace
            || eventType == XMLStreamConstants.SPACE
            || eventType == XMLStreamConstants.PROCESSING_INSTRUCTION
            || eventType == XMLStreamConstants.COMMENT) {
        eventType = next();
    }

    if (eventType != XMLStreamConstants.START_ELEMENT && eventType != XMLStreamConstants.END_ELEMENT) {
        throw new XMLStreamException(
                "found: " + getEventTypeString(eventType)
                + ", expected " + getEventTypeString(XMLStreamConstants.START_ELEMENT)
                + " or " + getEventTypeString(XMLStreamConstants.END_ELEMENT),
                getLocation());
    }

    return eventType;
}
 
Example 3
Source File: XMLStreamReaderImpl.java    From jdk1.8-source-analysis with Apache License 2.0 6 votes vote down vote up
/** Skips any insignificant events (COMMENT and PROCESSING_INSTRUCTION)
 * until a START_ELEMENT or
 * END_ELEMENT is reached. If other than space characters are
 * encountered, an exception is thrown. This method should
 * be used when processing element-only content because
 * the parser is not able to recognize ignorable whitespace if
 * then DTD is missing or not interpreted.
 * @return the event type of the element read
 * @throws XMLStreamException if the current event is not white space
 */
public int nextTag() throws XMLStreamException {

    int eventType = next();
    while((eventType == XMLStreamConstants.CHARACTERS && isWhiteSpace()) // skip whitespace
    || (eventType == XMLStreamConstants.CDATA && isWhiteSpace())
    // skip whitespace
    || eventType == XMLStreamConstants.SPACE
    || eventType == XMLStreamConstants.PROCESSING_INSTRUCTION
    || eventType == XMLStreamConstants.COMMENT
    ) {
        eventType = next();
    }

    if (eventType != XMLStreamConstants.START_ELEMENT && eventType != XMLStreamConstants.END_ELEMENT) {
        throw new XMLStreamException(
                "found: " + getEventTypeString(eventType)
                + ", expected " + getEventTypeString(XMLStreamConstants.START_ELEMENT)
                + " or " + getEventTypeString(XMLStreamConstants.END_ELEMENT),
                getLocation());
    }

    return eventType;
}
 
Example 4
Source File: XMLStreamReaderImpl.java    From JDKSourceCode1.8 with MIT License 6 votes vote down vote up
/** Skips any insignificant events (COMMENT and PROCESSING_INSTRUCTION)
 * until a START_ELEMENT or
 * END_ELEMENT is reached. If other than space characters are
 * encountered, an exception is thrown. This method should
 * be used when processing element-only content because
 * the parser is not able to recognize ignorable whitespace if
 * then DTD is missing or not interpreted.
 * @return the event type of the element read
 * @throws XMLStreamException if the current event is not white space
 */
public int nextTag() throws XMLStreamException {

    int eventType = next();
    while((eventType == XMLStreamConstants.CHARACTERS && isWhiteSpace()) // skip whitespace
    || (eventType == XMLStreamConstants.CDATA && isWhiteSpace())
    // skip whitespace
    || eventType == XMLStreamConstants.SPACE
    || eventType == XMLStreamConstants.PROCESSING_INSTRUCTION
    || eventType == XMLStreamConstants.COMMENT
    ) {
        eventType = next();
    }

    if (eventType != XMLStreamConstants.START_ELEMENT && eventType != XMLStreamConstants.END_ELEMENT) {
        throw new XMLStreamException(
                "found: " + getEventTypeString(eventType)
                + ", expected " + getEventTypeString(XMLStreamConstants.START_ELEMENT)
                + " or " + getEventTypeString(XMLStreamConstants.END_ELEMENT),
                getLocation());
    }

    return eventType;
}
 
Example 5
Source File: XMLStreamReaderImpl.java    From TencentKona-8 with GNU General Public License v2.0 5 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 {

    if(getEventType() != XMLStreamConstants.START_ELEMENT) {
        throw new XMLStreamException(
        "parser must be on START_ELEMENT to read next text", getLocation());
    }
    int eventType = next();
    StringBuffer content = new StringBuffer();
    while(eventType != XMLStreamConstants.END_ELEMENT ) {
        if(eventType == XMLStreamConstants.CHARACTERS
        || eventType == XMLStreamConstants.CDATA
        || eventType == XMLStreamConstants.SPACE
        || eventType == XMLStreamConstants.ENTITY_REFERENCE) {
            content.append(getText());
        } else if(eventType == XMLStreamConstants.PROCESSING_INSTRUCTION
        || eventType == XMLStreamConstants.COMMENT) {
            // skipping
        } else if(eventType == XMLStreamConstants.END_DOCUMENT) {
            throw new XMLStreamException("unexpected end of document when reading element text content");
        } else if(eventType == XMLStreamConstants.START_ELEMENT) {
            throw new XMLStreamException(
            "elementGetText() function expects text only elment but START_ELEMENT was encountered.", getLocation());
        } else {
            throw new XMLStreamException(
            "Unexpected event type "+ eventType, getLocation());
        }
        eventType = next();
    }
    return content.toString();
}
 
Example 6
Source File: StAXEventConnector.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
public void bridge() throws XMLStreamException {

        try {
            // remembers the nest level of elements to know when we are done.
            int depth=0;

            event = staxEventReader.peek();

            if( !event.isStartDocument() && !event.isStartElement() )
                throw new IllegalStateException();

            // if the parser is on START_DOCUMENT, skip ahead to the first element
            do {
                event = staxEventReader.nextEvent();
            } while( !event.isStartElement() );

            handleStartDocument(event.asStartElement().getNamespaceContext());

            OUTER:
            while(true) {
                // These are all of the events listed in the javadoc for
                // XMLEvent.
                // The spec only really describes 11 of them.
                switch (event.getEventType()) {
                    case XMLStreamConstants.START_ELEMENT :
                        handleStartElement(event.asStartElement());
                        depth++;
                        break;
                    case XMLStreamConstants.END_ELEMENT :
                        depth--;
                        handleEndElement(event.asEndElement());
                        if(depth==0)    break OUTER;
                        break;
                    case XMLStreamConstants.CHARACTERS :
                    case XMLStreamConstants.CDATA :
                    case XMLStreamConstants.SPACE :
                        handleCharacters(event.asCharacters());
                        break;
                }


                event=staxEventReader.nextEvent();
            }

            handleEndDocument();
            event = null; // avoid keeping a stale reference
        } catch (SAXException e) {
            throw new XMLStreamException(e);
        }
    }
 
Example 7
Source File: StAXSchemaParser.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public void parse(XMLStreamReader input) throws XMLStreamException, XNIException {
    if (input.hasNext()) {
        int eventType = input.getEventType();
        if (eventType != XMLStreamConstants.START_DOCUMENT &&
            eventType != XMLStreamConstants.START_ELEMENT) {
            throw new XMLStreamException();
        }
        fLocationWrapper.setLocation(input.getLocation());
        fSchemaDOMParser.startDocument(fLocationWrapper, null, fNamespaceContext, null);
        boolean first = true;
        loop: while (input.hasNext()) {
            if (!first) {
                eventType = input.next();
            }
            else {
                first = false;
            }
            switch (eventType) {
            case XMLStreamConstants.START_ELEMENT:
                ++fDepth;
                fLocationWrapper.setLocation(input.getLocation());
                fNamespaceContext.setNamespaceContext(input.getNamespaceContext());
                fillQName(fElementQName, input.getNamespaceURI(),
                    input.getLocalName(), input.getPrefix());
                fillXMLAttributes(input);
                fillDeclaredPrefixes(input);
                addNamespaceDeclarations();
                fNamespaceContext.pushContext();
                fSchemaDOMParser.startElement(fElementQName, fAttributes, null);
                break;
            case XMLStreamConstants.END_ELEMENT:
                fLocationWrapper.setLocation(input.getLocation());
                fNamespaceContext.setNamespaceContext(input.getNamespaceContext());
                fillQName(fElementQName, input.getNamespaceURI(),
                    input.getLocalName(), input.getPrefix());
                fillDeclaredPrefixes(input);
                fSchemaDOMParser.endElement(fElementQName, null);
                fNamespaceContext.popContext();
                --fDepth;
                if (fDepth <= 0) {
                    break loop;
                }
                break;
            case XMLStreamConstants.CHARACTERS:
                fTempString.setValues(input.getTextCharacters(),
                    input.getTextStart(), input.getTextLength());
                fSchemaDOMParser.characters(fTempString, null);
                break;
            case XMLStreamConstants.SPACE:
                fTempString.setValues(input.getTextCharacters(),
                    input.getTextStart(), input.getTextLength());
                fSchemaDOMParser.ignorableWhitespace(fTempString, null);
                break;
            case XMLStreamConstants.CDATA:
                fSchemaDOMParser.startCDATA(null);
                fTempString.setValues(input.getTextCharacters(),
                    input.getTextStart(), input.getTextLength());
                fSchemaDOMParser.characters(fTempString, null);
                fSchemaDOMParser.endCDATA(null);
                break;
            case XMLStreamConstants.PROCESSING_INSTRUCTION:
                fillProcessingInstruction(input.getPIData());
                fSchemaDOMParser.processingInstruction(input.getPITarget(), fTempString, null);
                break;
            case XMLStreamConstants.DTD:
                /* There shouldn't be a DTD in the schema */
                break;
            case XMLStreamConstants.ENTITY_REFERENCE:
                /* Not needed for schemas */
                break;
            case XMLStreamConstants.COMMENT:
                /* No point in sending comments */
                break;
            case XMLStreamConstants.START_DOCUMENT:
                ++fDepth;
                /* We automatically call startDocument before the loop */
                break;
            case XMLStreamConstants.END_DOCUMENT:
                /* We automatically call endDocument after the loop */
                break;
            }
        }
        fLocationWrapper.setLocation(null);
        fNamespaceContext.setNamespaceContext(null);
        fSchemaDOMParser.endDocument(null);
    }
}
 
Example 8
Source File: StAXStreamConnector.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
public void bridge() throws XMLStreamException {

        try {
            // remembers the nest level of elements to know when we are done.
            int depth=0;

            // if the parser is at the start tag, proceed to the first element
            int event = staxStreamReader.getEventType();
            if(event == XMLStreamConstants.START_DOCUMENT) {
                // nextTag doesn't correctly handle DTDs
                while( !staxStreamReader.isStartElement() )
                    event = staxStreamReader.next();
            }


            if( event!=XMLStreamConstants.START_ELEMENT)
                throw new IllegalStateException("The current event is not START_ELEMENT\n but " + event);

            handleStartDocument(staxStreamReader.getNamespaceContext());

            OUTER:
            while(true) {
                // These are all of the events listed in the javadoc for
                // XMLEvent.
                // The spec only really describes 11 of them.
                switch (event) {
                    case XMLStreamConstants.START_ELEMENT :
                        handleStartElement();
                        depth++;
                        break;
                    case XMLStreamConstants.END_ELEMENT :
                        depth--;
                        handleEndElement();
                        if(depth==0)    break OUTER;
                        break;
                    case XMLStreamConstants.CHARACTERS :
                    case XMLStreamConstants.CDATA :
                    case XMLStreamConstants.SPACE :
                        handleCharacters();
                        break;
                    // otherwise simply ignore
                }

                event=staxStreamReader.next();
            }

            staxStreamReader.next();    // move beyond the end tag.

            handleEndDocument();
        } catch (SAXException e) {
            throw new XMLStreamException(e);
        }
    }
 
Example 9
Source File: StAXSchemaParser.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
public void parse(XMLStreamReader input) throws XMLStreamException, XNIException {
    if (input.hasNext()) {
        int eventType = input.getEventType();
        if (eventType != XMLStreamConstants.START_DOCUMENT &&
            eventType != XMLStreamConstants.START_ELEMENT) {
            throw new XMLStreamException();
        }
        fLocationWrapper.setLocation(input.getLocation());
        fSchemaDOMParser.startDocument(fLocationWrapper, null, fNamespaceContext, null);
        boolean first = true;
        loop: while (input.hasNext()) {
            if (!first) {
                eventType = input.next();
            }
            else {
                first = false;
            }
            switch (eventType) {
            case XMLStreamConstants.START_ELEMENT:
                ++fDepth;
                fLocationWrapper.setLocation(input.getLocation());
                fNamespaceContext.setNamespaceContext(input.getNamespaceContext());
                fillQName(fElementQName, input.getNamespaceURI(),
                    input.getLocalName(), input.getPrefix());
                fillXMLAttributes(input);
                fillDeclaredPrefixes(input);
                addNamespaceDeclarations();
                fNamespaceContext.pushContext();
                fSchemaDOMParser.startElement(fElementQName, fAttributes, null);
                break;
            case XMLStreamConstants.END_ELEMENT:
                fLocationWrapper.setLocation(input.getLocation());
                fNamespaceContext.setNamespaceContext(input.getNamespaceContext());
                fillQName(fElementQName, input.getNamespaceURI(),
                    input.getLocalName(), input.getPrefix());
                fillDeclaredPrefixes(input);
                fSchemaDOMParser.endElement(fElementQName, null);
                fNamespaceContext.popContext();
                --fDepth;
                if (fDepth <= 0) {
                    break loop;
                }
                break;
            case XMLStreamConstants.CHARACTERS:
                fTempString.setValues(input.getTextCharacters(),
                    input.getTextStart(), input.getTextLength());
                fSchemaDOMParser.characters(fTempString, null);
                break;
            case XMLStreamConstants.SPACE:
                fTempString.setValues(input.getTextCharacters(),
                    input.getTextStart(), input.getTextLength());
                fSchemaDOMParser.ignorableWhitespace(fTempString, null);
                break;
            case XMLStreamConstants.CDATA:
                fSchemaDOMParser.startCDATA(null);
                fTempString.setValues(input.getTextCharacters(),
                    input.getTextStart(), input.getTextLength());
                fSchemaDOMParser.characters(fTempString, null);
                fSchemaDOMParser.endCDATA(null);
                break;
            case XMLStreamConstants.PROCESSING_INSTRUCTION:
                fillProcessingInstruction(input.getPIData());
                fSchemaDOMParser.processingInstruction(input.getPITarget(), fTempString, null);
                break;
            case XMLStreamConstants.DTD:
                /* There shouldn't be a DTD in the schema */
                break;
            case XMLStreamConstants.ENTITY_REFERENCE:
                /* Not needed for schemas */
                break;
            case XMLStreamConstants.COMMENT:
                /* No point in sending comments */
                break;
            case XMLStreamConstants.START_DOCUMENT:
                ++fDepth;
                /* We automatically call startDocument before the loop */
                break;
            case XMLStreamConstants.END_DOCUMENT:
                /* We automatically call endDocument after the loop */
                break;
            }
        }
        fLocationWrapper.setLocation(null);
        fNamespaceContext.setNamespaceContext(null);
        fSchemaDOMParser.endDocument(null);
    }
}
 
Example 10
Source File: StAXStreamConnector.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public void bridge() throws XMLStreamException {

        try {
            // remembers the nest level of elements to know when we are done.
            int depth=0;

            // if the parser is at the start tag, proceed to the first element
            int event = staxStreamReader.getEventType();
            if(event == XMLStreamConstants.START_DOCUMENT) {
                // nextTag doesn't correctly handle DTDs
                while( !staxStreamReader.isStartElement() )
                    event = staxStreamReader.next();
            }


            if( event!=XMLStreamConstants.START_ELEMENT)
                throw new IllegalStateException("The current event is not START_ELEMENT\n but " + event);

            handleStartDocument(staxStreamReader.getNamespaceContext());

            OUTER:
            while(true) {
                // These are all of the events listed in the javadoc for
                // XMLEvent.
                // The spec only really describes 11 of them.
                switch (event) {
                    case XMLStreamConstants.START_ELEMENT :
                        handleStartElement();
                        depth++;
                        break;
                    case XMLStreamConstants.END_ELEMENT :
                        depth--;
                        handleEndElement();
                        if(depth==0)    break OUTER;
                        break;
                    case XMLStreamConstants.CHARACTERS :
                    case XMLStreamConstants.CDATA :
                    case XMLStreamConstants.SPACE :
                        handleCharacters();
                        break;
                    // otherwise simply ignore
                }

                event=staxStreamReader.next();
            }

            staxStreamReader.next();    // move beyond the end tag.

            handleEndDocument();
        } catch (SAXException e) {
            throw new XMLStreamException(e);
        }
    }
 
Example 11
Source File: XMLStreamReaderToContentHandler.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
public void bridge() throws XMLStreamException {

        try {
            // remembers the nest level of elements to know when we are done.
            int depth=0;

            // if the parser is at the start tag, proceed to the first element
            int event = staxStreamReader.getEventType();
            if(event == XMLStreamConstants.START_DOCUMENT) {
                // nextTag doesn't correctly handle DTDs
                while( !staxStreamReader.isStartElement() )
                    event = staxStreamReader.next();
            }


            if( event!=XMLStreamConstants.START_ELEMENT)
                throw new IllegalStateException("The current event is not START_ELEMENT\n but " + event);

            handleStartDocument();

            for(int i=0; i < inscopeNamespaces.length; i+=2) {
                saxHandler.startPrefixMapping(inscopeNamespaces[i], inscopeNamespaces[i+1]);
            }

            OUTER:
            do {
                // These are all of the events listed in the javadoc for
                // XMLEvent.
                // The spec only really describes 11 of them.
                switch (event) {
                    case XMLStreamConstants.START_ELEMENT :
                        depth++;
                        handleStartElement();
                        break;
                    case XMLStreamConstants.END_ELEMENT :
                        handleEndElement();
                        depth--;
                        if(depth==0 && eagerQuit)
                            break OUTER;
                        break;
                    case XMLStreamConstants.CHARACTERS :
                        handleCharacters();
                        break;
                    case XMLStreamConstants.ENTITY_REFERENCE :
                        handleEntityReference();
                        break;
                    case XMLStreamConstants.PROCESSING_INSTRUCTION :
                        handlePI();
                        break;
                    case XMLStreamConstants.COMMENT :
                        handleComment();
                        break;
                    case XMLStreamConstants.DTD :
                        handleDTD();
                        break;
                    case XMLStreamConstants.ATTRIBUTE :
                        handleAttribute();
                        break;
                    case XMLStreamConstants.NAMESPACE :
                        handleNamespace();
                        break;
                    case XMLStreamConstants.CDATA :
                        handleCDATA();
                        break;
                    case XMLStreamConstants.ENTITY_DECLARATION :
                        handleEntityDecl();
                        break;
                    case XMLStreamConstants.NOTATION_DECLARATION :
                        handleNotationDecl();
                        break;
                    case XMLStreamConstants.SPACE :
                        handleSpace();
                        break;
                    default :
                        throw new InternalError("processing event: " + event);
                }

                event=staxStreamReader.next();
            } while (depth!=0);

            for(int i=0; i < inscopeNamespaces.length; i+=2) {
                saxHandler.endPrefixMapping(inscopeNamespaces[i]);
            }

            handleEndDocument();
        } catch (SAXException e) {
            throw new XMLStreamException2(e);
        }
    }
 
Example 12
Source File: BaseXMLEventReader.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@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 13
Source File: StAXSchemaParser.java    From JDKSourceCode1.8 with MIT License 4 votes vote down vote up
public void parse(XMLEventReader input) throws XMLStreamException, XNIException {
    XMLEvent currentEvent = input.peek();
    if (currentEvent != null) {
        int eventType = currentEvent.getEventType();
        if (eventType != XMLStreamConstants.START_DOCUMENT &&
            eventType != XMLStreamConstants.START_ELEMENT) {
            throw new XMLStreamException();
        }
        fLocationWrapper.setLocation(currentEvent.getLocation());
        fSchemaDOMParser.startDocument(fLocationWrapper, null, fNamespaceContext, null);
        loop: while (input.hasNext()) {
            currentEvent = input.nextEvent();
            eventType = currentEvent.getEventType();
            switch (eventType) {
            case XMLStreamConstants.START_ELEMENT:
                ++fDepth;
                StartElement start = currentEvent.asStartElement();
                fillQName(fElementQName, start.getName());
                fLocationWrapper.setLocation(start.getLocation());
                fNamespaceContext.setNamespaceContext(start.getNamespaceContext());
                fillXMLAttributes(start);
                fillDeclaredPrefixes(start);
                addNamespaceDeclarations();
                fNamespaceContext.pushContext();
                fSchemaDOMParser.startElement(fElementQName, fAttributes, null);
                break;
            case XMLStreamConstants.END_ELEMENT:
                EndElement end = currentEvent.asEndElement();
                fillQName(fElementQName, end.getName());
                fillDeclaredPrefixes(end);
                fLocationWrapper.setLocation(end.getLocation());
                fSchemaDOMParser.endElement(fElementQName, null);
                fNamespaceContext.popContext();
                --fDepth;
                if (fDepth <= 0) {
                    break loop;
                }
                break;
            case XMLStreamConstants.CHARACTERS:
                sendCharactersToSchemaParser(currentEvent.asCharacters().getData(), false);
                break;
            case XMLStreamConstants.SPACE:
                sendCharactersToSchemaParser(currentEvent.asCharacters().getData(), true);
                break;
            case XMLStreamConstants.CDATA:
                fSchemaDOMParser.startCDATA(null);
                sendCharactersToSchemaParser(currentEvent.asCharacters().getData(), false);
                fSchemaDOMParser.endCDATA(null);
                break;
            case XMLStreamConstants.PROCESSING_INSTRUCTION:
                ProcessingInstruction pi = (ProcessingInstruction)currentEvent;
                fillProcessingInstruction(pi.getData());
                fSchemaDOMParser.processingInstruction(pi.getTarget(), fTempString, null);
                break;
            case XMLStreamConstants.DTD:
                /* There shouldn't be a DTD in the schema */
                break;
            case XMLStreamConstants.ENTITY_REFERENCE:
                /* Not needed for schemas */
                break;
            case XMLStreamConstants.COMMENT:
                /* No point in sending comments */
                break;
            case XMLStreamConstants.START_DOCUMENT:
                fDepth++;
                /* We automatically call startDocument before the loop */
                break;
            case XMLStreamConstants.END_DOCUMENT:
                /* We automatically call endDocument after the loop */
                break;
            }
        }
        fLocationWrapper.setLocation(null);
        fNamespaceContext.setNamespaceContext(null);
        fSchemaDOMParser.endDocument(null);
    }
}
 
Example 14
Source File: StAXSchemaParser.java    From jdk1.8-source-analysis with Apache License 2.0 4 votes vote down vote up
public void parse(XMLStreamReader input) throws XMLStreamException, XNIException {
    if (input.hasNext()) {
        int eventType = input.getEventType();
        if (eventType != XMLStreamConstants.START_DOCUMENT &&
            eventType != XMLStreamConstants.START_ELEMENT) {
            throw new XMLStreamException();
        }
        fLocationWrapper.setLocation(input.getLocation());
        fSchemaDOMParser.startDocument(fLocationWrapper, null, fNamespaceContext, null);
        boolean first = true;
        loop: while (input.hasNext()) {
            if (!first) {
                eventType = input.next();
            }
            else {
                first = false;
            }
            switch (eventType) {
            case XMLStreamConstants.START_ELEMENT:
                ++fDepth;
                fLocationWrapper.setLocation(input.getLocation());
                fNamespaceContext.setNamespaceContext(input.getNamespaceContext());
                fillQName(fElementQName, input.getNamespaceURI(),
                    input.getLocalName(), input.getPrefix());
                fillXMLAttributes(input);
                fillDeclaredPrefixes(input);
                addNamespaceDeclarations();
                fNamespaceContext.pushContext();
                fSchemaDOMParser.startElement(fElementQName, fAttributes, null);
                break;
            case XMLStreamConstants.END_ELEMENT:
                fLocationWrapper.setLocation(input.getLocation());
                fNamespaceContext.setNamespaceContext(input.getNamespaceContext());
                fillQName(fElementQName, input.getNamespaceURI(),
                    input.getLocalName(), input.getPrefix());
                fillDeclaredPrefixes(input);
                fSchemaDOMParser.endElement(fElementQName, null);
                fNamespaceContext.popContext();
                --fDepth;
                if (fDepth <= 0) {
                    break loop;
                }
                break;
            case XMLStreamConstants.CHARACTERS:
                fTempString.setValues(input.getTextCharacters(),
                    input.getTextStart(), input.getTextLength());
                fSchemaDOMParser.characters(fTempString, null);
                break;
            case XMLStreamConstants.SPACE:
                fTempString.setValues(input.getTextCharacters(),
                    input.getTextStart(), input.getTextLength());
                fSchemaDOMParser.ignorableWhitespace(fTempString, null);
                break;
            case XMLStreamConstants.CDATA:
                fSchemaDOMParser.startCDATA(null);
                fTempString.setValues(input.getTextCharacters(),
                    input.getTextStart(), input.getTextLength());
                fSchemaDOMParser.characters(fTempString, null);
                fSchemaDOMParser.endCDATA(null);
                break;
            case XMLStreamConstants.PROCESSING_INSTRUCTION:
                fillProcessingInstruction(input.getPIData());
                fSchemaDOMParser.processingInstruction(input.getPITarget(), fTempString, null);
                break;
            case XMLStreamConstants.DTD:
                /* There shouldn't be a DTD in the schema */
                break;
            case XMLStreamConstants.ENTITY_REFERENCE:
                /* Not needed for schemas */
                break;
            case XMLStreamConstants.COMMENT:
                /* No point in sending comments */
                break;
            case XMLStreamConstants.START_DOCUMENT:
                ++fDepth;
                /* We automatically call startDocument before the loop */
                break;
            case XMLStreamConstants.END_DOCUMENT:
                /* We automatically call endDocument after the loop */
                break;
            }
        }
        fLocationWrapper.setLocation(null);
        fNamespaceContext.setNamespaceContext(null);
        fSchemaDOMParser.endDocument(null);
    }
}
 
Example 15
Source File: ParsingUtils.java    From galleon with Apache License 2.0 4 votes vote down vote up
public static XMLStreamException unexpectedContent(final XMLStreamReader reader) {
    final String kind;
    switch (reader.getEventType()) {
        case XMLStreamConstants.ATTRIBUTE:
            kind = "attribute";
            break;
        case XMLStreamConstants.CDATA:
            kind = "cdata";
            break;
        case XMLStreamConstants.CHARACTERS:
            kind = "characters";
            break;
        case XMLStreamConstants.COMMENT:
            kind = "comment";
            break;
        case XMLStreamConstants.DTD:
            kind = "dtd";
            break;
        case XMLStreamConstants.END_DOCUMENT:
            kind = "document end";
            break;
        case XMLStreamConstants.END_ELEMENT:
            kind = "element end";
            break;
        case XMLStreamConstants.ENTITY_DECLARATION:
            kind = "entity declaration";
            break;
        case XMLStreamConstants.ENTITY_REFERENCE:
            kind = "entity ref";
            break;
        case XMLStreamConstants.NAMESPACE:
            kind = "namespace";
            break;
        case XMLStreamConstants.NOTATION_DECLARATION:
            kind = "notation declaration";
            break;
        case XMLStreamConstants.PROCESSING_INSTRUCTION:
            kind = "processing instruction";
            break;
        case XMLStreamConstants.SPACE:
            kind = "whitespace";
            break;
        case XMLStreamConstants.START_DOCUMENT:
            kind = "document start";
            break;
        case XMLStreamConstants.START_ELEMENT:
            kind = "element start";
            break;
        default:
            kind = "unknown";
            break;
    }

    return new XMLStreamException("unexpected content: " + kind + (reader.hasName() ? reader.getName() : null) +
            (reader.hasText() ? reader.getText() : null), reader.getLocation());
}
 
Example 16
Source File: StAXSchemaParser.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
public void parse(XMLEventReader input) throws XMLStreamException, XNIException {
    XMLEvent currentEvent = input.peek();
    if (currentEvent != null) {
        int eventType = currentEvent.getEventType();
        if (eventType != XMLStreamConstants.START_DOCUMENT &&
            eventType != XMLStreamConstants.START_ELEMENT) {
            throw new XMLStreamException();
        }
        fLocationWrapper.setLocation(currentEvent.getLocation());
        fSchemaDOMParser.startDocument(fLocationWrapper, null, fNamespaceContext, null);
        loop: while (input.hasNext()) {
            currentEvent = input.nextEvent();
            eventType = currentEvent.getEventType();
            switch (eventType) {
            case XMLStreamConstants.START_ELEMENT:
                ++fDepth;
                StartElement start = currentEvent.asStartElement();
                fillQName(fElementQName, start.getName());
                fLocationWrapper.setLocation(start.getLocation());
                fNamespaceContext.setNamespaceContext(start.getNamespaceContext());
                fillXMLAttributes(start);
                fillDeclaredPrefixes(start);
                addNamespaceDeclarations();
                fNamespaceContext.pushContext();
                fSchemaDOMParser.startElement(fElementQName, fAttributes, null);
                break;
            case XMLStreamConstants.END_ELEMENT:
                EndElement end = currentEvent.asEndElement();
                fillQName(fElementQName, end.getName());
                fillDeclaredPrefixes(end);
                fLocationWrapper.setLocation(end.getLocation());
                fSchemaDOMParser.endElement(fElementQName, null);
                fNamespaceContext.popContext();
                --fDepth;
                if (fDepth <= 0) {
                    break loop;
                }
                break;
            case XMLStreamConstants.CHARACTERS:
                sendCharactersToSchemaParser(currentEvent.asCharacters().getData(), false);
                break;
            case XMLStreamConstants.SPACE:
                sendCharactersToSchemaParser(currentEvent.asCharacters().getData(), true);
                break;
            case XMLStreamConstants.CDATA:
                fSchemaDOMParser.startCDATA(null);
                sendCharactersToSchemaParser(currentEvent.asCharacters().getData(), false);
                fSchemaDOMParser.endCDATA(null);
                break;
            case XMLStreamConstants.PROCESSING_INSTRUCTION:
                ProcessingInstruction pi = (ProcessingInstruction)currentEvent;
                fillProcessingInstruction(pi.getData());
                fSchemaDOMParser.processingInstruction(pi.getTarget(), fTempString, null);
                break;
            case XMLStreamConstants.DTD:
                /* There shouldn't be a DTD in the schema */
                break;
            case XMLStreamConstants.ENTITY_REFERENCE:
                /* Not needed for schemas */
                break;
            case XMLStreamConstants.COMMENT:
                /* No point in sending comments */
                break;
            case XMLStreamConstants.START_DOCUMENT:
                fDepth++;
                /* We automatically call startDocument before the loop */
                break;
            case XMLStreamConstants.END_DOCUMENT:
                /* We automatically call endDocument after the loop */
                break;
            }
        }
        fLocationWrapper.setLocation(null);
        fNamespaceContext.setNamespaceContext(null);
        fSchemaDOMParser.endDocument(null);
    }
}
 
Example 17
Source File: FastInfosetConnector.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public void bridge() throws XMLStreamException {
    try {
        // remembers the nest level of elements to know when we are done.
        int depth=0;

        // if the parser is at the start tag, proceed to the first element
        int event = fastInfosetStreamReader.getEventType();
        if(event == XMLStreamConstants.START_DOCUMENT) {
            // nextTag doesn't correctly handle DTDs
            while( !fastInfosetStreamReader.isStartElement() )
                event = fastInfosetStreamReader.next();
        }


        if( event!=XMLStreamConstants.START_ELEMENT)
            throw new IllegalStateException("The current event is not START_ELEMENT\n but " + event);

        // TODO: we don't have to rely on this hack --- we can just emulate
        // start/end prefix mappings. But for now, I'll rely on this hack.
        handleStartDocument(fastInfosetStreamReader.getNamespaceContext());

        OUTER:
        while(true) {
            // These are all of the events listed in the javadoc for
            // XMLEvent.
            // The spec only really describes 11 of them.
            switch (event) {
                case XMLStreamConstants.START_ELEMENT :
                    handleStartElement();
                    depth++;
                    break;
                case XMLStreamConstants.END_ELEMENT :
                    depth--;
                    handleEndElement();
                    if(depth==0)    break OUTER;
                    break;
                case XMLStreamConstants.CHARACTERS :
                case XMLStreamConstants.CDATA :
                case XMLStreamConstants.SPACE :
                    if (predictor.expectText()) {
                        // Peek at the next event to see if there are
                        // fragmented characters
                        event = fastInfosetStreamReader.peekNext();
                        if (event == XMLStreamConstants.END_ELEMENT)
                            processNonIgnorableText();
                        else if (event == XMLStreamConstants.START_ELEMENT)
                            processIgnorableText();
                        else
                            handleFragmentedCharacters();
                    }
                    break;
                // otherwise simply ignore
            }

            event=fastInfosetStreamReader.next();
        }

        fastInfosetStreamReader.next();    // move beyond the end tag.

        handleEndDocument();
    } catch (SAXException e) {
        throw new XMLStreamException(e);
    }
}
 
Example 18
Source File: StaxEventXMLReader.java    From java-technology-stack with MIT License 4 votes vote down vote up
@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();
	}

}
 
Example 19
Source File: StAXSchemaParser.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
public void parse(XMLStreamReader input) throws XMLStreamException, XNIException {
    if (input.hasNext()) {
        int eventType = input.getEventType();
        if (eventType != XMLStreamConstants.START_DOCUMENT &&
            eventType != XMLStreamConstants.START_ELEMENT) {
            throw new XMLStreamException();
        }
        fLocationWrapper.setLocation(input.getLocation());
        fSchemaDOMParser.startDocument(fLocationWrapper, null, fNamespaceContext, null);
        boolean first = true;
        loop: while (input.hasNext()) {
            if (!first) {
                eventType = input.next();
            }
            else {
                first = false;
            }
            switch (eventType) {
            case XMLStreamConstants.START_ELEMENT:
                ++fDepth;
                fLocationWrapper.setLocation(input.getLocation());
                fNamespaceContext.setNamespaceContext(input.getNamespaceContext());
                fillQName(fElementQName, input.getNamespaceURI(),
                    input.getLocalName(), input.getPrefix());
                fillXMLAttributes(input);
                fillDeclaredPrefixes(input);
                addNamespaceDeclarations();
                fNamespaceContext.pushContext();
                fSchemaDOMParser.startElement(fElementQName, fAttributes, null);
                break;
            case XMLStreamConstants.END_ELEMENT:
                fLocationWrapper.setLocation(input.getLocation());
                fNamespaceContext.setNamespaceContext(input.getNamespaceContext());
                fillQName(fElementQName, input.getNamespaceURI(),
                    input.getLocalName(), input.getPrefix());
                fillDeclaredPrefixes(input);
                fSchemaDOMParser.endElement(fElementQName, null);
                fNamespaceContext.popContext();
                --fDepth;
                if (fDepth <= 0) {
                    break loop;
                }
                break;
            case XMLStreamConstants.CHARACTERS:
                fTempString.setValues(input.getTextCharacters(),
                    input.getTextStart(), input.getTextLength());
                fSchemaDOMParser.characters(fTempString, null);
                break;
            case XMLStreamConstants.SPACE:
                fTempString.setValues(input.getTextCharacters(),
                    input.getTextStart(), input.getTextLength());
                fSchemaDOMParser.ignorableWhitespace(fTempString, null);
                break;
            case XMLStreamConstants.CDATA:
                fSchemaDOMParser.startCDATA(null);
                fTempString.setValues(input.getTextCharacters(),
                    input.getTextStart(), input.getTextLength());
                fSchemaDOMParser.characters(fTempString, null);
                fSchemaDOMParser.endCDATA(null);
                break;
            case XMLStreamConstants.PROCESSING_INSTRUCTION:
                fillProcessingInstruction(input.getPIData());
                fSchemaDOMParser.processingInstruction(input.getPITarget(), fTempString, null);
                break;
            case XMLStreamConstants.DTD:
                /* There shouldn't be a DTD in the schema */
                break;
            case XMLStreamConstants.ENTITY_REFERENCE:
                /* Not needed for schemas */
                break;
            case XMLStreamConstants.COMMENT:
                /* No point in sending comments */
                break;
            case XMLStreamConstants.START_DOCUMENT:
                ++fDepth;
                /* We automatically call startDocument before the loop */
                break;
            case XMLStreamConstants.END_DOCUMENT:
                /* We automatically call endDocument after the loop */
                break;
            }
        }
        fLocationWrapper.setLocation(null);
        fNamespaceContext.setNamespaceContext(null);
        fSchemaDOMParser.endDocument(null);
    }
}
 
Example 20
Source File: StAXStream2SAX.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
public void bridge() throws XMLStreamException {

        try {
            // remembers the nest level of elements to know when we are done.
            int depth=0;

            // skip over START_DOCUMENT
            int event = staxStreamReader.getEventType();
            if (event == XMLStreamConstants.START_DOCUMENT) {
                event = staxStreamReader.next();
            }

            // If not a START_ELEMENT (e.g., a DTD), skip to next tag
            if (event != XMLStreamConstants.START_ELEMENT) {
                event = staxStreamReader.nextTag();
                // An error if a START_ELEMENT isn't found now
                if (event != XMLStreamConstants.START_ELEMENT) {
                    throw new IllegalStateException("The current event is " +
                            "not START_ELEMENT\n but" + event);
                }
            }

            handleStartDocument();

            do {
                // These are all of the events listed in the javadoc for
                // XMLEvent.
                // The spec only really describes 11 of them.
                switch (event) {
                    case XMLStreamConstants.START_ELEMENT :
                        depth++;
                        handleStartElement();
                        break;
                    case XMLStreamConstants.END_ELEMENT :
                        handleEndElement();
                        depth--;
                        break;
                    case XMLStreamConstants.CHARACTERS :
                        handleCharacters();
                        break;
                    case XMLStreamConstants.ENTITY_REFERENCE :
                        handleEntityReference();
                        break;
                    case XMLStreamConstants.PROCESSING_INSTRUCTION :
                        handlePI();
                        break;
                    case XMLStreamConstants.COMMENT :
                        handleComment();
                        break;
                    case XMLStreamConstants.DTD :
                        handleDTD();
                        break;
                    case XMLStreamConstants.ATTRIBUTE :
                        handleAttribute();
                        break;
                    case XMLStreamConstants.NAMESPACE :
                        handleNamespace();
                        break;
                    case XMLStreamConstants.CDATA :
                        handleCDATA();
                        break;
                    case XMLStreamConstants.ENTITY_DECLARATION :
                        handleEntityDecl();
                        break;
                    case XMLStreamConstants.NOTATION_DECLARATION :
                        handleNotationDecl();
                        break;
                    case XMLStreamConstants.SPACE :
                        handleSpace();
                        break;
                    default :
                        throw new InternalError("processing event: " + event);
                }

                event=staxStreamReader.next();
            } while (depth!=0);

            handleEndDocument();
        } catch (SAXException e) {
            throw new XMLStreamException(e);
        }
    }