Examples of XMLStreamReader2


Examples of org.codehaus.stax2.XMLStreamReader2

         * PIs and comments outside of root node), we must start with
         * START_DOCUMENT event.
         */
        boolean wholeDoc = (r.getEventType() == XMLStreamConstants.START_DOCUMENT);

        XMLStreamReader2 sr = Stax2ReaderAdapter.wrapIfNecessary(r);
        QNameRecycler recycler = new QNameRecycler();
        boolean nsAware = _isNamespaceAware(sr);
        Node current = doc; // At top level

    main_loop:
        for (int evtType = sr.getEventType(); true; evtType = sr.next()) {
            Node child;

            switch (evtType) {
            case XMLStreamConstants.CDATA:
                child = doc.createCDATASection(sr.getText());
                break;

            case XMLStreamConstants.SPACE:
                if (_inputCfgIgnoreWs) {
                    continue main_loop;
                }
                /* Oh great. DOM is brain-dead in that ignorable white space
                 * can not be added, even though it is legal, and often
                 * reported by StAX/SAX impls...
                 */
                if (current == doc) { // better just ignore, thus...
                    continue;
                }
                // fall through

            case XMLStreamConstants.CHARACTERS:
                child = doc.createTextNode(sr.getText());
                break;

            case XMLStreamConstants.COMMENT:
                child = doc.createComment(sr.getText());
                break;

            case XMLStreamConstants.END_DOCUMENT:
                break main_loop;

            case XMLStreamConstants.END_ELEMENT:
                current = current.getParentNode();
                if (current == null || current == doc) {
                    /* 19-Nov-2010, tatu: If the root element closed, we now need
                     *    to bail out UNLESS we are building "whole document"
                     *    (in which case still need to get possible PIs, comments)
                     */
                    if (!wholeDoc) {
                        break main_loop;
                    }
                }
                continue main_loop;

            case XMLStreamConstants.ENTITY_DECLARATION:
            case XMLStreamConstants.NOTATION_DECLARATION:
                /* Shouldn't really get these, but maybe some stream readers
                 * do provide the info. If so, better ignore it -- DTD event
                 * should have most/all we need.
                 */
                continue main_loop;

            case XMLStreamConstants.ENTITY_REFERENCE:
                child = doc.createEntityReference(sr.getLocalName());
                break;

            case XMLStreamConstants.PROCESSING_INSTRUCTION:
                child = doc.createProcessingInstruction(sr.getPITarget(), sr.getPIData());
                break;

            case XMLStreamConstants.START_ELEMENT:
                // Ok, need to add a new element...
                {
                    String ln = sr.getLocalName();
                    Element newElem;

                    if (nsAware) {
                        String qname = sr.getPrefixedName();
                        newElem = doc.createElementNS(sr.getNamespaceURI(), qname);
                    } else { // if non-ns-aware, things are simpler:
                        newElem = doc.createElement(ln);
                    }

                    /* Silly old DOM: must mix in namespace declarations
                     * in there...
                     */
                    for (int i = 0, len = sr.getNamespaceCount(); i < len; ++i) {
                        String prefix = sr.getNamespacePrefix(i);
                        String qname;
                        if (prefix == null || prefix.length() == 0) {
                            qname = "xmlns";
                        } else {
                            qname = recycler.getQualified("xmlns", prefix);
                        }
                        newElem.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, qname,  sr.getNamespaceURI(i));
                    }

                    // And then the attributes:
                    for (int i = 0, len = sr.getAttributeCount(); i < len; ++i) {
                        ln = sr.getAttributeLocalName(i);
                        if (nsAware) {
                            String prefix = sr.getAttributePrefix(i);
                            if (prefix != null && prefix.length() > 0) {
                                ln = recycler.getQualified(prefix, ln);
                            }
                            newElem.setAttributeNS(sr.getAttributeNamespace(i), ln, sr.getAttributeValue(i));
                        } else {
                            newElem.setAttribute(ln, sr.getAttributeValue(i));
                        }
                    }
                    // And then 'push' new element...
                    current.appendChild(newElem);
                    current = newElem;
                    continue main_loop;
                }

            case XMLStreamConstants.START_DOCUMENT:
                /* This should only be received at the beginning of document...
                 * so, should we indicate the problem or not?
                 */
                /* For now, let it pass: maybe some (broken) readers pass
                 * that info as first event in beginning of doc?
                 */
                continue main_loop;

            case XMLStreamConstants.DTD:
                /* !!! Note: StAX does not expose enough information about
                 *  doctype declaration (specifically, public and system id!);
                 *  (altough StAX2 would...)
                 *
                 * Worse, DOM1/2 do not specify a way to create the DocType
                 * node, even if StAX provided it. This is pretty silly,
                 * all in all.
                 */
                continue main_loop;

            // Should never get these, from a stream reader:
               
            /* (commented out entries are just FYI; default catches
             * them all)
             */

            //case XMLStreamConstants.ATTRIBUTE:
            //case XMLStreamConstants.NAMESPACE:
            default:
                throw new XMLStreamException("Unrecognized iterator event type: "+sr.getEventType()+"; should not receive such types (broken stream reader?)");
            }

            if (child != null) {
                current.appendChild(child);
            }
View Full Code Here

Examples of org.codehaus.stax2.XMLStreamReader2

    data.scenarioAliases = new HashMap<>();
    data.groupAliases = new HashMap<>();
   
    if(aliasDefinitionsFile.exists()) {
      try(Reader fileReader = new UnicodeReader(aliasDefinitionsFile, defaultFileCharacterEncoding)) {
        XMLStreamReader2 streamReader = XmlStreamReaderFactory.createReader(fileReader, aliasDefinitionsSchema);
        XmlStreamCursor cursor = new XmlStreamCursor(streamReader);
       
        while(cursor.isWithinInitialNode()) {
          if(streamReader.getEventType() == XMLStreamReader.START_ELEMENT) {
            switch(streamReader.getLocalName()) {
              case "alias":
                processAlias(streamReader, data, aliasDefinitionsFile, assetLocation);
                break;
             
              case "group":
View Full Code Here

Examples of org.codehaus.stax2.XMLStreamReader2

public class XmlStreamReaderFactory {
  private static final XMLInputFactory2 inputFactory = new WstxInputFactory();
 
  public static XMLStreamReader2 createReader(Reader fileReader, XMLValidationSchema xmlSchema) throws XMLStreamException {
    XMLStreamReader2 streamReader = (XMLStreamReader2) inputFactory.createXMLStreamReader(fileReader);
    streamReader.validateAgainst(xmlSchema);
   
    return streamReader;
  }
View Full Code Here

Examples of org.codehaus.stax2.XMLStreamReader2

    aliasesData.aliasOverrides = new ArrayList<>();
    aliasesData.groupNames = new ArrayList<>();
   
    if(aliasesFile.exists()) {
      try(Reader fileReader = new UnicodeReader(aliasesFile, defaultFileCharacterEncoding)) {
        XMLStreamReader2 streamReader = XmlStreamReaderFactory.createReader(fileReader, aliasesSchema);
       
        while(streamReader.hasNext()) {
          if(streamReader.getEventType() == XMLStreamReader.START_ELEMENT) {
            switch(streamReader.getLocalName()) {
              case "aliases":
                processAliases(streamReader, aliasesData);
                break;
             
              case "alias":
                processAlias(streamReader, aliasesData, aliasesFile);
                break;
            }
          }
         
          streamReader.next();
        }
      }
      catch (XMLStreamException e) {
        Location location = e.getLocation();
       
View Full Code Here

Examples of org.codehaus.stax2.XMLStreamReader2

        // Gosh, this is bad, but I don't know a better solution, unless we're willing
        // to require the stax2 API no matter what.
        if (reader instanceof DepthXMLStreamReader) {
            reader = ((DepthXMLStreamReader)reader).getReader();
        }
        XMLStreamReader2 reader2 = (XMLStreamReader2)reader;
        XMLValidationSchema vs = getValidator(schemas);
        reader2.validateAgainst(vs);


    }
View Full Code Here

Examples of org.codehaus.stax2.XMLStreamReader2

    }

    public void parse(InputStream inputStream) {
        final NodeContext context = new NodeContext();
        try {
            XMLStreamReader2 xmlr = (XMLStreamReader2) factory.createXMLStreamReader(inputStream);
            String curElement = "";
            int eventType = xmlr.getEventType();
            while (xmlr.hasNext()) {
                eventType = xmlr.next();

                switch (eventType) {
                case XMLEvent.START_ELEMENT:
                    curElement = curElement + SLASH + getName(xmlr);
                    processStartElement(xmlr, context, curElement);
                    break;

                case XMLEvent.CHARACTERS:
                    context.updateText(curElement,
                        new String(xmlr.getTextCharacters(), xmlr.getTextStart(), xmlr.getTextLength()));
                    break;

                case XMLEvent.END_ELEMENT:
                    processEndElement(xmlr, context, curElement);
                    curElement = curElement.substring(0, curElement.lastIndexOf(SLASH + getName(xmlr)));
View Full Code Here

Examples of org.codehaus.stax2.XMLStreamReader2

    */
   public PackageType unmarshal(URL packageXml) throws Exception
   {

      XMLInputFactory2 xmlFactory = (XMLInputFactory2) XMLInputFactory2.newInstance();
      XMLStreamReader2 xmlStreamReader = (XMLStreamReader2) xmlFactory.createXMLStreamReader(packageXml.openStream());

      // create a validator for the package.xml
      XMLValidationSchemaFactory validationSchemaFactory = XMLValidationSchemaFactory
            .newInstance(XMLValidationSchema.SCHEMA_ID_W3C_SCHEMA);
      // TODO: Is this good enough to get hold of package.xsd? Need to think about this.
      URL packageXsd = Thread.currentThread().getContextClassLoader().getResource("package.xsd");
      XMLValidationSchema schema = validationSchemaFactory.createSchema(packageXsd);;
      // enable validation (note: validation will happen during parse)
      xmlStreamReader.validateAgainst(schema);

      // parse the xml
      PackageType pkgMetadata = null;
      while (xmlStreamReader.hasNext())
      {
         int event = xmlStreamReader.next();
         if (event == XMLEvent.START_ELEMENT && xmlStreamReader.getLocalName().equals("package"))
         {
            pkgMetadata = processPackage(xmlStreamReader);
         }
      }
      return pkgMetadata;
View Full Code Here

Examples of org.codehaus.stax2.XMLStreamReader2

        throws IOException, XMLStreamException
    {
        final String INTERNAL_SUBSET = "<!--test-->";
       
        String DOC = "<?xml version='1.0'?><!DOCTYPE root ["+INTERNAL_SUBSET+"]> <root />";
        XMLStreamReader2 sr = createReader(DOC, encoding);
        assertTokenType(START_DOCUMENT, sr.getEventType());
        int t = sr.next();
        assertTokenType(DTD, t);
        if (!skip) {
            assertEquals("root", sr.getPrefixedName());
            assertEquals(INTERNAL_SUBSET, sr.getText());
        }
        assertTokenType(START_ELEMENT, sr.next());
        assertEquals("root", sr.getLocalName());
        assertTokenType(END_ELEMENT, sr.next());
        assertEquals("root", sr.getLocalName());
        assertTokenType(END_DOCUMENT, sr.next());
        sr.close();
    }
View Full Code Here

Examples of org.codehaus.stax2.XMLStreamReader2

    private void _doTestInvalidDup(String encoding, boolean skip)
        throws IOException, XMLStreamException
    {
        String DOC = "<?xml version='1.0'?><!DOCTYPE root>  <!DOCTYPE root>";
        XMLStreamReader2 sr = createReader(DOC, encoding);
        assertTokenType(START_DOCUMENT, sr.getEventType());
        int t = sr.next();
        assertTokenType(DTD, t);
        if (!skip) {
            assertEquals("root", sr.getPrefixedName());
            sr.getText(); // just to force its parsing
        }
        // But second one should fail
        try {
            t = sr.next(); // to get second DTD
            fail("Should fail on invalid DOCTYPE declaration: instead got "+t);
        } catch (XMLStreamException e) {
            verifyException(e, "Duplicate DOCTYPE declaration");
        }
        sr.close();
    }
View Full Code Here

Examples of org.codehaus.stax2.XMLStreamReader2

   
    public void _testNamespaces(boolean useBytes) throws Exception
    {
        // note: must specify encoding so parsers knows which decoder to use
        String DOC = "<root xmlns='abc' xmlns:a='b' xmlns:b='c'>\n</root>";
        XMLStreamReader2 sr = createReader(DOC, "UTF-8", useBytes);
        assertTokenType(START_DOCUMENT, sr.getEventType());
        assertTokenType(START_ELEMENT, sr.next());
        assertEquals("root", sr.getLocalName());
        assertEquals(3, sr.getNamespaceCount());
       
        /* Although Stax does not mandate that ordering of namespace
         * declarations is preserved, ideally we would want to have them
         * that way...
         */
        assertEquals("", sr.getNamespacePrefix(0));
        assertEquals("abc", sr.getNamespaceURI(0));
        assertEquals("a", sr.getNamespacePrefix(1));
        assertEquals("b", sr.getNamespaceURI(1));
        assertEquals("b", sr.getNamespacePrefix(2));
        assertEquals("c", sr.getNamespaceURI(2));

        // Aalto follows Woodstox, and by default reports token start loc for "getLocation()"
        Location loc = sr.getLocation();
        assertEquals(1, loc.getLineNumber());
        assertEquals(1, loc.getColumnNumber());
        assertEquals(0, loc.getCharacterOffset());

        // and end should differ a bit
        loc = sr.getLocationInfo().getEndLocation();
        assertEquals(1, loc.getLineNumber());
        assertEquals(43,  loc.getColumnNumber());
        assertEquals(42, loc.getCharacterOffset());
       
        assertTokenType(CHARACTERS, sr.next());
        assertEquals("\n", sr.getText());

        loc = sr.getLocation();
        assertEquals(1, loc.getLineNumber());
        assertEquals(43,  loc.getColumnNumber());
        assertEquals(42, loc.getCharacterOffset());
        loc = sr.getLocationInfo().getEndLocation();
        assertEquals(2, loc.getLineNumber());
        assertEquals(1,  loc.getColumnNumber());
        assertEquals(43, loc.getCharacterOffset());
       
        assertTokenType(END_ELEMENT, sr.next());
        assertEquals("root", sr.getLocalName());
        assertEquals(useBytes ? -1L : 43L, sr.getLocationInfo().getStartingCharOffset());
        assertEquals(useBytes ? 43L : -1L, sr.getLocationInfo().getStartingByteOffset());
        assertEquals(useBytes ? -1L : 50L, sr.getLocationInfo().getEndingCharOffset());
        assertEquals(useBytes ? 50L : -1L, sr.getLocationInfo().getEndingByteOffset());
       
        loc = sr.getLocation();
        assertEquals(1, loc.getColumnNumber());
        assertEquals(2, loc.getLineNumber());
        assertEquals(43, loc.getCharacterOffset());

        assertTokenType(END_DOCUMENT, sr.next());
        loc = sr.getLocation();
        assertEquals(2, loc.getLineNumber());
        assertEquals(8, loc.getColumnNumber());
        assertEquals(50, loc.getCharacterOffset());
        sr.close();
    }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. 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.