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

The following examples show how to use javax.xml.stream.XMLStreamConstants#END_DOCUMENT . 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: XMLStreamReaderUtil.java    From activiti6-boot2 with Apache License 2.0 6 votes vote down vote up
public static boolean moveToEndOfElement(XMLStreamReader xtr, String elementName) {
  try {
    while (xtr.hasNext()) {
      int event = xtr.next();
      switch (event) {
      case XMLStreamConstants.END_DOCUMENT:
        return false;
      case XMLStreamConstants.END_ELEMENT:
        if (xtr.getLocalName().equals(elementName))
          return true;
        break;
      }
    }
  } catch (Exception e) {
    LOGGER.warn("Error while moving to end of element {}", elementName, e);
  }
  return false;
}
 
Example 2
Source File: XMLStreamReaderUtil.java    From activiti6-boot2 with Apache License 2.0 6 votes vote down vote up
public static String moveDown(XMLStreamReader xtr) {
  try {
    while (xtr.hasNext()) {
      int event = xtr.next();
      switch (event) {
      case XMLStreamConstants.END_DOCUMENT:
        return null;
      case XMLStreamConstants.START_ELEMENT:
        return xtr.getLocalName();
      case XMLStreamConstants.END_ELEMENT:
        return null;
      }
    }
  } catch (Exception e) {
    LOGGER.warn("Error while moving down in XML document", e);
  }
  return null;
}
 
Example 3
Source File: StaxReader.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
protected int pullNextEvent() {
    try {
        switch(in.next()) {
            case XMLStreamConstants.START_DOCUMENT:
            case XMLStreamConstants.START_ELEMENT:
                return START_NODE;
            case XMLStreamConstants.END_DOCUMENT:
            case XMLStreamConstants.END_ELEMENT:
                return END_NODE;
            case XMLStreamConstants.CHARACTERS:
                return TEXT;
            case XMLStreamConstants.COMMENT:
                return COMMENT;
            default:
                return OTHER;
        }
    } catch (XMLStreamException e) {
        throw new StreamException(e);
    }
}
 
Example 4
Source File: XMLStreamReaderUtil.java    From activiti6-boot2 with Apache License 2.0 6 votes vote down vote up
public static String moveDown(XMLStreamReader xtr) {
  try {
    while (xtr.hasNext()) {
      int event = xtr.next();
      switch (event) {
      case XMLStreamConstants.END_DOCUMENT:
        return null;
      case XMLStreamConstants.START_ELEMENT:
        return xtr.getLocalName();
      case XMLStreamConstants.END_ELEMENT:
        return null;
      }
    }
  } catch (Exception e) {
    LOGGER.warn("Error while moving down in XML document", e);
  }
  return null;
}
 
Example 5
Source File: XMLStreamReaderUtil.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public static void readRest(XMLStreamReader reader) {
    try {
        while(reader.getEventType() != XMLStreamConstants.END_DOCUMENT) {
            reader.next();
        }
    } catch (XMLStreamException e) {
        throw wrapException(e);
    }
}
 
Example 6
Source File: XMLStreamReaderUtil.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public static void readRest(XMLStreamReader reader) {
    try {
        while(reader.getEventType() != XMLStreamConstants.END_DOCUMENT) {
            reader.next();
        }
    } catch (XMLStreamException e) {
        throw wrapException(e);
    }
}
 
Example 7
Source File: AbstractXMLStreamReader.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public String getElementText() throws XMLStreamException {
	if (getEventType() != XMLStreamConstants.START_ELEMENT) {
		throw new XMLStreamException("Parser must be on START_ELEMENT to read next text", getLocation());
	}
	int eventType = next();
	StringBuilder builder = new StringBuilder();
	while (eventType != XMLStreamConstants.END_ELEMENT) {
		if (eventType == XMLStreamConstants.CHARACTERS || eventType == XMLStreamConstants.CDATA ||
				eventType == XMLStreamConstants.SPACE || eventType == XMLStreamConstants.ENTITY_REFERENCE) {
			builder.append(getText());
		}
		else if (eventType == XMLStreamConstants.PROCESSING_INSTRUCTION ||
				eventType == XMLStreamConstants.COMMENT) {
			// skipping
		}
		else if (eventType == XMLStreamConstants.END_DOCUMENT) {
			throw new XMLStreamException("Unexpected end of document when reading element text content",
					getLocation());
		}
		else if (eventType == XMLStreamConstants.START_ELEMENT) {
			throw new XMLStreamException("Element text content may not contain START_ELEMENT", getLocation());
		}
		else {
			throw new XMLStreamException("Unexpected event type " + eventType, getLocation());
		}
		eventType = next();
	}
	return builder.toString();
}
 
Example 8
Source File: XMLStreamReaderImpl.java    From openjdk-jdk9 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();
    StringBuilder content = new StringBuilder();
    while (eventType != XMLStreamConstants.END_ELEMENT) {
        if (eventType == XMLStreamConstants.CHARACTERS
                || eventType == XMLStreamConstants.CDATA
                || eventType == XMLStreamConstants.SPACE
                || eventType == XMLStreamConstants.ENTITY_REFERENCE) {
            content.append(getText());
        } else if (eventType == XMLStreamConstants.PROCESSING_INSTRUCTION
                || eventType == XMLStreamConstants.COMMENT) {
            // skipping
        } else if (eventType == XMLStreamConstants.END_DOCUMENT) {
            throw new XMLStreamException(
                    "unexpected end of document when reading element text content");
        } else if (eventType == XMLStreamConstants.START_ELEMENT) {
            throw new XMLStreamException("elementGetText() function expects text "
                    + "only elment but START_ELEMENT was encountered.", getLocation());
        } else {
            throw new XMLStreamException(
                    "Unexpected event type " + eventType, getLocation());
        }
        eventType = next();
    }
    return content.toString();
}
 
Example 9
Source File: ExternalAttachmentsUnmarshaller.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private Map<URI, Policy> unmarshal(final XMLEventReader reader, final StartElement parentElement) throws PolicyException {
    XMLEvent event = null;
    while (reader.hasNext()) {
        try {
            event = reader.peek();
            switch (event.getEventType()) {
                case XMLStreamConstants.START_DOCUMENT:
                case XMLStreamConstants.COMMENT:
                    reader.nextEvent();
                    break;

                case XMLStreamConstants.CHARACTERS:
                    processCharacters(event.asCharacters(), parentElement, map);
                    reader.nextEvent();
                    break;

                case XMLStreamConstants.END_ELEMENT:
                    processEndTag(event.asEndElement(), parentElement);
                    reader.nextEvent();
                    return map;

                case XMLStreamConstants.START_ELEMENT:
                    final StartElement element = event.asStartElement();
                    processStartTag(element, parentElement, reader, map);
                    break;

                case XMLStreamConstants.END_DOCUMENT:
                    return map;

                default:
                    throw LOGGER.logSevereException(new PolicyException(LocalizationMessages.WSP_0087_UNKNOWN_EVENT(event)));
            }
        } catch (XMLStreamException e) {
            final Location location = event == null ? null : event.getLocation();
            throw LOGGER.logSevereException(new PolicyException(LocalizationMessages.WSP_0088_FAILED_PARSE(location)), e);
        }
    }
    return map;
}
 
Example 10
Source File: Util.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
public final static String getEventTypeString(int eventType) {
    switch (eventType){
        case XMLStreamConstants.START_ELEMENT:
            return "START_ELEMENT";
        case XMLStreamConstants.END_ELEMENT:
            return "END_ELEMENT";
        case XMLStreamConstants.PROCESSING_INSTRUCTION:
            return "PROCESSING_INSTRUCTION";
        case XMLStreamConstants.CHARACTERS:
            return "CHARACTERS";
        case XMLStreamConstants.COMMENT:
            return "COMMENT";
        case XMLStreamConstants.START_DOCUMENT:
            return "START_DOCUMENT";
        case XMLStreamConstants.END_DOCUMENT:
            return "END_DOCUMENT";
        case XMLStreamConstants.ENTITY_REFERENCE:
            return "ENTITY_REFERENCE";
        case XMLStreamConstants.ATTRIBUTE:
            return "ATTRIBUTE";
        case XMLStreamConstants.DTD:
            return "DTD";
        case XMLStreamConstants.CDATA:
            return "CDATA";
    }
    return "UNKNOWN_EVENT_TYPE";
}
 
Example 11
Source File: ExternalAttachmentsUnmarshaller.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private Map<URI, Policy> unmarshal(final XMLEventReader reader, final StartElement parentElement) throws PolicyException {
    XMLEvent event = null;
    while (reader.hasNext()) {
        try {
            event = reader.peek();
            switch (event.getEventType()) {
                case XMLStreamConstants.START_DOCUMENT:
                case XMLStreamConstants.COMMENT:
                    reader.nextEvent();
                    break;

                case XMLStreamConstants.CHARACTERS:
                    processCharacters(event.asCharacters(), parentElement, map);
                    reader.nextEvent();
                    break;

                case XMLStreamConstants.END_ELEMENT:
                    processEndTag(event.asEndElement(), parentElement);
                    reader.nextEvent();
                    return map;

                case XMLStreamConstants.START_ELEMENT:
                    final StartElement element = event.asStartElement();
                    processStartTag(element, parentElement, reader, map);
                    break;

                case XMLStreamConstants.END_DOCUMENT:
                    return map;

                default:
                    throw LOGGER.logSevereException(new PolicyException(LocalizationMessages.WSP_0087_UNKNOWN_EVENT(event)));
            }
        } catch (XMLStreamException e) {
            final Location location = event == null ? null : event.getLocation();
            throw LOGGER.logSevereException(new PolicyException(LocalizationMessages.WSP_0088_FAILED_PARSE(location)), e);
        }
    }
    return map;
}
 
Example 12
Source File: XMLStreamReaderUtil.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public static void readRest(XMLStreamReader reader) {
    try {
        while(reader.getEventType() != XMLStreamConstants.END_DOCUMENT) {
            reader.next();
        }
    } catch (XMLStreamException e) {
        throw wrapException(e);
    }
}
 
Example 13
Source File: MysqlSQLXML.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private Reader binaryInputStreamStreamToReader(ByteArrayOutputStream out) {

        try {
            // There's got to be an easier way to do this, but I don't feel like coding up Appendix F of the XML Spec myself, when there's a reusable way to do
            // it, and we can warn folks away from BINARY xml streams that have to be parsed to determine the character encoding :P

            String encoding = "UTF-8";

            try {
                ByteArrayInputStream bIn = new ByteArrayInputStream(out.toByteArray());
                XMLStreamReader reader = this.inputFactory.createXMLStreamReader(bIn);

                int eventType = 0;

                while ((eventType = reader.next()) != XMLStreamConstants.END_DOCUMENT) {
                    if (eventType == XMLStreamConstants.START_DOCUMENT) {
                        String possibleEncoding = reader.getEncoding();

                        if (possibleEncoding != null) {
                            encoding = possibleEncoding;
                        }

                        break;
                    }
                }
            } catch (Throwable t) {
                // ignore, dealt with later when the string can't be parsed into valid XML
            }

            return new StringReader(new String(out.toByteArray(), encoding));
        } catch (UnsupportedEncodingException badEnc) {
            throw new RuntimeException(badEnc);
        }
    }
 
Example 14
Source File: XMLDocumentFragmentScannerImpl.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Scans a document.
 *
 * @param complete True if the scanner should scan the document
 *                 completely, pushing all events to the registered
 *                 document handler. A value of false indicates that
 *                 that the scanner should only scan the next portion
 *                 of the document and return. A scanner instance is
 *                 permitted to completely scan a document if it does
 *                 not support this "pull" scanning model.
 *
 * @return True if there is more to scan, false otherwise.
 */
public boolean scanDocument(boolean complete)
throws IOException, XNIException {

    // keep dispatching "events"
    fEntityManager.setEntityHandler(this);
    //System.out.println(" get Document Handler in NSDocumentHandler " + fDocumentHandler );

    int event = next();
    do {
        switch (event) {
            case XMLStreamConstants.START_DOCUMENT :
                //fDocumentHandler.startDocument(fEntityManager.getEntityScanner(),fEntityManager.getEntityScanner().getVersion(),fNamespaceContext,null);// not able to get
                break;
            case XMLStreamConstants.START_ELEMENT :
                //System.out.println(" in scann element");
                //fDocumentHandler.startElement(getElementQName(),fAttributes,null);
                break;
            case XMLStreamConstants.CHARACTERS :
                fEntityScanner.checkNodeCount(fEntityScanner.fCurrentEntity);
                fDocumentHandler.characters(getCharacterData(),null);
                break;
            case XMLStreamConstants.SPACE:
                //check if getCharacterData() is the right function to retrieve ignorableWhitespace information.
                //System.out.println("in the space");
                //fDocumentHandler.ignorableWhitespace(getCharacterData(), null);
                break;
            case XMLStreamConstants.ENTITY_REFERENCE :
                fEntityScanner.checkNodeCount(fEntityScanner.fCurrentEntity);
                //entity reference callback are given in startEntity
                break;
            case XMLStreamConstants.PROCESSING_INSTRUCTION :
                fEntityScanner.checkNodeCount(fEntityScanner.fCurrentEntity);
                fDocumentHandler.processingInstruction(getPITarget(),getPIData(),null);
                break;
            case XMLStreamConstants.COMMENT :
                fEntityScanner.checkNodeCount(fEntityScanner.fCurrentEntity);
                fDocumentHandler.comment(getCharacterData(),null);
                break;
            case XMLStreamConstants.DTD :
                //all DTD related callbacks are handled in DTDScanner.
                //1. Stax doesn't define DTD states as it does for XML Document.
                //therefore we don't need to take care of anything here. So Just break;
                break;
            case XMLStreamConstants.CDATA:
               fEntityScanner.checkNodeCount(fEntityScanner.fCurrentEntity);
                if (fCDataStart) {
                    fDocumentHandler.startCDATA(null);
                    fCDataStart = false;
                    fInCData = true;
                }

                fDocumentHandler.characters(getCharacterData(),null);
                if (fCDataEnd) {
                    fDocumentHandler.endCDATA(null);
                    fCDataEnd = false;
                }
                break;
            case XMLStreamConstants.NOTATION_DECLARATION :
                break;
            case XMLStreamConstants.ENTITY_DECLARATION :
                break;
            case XMLStreamConstants.NAMESPACE :
                break;
            case XMLStreamConstants.ATTRIBUTE :
                break;
            case XMLStreamConstants.END_ELEMENT :
                //do not give callback here.
                //this callback is given in scanEndElement function.
                //fDocumentHandler.endElement(getElementQName(),null);
                break;
            default :
                // Errors should have already been handled by the Scanner
                return false;

        }
        //System.out.println("here in before calling next");
        event = next();
        //System.out.println("here in after calling next");
    } while (event!=XMLStreamConstants.END_DOCUMENT && complete);

    if(event == XMLStreamConstants.END_DOCUMENT) {
        fDocumentHandler.endDocument(null);
        return false;
    }

    return true;

}
 
Example 15
Source File: XMLDocumentFragmentScannerImpl.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Scans a document.
 *
 * @param complete True if the scanner should scan the document
 *                 completely, pushing all events to the registered
 *                 document handler. A value of false indicates that
 *                 that the scanner should only scan the next portion
 *                 of the document and return. A scanner instance is
 *                 permitted to completely scan a document if it does
 *                 not support this "pull" scanning model.
 *
 * @return True if there is more to scan, false otherwise.
 */
public boolean scanDocument(boolean complete)
throws IOException, XNIException {

    // keep dispatching "events"
    fEntityManager.setEntityHandler(this);
    //System.out.println(" get Document Handler in NSDocumentHandler " + fDocumentHandler );

    int event = next();
    do {
        switch (event) {
            case XMLStreamConstants.START_DOCUMENT :
                //fDocumentHandler.startDocument(fEntityManager.getEntityScanner(),fEntityManager.getEntityScanner().getVersion(),fNamespaceContext,null);// not able to get
                break;
            case XMLStreamConstants.START_ELEMENT :
                //System.out.println(" in scann element");
                //fDocumentHandler.startElement(getElementQName(),fAttributes,null);
                break;
            case XMLStreamConstants.CHARACTERS :
                fEntityScanner.checkNodeCount(fEntityScanner.fCurrentEntity);
                fDocumentHandler.characters(getCharacterData(),null);
                break;
            case XMLStreamConstants.SPACE:
                //check if getCharacterData() is the right function to retrieve ignorableWhitespace information.
                //System.out.println("in the space");
                //fDocumentHandler.ignorableWhitespace(getCharacterData(), null);
                break;
            case XMLStreamConstants.ENTITY_REFERENCE :
                fEntityScanner.checkNodeCount(fEntityScanner.fCurrentEntity);
                //entity reference callback are given in startEntity
                break;
            case XMLStreamConstants.PROCESSING_INSTRUCTION :
                fEntityScanner.checkNodeCount(fEntityScanner.fCurrentEntity);
                fDocumentHandler.processingInstruction(getPITarget(),getPIData(),null);
                break;
            case XMLStreamConstants.COMMENT :
                fEntityScanner.checkNodeCount(fEntityScanner.fCurrentEntity);
                fDocumentHandler.comment(getCharacterData(),null);
                break;
            case XMLStreamConstants.DTD :
                //all DTD related callbacks are handled in DTDScanner.
                //1. Stax doesn't define DTD states as it does for XML Document.
                //therefore we don't need to take care of anything here. So Just break;
                break;
            case XMLStreamConstants.CDATA:
                fEntityScanner.checkNodeCount(fEntityScanner.fCurrentEntity);
                fDocumentHandler.startCDATA(null);
                //xxx: check if CDATA values comes from getCharacterData() function
                fDocumentHandler.characters(getCharacterData(),null);
                fDocumentHandler.endCDATA(null);
                //System.out.println(" in CDATA of the XMLNSDocumentScannerImpl");
                break;
            case XMLStreamConstants.NOTATION_DECLARATION :
                break;
            case XMLStreamConstants.ENTITY_DECLARATION :
                break;
            case XMLStreamConstants.NAMESPACE :
                break;
            case XMLStreamConstants.ATTRIBUTE :
                break;
            case XMLStreamConstants.END_ELEMENT :
                //do not give callback here.
                //this callback is given in scanEndElement function.
                //fDocumentHandler.endElement(getElementQName(),null);
                break;
            default :
                throw new InternalError("processing event: " + event);

        }
        //System.out.println("here in before calling next");
        event = next();
        //System.out.println("here in after calling next");
    } while (event!=XMLStreamConstants.END_DOCUMENT && complete);

    if(event == XMLStreamConstants.END_DOCUMENT) {
        fDocumentHandler.endDocument(null);
        return false;
    }

    return true;

}
 
Example 16
Source File: XMLDocumentFragmentScannerImpl.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
/**
 * Scans a document.
 *
 * @param complete True if the scanner should scan the document
 *                 completely, pushing all events to the registered
 *                 document handler. A value of false indicates that
 *                 that the scanner should only scan the next portion
 *                 of the document and return. A scanner instance is
 *                 permitted to completely scan a document if it does
 *                 not support this "pull" scanning model.
 *
 * @return True if there is more to scan, false otherwise.
 */
public boolean scanDocument(boolean complete)
throws IOException, XNIException {

    // keep dispatching "events"
    fEntityManager.setEntityHandler(this);
    //System.out.println(" get Document Handler in NSDocumentHandler " + fDocumentHandler );

    int event = next();
    do {
        switch (event) {
            case XMLStreamConstants.START_DOCUMENT :
                //fDocumentHandler.startDocument(fEntityManager.getEntityScanner(),fEntityManager.getEntityScanner().getVersion(),fNamespaceContext,null);// not able to get
                break;
            case XMLStreamConstants.START_ELEMENT :
                //System.out.println(" in scann element");
                //fDocumentHandler.startElement(getElementQName(),fAttributes,null);
                break;
            case XMLStreamConstants.CHARACTERS :
                fEntityScanner.checkNodeCount(fEntityScanner.fCurrentEntity);
                fDocumentHandler.characters(getCharacterData(),null);
                break;
            case XMLStreamConstants.SPACE:
                //check if getCharacterData() is the right function to retrieve ignorableWhitespace information.
                //System.out.println("in the space");
                //fDocumentHandler.ignorableWhitespace(getCharacterData(), null);
                break;
            case XMLStreamConstants.ENTITY_REFERENCE :
                fEntityScanner.checkNodeCount(fEntityScanner.fCurrentEntity);
                //entity reference callback are given in startEntity
                break;
            case XMLStreamConstants.PROCESSING_INSTRUCTION :
                fEntityScanner.checkNodeCount(fEntityScanner.fCurrentEntity);
                fDocumentHandler.processingInstruction(getPITarget(),getPIData(),null);
                break;
            case XMLStreamConstants.COMMENT :
                fEntityScanner.checkNodeCount(fEntityScanner.fCurrentEntity);
                fDocumentHandler.comment(getCharacterData(),null);
                break;
            case XMLStreamConstants.DTD :
                //all DTD related callbacks are handled in DTDScanner.
                //1. Stax doesn't define DTD states as it does for XML Document.
                //therefore we don't need to take care of anything here. So Just break;
                break;
            case XMLStreamConstants.CDATA:
               fEntityScanner.checkNodeCount(fEntityScanner.fCurrentEntity);
                if (fCDataStart) {
                    fDocumentHandler.startCDATA(null);
                    fCDataStart = false;
                    fInCData = true;
                }

                fDocumentHandler.characters(getCharacterData(),null);
                if (fCDataEnd) {
                    fDocumentHandler.endCDATA(null);
                    fCDataEnd = false;
                }
                break;
            case XMLStreamConstants.NOTATION_DECLARATION :
                break;
            case XMLStreamConstants.ENTITY_DECLARATION :
                break;
            case XMLStreamConstants.NAMESPACE :
                break;
            case XMLStreamConstants.ATTRIBUTE :
                break;
            case XMLStreamConstants.END_ELEMENT :
                //do not give callback here.
                //this callback is given in scanEndElement function.
                //fDocumentHandler.endElement(getElementQName(),null);
                break;
            default :
                // Errors should have already been handled by the Scanner
                return false;

        }
        //System.out.println("here in before calling next");
        event = next();
        //System.out.println("here in after calling next");
    } while (event!=XMLStreamConstants.END_DOCUMENT && complete);

    if(event == XMLStreamConstants.END_DOCUMENT) {
        fDocumentHandler.endDocument(null);
        return false;
    }

    return true;

}
 
Example 17
Source File: StAXSchemaParser.java    From openjdk-jdk8u-backup with GNU General Public License v2.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 18
Source File: IgnoreExternalDTDTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
private void parse(XMLInputFactory xif) throws XMLStreamException {
    XMLStreamReader xsr = xif.createXMLStreamReader(new StringReader("<?xml version='1.0'?><!DOCTYPE root PUBLIC 'abc' 'def'><abc />"));
    while (xsr.next() != XMLStreamConstants.END_DOCUMENT)
        ;
}
 
Example 19
Source File: StAX2SAXReader.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
public void adapt() throws XMLStreamException, SAXException {
    QName qname;
    String prefix, localPart;
    AttributesImpl attrs = new AttributesImpl();
    char[] buffer;
    int nsc;
    int nat;

    _handler.startDocument();

    try {

        while (_reader.hasNext()) {
            int event = _reader.next();


            switch(event) {
            case  XMLStreamConstants.START_ELEMENT: {
                // Report namespace events first
                nsc = _reader.getNamespaceCount();
                for (int i = 0; i < nsc; i++) {
                    _handler.startPrefixMapping(_reader.getNamespacePrefix(i),
                        _reader.getNamespaceURI(i));
                }

                // Collect list of attributes
                attrs.clear();
                nat = _reader.getAttributeCount();
                for (int i = 0; i < nat; i++) {
                    QName q = _reader.getAttributeName(i);
                    String qName = _reader.getAttributePrefix(i);
                    if (qName == null || qName == "") {
                        qName = q.getLocalPart();
                    } else {
                        qName = qName + ":" +  q.getLocalPart();
                    }
                    attrs.addAttribute(_reader.getAttributeNamespace(i),
                                       q.getLocalPart(),
                                       qName,
                                       _reader.getAttributeType(i),
                                       _reader.getAttributeValue(i));
                }

                // Report start element
                qname = _reader.getName();
                prefix = qname.getPrefix();
                localPart = qname.getLocalPart();

                _handler.startElement(_reader.getNamespaceURI(),
                                      localPart,
                                      (prefix.length() > 0) ?
                                          (prefix + ":" + localPart) : localPart,
                                      attrs);
                break;
            }
            case  XMLStreamConstants.END_ELEMENT: {
                // Report end element
                qname = _reader.getName();
                prefix = qname.getPrefix();
                localPart = qname.getLocalPart();

                _handler.endElement(_reader.getNamespaceURI(),
                                    localPart,
                                    (prefix.length() > 0) ?
                                        (prefix + ":" + localPart) : localPart);

                // Report end namespace events
                nsc = _reader.getNamespaceCount();
                for (int i = 0; i < nsc; i++) {
                    _handler.endPrefixMapping(_reader.getNamespacePrefix(i));
                }
                break;
            }
            case XMLStreamConstants.CHARACTERS:
                _handler.characters(_reader.getTextCharacters(), _reader.getTextStart(), _reader.getTextLength());
                break;
            case XMLStreamConstants.COMMENT:
                _lexicalHandler.comment(_reader.getTextCharacters(), _reader.getTextStart(), _reader.getTextLength());
                break;
            case XMLStreamConstants.PROCESSING_INSTRUCTION:
                _handler.processingInstruction(_reader.getPITarget(), _reader.getPIData());
                break;
            case XMLStreamConstants.END_DOCUMENT:
                break;
            default:
                throw new RuntimeException(CommonResourceBundle.getInstance().getString("message.StAX2SAXReader", new Object[]{Integer.valueOf(event)}));
            } // switch
        }
    }
    catch (XMLStreamException e) {
        _handler.endDocument();     // flush whatever we have
        throw e;
    }

    _handler.endDocument();
}
 
Example 20
Source File: StaxEventXMLReader.java    From spring-analysis-note 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();
	}

}