Java Code Examples for javax.xml.stream.XMLStreamReader#isWhiteSpace()

The following examples show how to use javax.xml.stream.XMLStreamReader#isWhiteSpace() . 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: JBossDeploymentStructureParser12.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
private static void parseEndDocument(final XMLStreamReader reader) throws XMLStreamException {
    while (reader.hasNext()) {
        switch (reader.next()) {
            case XMLStreamConstants.END_DOCUMENT: {
                return;
            }
            case XMLStreamConstants.CHARACTERS: {
                if (!reader.isWhiteSpace()) {
                    throw unexpectedContent(reader);
                }
                // ignore
                break;
            }
            case XMLStreamConstants.COMMENT:
            case XMLStreamConstants.SPACE: {
                // ignore
                break;
            }
            default: {
                throw unexpectedContent(reader);
            }
        }
    }
    return;
}
 
Example 2
Source File: JBossDeploymentStructureParser10.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
private static void parseEndDocument(final XMLStreamReader reader) throws XMLStreamException {
    while (reader.hasNext()) {
        switch (reader.next()) {
            case XMLStreamConstants.END_DOCUMENT: {
                return;
            }
            case XMLStreamConstants.CHARACTERS: {
                if (!reader.isWhiteSpace()) {
                    throw unexpectedContent(reader);
                }
                // ignore
                break;
            }
            case XMLStreamConstants.COMMENT:
            case XMLStreamConstants.SPACE: {
                // ignore
                break;
            }
            default: {
                throw unexpectedContent(reader);
            }
        }
    }
    return;
}
 
Example 3
Source File: XMLStreamReaderUtil.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Read spaces from the reader as long as to the next element, starting from
 * current position. Comments are ignored.
 * @param reader
 * @return
 */
public static String currentWhiteSpaceContent(XMLStreamReader reader) {

    // since the there might be several valid chunks (spaces/comment/spaces)
    // StringBuilder must be used; it's initialized lazily, only when needed
    StringBuilder whiteSpaces = null;

    for (;;) {
        switch (reader.getEventType()) {
            case START_ELEMENT:
            case END_ELEMENT:
            case END_DOCUMENT:
                return whiteSpaces == null ? null : whiteSpaces.toString();
            case CHARACTERS:
                if (reader.isWhiteSpace()) {
                    if (whiteSpaces == null) {
                        whiteSpaces = new StringBuilder();
                    }
                    whiteSpaces.append(reader.getText());
                } else {
                    throw new XMLStreamReaderException(
                            "xmlreader.unexpectedCharacterContent", reader.getText());
                }
        }
        next(reader);
    }
}
 
Example 4
Source File: XMLEditor.java    From MusicPlayer with MIT License 5 votes vote down vote up
private static void xmlSongsFilePathFinder() {
	try {
		// Creates reader for xml file.
		XMLInputFactory factory = XMLInputFactory.newInstance();
		factory.setProperty("javax.xml.stream.isCoalescing", true);
		FileInputStream is = new FileInputStream(new File(Resources.JAR + "library.xml"));
		XMLStreamReader reader = factory.createXMLStreamReader(is, "UTF-8");
		
		String element = null;
		String songLocation;
		
		// Loops through xml file looking for song titles.
		// Stores the song title in the xmlSongsFileNames array list.
		while(reader.hasNext()) {
		    reader.next();
		    if (reader.isWhiteSpace()) {
		        continue;
		    } else if (reader.isStartElement()) {
		    	element = reader.getName().getLocalPart();
		    } else if (reader.isCharacters() && element.equals("location")) {
		    	// Retrieves the song location and adds it to the corresponding array list.
		    	songLocation = reader.getText();
		    	xmlSongsFilePaths.add(songLocation);
		    	
		    	// Retrieves the file name from the file path and adds it to the xmlSongsFileNames array list.
		    	int i = songLocation.lastIndexOf("\\");
		    	String songFileName = songLocation.substring(i + 1, songLocation.length());
		    	xmlSongsFileNames.add(songFileName);
		    }
		}
		// Closes xml reader.
		reader.close();
	} catch (Exception e) {
		e.printStackTrace();
	}
}
 
Example 5
Source File: SOAPHandlerInterceptorTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
private XMLStreamReader preparemXMLStreamReader(String resouceName) throws Exception {
    InputStream is = this.getClass().getResourceAsStream(resouceName);
    XMLStreamReader xmlReader = XMLInputFactory.newInstance().createXMLStreamReader(is);

    // skip until soap body
    if (xmlReader.nextTag() == XMLStreamConstants.START_ELEMENT) {
        String ns = xmlReader.getNamespaceURI();
        SoapVersion soapVersion = SoapVersionFactory.getInstance().getSoapVersion(ns);
        // message.setVersion(soapVersion);

        QName qn = xmlReader.getName();
        while (!qn.equals(soapVersion.getBody()) && !qn.equals(soapVersion.getHeader())) {
            while (xmlReader.nextTag() != XMLStreamConstants.START_ELEMENT) {
                // nothing to do
            }
            qn = xmlReader.getName();
        }
        if (qn.equals(soapVersion.getHeader())) {
            XMLStreamReader filteredReader = new PartialXMLStreamReader(xmlReader, soapVersion.getBody());

            StaxUtils.read(filteredReader);
        }
        // advance just past body.
        xmlReader.next();

        while (xmlReader.isWhiteSpace()) {
            xmlReader.next();
        }
    }
    return xmlReader;
}
 
Example 6
Source File: XMLStreamReaderUtil.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Read spaces from the reader as long as to the next element, starting from
 * current position. Comments are ignored.
 * @param reader
 * @return
 */
public static String currentWhiteSpaceContent(XMLStreamReader reader) {

    // since the there might be several valid chunks (spaces/comment/spaces)
    // StringBuilder must be used; it's initialized lazily, only when needed
    StringBuilder whiteSpaces = null;

    for (;;) {
        switch (reader.getEventType()) {
            case START_ELEMENT:
            case END_ELEMENT:
            case END_DOCUMENT:
                return whiteSpaces == null ? null : whiteSpaces.toString();
            case CHARACTERS:
                if (reader.isWhiteSpace()) {
                    if (whiteSpaces == null) {
                        whiteSpaces = new StringBuilder();
                    }
                    whiteSpaces.append(reader.getText());
                } else {
                    throw new XMLStreamReaderException(
                            "xmlreader.unexpectedCharacterContent", reader.getText());
                }
        }
        next(reader);
    }
}
 
Example 7
Source File: XMLStreamReaderUtil.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Read spaces from the reader as long as to the next element, starting from
 * current position. Comments are ignored.
 * @param reader
 * @return
 */
public static String currentWhiteSpaceContent(XMLStreamReader reader) {

    // since the there might be several valid chunks (spaces/comment/spaces)
    // StringBuilder must be used; it's initialized lazily, only when needed
    StringBuilder whiteSpaces = null;

    for (;;) {
        switch (reader.getEventType()) {
            case START_ELEMENT:
            case END_ELEMENT:
            case END_DOCUMENT:
                return whiteSpaces == null ? null : whiteSpaces.toString();
            case CHARACTERS:
                if (reader.isWhiteSpace()) {
                    if (whiteSpaces == null) {
                        whiteSpaces = new StringBuilder();
                    }
                    whiteSpaces.append(reader.getText());
                } else {
                    throw new XMLStreamReaderException(
                            "xmlreader.unexpectedCharacterContent", reader.getText());
                }
        }
        next(reader);
    }
}
 
Example 8
Source File: XMLStreamReaderUtil.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
public static int nextContent(XMLStreamReader reader) {
    for (;;) {
        int state = next(reader);
        switch (state) {
            case START_ELEMENT:
            case END_ELEMENT:
            case END_DOCUMENT:
                return state;
            case CHARACTERS:
                if (!reader.isWhiteSpace()) {
                    return CHARACTERS;
                }
        }
    }
}
 
Example 9
Source File: XMLStreamReaderUtil.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public static int nextContent(XMLStreamReader reader) {
    for (;;) {
        int state = next(reader);
        switch (state) {
            case START_ELEMENT:
            case END_ELEMENT:
            case END_DOCUMENT:
                return state;
            case CHARACTERS:
                if (!reader.isWhiteSpace()) {
                    return CHARACTERS;
                }
        }
    }
}
 
Example 10
Source File: XMLStreamReaderUtil.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Read spaces from the reader as long as to the next element, starting from
 * current position. Comments are ignored.
 * @param reader
 * @return
 */
public static String currentWhiteSpaceContent(XMLStreamReader reader) {

    // since the there might be several valid chunks (spaces/comment/spaces)
    // StringBuilder must be used; it's initialized lazily, only when needed
    StringBuilder whiteSpaces = null;

    for (;;) {
        switch (reader.getEventType()) {
            case START_ELEMENT:
            case END_ELEMENT:
            case END_DOCUMENT:
                return whiteSpaces == null ? null : whiteSpaces.toString();
            case CHARACTERS:
                if (reader.isWhiteSpace()) {
                    if (whiteSpaces == null) {
                        whiteSpaces = new StringBuilder();
                    }
                    whiteSpaces.append(reader.getText());
                } else {
                    throw new XMLStreamReaderException(
                            "xmlreader.unexpectedCharacterContent", reader.getText());
                }
        }
        next(reader);
    }
}
 
Example 11
Source File: XMLStreamReaderUtil.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Read spaces from the reader as long as to the next element, starting from
 * current position. Comments are ignored.
 * @param reader
 * @return
 */
public static String currentWhiteSpaceContent(XMLStreamReader reader) {

    // since the there might be several valid chunks (spaces/comment/spaces)
    // StringBuilder must be used; it's initialized lazily, only when needed
    StringBuilder whiteSpaces = null;

    for (;;) {
        switch (reader.getEventType()) {
            case START_ELEMENT:
            case END_ELEMENT:
            case END_DOCUMENT:
                return whiteSpaces == null ? null : whiteSpaces.toString();
            case CHARACTERS:
                if (reader.isWhiteSpace()) {
                    if (whiteSpaces == null) {
                        whiteSpaces = new StringBuilder();
                    }
                    whiteSpaces.append(reader.getText());
                } else {
                    throw new XMLStreamReaderException(
                            "xmlreader.unexpectedCharacterContent", reader.getText());
                }
        }
        next(reader);
    }
}
 
Example 12
Source File: XMLStreamReaderUtil.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public static int nextContent(XMLStreamReader reader) {
    for (;;) {
        int state = next(reader);
        switch (state) {
            case START_ELEMENT:
            case END_ELEMENT:
            case END_DOCUMENT:
                return state;
            case CHARACTERS:
                if (!reader.isWhiteSpace()) {
                    return CHARACTERS;
                }
        }
    }
}
 
Example 13
Source File: StAXEventAllocatorBase.java    From openjdk-8-source 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 14
Source File: StAXEventAllocatorBase.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
XMLEvent getXMLEvent(XMLStreamReader reader){
    XMLEvent event = null;
    //returns the current event
    int eventType = reader.getEventType();
    //this needs to be set before creating events
    factory.setLocation(reader.getLocation());
    switch(eventType){

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

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

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

        }
        case XMLEvent.ATTRIBUTE:{
            event = null ;
            break;
        }
        case XMLEvent.DTD:{
            event = factory.createDTD(reader.getText());
            break;
        }
        case XMLEvent.CDATA:{
            event = factory.createCData(reader.getText());
            break;
        }
        case XMLEvent.SPACE:{
            event = factory.createSpace(reader.getText());
            break;
        }
    }
    return event ;
}
 
Example 15
Source File: Library.java    From MusicPlayer with MIT License 4 votes vote down vote up
private static void updateSongsList() {
    try {

        XMLInputFactory factory = XMLInputFactory.newInstance();
        factory.setProperty("javax.xml.stream.isCoalescing", true);
        FileInputStream is = new FileInputStream(new File(Resources.JAR + "library.xml"));
        XMLStreamReader reader = factory.createXMLStreamReader(is, "UTF-8");

        String element = "";
        int id = -1;
        String title = null;
        String artist = null;
        String album = null;
        Duration length = null;
        int trackNumber = -1;
        int discNumber = -1;
        int playCount = -1;
        LocalDateTime playDate = null;
        String location = null;

        while(reader.hasNext()) {
            reader.next();

            if (reader.isWhiteSpace()) {
                continue;
            } else if (reader.isStartElement()) {
                element = reader.getName().getLocalPart();
            } else if (reader.isCharacters()) {
                String value = reader.getText();

                switch (element) {
                    case ID:
                        id = Integer.parseInt(value);
                        break;
                    case TITLE:
                        title = value;
                        break;
                    case ARTIST:
                        artist = value;
                        break;
                    case ALBUM:
                        album = value;
                        break;
                    case LENGTH:
                        length = Duration.ofSeconds(Long.parseLong(value));
                        break;
                    case TRACKNUMBER:
                        trackNumber = Integer.parseInt(value);
                        break;
                    case DISCNUMBER:
                        discNumber = Integer.parseInt(value);
                        break;
                    case PLAYCOUNT:
                        playCount = Integer.parseInt(value);
                        break;
                    case PLAYDATE:
                        playDate = LocalDateTime.parse(value);
                        break;
                    case LOCATION:
                        location = value;
                        break;
                } // End switch
            } else if (reader.isEndElement() && reader.getName().getLocalPart().equals("song")) {

                songs.add(new Song(id, title, artist, album, length, trackNumber, discNumber, playCount, playDate, location));
                id = -1;
                title = null;
                artist = null;
                album = null;
                length = null;
                trackNumber = -1;
                discNumber = -1;
                playCount = -1;
                playDate = null;
                location = null;

            } else if (reader.isEndElement() && reader.getName().getLocalPart().equals("songs")) {

                reader.close();
                break;
            }
        } // End while

        reader.close();

    } catch (Exception ex) {
        ex.printStackTrace();
    }
}
 
Example 16
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 17
Source File: XMLEditor.java    From MusicPlayer with MIT License 4 votes vote down vote up
private static int xmlNewLastIdAssignedFinder() {
try {
	// Creates reader for xml file.
	XMLInputFactory factory = XMLInputFactory.newInstance();
	factory.setProperty("javax.xml.stream.isCoalescing", true);
	FileInputStream is = new FileInputStream(new File(Resources.JAR + "library.xml"));
	XMLStreamReader reader = factory.createXMLStreamReader(is, "UTF-8");
	
	String element = null;
	String location;
	
	String currentSongId = null;
	String xmlNewLastIdAssigned = null;
	
	// Loops through xml file looking for the music directory file path.
	while(reader.hasNext()) {
	    reader.next();
	    if (reader.isWhiteSpace()) {
	        continue;
	    } else if (reader.isStartElement()) {
	    	element = reader.getName().getLocalPart();
	    } else if (reader.isCharacters() && element.equals("id")) {
	    	currentSongId = reader.getText();
	    } else if (reader.isCharacters() && element.equals("location")) {
	    	location = reader.getText();
	    	// If the current location is does not correspond to one of the files to be deleted,
	    	// then the current id is assigned as the newLastIdAssigned.
	    	if (!songPathsToDelete.contains(location)) {
	    		xmlNewLastIdAssigned = currentSongId;
	    	}
	    } else if (reader.isEndElement() && reader.getName().getLocalPart().equals("songs")) {
	    	break;
	    }
	}
	// Closes xml reader.
	reader.close();
	
	// Converts the file number to an int and returns the value. 
	return Integer.parseInt(xmlNewLastIdAssigned);
} catch (Exception e) {
	e.printStackTrace();
	return 0;
}
  }
 
Example 18
Source File: StAXEventAllocatorBase.java    From openjdk-jdk8u-backup 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: XElement.java    From open-ig with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Parse an XML from an XML stream reader. Does not close the stream
 * @param in the XMLStreamReader object
 * @return az XElement object
 * @throws XMLStreamException on error
 */
private static XElement parseXML(XMLStreamReader in) throws XMLStreamException {
	XElement node = null;
	XElement root = null;
	final StringBuilder emptyBuilder = new StringBuilder();
	StringBuilder b = null;
	Deque<StringBuilder> stack = new LinkedList<>();
	
	while (in.hasNext()) {
		int type = in.next();
		switch(type) {
		case XMLStreamConstants.START_ELEMENT:
			if (b != null) {
				stack.push(b);
				b = null;
			} else {
				stack.push(emptyBuilder);
			}
			XElement n = new XElement(in.getName().getLocalPart());
			n.parent = node;
			int attCount = in.getAttributeCount();
			if (attCount > 0) {
				for (int i = 0; i < attCount; i++) {
					n.attributes.put(in.getAttributeLocalName(i), in.getAttributeValue(i));
				}
			}
			if (node != null) {
				node.children.add(n);
			}
			node = n;
			if (root == null) {
				root = n;
			}
			break;
		case XMLStreamConstants.CDATA:
		case XMLStreamConstants.CHARACTERS:
			if (node != null && !in.isWhiteSpace()) {
				if (b == null) {
					b = new StringBuilder();
				}
				b.append(in.getText());
			}
			break;
		case XMLStreamConstants.END_ELEMENT:
			if (node != null) {
				if (b != null) {
					node.content = b.toString();
				}
				node = node.parent;
			}
			b = stack.pop();
			if (b == emptyBuilder) {
				b = null;
			}
			break;
		default:
			// ignore others.
		}
	}
	in.close();
	return root;
}
 
Example 20
Source File: StAXEventAllocatorBase.java    From openjdk-jdk8u 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 ;
}