Search in sources :

Example 1 with StreamFilter

use of javax.xml.stream.StreamFilter in project knetbuilder by Rothamsted.

the class Parser method start.

/**
 * Parses OXL from an XMLStreamReader.
 *
 * Beware that this method wraps the parameter reader with a filter that strips all extra-whitespaces away
 * from the input (See <a href='https://goo.gl/wirWjg'>here</a> for details). This was made necessary by
 * the rdf2oxl tool in 2018, which doesn't spawn a compact XML.
 *
 * @param xmlr
 *            the stream to parse XML from
 * @throws net.sourceforge.ondex.exception.type.ParsingFailedException
 *             if the oxl xml could not be processed
 * @throws InconsistencyException
 */
public void start(XMLStreamReader xmlr) throws ParsingFailedException {
    try {
        // We need to ignore the extra-whitespaces, our parsers rely on that heavily
        xmlr = xmlif.createFilteredReader(xmlr, new StreamFilter() {

            @Override
            public boolean accept(XMLStreamReader reader) {
                return !reader.isWhiteSpace();
            }
        });
        if (this.graph.isLoadingModeSupported())
            // If the graph is empty, we can retain the IDs in the OXL (if this is supported).
            this.graph.setLoadingMode(graph.getConcepts().size() == 0);
        XmlParser parser = new XmlParser(this);
        // hashtable for id mapping old to new concept ids
        // This returns identities if we're in loading mode (see above).
        Map<Integer, Integer> table = new HashMap<>();
        Map<Integer, Set<Integer>> context = new HashMap<>();
        parser.registerParser("cv", new ConceptMetaDataParser(graph, "cv"));
        parser.registerParser("unit", new GeneralMetaDataParser(graph, "unit"));
        parser.registerParser("attrname", new GeneralMetaDataParser(graph, "attrname"));
        parser.registerParser("evidences", new GeneralMetaDataParser(graph, "evidences"));
        parser.registerParser("cc", new ConceptMetaDataParser(graph, "cc"));
        parser.registerParser("relation_type", new RelationMetaDataParser(graph, "relation_type"));
        parser.registerParser("relationtypeset", new RelationMetaDataParser(graph, "relationtypeset"));
        ConceptParser cp = new ConceptParser(graph, table, context);
        parser.registerParser("concept", cp);
        cp.setIgnoreAttributes(ignoreGDSAttributeGDS);
        RelationParser rp = new RelationParser(graph, table);
        parser.registerParser("relation", rp);
        rp.setIgnoreAttributes(ignoreGDSAttributeGDS);
        parser.parse(xmlr);
        ConceptParser.syncContext(graph, table, context);
        // catch exceptions and throw them upwards
        if (cp.errorMessages.size() > 0) {
            fireEventOccurred(new ParsingErrorEvent(cp.errorMessages.toString(), "[Parser - start]"));
            throw new ParsingFailedException(cp.errorMessages.toString());
        }
        // close reader
        xmlr.close();
    } catch (InconsistencyException | XMLStreamException | JAXBException | ClassNotFoundException | InstantiationException | IllegalAccessException e) {
        fireEventOccurred(new ParsingErrorEvent(e.getMessage(), "[Parser - start]"));
        throw new ParsingFailedException(e);
    } finally {
        if (this.graph.isLoadingMode())
            this.graph.setLoadingMode(false);
    }
}
Also used : ParsingFailedException(net.sourceforge.ondex.exception.type.ParsingFailedException) XMLStreamReader(javax.xml.stream.XMLStreamReader) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) JAXBException(javax.xml.bind.JAXBException) StreamFilter(javax.xml.stream.StreamFilter) ParsingErrorEvent(net.sourceforge.ondex.event.type.ParsingErrorEvent) XMLStreamException(javax.xml.stream.XMLStreamException) InconsistencyException(net.sourceforge.ondex.exception.type.InconsistencyException)

Example 2 with StreamFilter

use of javax.xml.stream.StreamFilter in project exist by eXist-db.

the class LocationStep method getSiblings.

/**
 * Get's the sibling nodes of the context set
 *
 * @param context         a <code>XQueryContext</code> value
 * @param contextSequence a <code>NodeSet</code> value
 * @return a <code>NodeSet</code> value
 * @throws XPathException in case of dynamic error
 */
protected Sequence getSiblings(final XQueryContext context, final Sequence contextSequence) throws XPathException {
    if (!contextSequence.isPersistentSet()) {
        final MemoryNodeSet nodes = contextSequence.toMemNodeSet();
        if (axis == Constants.PRECEDING_SIBLING_AXIS) {
            return nodes.getPrecedingSiblings(test);
        } else {
            return nodes.getFollowingSiblings(test);
        }
    }
    final NodeSet contextSet = contextSequence.toNodeSet();
    // similar way ? -pb
    if (test.getType() == Type.PROCESSING_INSTRUCTION) {
        final VirtualNodeSet vset = new VirtualNodeSet(context.getBroker(), axis, test, contextId, contextSet);
        vset.setInPredicate(Expression.NO_CONTEXT_ID != contextId);
        return vset;
    }
    if (test.isWildcardTest()) {
        final AVLTreeNodeSet result = new AVLTreeNodeSet();
        try {
            final int limit = computeLimit();
            for (final NodeProxy current : contextSet) {
                // document-node() does not have any preceding or following elements
                if (NodeId.DOCUMENT_NODE.equals(current.getNodeId())) {
                    continue;
                }
                final IEmbeddedXMLStreamReader reader;
                final StreamFilter filter;
                if (axis == Constants.PRECEDING_SIBLING_AXIS) {
                    final NodeId startNodeId;
                    if (NodeId.DOCUMENT_NODE.equals(current.getNodeId().getParentId())) {
                        // parent would be document-node(), start from document-node()/node()[1]
                        startNodeId = NodeId.ROOT_NODE;
                    } else {
                        startNodeId = current.getNodeId().getParentId().getChild(1);
                    }
                    final NodeProxy startNode = new NodeProxy(current.getOwnerDocument(), startNodeId);
                    reader = context.getBroker().getXMLStreamReader(startNode, false);
                    filter = new PrecedingSiblingFilter(test, startNode, current, result, contextId);
                } else {
                    reader = context.getBroker().getXMLStreamReader(current, false);
                    filter = new FollowingSiblingFilter(test, current, result, contextId, limit);
                }
                reader.filter(filter);
            }
        } catch (final IOException | XMLStreamException e) {
            throw new XPathException(this, e);
        }
        return result;
    } else {
        // TODO : no test on preloaded data ?
        final DocumentSet docs = getDocumentSet(contextSet);
        synchronized (context) {
            if (currentSet == null || currentDocs == null || !(docs.equalDocs(currentDocs))) {
                final StructuralIndex index = context.getBroker().getStructuralIndex();
                if (context.getProfiler().isEnabled()) {
                    context.getProfiler().message(this, Profiler.OPTIMIZATIONS, "OPTIMIZATION", "Using structural index '" + index.toString() + "'");
                }
                currentSet = index.findElementsByTagName(ElementValue.ELEMENT, docs, test.getName(), null, this);
                currentDocs = docs;
                registerUpdateListener();
            }
            switch(axis) {
                case Constants.PRECEDING_SIBLING_AXIS:
                    return currentSet.selectPrecedingSiblings(contextSet, contextId);
                case Constants.FOLLOWING_SIBLING_AXIS:
                    return currentSet.selectFollowingSiblings(contextSet, contextId);
                default:
                    throw new IllegalArgumentException("Unsupported axis specified");
            }
        }
    }
}
Also used : InMemoryNodeSet(org.exist.dom.memtree.InMemoryNodeSet) InMemoryNodeSet(org.exist.dom.memtree.InMemoryNodeSet) StructuralIndex(org.exist.indexing.StructuralIndex) IOException(java.io.IOException) StreamFilter(javax.xml.stream.StreamFilter) XMLStreamException(javax.xml.stream.XMLStreamException) NodeId(org.exist.numbering.NodeId)

Example 3 with StreamFilter

use of javax.xml.stream.StreamFilter in project exist by eXist-db.

the class LocationStep method getPrecedingOrFollowing.

/**
 * Get the preceding or following axis nodes
 *
 * @param context the xquery context
 * @param contextSequence the context sequence
 *
 * @return the nodes from the preceding or following axis
 *
 * @throws XPathException if an error occurs
 */
private Sequence getPrecedingOrFollowing(final XQueryContext context, final Sequence contextSequence) throws XPathException {
    final int position = computeLimit();
    // process an in-memory node set
    if (!contextSequence.isPersistentSet()) {
        final MemoryNodeSet nodes = contextSequence.toMemNodeSet();
        if (position > -1) {
            applyPredicate = false;
        }
        if (axis == Constants.PRECEDING_AXIS) {
            return nodes.getPreceding(test, position);
        } else {
            return nodes.getFollowing(test, position);
        }
    }
    final NodeSet contextSet = contextSequence.toNodeSet();
    // similar way ? -pb
    if (test.getType() == Type.PROCESSING_INSTRUCTION) {
        final VirtualNodeSet vset = new VirtualNodeSet(context.getBroker(), axis, test, contextId, contextSet);
        vset.setInPredicate(Expression.NO_CONTEXT_ID != contextId);
        return vset;
    }
    // handle node(), * etc.
    if (test.isWildcardTest()) {
        try {
            final NodeSet result = new NewArrayNodeSet();
            for (final NodeProxy next : contextSet) {
                final NodeList cl = next.getOwnerDocument().getChildNodes();
                for (int j = 0; j < cl.getLength(); j++) {
                    final NodeHandle node = (NodeHandle) cl.item(j);
                    final NodeProxy root = new NodeProxy(node);
                    final StreamFilter filter;
                    if (axis == Constants.PRECEDING_AXIS) {
                        filter = new PrecedingFilter(test, root, next, result, contextId);
                    } else {
                        filter = new FollowingFilter(test, root, next, result, contextId, position);
                    }
                    final IEmbeddedXMLStreamReader reader = context.getBroker().getXMLStreamReader(root, false);
                    reader.filter(filter);
                }
            }
            return result;
        } catch (final XMLStreamException | IOException e) {
            throw new XPathException(this, e);
        }
    } else {
        // TODO : no test on preloaded data ?
        final DocumentSet docs = getDocumentSet(contextSet);
        synchronized (context) {
            if (currentSet == null || currentDocs == null || !(docs.equalDocs(currentDocs))) {
                final StructuralIndex index = context.getBroker().getStructuralIndex();
                if (context.getProfiler().isEnabled()) {
                    context.getProfiler().message(this, Profiler.OPTIMIZATIONS, "OPTIMIZATION", "Using structural index '" + index.toString() + "'");
                }
                currentSet = index.findElementsByTagName(ElementValue.ELEMENT, docs, test.getName(), null, this);
                currentDocs = docs;
                registerUpdateListener();
            }
            if (position > -1) {
                try {
                    applyPredicate = false;
                    if (axis == Constants.PRECEDING_AXIS) {
                        return currentSet.selectPreceding(contextSet, position, contextId);
                    } else {
                        return currentSet.selectFollowing(contextSet, position, contextId);
                    }
                } catch (final UnsupportedOperationException e) {
                    if (axis == Constants.PRECEDING_AXIS) {
                        return currentSet.selectPreceding(contextSet, contextId);
                    } else {
                        return currentSet.selectFollowing(contextSet, contextId);
                    }
                }
            } else {
                if (axis == Constants.PRECEDING_AXIS) {
                    return currentSet.selectPreceding(contextSet, contextId);
                } else {
                    return currentSet.selectFollowing(contextSet, contextId);
                }
            }
        }
    }
}
Also used : InMemoryNodeSet(org.exist.dom.memtree.InMemoryNodeSet) InMemoryNodeSet(org.exist.dom.memtree.InMemoryNodeSet) StructuralIndex(org.exist.indexing.StructuralIndex) NodeList(org.w3c.dom.NodeList) IOException(java.io.IOException) StreamFilter(javax.xml.stream.StreamFilter) XMLStreamException(javax.xml.stream.XMLStreamException)

Aggregations

StreamFilter (javax.xml.stream.StreamFilter)3 XMLStreamException (javax.xml.stream.XMLStreamException)3 IOException (java.io.IOException)2 InMemoryNodeSet (org.exist.dom.memtree.InMemoryNodeSet)2 StructuralIndex (org.exist.indexing.StructuralIndex)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 JAXBException (javax.xml.bind.JAXBException)1 XMLStreamReader (javax.xml.stream.XMLStreamReader)1 ParsingErrorEvent (net.sourceforge.ondex.event.type.ParsingErrorEvent)1 InconsistencyException (net.sourceforge.ondex.exception.type.InconsistencyException)1 ParsingFailedException (net.sourceforge.ondex.exception.type.ParsingFailedException)1 NodeId (org.exist.numbering.NodeId)1 NodeList (org.w3c.dom.NodeList)1