Package org.exist.stax

Examples of org.exist.stax.EmbeddedXMLStreamReader


     */
    public ErrorReport checkDocument(final DocumentImpl doc) {
        final DOMFile domDb = ( (NativeBroker)broker ).getDOMFile();
        return (ErrorReport)new DOMTransaction( this, domDb, Lock.WRITE_LOCK, doc ) {
            public Object start() {
                EmbeddedXMLStreamReader reader = null;
                try {
                    final ElementImpl root = (ElementImpl)doc.getDocumentElement();
                    if (root == null) {
                        return new ErrorReport.ResourceError(ErrorReport.RESOURCE_ACCESS_FAILED, "Failed to access document data");
                    }
                } catch( final Exception e ) {
                    e.printStackTrace();
                    return( new ErrorReport.ResourceError( org.exist.backup.ErrorReport.RESOURCE_ACCESS_FAILED, e.getMessage(), e ) );
                } finally {
                    if (reader != null) {
                        try {
                            reader.close();
                        } catch (XMLStreamException e) {
                            e.printStackTrace();
                        }
                    }
                }
View Full Code Here


    public ErrorReport checkXMLTree( final DocumentImpl doc )
    {
        final DOMFile domDb = ( (NativeBroker)broker ).getDOMFile();
        return( (ErrorReport)new DOMTransaction( this, domDb, Lock.WRITE_LOCK, doc ) {
                    public Object start() {
                        EmbeddedXMLStreamReader reader = null;
                        try {
                            final ElementImpl             root            = (ElementImpl)doc.getDocumentElement();
                            reader = broker.getXMLStreamReader( root, true );
                            NodeId                  nodeId;
                            boolean                 attribsAllowed  = false;
                            int                     expectedAttribs = 0;
                            int                     attributeCount  = 0;

                            while( reader.hasNext() ) {
                                final int status = reader.next();

                                nodeId = (NodeId)reader.getProperty( EmbeddedXMLStreamReader.PROPERTY_NODE_ID );
                                ElementNode parent = null;

                                if( ( status != XMLStreamReader.END_ELEMENT ) && !elementStack.isEmpty() ) {
                                    parent = elementStack.peek();
                                    parent.childCount++;

                                    // test parent-child relation
                                    if( !nodeId.isChildOf( parent.elem.getNodeId() ) ) {
                                        return( new ErrorReport.ResourceError( ErrorReport.NODE_HIERARCHY, "Node " + nodeId + " is not a child of " + parent.elem.getNodeId() ) );
                                    }

                                    // test sibling relation
                                    if( ( parent.prevSibling != null ) && !( nodeId.isSiblingOf( parent.prevSibling ) && ( nodeId.compareTo( parent.prevSibling ) > 0 ) ) ) {
                                        return( new ErrorReport.ResourceError( ErrorReport.INCORRECT_NODE_ID, "Node " + nodeId + " is not a sibling of " + parent.prevSibling ) );
                                    }
                                    parent.prevSibling = nodeId;
                                }

                                switch( status ) {

                                    case XMLStreamReader.ATTRIBUTE: {
                                        attributeCount++;
                                        break;
                                    }

                                    case XMLStreamReader.END_ELEMENT: {
                                        if( elementStack.isEmpty() ) {
                                            return( new org.exist.backup.ErrorReport.ResourceError( ErrorReport.NODE_HIERARCHY, "Error in node hierarchy: received END_ELEMENT event " + "but stack was empty!" ) );
                                        }
                                        final ElementNode lastElem = elementStack.pop();
                                        if( lastElem.childCount != lastElem.elem.getChildCount() ) {
                                            return( new ErrorReport.ResourceError( org.exist.backup.ErrorReport.NODE_HIERARCHY, "Element reports incorrect child count: expected " + lastElem.elem.getChildCount() + " but found " + lastElem.childCount ) );
                                        }
                                        break;
                                    }

                                    case XMLStreamReader.START_ELEMENT: {
                                        if( nodeId.getTreeLevel() <= defaultIndexDepth ) {

                                            // check dom.dbx btree, which maps the node
                                            // id to the node's storage address
                                            // look up the node id and check if the
                                            // returned storage address is correct
                                            final NativeBroker.NodeRef nodeRef = new NativeBroker.NodeRef( doc.getDocId(), nodeId );

                                            try {
                                                final long p = domDb.findValue( nodeRef );

                                                if( p != reader.getCurrentPosition() ) {
                                                    final Value v = domDb.get( p );

                                                    if( v == null ) {
                                                        return( new ErrorReport.IndexError( ErrorReport.DOM_INDEX, "Failed to access node " + nodeId + " through dom.dbx index. Wrong storage address. Expected: " + p + "; got: " + reader.getCurrentPosition() + " - ", doc.getDocId() ) );
                                                    }
                                                }
                                            }
                                            catch( final Exception e ) {
                                                e.printStackTrace();
                                                return( new ErrorReport.IndexError( ErrorReport.DOM_INDEX, "Failed to access node " + nodeId + " through dom.dbx index.", e, doc.getDocId() ) );
                                            }
                                        }

                                        final StoredNode node = reader.getNode();
                                        if( node.getNodeType() != Node.ELEMENT_NODE ) {
                                            return( new org.exist.backup.ErrorReport.ResourceError( ErrorReport.INCORRECT_NODE_TYPE, "Expected an element node, received node of type " + node.getNodeType() ) );
                                        }
                                        elementStack.push( new ElementNode( (ElementImpl)node ) );
                                        attribsAllowed  = true;
                                        attributeCount  = 0;
                                        expectedAttribs = reader.getAttributeCount();
                                        break;
                                    }

                                    default: {
                                        if( attribsAllowed ) {

                                            if( attributeCount != expectedAttribs ) {
                                                return( new org.exist.backup.ErrorReport.ResourceError( ErrorReport.INCORRECT_NODE_TYPE, "Wrong number of attributes. Expected: " + expectedAttribs + "; found: " + attributeCount ) );
                                            }
                                        }
                                        attribsAllowed = false;
                                        break;
                                    }
                                }
                            }

                            if( !elementStack.isEmpty() ) {
                                return( new org.exist.backup.ErrorReport.ResourceError( ErrorReport.NODE_HIERARCHY, "Error in node hierarchy: reached end of tree but " + "stack was not empty!" ) );
                            }
                            return( null );
                        }
                        catch( final IOException e ) {
                            e.printStackTrace();
                            return( new org.exist.backup.ErrorReport.ResourceError( ErrorReport.RESOURCE_ACCESS_FAILED, e.getMessage(), e ) );
                        }
                        catch( final XMLStreamException e ) {
                            e.printStackTrace();
                            return( new ErrorReport.ResourceError( org.exist.backup.ErrorReport.RESOURCE_ACCESS_FAILED, e.getMessage(), e ) );
                        }
                        finally {
                            elementStack.clear();
                            if (reader != null) {
                                try {
                                    reader.close();
                                } catch (XMLStreamException e) {
                                    e.printStackTrace();
                                }
                            }
                        }
View Full Code Here

            {filter = new PrecedingSiblingFilter(test, current,
                result, contextId);}
          else
            {filter = new FollowingSiblingFilter(test, current,
                result, contextId);}
          final EmbeddedXMLStreamReader reader = context.getBroker()
              .getXMLStreamReader(parent, false);
          reader.filter(filter);
        }
      } catch (final IOException e) {
        throw new XPathException(this, e);
      } catch (final XMLStreamException e) {
        throw new XPathException(this, e);
View Full Code Here

          for (int j = 0; j < cl.getLength(); j++) {
            final StoredNode node = (StoredNode) cl.item(j);
            final NodeProxy root = new NodeProxy(node);
            final PrecedingFilter filter = new PrecedingFilter(test,
                next, result, contextId);
            final EmbeddedXMLStreamReader reader = context.getBroker()
                .getXMLStreamReader(root, false);
            reader.filter(filter);
          }
        }
        return result;
      } catch (final XMLStreamException e) {
        throw new XPathException(this, e);
View Full Code Here

          for (int j = 0; j < cl.getLength(); j++) {
            final StoredNode node = (StoredNode) cl.item(j);
            final NodeProxy root = new NodeProxy(node);
            final FollowingFilter filter = new FollowingFilter(test,
                next, result, contextId);
            final EmbeddedXMLStreamReader reader = context.getBroker()
                .getXMLStreamReader(root, false);
            reader.filter(filter);
          }
        }
        return result;
      } catch (final XMLStreamException e) {
        throw new XPathException(this, e);
View Full Code Here

    public NodeSet directSelectAttribute(DBBroker broker, org.exist.xquery.NodeTest test, int contextId) {
        if (nodeType != UNKNOWN_NODE_TYPE && nodeType != Node.ELEMENT_NODE)
            {return NodeSet.EMPTY_SET;}
        try {
            NewArrayNodeSet result = null;
            final EmbeddedXMLStreamReader reader = broker.getXMLStreamReader(this, true);
            int status = reader.next();
            if (status != XMLStreamReader.START_ELEMENT)
                {return NodeSet.EMPTY_SET;}
            final int attrs = reader.getAttributeCount();
            for (int i = 0; i < attrs; i++) {
                status = reader.next();
                if (status != XMLStreamReader.ATTRIBUTE)
                    {break;}
                final AttrImpl attr = (AttrImpl) reader.getNode();
                if (test.matches(attr)) {
                    final NodeProxy child = new NodeProxy(attr);
                    if (Expression.NO_CONTEXT_ID != contextId)
                        {child.addContextNode(contextId, this);}
                    else
View Full Code Here

    public boolean directMatchAttribute(DBBroker broker, org.exist.xquery.NodeTest test, int contextId) {
        if (nodeType != UNKNOWN_NODE_TYPE && nodeType != Node.ELEMENT_NODE)
            {return false;}
        try {
            final EmbeddedXMLStreamReader reader = broker.getXMLStreamReader(this, true);
            int status = reader.next();
            if (status != XMLStreamReader.START_ELEMENT)
                {return false;}
            final int attrs = reader.getAttributeCount();
            for (int i = 0; i < attrs; i++) {
                status = reader.next();
                if (status != XMLStreamReader.ATTRIBUTE)
                    {break;}
                final AttrImpl attr = (AttrImpl) reader.getNode();
                if (test.matches(attr)) {
                  return true;
                }
            }
            return false;
View Full Code Here

                    LOG.error("report me", bte);
                }
            } while (parentPointer == KEY_NOT_FOUND);
            try {
                final NodeProxy parent = new NodeProxy(doc, nodeID, parentPointer);
                final EmbeddedXMLStreamReader cursor = broker.getXMLStreamReader(parent, true);
                while(cursor.hasNext()) {
                    final int status = cursor.next();
                    if (status != XMLStreamReader.END_ELEMENT) {
                        final NodeId nextId = (NodeId) cursor.getProperty(EmbeddedXMLStreamReader.PROPERTY_NODE_ID);
                        if (nextId.equals(node.getNodeId())) {
                            return cursor.getCurrentPosition();
                        }
                    }
                }
                if (LOG.isDebugEnabled())
                    {LOG.debug("Node " + node.getNodeId() + " could not be found. Giving up. This is usually not an error.");}
View Full Code Here

        extractor.configure(config, idxConf);
        OffsetList offsets = new OffsetList();
        int level = 0;
        int textOffset = 0;
        try {
            EmbeddedXMLStreamReader reader = broker.getXMLStreamReader(p, false);
            while (reader.hasNext()) {
                int ev = reader.next();
                switch (ev) {
                    case XMLStreamConstants.END_ELEMENT:
                        if (--level < 0) {
                            break;
                        }
                        textOffset += extractor.endElement(reader.getQName());
                        break;
                    case XMLStreamConstants.START_ELEMENT:
                        ++level;
                        textOffset += extractor.startElement(reader.getQName());
                        break;
                    case XMLStreamConstants.CHARACTERS:
                        NodeId nodeId = (NodeId) reader.getProperty(ExtendedXMLStreamReader.PROPERTY_NODE_ID);
                        textOffset += extractor.beforeCharacters();
                        offsets.add(textOffset, nodeId);
                        textOffset += extractor.characters(reader.getXMLText());
                        break;
                }
            }
        } catch (IOException | XMLStreamException e) {
            LOG.warn("Problem found while serializing XML: " + e.getMessage(), e);
View Full Code Here

        }
        return changes;
    }

    protected DiffNode[] getNodes(DBBroker broker, DocumentImpl root) throws XMLStreamException, IOException {
        EmbeddedXMLStreamReader reader = broker.newXMLStreamReader(new NodeProxy(root, NodeId.DOCUMENT_NODE, root.getFirstChildAddress()), false);
        List<DiffNode> nodes = new ArrayList<DiffNode>();
        DiffNode node;
        while (reader.hasNext()) {
            int status = reader.next();
            NodeId nodeId = (NodeId) reader.getProperty(EmbeddedXMLStreamReader.PROPERTY_NODE_ID);
            switch (status) {
                case XMLStreamReader.START_ELEMENT:
                    node = new DiffNode(nodeId, status, reader.getQName());
                    nodes.add(node);

                    for (int i = 0; i < reader.getAttributeCount(); i++) {
                        nodeId = reader.getAttributeId(i);
                        String value = reader.getAttributeQName(i).getStringValue() + '=' +
                                reader.getAttributeValue(i);
                        node = new DiffNode(nodeId, XMLStreamReader.ATTRIBUTE, value);
                        nodes.add(node);
                    }
                    break;
                case XMLStreamReader.END_ELEMENT:
                    node = new DiffNode(nodeId, status, reader.getQName());
                    nodes.add(node);
                    break;
                case XMLStreamReader.CHARACTERS:
                case XMLStreamReader.COMMENT:
                    node = new DiffNode(nodeId, status, reader.getText());
                    nodes.add(node);
                    break;
                case XMLStreamReader.PROCESSING_INSTRUCTION:
                    String value = reader.getPITarget() + " " + reader.getPIData();
                    nodes.add(new DiffNode(nodeId, status, value));
                    break;
            }
        }
        DiffNode[] array = new DiffNode[nodes.size()];
View Full Code Here

TOP

Related Classes of org.exist.stax.EmbeddedXMLStreamReader

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.