Package javax.xml.stream.events

Examples of javax.xml.stream.events.XMLEvent


      ThreddsBuilderFactory catBuilderFac = new ThreddsBuilderFactoryImpl();
      ThreddsBuilder threddsBuilder = null;
      while ( eventReader.hasNext() )
      {
        XMLEvent event = eventReader.peek();
        if ( event.isEndDocument())
        { // Done!
          eventReader.next();
          break;
        }
        else if ( event.isStartDocument())
        { // Don't need any info from StartDocument event.
          eventReader.next();
        }
        else if ( event.isStartElement())
        {
          if ( this.catElemParserFactory.isEventMyStartElement( event.asStartElement() ))
          {
            CatalogElementParser catElemParser = this.catElemParserFactory.getNewParser( source.getSystemId(),
                                                                                         eventReader, catBuilderFac);
            threddsBuilder = catElemParser.parse();
          }
          // ToDo Implement reading a document with "dataset" root element.
          // ToDo Implement reading a document with "metadata" root element.
          else
          {
            // ToDo Save the results in a ThreddsXmlParserIssue (Warning) and report.
            StaxThreddsXmlParserUtils.consumeElementAndConvertToXmlString( eventReader );
            log.warn( "readCatalogXML(): Unrecognized start element [" + event.asStartElement().getName() + "]." );
            //eventReader.next();
          }
        }
        else if ( event.isEndElement())
        {
          log.error( "readCatalogXML(): Unrecognized end element [" + event.asEndElement().getName() + "]." );
          break;
        }
        else
        {
          log.debug( "readCatalogXML(): Unhandled event [" + event.getLocation() + "--" + event + "].");
          eventReader.next();
          continue;
        }
      }
View Full Code Here


  static void advanceReaderToFirstStartElement( XMLEventReader reader )
          throws XMLStreamException
  {
    while ( reader.hasNext() )
    {
      XMLEvent event = reader.peek();
      if ( event.isStartElement() )
        break;
      else if ( event.isCharacters() )
        event = reader.nextEvent();
      else if ( event.isStartDocument() )
        event = reader.nextEvent();
      else
        throw new IllegalStateException( "Unexpected event [" + event + "]." );
    }
  }
View Full Code Here

    StringWriter writer = new StringWriter();

    while ( eventReader.hasNext() )
    {
      XMLEvent event = eventReader.nextEvent();
      event.writeAsEncodedUnicode( writer );
    }

    writer.flush();
    String resultingXml = writer.toString();
    assertTrue( "XMLEvent.writeAsEncodedUnicode() is working.\n" + resultingXml,
View Full Code Here

      throw new IllegalStateException( "XMLEventReader has no further events." );

    StartElement startElement = null;
    try
    {
      XMLEvent event = xmlEventReader.peek();
      if ( ! event.isStartElement() )
        throw new IllegalStateException( "Next event must be StartElement." );

      if ( ! event.asStartElement().getName().equals( startElementName ) )
        throw new IllegalStateException( "Start element must be an '" + startElementName.getLocalPart() + "' element." );

      startElement = xmlEventReader.nextEvent().asStartElement();
    }
    catch ( XMLStreamException e )
View Full Code Here

    if ( ! xmlEventReader.hasNext() )
      throw new IllegalStateException( "XMLEventReader has no further events." );

    try
    {
      XMLEvent event = xmlEventReader.peek();
      if ( ! event.isEndElement() )
        throw new IllegalStateException( "Next event must be EndElement." );

      if ( ! event.asEndElement().getName().equals( elementName ) )
        throw new IllegalStateException( "End element must be an '" + elementName.getLocalPart() + "' element." );

      xmlEventReader.nextEvent(); // .asEndElement();
    }
    catch ( XMLStreamException e )
View Full Code Here

    if ( xmlEventReader == null )
      throw new IllegalArgumentException( "XMLEventReader may not be null.");
    if ( ! xmlEventReader.hasNext() )
      throw new IllegalArgumentException( "XMLEventReader must have next event.");

    XMLEvent nextEvent = null;
    try
    {
      nextEvent = xmlEventReader.peek();
    }
    catch ( XMLStreamException e )
    {
      throw new IllegalArgumentException( "Could not peek() next event.");
    }

    return nextEvent.getLocation();
  }
View Full Code Here

    StringWriter writerUsingWriteAsEncodedUnicode = new StringWriter();
    StringWriter writerUsingToString = new StringWriter();
    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();

      // Track start and end elements so know when done.
      // Use name list as FILO, push on name of start element and pop off matching name of end element.
      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
          {
            // Parser should have had FATAL error for this.
            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 );
          }
        }

        event.writeAsEncodedUnicode( writerUsingWriteAsEncodedUnicode );
        writerUsingToString.write( event.toString());
        if ( nameList.isEmpty() )
          break;
      }
    }
    catch ( XMLStreamException e )
View Full Code Here

    Location location = null;
    try
    {
      while ( xmlEventReader.hasNext() )
      {
        XMLEvent event = xmlEventReader.peek();
        location = event.getLocation();

        if ( event.isCharacters())
        {
          event = xmlEventReader.nextEvent();
          stringBuilder.append( event.asCharacters().getData());
        }
        else if ( event.isEndElement())
        {
          if ( event.asEndElement().getName().equals( containingElementName ))
          {
            return stringBuilder.toString().trim();
          }
          throw new IllegalStateException( "Badly formed XML? Unexpected end element [" + event.asEndElement().getName().getLocalPart() + "]["+location+"] doesn't match expected start element [" + containingElementName.getLocalPart() + "].");
        }
        else if ( event.isStartElement() )
        {
          throw new IllegalStateException( "Badly formed XML? Unexpected start element [" + event.asStartElement().getName().getLocalPart() + "][" + location + "] when characters expected." );
        }
        else
        {
          xmlEventReader.next();
        }
View Full Code Here

    {
      this.parseStartElement();

      while ( this.reader.hasNext() )
      {
        XMLEvent event = this.reader.peek();
        if ( event.isStartElement() )
        {
          this.handleChildStartElement();
        }
        else if ( event.isEndElement() )
        {
          if ( this.isSelfElement( event.asEndElement() ) )
          {
            this.reader.next();
            break;
          }
          else
          {
            if ( this instanceof ThreddsMetadataElementParser )
            {
              if ( log.isDebugEnabled())
              {
                String msg = "End element probably parent of ThreddsMetadata [" + event.asEndElement().getName() + "].";
                ThreddsXmlParserIssue issue = StaxThreddsXmlParserUtils
                        .createIssueForUnexpectedEvent( msg, ThreddsXmlParserIssue.Severity.WARNING,
                                                        this.reader, event );
                // ToDo Figure out a better way to deal with this situation.
                log.debug( "parse(): " + issue.getMessage() );
              }
              break;
            }
            else
            {
              String msg = "Unrecognized end element [" + event.asEndElement().getName() + "].";
              ThreddsXmlParserIssue issue = StaxThreddsXmlParserUtils
                      .createIssueForUnexpectedEvent( msg, ThreddsXmlParserIssue.Severity.FATAL,
                                                      this.reader, event );
              log.error( this.getClass().getName() + ".parse(): " + issue.getMessage() );
              // ToDo Gather issues (and "this.reader.next(); continue;") rather than throw exception.
              throw new ThreddsXmlParserException( issue );
            }
          }
        }
        else
        {
          log.debug( this.getClass().getName() + ".parse(): Unhandled event [" + event.getLocation() + "--" + event + "]." );
          this.reader.next();
          continue;
        }
      }
View Full Code Here

      throw new ThreddsXmlParserException( "XMLEventReader has no further events." );

    StartElement startElement = null;
    try
    {
      XMLEvent event = this.reader.peek();
      if ( ! event.isStartElement() )
        throw new ThreddsXmlParserException( "Next event must be StartElement." );

      if ( ! event.asStartElement().getName().equals( this.elementName ) )
        throw new ThreddsXmlParserException( "Start element must be an '" + this.elementName.getLocalPart() + "' element." );

      startElement = this.reader.nextEvent().asStartElement();
    }
    catch ( XMLStreamException e )
View Full Code Here

TOP

Related Classes of javax.xml.stream.events.XMLEvent

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.