Java Code Examples for javax.xml.stream.events.XMLEvent#asEndElement()

The following examples show how to use javax.xml.stream.events.XMLEvent#asEndElement() . 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: CheckstyleConfigurationsParser.java    From contribution with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Recursively processes parsing of the configuration XML file with StAX parser.
 * Configuration XML has recursive tree structure
 * where module tags are nested into other module tags, also
 * duplicates of the same modules may be present in one configuration,
 * so there are no other option but to parse configuration
 * into the same tree structure.
 *
 * @param reader
 *        StAX parser interface.
 * @param parent
 *        parent module tag.
 * @throws XMLStreamException
 *         on internal StAX parser error.
 */
private static void parseModule(XMLEventReader reader, ConfigurationModule parent)
        throws XMLStreamException {
    while (reader.hasNext()) {
        final XMLEvent event = reader.nextEvent();
        if (event.isStartElement()) {
            final StartElement startElement = event.asStartElement();
            final String startElementName = startElement.getName()
                    .getLocalPart();
            // module tag encounter
            if (startElementName.equals(MODULE_TAG)) {
                processModuleTag(reader, startElement, parent);
            }
            // property tag encounter
            else if (startElementName.equals(PROPERTY_TAG)) {
                processPropertyTag(startElement, parent);
            }
            // message tag encounter
            else if (startElementName.equals(MESSAGE_TAG)) {
                processMessageTag(startElement, parent);
            }
        }
        if (event.isEndElement()) {
            final EndElement endElement = event.asEndElement();
            if (endElement.getName().getLocalPart().equals(MODULE_TAG)) {
                // return from recursive method
                break;
            }
        }
    }
}
 
Example 2
Source File: ConfigReader.java    From ballerina-integrator with Apache License 2.0 5 votes vote down vote up
/**
 * Process XML as a stream of events
 *
 * @param inputStream
 */
public void readXML(InputStream inputStream) {
    XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
    try {
        XMLEventReader eventReader = xmlInputFactory.createXMLEventReader(inputStream);

        while (eventReader.hasNext()) {
            XMLEvent xmlEvent = eventReader.nextEvent();
            switch (xmlEvent.getEventType()) {
            case XMLStreamConstants.START_ELEMENT:
                StartElement startElement = xmlEvent.asStartElement();
                checkFlowState(getElementOrAttributeName(startElement.getName()), true);
                loadIntermediateMuleObjects(startElement);
                break;

            case XMLStreamConstants.CHARACTERS:
                break;

            case XMLStreamConstants.END_ELEMENT:
                EndElement endElement = xmlEvent.asEndElement();
                checkFlowState(getElementOrAttributeName(endElement.getName()), false);
                break;

            default:
                break;
            }
        }
    } catch (XMLStreamException e) {
        logger.error(e.getMessage(), e);
    }
}
 
Example 3
Source File: MetadataParser.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
void read(XMLEventReader reader, StartElement parentElement, T t, String... names)
    throws XMLStreamException {
  while (reader.hasNext()) {
    XMLEvent event = reader.peek();

    if (!parseAnnotations) {
      XMLEvent eventBefore = event;
      event = skipAnnotations(reader, event);
      // if annotation is stripped start again
      if (eventBefore != event) {            
        continue;
      }
    }

    if (!event.isStartElement() && !event.isEndElement()) {
      reader.nextEvent();
      continue;
    }

    if (parentElement != null && event.isEndElement()
        && ((EndElement) event).getName().equals(parentElement.getName())) {
      // end reached
      break;
    }

    boolean hit = false;

    for (String name : names) {
      if (event.isStartElement()) {
        StartElement element = event.asStartElement();
        if (element.getName().getLocalPart().equals(name)) {              
          reader.nextEvent(); // advance cursor start which is current
          build(reader, element, t, name);
          hit = true;
          break;
        }
      }
      if (event.isEndElement()) {
        EndElement e = event.asEndElement();
        if (e.getName().getLocalPart().equals(name)) {
          reader.nextEvent(); // advance cursor to end which is current
          hit = true;
          break;
        }
      }
    }
    if (!hit) {
      break;
    }
  }
}
 
Example 4
Source File: TubelineFeatureReader.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
private TubelineFeature parseFactories(final boolean enabled, final StartElement element, final XMLEventReader reader)
        throws WebServiceException {
    int elementRead = 0;
    loop:
    while (reader.hasNext()) {
        try {
            final XMLEvent event = reader.nextEvent();
            switch (event.getEventType()) {
                case XMLStreamConstants.COMMENT:
                    break; // skipping the comments and start document events
                case XMLStreamConstants.CHARACTERS:
                    if (event.asCharacters().isWhiteSpace()) {
                        break;
                    }
                    else {
                        // TODO: logging message
                        throw LOGGER.logSevereException(new WebServiceException("No character data allowed, was " + event.asCharacters()));
                    }
                case XMLStreamConstants.START_ELEMENT:
                    // TODO implement
                    elementRead++;
                    break;
                case XMLStreamConstants.END_ELEMENT:
                    elementRead--;
                    if (elementRead < 0) {
                        final EndElement endElement = event.asEndElement();
                        if (!element.getName().equals(endElement.getName())) {
                            // TODO logging message
                            throw LOGGER.logSevereException(new WebServiceException("End element does not match " + endElement));
                        }
                        break loop;
                    }
                    else {
                        break;
                    }
                default:
                    // TODO logging message
                    throw LOGGER.logSevereException(new WebServiceException("Unexpected event, was " + event));
            }
        } catch (XMLStreamException e) {
            // TODO logging message
            throw LOGGER.logSevereException(new WebServiceException("Failed to unmarshal XML document", e));
        }
    }

    // TODO implement
    return new TubelineFeature(enabled);
}
 
Example 5
Source File: StAXSchemaParser.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
public void parse(XMLEventReader input) throws XMLStreamException, XNIException {
    XMLEvent currentEvent = input.peek();
    if (currentEvent != null) {
        int eventType = currentEvent.getEventType();
        if (eventType != XMLStreamConstants.START_DOCUMENT &&
            eventType != XMLStreamConstants.START_ELEMENT) {
            throw new XMLStreamException();
        }
        fLocationWrapper.setLocation(currentEvent.getLocation());
        fSchemaDOMParser.startDocument(fLocationWrapper, null, fNamespaceContext, null);
        loop: while (input.hasNext()) {
            currentEvent = input.nextEvent();
            eventType = currentEvent.getEventType();
            switch (eventType) {
            case XMLStreamConstants.START_ELEMENT:
                ++fDepth;
                StartElement start = currentEvent.asStartElement();
                fillQName(fElementQName, start.getName());
                fLocationWrapper.setLocation(start.getLocation());
                fNamespaceContext.setNamespaceContext(start.getNamespaceContext());
                fillXMLAttributes(start);
                fillDeclaredPrefixes(start);
                addNamespaceDeclarations();
                fNamespaceContext.pushContext();
                fSchemaDOMParser.startElement(fElementQName, fAttributes, null);
                break;
            case XMLStreamConstants.END_ELEMENT:
                EndElement end = currentEvent.asEndElement();
                fillQName(fElementQName, end.getName());
                fillDeclaredPrefixes(end);
                fLocationWrapper.setLocation(end.getLocation());
                fSchemaDOMParser.endElement(fElementQName, null);
                fNamespaceContext.popContext();
                --fDepth;
                if (fDepth <= 0) {
                    break loop;
                }
                break;
            case XMLStreamConstants.CHARACTERS:
                sendCharactersToSchemaParser(currentEvent.asCharacters().getData(), false);
                break;
            case XMLStreamConstants.SPACE:
                sendCharactersToSchemaParser(currentEvent.asCharacters().getData(), true);
                break;
            case XMLStreamConstants.CDATA:
                fSchemaDOMParser.startCDATA(null);
                sendCharactersToSchemaParser(currentEvent.asCharacters().getData(), false);
                fSchemaDOMParser.endCDATA(null);
                break;
            case XMLStreamConstants.PROCESSING_INSTRUCTION:
                ProcessingInstruction pi = (ProcessingInstruction)currentEvent;
                fillProcessingInstruction(pi.getData());
                fSchemaDOMParser.processingInstruction(pi.getTarget(), fTempString, null);
                break;
            case XMLStreamConstants.DTD:
                /* There shouldn't be a DTD in the schema */
                break;
            case XMLStreamConstants.ENTITY_REFERENCE:
                /* Not needed for schemas */
                break;
            case XMLStreamConstants.COMMENT:
                /* No point in sending comments */
                break;
            case XMLStreamConstants.START_DOCUMENT:
                fDepth++;
                /* We automatically call startDocument before the loop */
                break;
            case XMLStreamConstants.END_DOCUMENT:
                /* We automatically call endDocument after the loop */
                break;
            }
        }
        fLocationWrapper.setLocation(null);
        fNamespaceContext.setNamespaceContext(null);
        fSchemaDOMParser.endDocument(null);
    }
}
 
Example 6
Source File: StaxParser.java    From tutorials with MIT License 4 votes vote down vote up
public List<Tutorial> getAllTutorial() {
    boolean bTitle = false;
    boolean bDescription = false;
    boolean bDate = false;
    boolean bAuthor = false;
    List<Tutorial> tutorials = new ArrayList<Tutorial>();
    try {
        XMLInputFactory factory = XMLInputFactory.newInstance();
        XMLEventReader eventReader = factory.createXMLEventReader(new FileReader(this.getFile()));
        Tutorial current = null;
        while (eventReader.hasNext()) {
            XMLEvent event = eventReader.nextEvent();
            switch (event.getEventType()) {
            case XMLStreamConstants.START_ELEMENT:
                StartElement startElement = event.asStartElement();
                String qName = startElement.getName().getLocalPart();
                if (qName.equalsIgnoreCase("tutorial")) {
                    current = new Tutorial();
                    Iterator<Attribute> attributes = startElement.getAttributes();
                    while (attributes.hasNext()) {
                        Attribute currentAt = attributes.next();
                        if (currentAt.getName().toString().equalsIgnoreCase("tutId")) {
                            current.setTutId(currentAt.getValue());
                        } else if (currentAt.getName().toString().equalsIgnoreCase("type")) {
                            current.setType(currentAt.getValue());
                        }
                    }
                } else if (qName.equalsIgnoreCase("title")) {
                    bTitle = true;
                } else if (qName.equalsIgnoreCase("description")) {
                    bDescription = true;
                } else if (qName.equalsIgnoreCase("date")) {
                    bDate = true;
                } else if (qName.equalsIgnoreCase("author")) {
                    bAuthor = true;
                }
                break;
            case XMLStreamConstants.CHARACTERS:
                Characters characters = event.asCharacters();
                if (bTitle) {
                    if (current != null) {
                        current.setTitle(characters.getData());
                    }
                    bTitle = false;
                }
                if (bDescription) {
                    if (current != null) {
                        current.setDescription(characters.getData());
                    }
                    bDescription = false;
                }
                if (bDate) {
                    if (current != null) {
                        current.setDate(characters.getData());
                    }
                    bDate = false;
                }
                if (bAuthor) {
                    if (current != null) {
                        current.setAuthor(characters.getData());
                    }
                    bAuthor = false;
                }
                break;
            case XMLStreamConstants.END_ELEMENT:
                EndElement endElement = event.asEndElement();
                if (endElement.getName().getLocalPart().equalsIgnoreCase("tutorial")) {
                    if (current != null) {
                        tutorials.add(current);
                    }
                }
                break;
            }
        }

    } catch (FileNotFoundException | XMLStreamException e) {
        e.printStackTrace();
    }

    return tutorials;
}
 
Example 7
Source File: StaxParser.java    From tutorials with MIT License 4 votes vote down vote up
public static List<WebSite> parse(String path) {
    List<WebSite> websites = new ArrayList<WebSite>();
    WebSite website = null;
    XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
    try {
        XMLEventReader reader = xmlInputFactory.createXMLEventReader(new FileInputStream(path));
        while (reader.hasNext()) {
            XMLEvent nextEvent = reader.nextEvent();
            if (nextEvent.isStartElement()) {
                StartElement startElement = nextEvent.asStartElement();
                switch (startElement.getName()
                    .getLocalPart()) {
                case "website":
                    website = new WebSite();
                    Attribute url = startElement.getAttributeByName(new QName("url"));
                    if (url != null) {
                        website.setUrl(url.getValue());
                    }
                    break;
                case "name":
                    nextEvent = reader.nextEvent();
                    website.setName(nextEvent.asCharacters()
                        .getData());
                    break;
                case "category":
                    nextEvent = reader.nextEvent();
                    website.setCategory(nextEvent.asCharacters()
                        .getData());
                    break;
                case "status":
                    nextEvent = reader.nextEvent();
                    website.setStatus(nextEvent.asCharacters()
                        .getData());
                    break;
                }
            }
            if (nextEvent.isEndElement()) {
                EndElement endElement = nextEvent.asEndElement();
                if (endElement.getName()
                    .getLocalPart()
                    .equals("website")) {
                    websites.add(website);
                }
            }
        }
    } catch (XMLStreamException xse) {
        System.out.println("XMLStreamException");
        xse.printStackTrace();
    } catch (FileNotFoundException fnfe) {
        System.out.println("FileNotFoundException");
        fnfe.printStackTrace();
    }
    return websites;
}
 
Example 8
Source File: CheckstyleReportsParser.java    From contribution with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Parses portion of the XML report.
 *
 * @param diffReport
 *        container for parsed data.
 * @param reader
 *        StAX parser interface.
 * @param numOfFilenames
 *        number of "file" tags to parse.
 * @param index
 *        internal index of the parsed file.
 * @throws XMLStreamException
 *         on internal parser error.
 */
private static void parseXmlPortion(DiffReport diffReport,
        XMLEventReader reader, int numOfFilenames, int index)
                throws XMLStreamException {
    int counter = numOfFilenames;
    String filename = null;
    List<CheckstyleRecord> records = null;
    while (reader.hasNext()) {
        final XMLEvent event = reader.nextEvent();
        if (event.isStartElement()) {
            final StartElement startElement = event.asStartElement();
            final String startElementName = startElement.getName()
                    .getLocalPart();
            // file tag encounter
            if (startElementName.equals(FILE_TAG)) {
                counter--;
                diffReport.getStatistics().incrementFileCount(index);
                final Iterator<Attribute> attributes = startElement
                        .getAttributes();
                while (attributes.hasNext()) {
                    final Attribute attribute = attributes.next();
                    if (attribute.getName().toString()
                            .equals(FILENAME_ATTR)) {
                        filename = attribute.getValue();
                    }
                }
                records = new ArrayList<>();
            }
            // error tag encounter
            else if (startElementName.equals(ERROR_TAG)) {
                records.add(parseErrorTag(startElement, diffReport.getStatistics(), index,
                        filename));
            }
        }
        if (event.isEndElement()) {
            final EndElement endElement = event.asEndElement();
            if (endElement.getName().getLocalPart().equals(FILE_TAG)) {
                diffReport.addRecords(records, filename);
                if (counter == 0) {
                    break;
                }
            }
        }
    }
}
 
Example 9
Source File: SurefireReportReader.java    From smart-testing with Apache License 2.0 4 votes vote down vote up
private static Collection<TestResult> readTestResults(XMLEventReader eventReader) throws XMLStreamException {
    final Set<TestResult> testResults = new HashSet<>();
    TestResult currentTestResult = null;

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

        if (event.isStartElement()) {
            final StartElement startElement = event.asStartElement();
            if ("testcase".equalsIgnoreCase(startElement.getName().getLocalPart())) {
                // Read attributes
                String name = null, classname = null;
                Status status = null;

                final Iterator<Attribute> attributes = startElement.getAttributes();

                while (attributes.hasNext()) {
                    final Attribute attribute = attributes.next();
                    if ("classname".equalsIgnoreCase(attribute.getName().toString())) {
                        classname = attribute.getValue();
                    }

                    if ("name".equalsIgnoreCase(attribute.getName().toString())) {
                        name = attribute.getValue();
                    }
                }

                currentTestResult = new TestResult(classname, name, Status.PASSED);
            }

            if ("failure".equalsIgnoreCase(startElement.getName().getLocalPart())) {
                currentTestResult.setStatus(Status.FAILURE);
            }

            if ("error".equalsIgnoreCase(startElement.getName().getLocalPart())) {
                currentTestResult.setStatus(Status.ERROR);
            }

            if ("skipped".equalsIgnoreCase(startElement.getName().getLocalPart())) {
                currentTestResult.setStatus(Status.SKIPPED);
            }

            if ("rerunFailure".equalsIgnoreCase(startElement.getName().getLocalPart())) {
                currentTestResult.setStatus(Status.RE_RUN_FAILURE);
            }
        }

        if (event.isEndElement()) {
            final EndElement endElementElement = event.asEndElement();
            if ("testcase".equalsIgnoreCase(endElementElement.getName().getLocalPart())) {
                testResults.add(currentTestResult);
            }
        }
    }
    return testResults;
}
 
Example 10
Source File: StAXSchemaParser.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
public void parse(XMLEventReader input) throws XMLStreamException, XNIException {
    XMLEvent currentEvent = input.peek();
    if (currentEvent != null) {
        int eventType = currentEvent.getEventType();
        if (eventType != XMLStreamConstants.START_DOCUMENT &&
            eventType != XMLStreamConstants.START_ELEMENT) {
            throw new XMLStreamException();
        }
        fLocationWrapper.setLocation(currentEvent.getLocation());
        fSchemaDOMParser.startDocument(fLocationWrapper, null, fNamespaceContext, null);
        loop: while (input.hasNext()) {
            currentEvent = input.nextEvent();
            eventType = currentEvent.getEventType();
            switch (eventType) {
            case XMLStreamConstants.START_ELEMENT:
                ++fDepth;
                StartElement start = currentEvent.asStartElement();
                fillQName(fElementQName, start.getName());
                fLocationWrapper.setLocation(start.getLocation());
                fNamespaceContext.setNamespaceContext(start.getNamespaceContext());
                fillXMLAttributes(start);
                fillDeclaredPrefixes(start);
                addNamespaceDeclarations();
                fNamespaceContext.pushContext();
                fSchemaDOMParser.startElement(fElementQName, fAttributes, null);
                break;
            case XMLStreamConstants.END_ELEMENT:
                EndElement end = currentEvent.asEndElement();
                fillQName(fElementQName, end.getName());
                fillDeclaredPrefixes(end);
                fLocationWrapper.setLocation(end.getLocation());
                fSchemaDOMParser.endElement(fElementQName, null);
                fNamespaceContext.popContext();
                --fDepth;
                if (fDepth <= 0) {
                    break loop;
                }
                break;
            case XMLStreamConstants.CHARACTERS:
                sendCharactersToSchemaParser(currentEvent.asCharacters().getData(), false);
                break;
            case XMLStreamConstants.SPACE:
                sendCharactersToSchemaParser(currentEvent.asCharacters().getData(), true);
                break;
            case XMLStreamConstants.CDATA:
                fSchemaDOMParser.startCDATA(null);
                sendCharactersToSchemaParser(currentEvent.asCharacters().getData(), false);
                fSchemaDOMParser.endCDATA(null);
                break;
            case XMLStreamConstants.PROCESSING_INSTRUCTION:
                ProcessingInstruction pi = (ProcessingInstruction)currentEvent;
                fillProcessingInstruction(pi.getData());
                fSchemaDOMParser.processingInstruction(pi.getTarget(), fTempString, null);
                break;
            case XMLStreamConstants.DTD:
                /* There shouldn't be a DTD in the schema */
                break;
            case XMLStreamConstants.ENTITY_REFERENCE:
                /* Not needed for schemas */
                break;
            case XMLStreamConstants.COMMENT:
                /* No point in sending comments */
                break;
            case XMLStreamConstants.START_DOCUMENT:
                fDepth++;
                /* We automatically call startDocument before the loop */
                break;
            case XMLStreamConstants.END_DOCUMENT:
                /* We automatically call endDocument after the loop */
                break;
            }
        }
        fLocationWrapper.setLocation(null);
        fNamespaceContext.setNamespaceContext(null);
        fSchemaDOMParser.endDocument(null);
    }
}
 
Example 11
Source File: TubelineFeatureReader.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
private TubelineFeature parseFactories(final boolean enabled, final StartElement element, final XMLEventReader reader)
        throws WebServiceException {
    int elementRead = 0;
    loop:
    while (reader.hasNext()) {
        try {
            final XMLEvent event = reader.nextEvent();
            switch (event.getEventType()) {
                case XMLStreamConstants.COMMENT:
                    break; // skipping the comments and start document events
                case XMLStreamConstants.CHARACTERS:
                    if (event.asCharacters().isWhiteSpace()) {
                        break;
                    }
                    else {
                        // TODO: logging message
                        throw LOGGER.logSevereException(new WebServiceException("No character data allowed, was " + event.asCharacters()));
                    }
                case XMLStreamConstants.START_ELEMENT:
                    // TODO implement
                    elementRead++;
                    break;
                case XMLStreamConstants.END_ELEMENT:
                    elementRead--;
                    if (elementRead < 0) {
                        final EndElement endElement = event.asEndElement();
                        if (!element.getName().equals(endElement.getName())) {
                            // TODO logging message
                            throw LOGGER.logSevereException(new WebServiceException("End element does not match " + endElement));
                        }
                        break loop;
                    }
                    else {
                        break;
                    }
                default:
                    // TODO logging message
                    throw LOGGER.logSevereException(new WebServiceException("Unexpected event, was " + event));
            }
        } catch (XMLStreamException e) {
            // TODO logging message
            throw LOGGER.logSevereException(new WebServiceException("Failed to unmarshal XML document", e));
        }
    }

    // TODO implement
    return new TubelineFeature(enabled);
}
 
Example 12
Source File: StAXSchemaParser.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
public void parse(XMLEventReader input) throws XMLStreamException, XNIException {
    XMLEvent currentEvent = input.peek();
    if (currentEvent != null) {
        int eventType = currentEvent.getEventType();
        if (eventType != XMLStreamConstants.START_DOCUMENT &&
            eventType != XMLStreamConstants.START_ELEMENT) {
            throw new XMLStreamException();
        }
        fLocationWrapper.setLocation(currentEvent.getLocation());
        fSchemaDOMParser.startDocument(fLocationWrapper, null, fNamespaceContext, null);
        loop: while (input.hasNext()) {
            currentEvent = input.nextEvent();
            eventType = currentEvent.getEventType();
            switch (eventType) {
            case XMLStreamConstants.START_ELEMENT:
                ++fDepth;
                StartElement start = currentEvent.asStartElement();
                fillQName(fElementQName, start.getName());
                fLocationWrapper.setLocation(start.getLocation());
                fNamespaceContext.setNamespaceContext(start.getNamespaceContext());
                fillXMLAttributes(start);
                fillDeclaredPrefixes(start);
                addNamespaceDeclarations();
                fNamespaceContext.pushContext();
                fSchemaDOMParser.startElement(fElementQName, fAttributes, null);
                break;
            case XMLStreamConstants.END_ELEMENT:
                EndElement end = currentEvent.asEndElement();
                fillQName(fElementQName, end.getName());
                fillDeclaredPrefixes(end);
                fLocationWrapper.setLocation(end.getLocation());
                fSchemaDOMParser.endElement(fElementQName, null);
                fNamespaceContext.popContext();
                --fDepth;
                if (fDepth <= 0) {
                    break loop;
                }
                break;
            case XMLStreamConstants.CHARACTERS:
                sendCharactersToSchemaParser(currentEvent.asCharacters().getData(), false);
                break;
            case XMLStreamConstants.SPACE:
                sendCharactersToSchemaParser(currentEvent.asCharacters().getData(), true);
                break;
            case XMLStreamConstants.CDATA:
                fSchemaDOMParser.startCDATA(null);
                sendCharactersToSchemaParser(currentEvent.asCharacters().getData(), false);
                fSchemaDOMParser.endCDATA(null);
                break;
            case XMLStreamConstants.PROCESSING_INSTRUCTION:
                ProcessingInstruction pi = (ProcessingInstruction)currentEvent;
                fillProcessingInstruction(pi.getData());
                fSchemaDOMParser.processingInstruction(pi.getTarget(), fTempString, null);
                break;
            case XMLStreamConstants.DTD:
                /* There shouldn't be a DTD in the schema */
                break;
            case XMLStreamConstants.ENTITY_REFERENCE:
                /* Not needed for schemas */
                break;
            case XMLStreamConstants.COMMENT:
                /* No point in sending comments */
                break;
            case XMLStreamConstants.START_DOCUMENT:
                fDepth++;
                /* We automatically call startDocument before the loop */
                break;
            case XMLStreamConstants.END_DOCUMENT:
                /* We automatically call endDocument after the loop */
                break;
            }
        }
        fLocationWrapper.setLocation(null);
        fNamespaceContext.setNamespaceContext(null);
        fSchemaDOMParser.endDocument(null);
    }
}
 
Example 13
Source File: SearchHandle.java    From java-client-api with Apache License 2.0 4 votes vote down vote up
private void handleMatch(XMLEventReader reader, StartElement element)
  throws XMLStreamException
{
  MatchLocationImpl location = new MatchLocationImpl(getAttribute(element, "path"));

  QName highlightName = new QName(SEARCH_NS, "highlight");

  StringBuilder buf = new StringBuilder();

  // assumes that highlight elements do not nest
  QName matchName = element.getName();
  events: while (reader.hasNext()) {
    XMLEvent event = reader.nextEvent();
    tempEvents.add(event);

    int eventType = event.getEventType();
    switch (eventType) {
      case XMLStreamConstants.START_ELEMENT:
        StartElement startElement = event.asStartElement();
        if (highlightName.equals(startElement.getName())) {
          // add any text preceding a highlight
          if (buf.length() > 0) {
            location.addMatchSnippet(new MatchSnippetImpl(false, buf.toString()));
            buf.setLength(0);
          }
        }
        break;
      case XMLStreamConstants.CDATA:
      case XMLStreamConstants.CHARACTERS:
        buf.append(event.asCharacters().getData());
        break;
      case XMLStreamConstants.END_ELEMENT:
        EndElement endElement = event.asEndElement();

        QName endName = endElement.getName();
        if (matchName.equals(endName)) {
          // add any text following the last highlight
          if (buf.length() > 0) {
            location.addMatchSnippet(new MatchSnippetImpl(false, buf.toString()));
          }
          break events;
        } else if (highlightName.equals(endName)) {
          // add any text contained by a highlight
          location.addMatchSnippet(new MatchSnippetImpl(true, buf.toString()));
          buf.setLength(0);
        }

        break;
    }
  }
  buf = null;

  currSummary.addLocation(location);
}
 
Example 14
Source File: TubelineFeatureReader.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
private TubelineFeature parseFactories(final boolean enabled, final StartElement element, final XMLEventReader reader)
        throws WebServiceException {
    int elementRead = 0;
    loop:
    while (reader.hasNext()) {
        try {
            final XMLEvent event = reader.nextEvent();
            switch (event.getEventType()) {
                case XMLStreamConstants.COMMENT:
                    break; // skipping the comments and start document events
                case XMLStreamConstants.CHARACTERS:
                    if (event.asCharacters().isWhiteSpace()) {
                        break;
                    }
                    else {
                        // TODO: logging message
                        throw LOGGER.logSevereException(new WebServiceException("No character data allowed, was " + event.asCharacters()));
                    }
                case XMLStreamConstants.START_ELEMENT:
                    // TODO implement
                    elementRead++;
                    break;
                case XMLStreamConstants.END_ELEMENT:
                    elementRead--;
                    if (elementRead < 0) {
                        final EndElement endElement = event.asEndElement();
                        if (!element.getName().equals(endElement.getName())) {
                            // TODO logging message
                            throw LOGGER.logSevereException(new WebServiceException("End element does not match " + endElement));
                        }
                        break loop;
                    }
                    else {
                        break;
                    }
                default:
                    // TODO logging message
                    throw LOGGER.logSevereException(new WebServiceException("Unexpected event, was " + event));
            }
        } catch (XMLStreamException e) {
            // TODO logging message
            throw LOGGER.logSevereException(new WebServiceException("Failed to unmarshal XML document", e));
        }
    }

    // TODO implement
    return new TubelineFeature(enabled);
}
 
Example 15
Source File: StAXSchemaParser.java    From JDKSourceCode1.8 with MIT License 4 votes vote down vote up
public void parse(XMLEventReader input) throws XMLStreamException, XNIException {
    XMLEvent currentEvent = input.peek();
    if (currentEvent != null) {
        int eventType = currentEvent.getEventType();
        if (eventType != XMLStreamConstants.START_DOCUMENT &&
            eventType != XMLStreamConstants.START_ELEMENT) {
            throw new XMLStreamException();
        }
        fLocationWrapper.setLocation(currentEvent.getLocation());
        fSchemaDOMParser.startDocument(fLocationWrapper, null, fNamespaceContext, null);
        loop: while (input.hasNext()) {
            currentEvent = input.nextEvent();
            eventType = currentEvent.getEventType();
            switch (eventType) {
            case XMLStreamConstants.START_ELEMENT:
                ++fDepth;
                StartElement start = currentEvent.asStartElement();
                fillQName(fElementQName, start.getName());
                fLocationWrapper.setLocation(start.getLocation());
                fNamespaceContext.setNamespaceContext(start.getNamespaceContext());
                fillXMLAttributes(start);
                fillDeclaredPrefixes(start);
                addNamespaceDeclarations();
                fNamespaceContext.pushContext();
                fSchemaDOMParser.startElement(fElementQName, fAttributes, null);
                break;
            case XMLStreamConstants.END_ELEMENT:
                EndElement end = currentEvent.asEndElement();
                fillQName(fElementQName, end.getName());
                fillDeclaredPrefixes(end);
                fLocationWrapper.setLocation(end.getLocation());
                fSchemaDOMParser.endElement(fElementQName, null);
                fNamespaceContext.popContext();
                --fDepth;
                if (fDepth <= 0) {
                    break loop;
                }
                break;
            case XMLStreamConstants.CHARACTERS:
                sendCharactersToSchemaParser(currentEvent.asCharacters().getData(), false);
                break;
            case XMLStreamConstants.SPACE:
                sendCharactersToSchemaParser(currentEvent.asCharacters().getData(), true);
                break;
            case XMLStreamConstants.CDATA:
                fSchemaDOMParser.startCDATA(null);
                sendCharactersToSchemaParser(currentEvent.asCharacters().getData(), false);
                fSchemaDOMParser.endCDATA(null);
                break;
            case XMLStreamConstants.PROCESSING_INSTRUCTION:
                ProcessingInstruction pi = (ProcessingInstruction)currentEvent;
                fillProcessingInstruction(pi.getData());
                fSchemaDOMParser.processingInstruction(pi.getTarget(), fTempString, null);
                break;
            case XMLStreamConstants.DTD:
                /* There shouldn't be a DTD in the schema */
                break;
            case XMLStreamConstants.ENTITY_REFERENCE:
                /* Not needed for schemas */
                break;
            case XMLStreamConstants.COMMENT:
                /* No point in sending comments */
                break;
            case XMLStreamConstants.START_DOCUMENT:
                fDepth++;
                /* We automatically call startDocument before the loop */
                break;
            case XMLStreamConstants.END_DOCUMENT:
                /* We automatically call endDocument after the loop */
                break;
            }
        }
        fLocationWrapper.setLocation(null);
        fNamespaceContext.setNamespaceContext(null);
        fSchemaDOMParser.endDocument(null);
    }
}
 
Example 16
Source File: StAXSchemaParser.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
public void parse(XMLEventReader input) throws XMLStreamException, XNIException {
    XMLEvent currentEvent = input.peek();
    if (currentEvent != null) {
        int eventType = currentEvent.getEventType();
        if (eventType != XMLStreamConstants.START_DOCUMENT &&
            eventType != XMLStreamConstants.START_ELEMENT) {
            throw new XMLStreamException();
        }
        fLocationWrapper.setLocation(currentEvent.getLocation());
        fSchemaDOMParser.startDocument(fLocationWrapper, null, fNamespaceContext, null);
        loop: while (input.hasNext()) {
            currentEvent = input.nextEvent();
            eventType = currentEvent.getEventType();
            switch (eventType) {
            case XMLStreamConstants.START_ELEMENT:
                ++fDepth;
                StartElement start = currentEvent.asStartElement();
                fillQName(fElementQName, start.getName());
                fLocationWrapper.setLocation(start.getLocation());
                fNamespaceContext.setNamespaceContext(start.getNamespaceContext());
                fillXMLAttributes(start);
                fillDeclaredPrefixes(start);
                addNamespaceDeclarations();
                fNamespaceContext.pushContext();
                fSchemaDOMParser.startElement(fElementQName, fAttributes, null);
                break;
            case XMLStreamConstants.END_ELEMENT:
                EndElement end = currentEvent.asEndElement();
                fillQName(fElementQName, end.getName());
                fillDeclaredPrefixes(end);
                fLocationWrapper.setLocation(end.getLocation());
                fSchemaDOMParser.endElement(fElementQName, null);
                fNamespaceContext.popContext();
                --fDepth;
                if (fDepth <= 0) {
                    break loop;
                }
                break;
            case XMLStreamConstants.CHARACTERS:
                sendCharactersToSchemaParser(currentEvent.asCharacters().getData(), false);
                break;
            case XMLStreamConstants.SPACE:
                sendCharactersToSchemaParser(currentEvent.asCharacters().getData(), true);
                break;
            case XMLStreamConstants.CDATA:
                fSchemaDOMParser.startCDATA(null);
                sendCharactersToSchemaParser(currentEvent.asCharacters().getData(), false);
                fSchemaDOMParser.endCDATA(null);
                break;
            case XMLStreamConstants.PROCESSING_INSTRUCTION:
                ProcessingInstruction pi = (ProcessingInstruction)currentEvent;
                fillProcessingInstruction(pi.getData());
                fSchemaDOMParser.processingInstruction(pi.getTarget(), fTempString, null);
                break;
            case XMLStreamConstants.DTD:
                /* There shouldn't be a DTD in the schema */
                break;
            case XMLStreamConstants.ENTITY_REFERENCE:
                /* Not needed for schemas */
                break;
            case XMLStreamConstants.COMMENT:
                /* No point in sending comments */
                break;
            case XMLStreamConstants.START_DOCUMENT:
                fDepth++;
                /* We automatically call startDocument before the loop */
                break;
            case XMLStreamConstants.END_DOCUMENT:
                /* We automatically call endDocument after the loop */
                break;
            }
        }
        fLocationWrapper.setLocation(null);
        fNamespaceContext.setNamespaceContext(null);
        fSchemaDOMParser.endDocument(null);
    }
}
 
Example 17
Source File: TubelineFeatureReader.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
private TubelineFeature parseFactories(final boolean enabled, final StartElement element, final XMLEventReader reader)
        throws WebServiceException {
    int elementRead = 0;
    loop:
    while (reader.hasNext()) {
        try {
            final XMLEvent event = reader.nextEvent();
            switch (event.getEventType()) {
                case XMLStreamConstants.COMMENT:
                    break; // skipping the comments and start document events
                case XMLStreamConstants.CHARACTERS:
                    if (event.asCharacters().isWhiteSpace()) {
                        break;
                    }
                    else {
                        // TODO: logging message
                        throw LOGGER.logSevereException(new WebServiceException("No character data allowed, was " + event.asCharacters()));
                    }
                case XMLStreamConstants.START_ELEMENT:
                    // TODO implement
                    elementRead++;
                    break;
                case XMLStreamConstants.END_ELEMENT:
                    elementRead--;
                    if (elementRead < 0) {
                        final EndElement endElement = event.asEndElement();
                        if (!element.getName().equals(endElement.getName())) {
                            // TODO logging message
                            throw LOGGER.logSevereException(new WebServiceException("End element does not match " + endElement));
                        }
                        break loop;
                    }
                    else {
                        break;
                    }
                default:
                    // TODO logging message
                    throw LOGGER.logSevereException(new WebServiceException("Unexpected event, was " + event));
            }
        } catch (XMLStreamException e) {
            // TODO logging message
            throw LOGGER.logSevereException(new WebServiceException("Failed to unmarshal XML document", e));
        }
    }

    // TODO implement
    return new TubelineFeature(enabled);
}
 
Example 18
Source File: StAXSchemaParser.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
public void parse(XMLEventReader input) throws XMLStreamException, XNIException {
    XMLEvent currentEvent = input.peek();
    if (currentEvent != null) {
        int eventType = currentEvent.getEventType();
        if (eventType != XMLStreamConstants.START_DOCUMENT &&
            eventType != XMLStreamConstants.START_ELEMENT) {
            throw new XMLStreamException();
        }
        fLocationWrapper.setLocation(currentEvent.getLocation());
        fSchemaDOMParser.startDocument(fLocationWrapper, null, fNamespaceContext, null);
        loop: while (input.hasNext()) {
            currentEvent = input.nextEvent();
            eventType = currentEvent.getEventType();
            switch (eventType) {
            case XMLStreamConstants.START_ELEMENT:
                ++fDepth;
                StartElement start = currentEvent.asStartElement();
                fillQName(fElementQName, start.getName());
                fLocationWrapper.setLocation(start.getLocation());
                fNamespaceContext.setNamespaceContext(start.getNamespaceContext());
                fillXMLAttributes(start);
                fillDeclaredPrefixes(start);
                addNamespaceDeclarations();
                fNamespaceContext.pushContext();
                fSchemaDOMParser.startElement(fElementQName, fAttributes, null);
                break;
            case XMLStreamConstants.END_ELEMENT:
                EndElement end = currentEvent.asEndElement();
                fillQName(fElementQName, end.getName());
                fillDeclaredPrefixes(end);
                fLocationWrapper.setLocation(end.getLocation());
                fSchemaDOMParser.endElement(fElementQName, null);
                fNamespaceContext.popContext();
                --fDepth;
                if (fDepth <= 0) {
                    break loop;
                }
                break;
            case XMLStreamConstants.CHARACTERS:
                sendCharactersToSchemaParser(currentEvent.asCharacters().getData(), false);
                break;
            case XMLStreamConstants.SPACE:
                sendCharactersToSchemaParser(currentEvent.asCharacters().getData(), true);
                break;
            case XMLStreamConstants.CDATA:
                fSchemaDOMParser.startCDATA(null);
                sendCharactersToSchemaParser(currentEvent.asCharacters().getData(), false);
                fSchemaDOMParser.endCDATA(null);
                break;
            case XMLStreamConstants.PROCESSING_INSTRUCTION:
                ProcessingInstruction pi = (ProcessingInstruction)currentEvent;
                fillProcessingInstruction(pi.getData());
                fSchemaDOMParser.processingInstruction(pi.getTarget(), fTempString, null);
                break;
            case XMLStreamConstants.DTD:
                /* There shouldn't be a DTD in the schema */
                break;
            case XMLStreamConstants.ENTITY_REFERENCE:
                /* Not needed for schemas */
                break;
            case XMLStreamConstants.COMMENT:
                /* No point in sending comments */
                break;
            case XMLStreamConstants.START_DOCUMENT:
                fDepth++;
                /* We automatically call startDocument before the loop */
                break;
            case XMLStreamConstants.END_DOCUMENT:
                /* We automatically call endDocument after the loop */
                break;
            }
        }
        fLocationWrapper.setLocation(null);
        fNamespaceContext.setNamespaceContext(null);
        fSchemaDOMParser.endDocument(null);
    }
}
 
Example 19
Source File: TubelineFeatureReader.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
private TubelineFeature parseFactories(final boolean enabled, final StartElement element, final XMLEventReader reader)
        throws WebServiceException {
    int elementRead = 0;
    loop:
    while (reader.hasNext()) {
        try {
            final XMLEvent event = reader.nextEvent();
            switch (event.getEventType()) {
                case XMLStreamConstants.COMMENT:
                    break; // skipping the comments and start document events
                case XMLStreamConstants.CHARACTERS:
                    if (event.asCharacters().isWhiteSpace()) {
                        break;
                    }
                    else {
                        // TODO: logging message
                        throw LOGGER.logSevereException(new WebServiceException("No character data allowed, was " + event.asCharacters()));
                    }
                case XMLStreamConstants.START_ELEMENT:
                    // TODO implement
                    elementRead++;
                    break;
                case XMLStreamConstants.END_ELEMENT:
                    elementRead--;
                    if (elementRead < 0) {
                        final EndElement endElement = event.asEndElement();
                        if (!element.getName().equals(endElement.getName())) {
                            // TODO logging message
                            throw LOGGER.logSevereException(new WebServiceException("End element does not match " + endElement));
                        }
                        break loop;
                    }
                    else {
                        break;
                    }
                default:
                    // TODO logging message
                    throw LOGGER.logSevereException(new WebServiceException("Unexpected event, was " + event));
            }
        } catch (XMLStreamException e) {
            // TODO logging message
            throw LOGGER.logSevereException(new WebServiceException("Failed to unmarshal XML document", e));
        }
    }

    // TODO implement
    return new TubelineFeature(enabled);
}
 
Example 20
Source File: StAXSchemaParser.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
public void parse(XMLEventReader input) throws XMLStreamException, XNIException {
    XMLEvent currentEvent = input.peek();
    if (currentEvent != null) {
        int eventType = currentEvent.getEventType();
        if (eventType != XMLStreamConstants.START_DOCUMENT &&
            eventType != XMLStreamConstants.START_ELEMENT) {
            throw new XMLStreamException();
        }
        fLocationWrapper.setLocation(currentEvent.getLocation());
        fSchemaDOMParser.startDocument(fLocationWrapper, null, fNamespaceContext, null);
        loop: while (input.hasNext()) {
            currentEvent = input.nextEvent();
            eventType = currentEvent.getEventType();
            switch (eventType) {
            case XMLStreamConstants.START_ELEMENT:
                ++fDepth;
                StartElement start = currentEvent.asStartElement();
                fillQName(fElementQName, start.getName());
                fLocationWrapper.setLocation(start.getLocation());
                fNamespaceContext.setNamespaceContext(start.getNamespaceContext());
                fillXMLAttributes(start);
                fillDeclaredPrefixes(start);
                addNamespaceDeclarations();
                fNamespaceContext.pushContext();
                fSchemaDOMParser.startElement(fElementQName, fAttributes, null);
                break;
            case XMLStreamConstants.END_ELEMENT:
                EndElement end = currentEvent.asEndElement();
                fillQName(fElementQName, end.getName());
                fillDeclaredPrefixes(end);
                fLocationWrapper.setLocation(end.getLocation());
                fSchemaDOMParser.endElement(fElementQName, null);
                fNamespaceContext.popContext();
                --fDepth;
                if (fDepth <= 0) {
                    break loop;
                }
                break;
            case XMLStreamConstants.CHARACTERS:
                sendCharactersToSchemaParser(currentEvent.asCharacters().getData(), false);
                break;
            case XMLStreamConstants.SPACE:
                sendCharactersToSchemaParser(currentEvent.asCharacters().getData(), true);
                break;
            case XMLStreamConstants.CDATA:
                fSchemaDOMParser.startCDATA(null);
                sendCharactersToSchemaParser(currentEvent.asCharacters().getData(), false);
                fSchemaDOMParser.endCDATA(null);
                break;
            case XMLStreamConstants.PROCESSING_INSTRUCTION:
                ProcessingInstruction pi = (ProcessingInstruction)currentEvent;
                fillProcessingInstruction(pi.getData());
                fSchemaDOMParser.processingInstruction(pi.getTarget(), fTempString, null);
                break;
            case XMLStreamConstants.DTD:
                /* There shouldn't be a DTD in the schema */
                break;
            case XMLStreamConstants.ENTITY_REFERENCE:
                /* Not needed for schemas */
                break;
            case XMLStreamConstants.COMMENT:
                /* No point in sending comments */
                break;
            case XMLStreamConstants.START_DOCUMENT:
                fDepth++;
                /* We automatically call startDocument before the loop */
                break;
            case XMLStreamConstants.END_DOCUMENT:
                /* We automatically call endDocument after the loop */
                break;
            }
        }
        fLocationWrapper.setLocation(null);
        fNamespaceContext.setNamespaceContext(null);
        fSchemaDOMParser.endDocument(null);
    }
}