/src/solidstack/xml/elements/StaxNodeReader.java

http://solidstack.googlecode.com/ · Java · 112 lines · 62 code · 12 blank · 38 comment · 16 complexity · 27387d69bf03232898e625cba5cd445c MD5 · raw file

  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. package solidstack.xml.elements;
  17. import javax.xml.stream.XMLStreamConstants;
  18. import javax.xml.stream.XMLStreamException;
  19. import javax.xml.stream.XMLStreamReader;
  20. import solidstack.lang.Assert;
  21. import solidstack.lang.SystemException;
  22. /**
  23. * Reads an element from an {@link XMLStreamReader}.
  24. *
  25. * @author René M. de Bloois
  26. */
  27. @SuppressWarnings( "restriction" )
  28. public class StaxNodeReader
  29. {
  30. /**
  31. * This utility class cannot be constructed.
  32. */
  33. private StaxNodeReader()
  34. {
  35. super();
  36. }
  37. /**
  38. * Reads a element from the given {@link XMLStreamReader}.
  39. *
  40. * @param reader The {@link XMLStreamReader} to read a node from.
  41. * @return The element that is read.
  42. * @throws XMLStreamException When thrown by the {@link XMLStreamReader}.
  43. */
  44. static public Element readNode( XMLStreamReader reader ) throws XMLStreamException
  45. {
  46. int event = reader.getEventType();
  47. Assert.isTrue( event == XMLStreamConstants.START_ELEMENT, "Expecting an element start" );
  48. Element element = new Element( reader.getLocalName(), null );
  49. read( reader, element );
  50. return element;
  51. }
  52. /**
  53. * A recursive method to read elements from the {@link XMLStreamReader}.
  54. *
  55. * @param reader The {@link XMLStreamReader} to read a node from.
  56. * @param element The element to populate with attributes and children.
  57. * @throws XMLStreamException When thrown by the {@link XMLStreamReader}.
  58. */
  59. static protected void read( XMLStreamReader reader, Element element ) throws XMLStreamException
  60. {
  61. for( int i = 0; i < reader.getAttributeCount(); i++ )
  62. element.addAttribute( reader.getAttributeLocalName( i ), reader.getAttributeValue( i ) );
  63. StringBuilder buffer = new StringBuilder();
  64. while( reader.hasNext() )
  65. {
  66. int event = reader.next();
  67. if( event == XMLStreamConstants.CHARACTERS )
  68. {
  69. if( !reader.isWhiteSpace() )
  70. buffer.append( reader.getText() );
  71. }
  72. else if( event == XMLStreamConstants.START_ELEMENT )
  73. {
  74. if( buffer.length() > 0 )
  75. {
  76. element.addText( buffer.toString() );
  77. buffer.setLength( 0 );
  78. }
  79. Element child = new Element( reader.getLocalName(), element );
  80. element.addChild( child );
  81. read( reader, child );
  82. }
  83. else if( event == XMLStreamConstants.END_ELEMENT )
  84. {
  85. if( buffer.length() > 0 )
  86. {
  87. element.addText( buffer.toString() );
  88. buffer.setLength( 0 );
  89. }
  90. return;
  91. }
  92. else if( event == XMLStreamConstants.COMMENT )
  93. {
  94. // Ignore comments
  95. }
  96. else
  97. throw new SystemException( "Unexpected event [" + event + "]" );
  98. }
  99. }
  100. }