@Test public void checkConsumeElementAndConvertToXmlString() throws ThreddsXmlParserException, XMLStreamException { String descriptionElementAsString = new StringBuilder() .append( "<description xmlns=\"http: .append( "Some really <strong>important</strong> text.") .append( "</description>" ) .toString(); String descriptionDocumentAsString = "<?xml version='1.0' encoding='UTF-8'?>" + descriptionElementAsString; XMLEventReader descriptionDocEventReader = StaxParserTestUtils.createXmlEventReaderOnXmlString( descriptionDocumentAsString, "http: skipToFirstStartElement( descriptionDocEventReader ); String descriptionElementAfterFirstRoundTripAsString = StaxThreddsXmlParserUtils.consumeElementAndConvertToXmlString( descriptionDocEventReader ); assertEquals( descriptionElementAsString, descriptionElementAfterFirstRoundTripAsString ); String descriptionDocumentAfterFirstRoundTripAsString = new StringBuilder() .append( "<?xml version='1.0' encoding='UTF-8'?>\n" ) .append( descriptionElementAfterFirstRoundTripAsString ).toString(); XMLEventReader descriptionDocumentAfterRoundTripEventReader = StaxParserTestUtils.createXmlEventReaderOnXmlString( descriptionDocumentAfterFirstRoundTripAsString, "http: skipToFirstStartElement( descriptionDocumentAfterRoundTripEventReader ); String descriptionElementAfterSecondRoundTripAsString = StaxThreddsXmlParserUtils.consumeElementAndConvertToXmlString( descriptionDocumentAfterRoundTripEventReader ); assertEquals( descriptionElementAsString, descriptionElementAfterSecondRoundTripAsString ); }
static String consumeElementAndConvertToXmlString( XMLEventReader xmlEventReader ) throws ThreddsXmlParserException { if ( xmlEventReader == null ) throw new IllegalArgumentException( "XMLEventReader may not be null." ); StringWriter writer = new StringWriter(); String resultString; Location startLocation = null; try { XMLEvent event = xmlEventReader.peek(); if ( ! event.isStartElement() ) throw new IllegalArgumentException( "Next event in reader must be start element." ); startLocation = event.getLocation(); XMLOutputFactory oFactory = XMLOutputFactory.newFactory(); oFactory.setProperty( XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.TRUE); if ( oFactory.isPropertySupported( "javax.xml.stream.isPrefixDefaulting" )) oFactory.setProperty( "javax.xml.stream.isPrefixDefaulting", Boolean.TRUE ); XMLEventWriter eventWriter = oFactory.createXMLEventWriter( writer ); List<QName> nameList = new ArrayList<QName>(); while ( xmlEventReader.hasNext() ) { event = xmlEventReader.nextEvent(); if ( event.isStartElement() ) { nameList.add( event.asStartElement().getName() ); } else if ( event.isEndElement() ) { QName endElemName = event.asEndElement().getName(); QName lastName = nameList.get( nameList.size() - 1 ); if ( lastName.equals( endElemName ) ) nameList.remove( nameList.size() - 1 ); else { String msg = "Badly formed XML? End element [" + endElemName.getLocalPart() + "] doesn't match expected start element [" + lastName.getLocalPart() + "]."; log.error( "consumeElementAndConvertToXmlString(): " + msg ); throw new ThreddsXmlParserException( "FATAL? " + msg ); } } eventWriter.add( event ); if ( nameList.isEmpty() ) break; } eventWriter.flush(); eventWriter.close(); resultString = writer.toString(); } catch ( XMLStreamException e ) { throw new ThreddsXmlParserException( "Problem reading unknown element [" + startLocation + "]. Underlying cause: " + e.getMessage(), e ); } return resultString; }
StaxThreddsXmlParserUtils { static String consumeElementAndConvertToXmlString( XMLEventReader xmlEventReader ) throws ThreddsXmlParserException { if ( xmlEventReader == null ) throw new IllegalArgumentException( "XMLEventReader may not be null." ); StringWriter writer = new StringWriter(); String resultString; Location startLocation = null; try { XMLEvent event = xmlEventReader.peek(); if ( ! event.isStartElement() ) throw new IllegalArgumentException( "Next event in reader must be start element." ); startLocation = event.getLocation(); XMLOutputFactory oFactory = XMLOutputFactory.newFactory(); oFactory.setProperty( XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.TRUE); if ( oFactory.isPropertySupported( "javax.xml.stream.isPrefixDefaulting" )) oFactory.setProperty( "javax.xml.stream.isPrefixDefaulting", Boolean.TRUE ); XMLEventWriter eventWriter = oFactory.createXMLEventWriter( writer ); List<QName> nameList = new ArrayList<QName>(); while ( xmlEventReader.hasNext() ) { event = xmlEventReader.nextEvent(); if ( event.isStartElement() ) { nameList.add( event.asStartElement().getName() ); } else if ( event.isEndElement() ) { QName endElemName = event.asEndElement().getName(); QName lastName = nameList.get( nameList.size() - 1 ); if ( lastName.equals( endElemName ) ) nameList.remove( nameList.size() - 1 ); else { String msg = "Badly formed XML? End element [" + endElemName.getLocalPart() + "] doesn't match expected start element [" + lastName.getLocalPart() + "]."; log.error( "consumeElementAndConvertToXmlString(): " + msg ); throw new ThreddsXmlParserException( "FATAL? " + msg ); } } eventWriter.add( event ); if ( nameList.isEmpty() ) break; } eventWriter.flush(); eventWriter.close(); resultString = writer.toString(); } catch ( XMLStreamException e ) { throw new ThreddsXmlParserException( "Problem reading unknown element [" + startLocation + "]. Underlying cause: " + e.getMessage(), e ); } return resultString; } }
StaxThreddsXmlParserUtils { static String consumeElementAndConvertToXmlString( XMLEventReader xmlEventReader ) throws ThreddsXmlParserException { if ( xmlEventReader == null ) throw new IllegalArgumentException( "XMLEventReader may not be null." ); StringWriter writer = new StringWriter(); String resultString; Location startLocation = null; try { XMLEvent event = xmlEventReader.peek(); if ( ! event.isStartElement() ) throw new IllegalArgumentException( "Next event in reader must be start element." ); startLocation = event.getLocation(); XMLOutputFactory oFactory = XMLOutputFactory.newFactory(); oFactory.setProperty( XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.TRUE); if ( oFactory.isPropertySupported( "javax.xml.stream.isPrefixDefaulting" )) oFactory.setProperty( "javax.xml.stream.isPrefixDefaulting", Boolean.TRUE ); XMLEventWriter eventWriter = oFactory.createXMLEventWriter( writer ); List<QName> nameList = new ArrayList<QName>(); while ( xmlEventReader.hasNext() ) { event = xmlEventReader.nextEvent(); if ( event.isStartElement() ) { nameList.add( event.asStartElement().getName() ); } else if ( event.isEndElement() ) { QName endElemName = event.asEndElement().getName(); QName lastName = nameList.get( nameList.size() - 1 ); if ( lastName.equals( endElemName ) ) nameList.remove( nameList.size() - 1 ); else { String msg = "Badly formed XML? End element [" + endElemName.getLocalPart() + "] doesn't match expected start element [" + lastName.getLocalPart() + "]."; log.error( "consumeElementAndConvertToXmlString(): " + msg ); throw new ThreddsXmlParserException( "FATAL? " + msg ); } } eventWriter.add( event ); if ( nameList.isEmpty() ) break; } eventWriter.flush(); eventWriter.close(); resultString = writer.toString(); } catch ( XMLStreamException e ) { throw new ThreddsXmlParserException( "Problem reading unknown element [" + startLocation + "]. Underlying cause: " + e.getMessage(), e ); } return resultString; } private  StaxThreddsXmlParserUtils(); }
StaxThreddsXmlParserUtils { static String consumeElementAndConvertToXmlString( XMLEventReader xmlEventReader ) throws ThreddsXmlParserException { if ( xmlEventReader == null ) throw new IllegalArgumentException( "XMLEventReader may not be null." ); StringWriter writer = new StringWriter(); String resultString; Location startLocation = null; try { XMLEvent event = xmlEventReader.peek(); if ( ! event.isStartElement() ) throw new IllegalArgumentException( "Next event in reader must be start element." ); startLocation = event.getLocation(); XMLOutputFactory oFactory = XMLOutputFactory.newFactory(); oFactory.setProperty( XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.TRUE); if ( oFactory.isPropertySupported( "javax.xml.stream.isPrefixDefaulting" )) oFactory.setProperty( "javax.xml.stream.isPrefixDefaulting", Boolean.TRUE ); XMLEventWriter eventWriter = oFactory.createXMLEventWriter( writer ); List<QName> nameList = new ArrayList<QName>(); while ( xmlEventReader.hasNext() ) { event = xmlEventReader.nextEvent(); if ( event.isStartElement() ) { nameList.add( event.asStartElement().getName() ); } else if ( event.isEndElement() ) { QName endElemName = event.asEndElement().getName(); QName lastName = nameList.get( nameList.size() - 1 ); if ( lastName.equals( endElemName ) ) nameList.remove( nameList.size() - 1 ); else { String msg = "Badly formed XML? End element [" + endElemName.getLocalPart() + "] doesn't match expected start element [" + lastName.getLocalPart() + "]."; log.error( "consumeElementAndConvertToXmlString(): " + msg ); throw new ThreddsXmlParserException( "FATAL? " + msg ); } } eventWriter.add( event ); if ( nameList.isEmpty() ) break; } eventWriter.flush(); eventWriter.close(); resultString = writer.toString(); } catch ( XMLStreamException e ) { throw new ThreddsXmlParserException( "Problem reading unknown element [" + startLocation + "]. Underlying cause: " + e.getMessage(), e ); } return resultString; } private  StaxThreddsXmlParserUtils(); static StartElement readNextEventCheckItIsStartElementWithExpectedName( XMLEventReader xmlEventReader,
                                                                          QName startElementName ); static void readNextEventCheckItIsEndElementWithExpectedName( XMLEventReader xmlEventReader,
                                                                      QName elementName ); }
StaxThreddsXmlParserUtils { static String consumeElementAndConvertToXmlString( XMLEventReader xmlEventReader ) throws ThreddsXmlParserException { if ( xmlEventReader == null ) throw new IllegalArgumentException( "XMLEventReader may not be null." ); StringWriter writer = new StringWriter(); String resultString; Location startLocation = null; try { XMLEvent event = xmlEventReader.peek(); if ( ! event.isStartElement() ) throw new IllegalArgumentException( "Next event in reader must be start element." ); startLocation = event.getLocation(); XMLOutputFactory oFactory = XMLOutputFactory.newFactory(); oFactory.setProperty( XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.TRUE); if ( oFactory.isPropertySupported( "javax.xml.stream.isPrefixDefaulting" )) oFactory.setProperty( "javax.xml.stream.isPrefixDefaulting", Boolean.TRUE ); XMLEventWriter eventWriter = oFactory.createXMLEventWriter( writer ); List<QName> nameList = new ArrayList<QName>(); while ( xmlEventReader.hasNext() ) { event = xmlEventReader.nextEvent(); if ( event.isStartElement() ) { nameList.add( event.asStartElement().getName() ); } else if ( event.isEndElement() ) { QName endElemName = event.asEndElement().getName(); QName lastName = nameList.get( nameList.size() - 1 ); if ( lastName.equals( endElemName ) ) nameList.remove( nameList.size() - 1 ); else { String msg = "Badly formed XML? End element [" + endElemName.getLocalPart() + "] doesn't match expected start element [" + lastName.getLocalPart() + "]."; log.error( "consumeElementAndConvertToXmlString(): " + msg ); throw new ThreddsXmlParserException( "FATAL? " + msg ); } } eventWriter.add( event ); if ( nameList.isEmpty() ) break; } eventWriter.flush(); eventWriter.close(); resultString = writer.toString(); } catch ( XMLStreamException e ) { throw new ThreddsXmlParserException( "Problem reading unknown element [" + startLocation + "]. Underlying cause: " + e.getMessage(), e ); } return resultString; } private  StaxThreddsXmlParserUtils(); static StartElement readNextEventCheckItIsStartElementWithExpectedName( XMLEventReader xmlEventReader,
                                                                          QName startElementName ); static void readNextEventCheckItIsEndElementWithExpectedName( XMLEventReader xmlEventReader,
                                                                      QName elementName );  }