Package net.sf.saxon.s9api

Examples of net.sf.saxon.s9api.XdmNode


        ProcessMatch matcher = null;

        matcher = new ProcessMatch(runtime, this);
        matcher.match(source.read(), getOption(_match));

        XdmNode tree = matcher.getResult();
        result.write(tree);
    }
View Full Code Here


    }

    public void run() throws SaxonApiException {
        super.run();

        XdmNode doc = null;

        try {
            VerifierFactory vfactory = new com.sun.msv.verifier.jarv.TheFactoryImpl();
            // FIXME: VerifierFactory.newInstance(language);

            Verifier verifier = null;
            XdmNode schemaNode = schema.read();
            InputSource schemaSource = S9apiUtils.xdmToInputSource(runtime, schemaNode);
            schemaSource.setSystemId(schemaNode.getBaseURI().toASCIIString());

            Schema docSchema = vfactory.compileSchema(schemaSource);
            verifier = docSchema.newVerifier();
            verifier.setErrorHandler(new RNGErrorHandler());
View Full Code Here

                errorCode = new QName(cpfx == null ? "" : cpfx, cns, codeNameStr);
            }
        }

        while (report.moreDocuments()) {
            XdmNode doc = report.read();
            XdmSequenceIterator iter = doc.axisIterator(Axis.DESCENDANT, c_error);
            if (iter.hasNext()) {
                while (iter.hasNext()) {
                    runtime.warning(this, doc, iter.next().getStringValue());
                }
            } else {
                runtime.warning(this, doc, doc.getStringValue());
            }
        }

        if (errorCode != null) {
            throw new XProcException(errorCode, "error code?");
        }

        while (source.moreDocuments()) {
            XdmNode doc = source.read();
            logger.trace(MessageFormatter.nodeMessage(step.getNode(),
                    "ReportErrors step " + step.getName() + " read " + doc.getDocumentURI()));
            result.write(doc);
        }
    }
View Full Code Here

    }

    public void run() throws SaxonApiException {
        super.run();

        XdmNode doc = source.read();

        try {
            Sink sink = new Sink();
            StreamProcessor sp = new StreamProcessor(RdfaParser.connect(sink));

            // HACK!!!
            // FIXME: set serializer properties appropriately!
            Serializer serializer = makeSerializer();
            StringWriter writer = new StringWriter();
            serializer.setOutputWriter(writer);
            S9apiUtils.serialize(runtime, doc, serializer);
            writer.close();

            ByteArrayInputStream bais = new ByteArrayInputStream(writer.toString().getBytes("UTF-8"));
            sp.process(bais, doc.getBaseURI().toASCIIString());
        } catch (IOException e) {
            throw new XProcException(e);
        } catch (ParseException e) {
            throw new XProcException(e);
        }
View Full Code Here

        @Override
        public void endStream() throws ParseException {
            tree.addEndElement();
            tree.endDocument();
            if (count > 0) {
                XdmNode out = tree.getResult();
                result.write(out);
            }
        }
View Full Code Here

            count += 1;
            if (count >= limit) {
                tree.addEndElement();
                tree.endDocument();

                XdmNode out = tree.getResult();
                result.write(out);

                tree = new TreeWriter(runtime);
                tree.startDocument(step.getNode().getBaseURI());
                tree.addStartElement(sem_triples);
View Full Code Here

    }

    public void run() throws SaxonApiException {
        super.run();

        XdmNode stylesheet = stylesheetPipe.read();
        if (stylesheet == null) {
            throw XProcException.dynamicError(6, step.getNode(), "No stylesheet provided.");
        }

        Vector<XdmNode> defaultCollection = new Vector<XdmNode> ();

        while (sourcePipe.moreDocuments()) {
            defaultCollection.add(sourcePipe.read());
        }

        XdmNode document = null;
        if (defaultCollection.size() > 0) {
            document = defaultCollection.firstElement();
        }

        String version = null;
        if (getOption(_version) == null) {
            XdmNode ssroot = S9apiUtils.getDocumentElement(stylesheet);
            version = ssroot.getAttributeValue(new QName("","version"));
            if (version == null) {
                version = ssroot.getAttributeValue(new QName("http://www.w3.org/1999/XSL/Transform","version"));
            }
            if (version == null) {
                version = "2.0"; // WTF?
            }
        } else {
            version = getOption(_version).getString();
        }
       
        if ("3.0".equals(version) && Configuration.softwareEdition.toLowerCase().equals("he")) {
            throw XProcException.stepError(38, "XSLT version '" + version + "' is not supported (Saxon PE or EE processor required).");
        }
       
        // We used to check if the XSLT version was supported, but I've removed that check.
        // If it's not supported by Saxon, we'll get an error from Saxon. Otherwise, we'll
        // get the results we get.

        if ("1.0".equals(version) && defaultCollection.size() > 1) {
            throw XProcException.stepError(39);
        }
       
        if ("1.0".equals(version) && runtime.getUseXslt10Processor()) {
            run10(stylesheet, document);
            return;
        }

        QName initialMode = null;
        QName templateName = null;
        String outputBaseURI = null;

        RuntimeValue opt = getOption(_initial_mode);
        if (opt != null) {
            initialMode = opt.getQName();
        }

        opt = getOption(_template_name);
        if (opt != null) {
            templateName = opt.getQName();
        }

        opt = getOption(_output_base_uri);
        if (opt != null) {
            outputBaseURI = opt.getString();
        }

        Processor processor = runtime.getProcessor();
        Configuration config = processor.getUnderlyingConfiguration();

        runtime.getConfigurer().getSaxonConfigurer().configXSLT(config);

        OutputURIResolver uriResolver = config.getOutputURIResolver();
        CollectionURIResolver collectionResolver = config.getCollectionURIResolver();
        UnparsedTextURIResolver unparsedTextURIResolver = runtime.getResolver();

        config.setOutputURIResolver(new OutputResolver());
        config.setCollectionURIResolver(new CollectionResolver(runtime, defaultCollection, collectionResolver));

        XdmDestination result = null;
        try {
            XsltCompiler compiler = runtime.getProcessor().newXsltCompiler();
            compiler.setSchemaAware(processor.isSchemaAware());
            XsltExecutable exec = compiler.compile(stylesheet.asSource());
            XsltTransformer transformer = exec.load();

            for (QName name : params.keySet()) {
                RuntimeValue v = params.get(name);
                if (runtime.getAllowGeneralExpressions()) {
                    transformer.setParameter(name, v.getValue());
                } else {
                    transformer.setParameter(name, new XdmAtomicValue(v.getString()));
                }
            }

            if (document != null) {
                transformer.setInitialContextNode(document);
            }
            transformer.setMessageListener(new CatchMessages());
            result = new XdmDestination();
            transformer.setDestination(result);

            if (initialMode != null) {
                transformer.setInitialMode(initialMode);
            }

            if (templateName != null) {
                transformer.setInitialTemplate(templateName);
            }

            if (outputBaseURI != null) {
                transformer.setBaseOutputURI(outputBaseURI);
                // The following hack works around https://saxonica.plan.io/issues/1724
                try {
                    result.setBaseURI(new URI(outputBaseURI));
                } catch (URISyntaxException use) {
                    // whatever
                }
            }

            transformer.setSchemaValidationMode(ValidationMode.DEFAULT);
            transformer.getUnderlyingController().setUnparsedTextURIResolver(unparsedTextURIResolver);
            transformer.transform();
        } finally {
            config.setOutputURIResolver(uriResolver);
            config.setCollectionURIResolver(collectionResolver);
        }

        XdmNode xformed = result.getXdmNode();

        // Can be null when nothing is written to the principle result tree...
        if (xformed != null) {
            if (document != null
                && (xformed.getBaseURI() == null
                    || "".equals(xformed.getBaseURI().toASCIIString()))) {
                String sysId = document.getBaseURI().toASCIIString();
                xformed.getUnderlyingNode().setSystemId(sysId);
            }
            resultPipe.write(xformed);
        }
    }
View Full Code Here

            DOMResult result = new DOMResult();
            is = S9apiUtils.xdmToInputSource(runtime, document);
            transformer.transform(new SAXSource(is), result);

            DocumentBuilder xdmBuilder = runtime.getConfiguration().getProcessor().newDocumentBuilder();
            XdmNode xformed = xdmBuilder.build(new DOMSource(result.getNode()));

            // Can be null when nothing is written to the principle result tree...
            if (xformed != null) {
                if (document != null
                        && (xformed.getBaseURI() == null
                        || "".equals(xformed.getBaseURI().toASCIIString()))) {
                    String sysId = document.getBaseURI().toASCIIString();
                    xformed.getUnderlyingNode().setSystemId(sysId);
                }
                resultPipe.write(xformed);
            }
        } catch (SaxonApiException sae) {
            throw new XProcException(sae);
View Full Code Here

    private static void removeNamespacesWriter(TreeWriter tree, XdmNode node, HashSet<String> excludeNS, boolean preserveUsed) {
        if (node.getNodeKind() == XdmNodeKind.DOCUMENT) {
            XdmSequenceIterator iter = node.axisIterator(Axis.CHILD);
            while (iter.hasNext()) {
                XdmNode cnode = (XdmNode) iter.next();
                removeNamespacesWriter(tree, cnode, excludeNS, preserveUsed);
            }
        } else if (node.getNodeKind() == XdmNodeKind.ELEMENT) {
            boolean usesDefaultNS = ("".equals(node.getNodeName().getPrefix())
                                     && !"".equals(node.getNodeName().getNamespaceURI()));

            NodeInfo inode = node.getUnderlyingNode();
            int nscount = 0;
            Iterator<NamespaceBinding> nsiter = NamespaceIterator.iterateNamespaces(inode);
            while (nsiter.hasNext()) {
                nscount++;
                nsiter.next();
            }

            boolean excludeDefault = false;
            boolean changed = false;
            NamespaceBinding newNS[] = null;
            if (nscount > 0) {
                newNS = new NamespaceBinding[nscount];
                int newpos = 0;
                nsiter = NamespaceIterator.iterateNamespaces(inode);
                while (nsiter.hasNext()) {
                    NamespaceBinding ns = nsiter.next();
                    String pfx = ns.getPrefix();
                    String uri = ns.getURI();

                    boolean delete = excludeNS.contains(uri);
                    excludeDefault = excludeDefault || ("".equals(pfx) && delete);

                    // You can't exclude the default namespace if it's in use
                    if ("".equals(pfx) && usesDefaultNS && preserveUsed) {
                        delete = false;
                    }

                    changed = changed || delete;

                    if (!delete) {
                        newNS[newpos++] = ns;
                    }
                }
                NamespaceBinding onlyNewNS[] = new NamespaceBinding[newpos];
                for (int pos = 0; pos < newpos; pos++) {
                    onlyNewNS[pos] = newNS[pos];
                }
                newNS = onlyNewNS;
            }

            NodeName newName = new NameOfNode(inode);
            if (!preserveUsed) {
                NamespaceBinding binding = newName.getNamespaceBinding();
                if (excludeNS.contains(binding.getURI())) {
                    newName = new FingerprintedQName("", "", newName.getLocalPart());
                }
            }

            tree.addStartElement(newName, inode.getSchemaType(), newNS);

            if (!preserveUsed) {
                // In this case, we may need to change some attributes too
                XdmSequenceIterator attriter = node.axisIterator(Axis.ATTRIBUTE);
                while (attriter.hasNext()) {
                    XdmNode attr = (XdmNode) attriter.next();
                    String attrns = attr.getNodeName().getNamespaceURI();
                    if (excludeNS.contains(attrns)) {
                        tree.addAttribute(new QName(attr.getNodeName().getLocalName()), attr.getStringValue());
                    } else {
                        tree.addAttribute(attr);
                    }
                }
            } else {
                tree.addAttributes(node);
            }

            XdmSequenceIterator iter = node.axisIterator(Axis.CHILD);
            while (iter.hasNext()) {
                XdmNode cnode = (XdmNode) iter.next();
                removeNamespacesWriter(tree, cnode, excludeNS, preserveUsed);
            }
            tree.addEndElement();
        } else {
            tree.addSubtree(node);
View Full Code Here

        NodeInfo treeNode = tree.getUnderlyingNode();
        System.err.println(message);
        System.err.println("Dumping tree: " + treeNode.getSystemId() + ", " + tree.getBaseURI());
        XdmSequenceIterator iter = tree.axisIterator(Axis.CHILD);
        while (iter.hasNext()) {
            XdmNode child = (XdmNode) iter.next();
            dumpTreeNode(child, "  ");
        }
    }
View Full Code Here

TOP

Related Classes of net.sf.saxon.s9api.XdmNode

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.