/src/solidstack/xml/elements/StaxNodeReader.java
Java | 112 lines | 62 code | 12 blank | 38 comment | 16 complexity | 27387d69bf03232898e625cba5cd445c MD5 | raw file
Possible License(s): Apache-2.0
1/*-- 2 * Copyright 2010 René M. de Bloois 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package solidstack.xml.elements; 18 19import javax.xml.stream.XMLStreamConstants; 20import javax.xml.stream.XMLStreamException; 21import javax.xml.stream.XMLStreamReader; 22 23import solidstack.lang.Assert; 24import solidstack.lang.SystemException; 25 26 27/** 28 * Reads an element from an {@link XMLStreamReader}. 29 * 30 * @author René M. de Bloois 31 */ 32@SuppressWarnings( "restriction" ) 33public class StaxNodeReader 34{ 35 /** 36 * This utility class cannot be constructed. 37 */ 38 private StaxNodeReader() 39 { 40 super(); 41 } 42 43 /** 44 * Reads a element from the given {@link XMLStreamReader}. 45 * 46 * @param reader The {@link XMLStreamReader} to read a node from. 47 * @return The element that is read. 48 * @throws XMLStreamException When thrown by the {@link XMLStreamReader}. 49 */ 50 static public Element readNode( XMLStreamReader reader ) throws XMLStreamException 51 { 52 int event = reader.getEventType(); 53 Assert.isTrue( event == XMLStreamConstants.START_ELEMENT, "Expecting an element start" ); 54 55 Element element = new Element( reader.getLocalName(), null ); 56 read( reader, element ); 57 return element; 58 } 59 60 /** 61 * A recursive method to read elements from the {@link XMLStreamReader}. 62 * 63 * @param reader The {@link XMLStreamReader} to read a node from. 64 * @param element The element to populate with attributes and children. 65 * @throws XMLStreamException When thrown by the {@link XMLStreamReader}. 66 */ 67 static protected void read( XMLStreamReader reader, Element element ) throws XMLStreamException 68 { 69 for( int i = 0; i < reader.getAttributeCount(); i++ ) 70 element.addAttribute( reader.getAttributeLocalName( i ), reader.getAttributeValue( i ) ); 71 72 StringBuilder buffer = new StringBuilder(); 73 74 while( reader.hasNext() ) 75 { 76 int event = reader.next(); 77 if( event == XMLStreamConstants.CHARACTERS ) 78 { 79 if( !reader.isWhiteSpace() ) 80 buffer.append( reader.getText() ); 81 } 82 else if( event == XMLStreamConstants.START_ELEMENT ) 83 { 84 if( buffer.length() > 0 ) 85 { 86 element.addText( buffer.toString() ); 87 buffer.setLength( 0 ); 88 } 89 90 Element child = new Element( reader.getLocalName(), element ); 91 element.addChild( child ); 92 93 read( reader, child ); 94 } 95 else if( event == XMLStreamConstants.END_ELEMENT ) 96 { 97 if( buffer.length() > 0 ) 98 { 99 element.addText( buffer.toString() ); 100 buffer.setLength( 0 ); 101 } 102 return; 103 } 104 else if( event == XMLStreamConstants.COMMENT ) 105 { 106 // Ignore comments 107 } 108 else 109 throw new SystemException( "Unexpected event [" + event + "]" ); 110 } 111 } 112}