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

The following examples show how to use javax.xml.stream.events.XMLEvent#PROCESSING_INSTRUCTION . 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 TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/** Get the target of a processing instruction
 * @return the target or null
 */
public String getPITarget() {
    if( fEventType == XMLEvent.PROCESSING_INSTRUCTION){
        return fScanner.getPITarget();
    }
    else throw new java.lang.IllegalStateException("Current state of the parser is " + getEventTypeString(fEventType) +
    " But Expected state is " + XMLEvent.PROCESSING_INSTRUCTION  ) ;

}
 
Example 2
Source File: XMLStreamReaderImpl.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
final static String getEventTypeString(int eventType) {
    switch (eventType){
        case XMLEvent.START_ELEMENT:
            return "START_ELEMENT";
        case XMLEvent.END_ELEMENT:
            return "END_ELEMENT";
        case XMLEvent.PROCESSING_INSTRUCTION:
            return "PROCESSING_INSTRUCTION";
        case XMLEvent.CHARACTERS:
            return "CHARACTERS";
        case XMLEvent.COMMENT:
            return "COMMENT";
        case XMLEvent.START_DOCUMENT:
            return "START_DOCUMENT";
        case XMLEvent.END_DOCUMENT:
            return "END_DOCUMENT";
        case XMLEvent.ENTITY_REFERENCE:
            return "ENTITY_REFERENCE";
        case XMLEvent.ATTRIBUTE:
            return "ATTRIBUTE";
        case XMLEvent.DTD:
            return "DTD";
        case XMLEvent.CDATA:
            return "CDATA";
        case XMLEvent.SPACE:
            return "SPACE";
    }
    return "UNKNOWN_EVENT_TYPE, " + String.valueOf(eventType);
}
 
Example 3
Source File: XMLStreamReaderImpl.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the data section of a processing instruction
 *
 * @return the data or null
 */
public String getPIData() {
    if (fEventType == XMLEvent.PROCESSING_INSTRUCTION) {
        return fScanner.getPIData().toString();
    } else {
        throw new java.lang.IllegalStateException("Current state of the parser is " + getEventTypeString(fEventType)
                + " But Expected state is " + XMLEvent.PROCESSING_INSTRUCTION);
    }
}
 
Example 4
Source File: XMLStreamReaderImpl.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
/**
 * Get the target of a processing instruction
 *
 * @return the target or null
 */
public String getPITarget() {
    if (fEventType == XMLEvent.PROCESSING_INSTRUCTION) {
        return fScanner.getPITarget();
    } else {
        throw new java.lang.IllegalStateException("Current state of the parser is " + getEventTypeString(fEventType)
                + " But Expected state is " + XMLEvent.PROCESSING_INSTRUCTION);
    }

}
 
Example 5
Source File: XMLStreamReaderImpl.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/** Get the data section of a processing instruction
 * @return the data or null
 */

public String getPIData() {
    if( fEventType == XMLEvent.PROCESSING_INSTRUCTION){
        return fScanner.getPIData().toString();
    }
    else throw new java.lang.IllegalStateException("Current state of the parser is " + getEventTypeString(fEventType) +
    " But Expected state is " + XMLEvent.PROCESSING_INSTRUCTION  ) ;
}
 
Example 6
Source File: XMLStreamReaderImpl.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/** Get the target of a processing instruction
 * @return the target or null
 */
public String getPITarget() {
    if( fEventType == XMLEvent.PROCESSING_INSTRUCTION){
        return fScanner.getPITarget();
    }
    else throw new java.lang.IllegalStateException("Current state of the parser is " + getEventTypeString(fEventType) +
    " But Expected state is " + XMLEvent.PROCESSING_INSTRUCTION  ) ;

}
 
Example 7
Source File: XMLStreamReaderImpl.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
final static String getEventTypeString(int eventType) {
    switch (eventType){
        case XMLEvent.START_ELEMENT:
            return "START_ELEMENT";
        case XMLEvent.END_ELEMENT:
            return "END_ELEMENT";
        case XMLEvent.PROCESSING_INSTRUCTION:
            return "PROCESSING_INSTRUCTION";
        case XMLEvent.CHARACTERS:
            return "CHARACTERS";
        case XMLEvent.COMMENT:
            return "COMMENT";
        case XMLEvent.START_DOCUMENT:
            return "START_DOCUMENT";
        case XMLEvent.END_DOCUMENT:
            return "END_DOCUMENT";
        case XMLEvent.ENTITY_REFERENCE:
            return "ENTITY_REFERENCE";
        case XMLEvent.ATTRIBUTE:
            return "ATTRIBUTE";
        case XMLEvent.DTD:
            return "DTD";
        case XMLEvent.CDATA:
            return "CDATA";
        case XMLEvent.SPACE:
            return "SPACE";
    }
    return "UNKNOWN_EVENT_TYPE, " + String.valueOf(eventType);
}
 
Example 8
Source File: XMLStreamReaderImpl.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/** Get the target of a processing instruction
 * @return the target or null
 */
public String getPITarget() {
    if( fEventType == XMLEvent.PROCESSING_INSTRUCTION){
        return fScanner.getPITarget();
    }
    else throw new java.lang.IllegalStateException("Current state of the parser is " + getEventTypeString(fEventType) +
    " But Expected state is " + XMLEvent.PROCESSING_INSTRUCTION  ) ;

}
 
Example 9
Source File: XMLStreamReaderImpl.java    From openjdk-8-source with GNU General Public License v2.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 10
Source File: XMLStreamReaderImpl.java    From openjdk-jdk9 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 11
Source File: XMLStreamReaderImpl.java    From jdk1.8-source-analysis with Apache License 2.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 12
Source File: DummyEvent.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
public boolean isProcessingInstruction() {
    return fEventType == XMLEvent.PROCESSING_INSTRUCTION;
}
 
Example 13
Source File: XMLEventReaderImpl.java    From openjdk-8 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 14
Source File: DummyEvent.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
public boolean isProcessingInstruction() {
    return fEventType == XMLEvent.PROCESSING_INSTRUCTION;
}
 
Example 15
Source File: XMLToImportantChemicals.java    From act with GNU General Public License v3.0 4 votes vote down vote up
private void process(XMLStreamReader xml) throws XMLStreamException {
  String tag;
  String root = null;
  Stack<DBObject> json = new Stack<DBObject>();
  DBObject js;
  while (xml.hasNext()) {
    int eventType = xml.next();
    while (xml.isWhiteSpace() || eventType == XMLEvent.SPACE)
      eventType = xml.next();

    switch (eventType) {
      case XMLEvent.START_ELEMENT:
        tag = xml.getLocalName();
        if (root == null) {
          root = tag;
        } else {
          json.push(new BasicDBObject());
        }
        break;
      case XMLEvent.END_ELEMENT:
        tag = xml.getLocalName();
        if (tag.equals(root)) {
          // will terminate in next iteration
        } else {
          js = json.pop();
          if (json.size() == 0) {
            if (tag.equals(rowTag))
              printEntry(js);
            else
              printUnwantedEntry(js);
          } else {
            putListStrOrJSON(json.peek(), tag, js);
          }
        }
        break;

      case XMLEvent.CHARACTERS:
        String txt = xml.getText();
        js = json.peek();
        if (js.containsField(strTag)) {
          txt = js.get(strTag) + txt;
          js.removeField(strTag);
        }
        js.put(strTag, txt);
        break;

      case XMLEvent.START_DOCUMENT:
        break;
      case XMLEvent.END_DOCUMENT:
        break;
      case XMLEvent.COMMENT:
      case XMLEvent.ENTITY_REFERENCE:
      case XMLEvent.ATTRIBUTE:
      case XMLEvent.PROCESSING_INSTRUCTION:
      case XMLEvent.DTD:
      case XMLEvent.CDATA:
      case XMLEvent.SPACE:
        System.out.format("%s --\n", eventType);
        break;
    }
  }
}
 
Example 16
Source File: DummyEvent.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public boolean isProcessingInstruction() {
    return fEventType == XMLEvent.PROCESSING_INSTRUCTION;
}
 
Example 17
Source File: StAXEventAllocatorBase.java    From jdk8u60 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 18
Source File: StAXEventAllocatorBase.java    From openjdk-jdk9 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 19
Source File: DummyEvent.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
public boolean isProcessingInstruction() {
    return fEventType == XMLEvent.PROCESSING_INSTRUCTION;
}
 
Example 20
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;
}