Package org.codehaus.stax2

Examples of org.codehaus.stax2.XMLStreamReader2


     * is encountered.
     */
    protected void skipToEndElement()
        throws XMLStreamException
    {
        XMLStreamReader2 sr = _streamReader;
        /* Here we have two choices: first, depth of current START_ELEMENT should
         * match that of matching END_ELEMENT. Additionally, START_ELEMENT's depth
         * for hierarchic cursors must be baseDepth+1.
         */
        //int endDepth = sr.getDepth();
        int endDepth = _baseDepth+1;

        while (true) {
            int type = sr.next();
            if (type == XMLStreamConstants.END_ELEMENT) {
                int depth = sr.getDepth();
                if (depth > endDepth) {
                    continue;
                }
                if (depth != endDepth) { // sanity check
                    _throwWrongEndElem(endDepth, depth);
View Full Code Here


        /* Base depth to match is always known by the child in question,
         * so let's ask it (hierarchic cursor parent also knows it)
         */
        final int endDepth = child.getBaseParentCount();
        final XMLStreamReader2 sr = _streamReader;

        for (int type = sr.getEventType(); true; type = sr.next()) {
            if (type == XMLStreamConstants.END_ELEMENT) {
                int depth = sr.getDepth();
                if (depth > endDepth) {
                    continue;
                }
                if (depth != endDepth) { // sanity check
                    _throwWrongEndElem(endDepth, depth);
View Full Code Here

    }
   
    private SMInputCursor _rootCursor(boolean wrap, String XML) throws Exception
    {   
        XMLStreamReader sr = getStaxInputFactory().createXMLStreamReader(new StringReader(XML));
        XMLStreamReader2 sr2;
        if (wrap || !(sr instanceof XMLStreamReader2)) {
            sr2 = forceWrapping(sr);
        } else {
            sr2 = (XMLStreamReader2) sr;
        }
View Full Code Here

         * 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

    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

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

    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

        // 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

    }

    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

    */
   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

TOP

Related Classes of org.codehaus.stax2.XMLStreamReader2

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.