Package net.sf.saxon.s9api

Examples of net.sf.saxon.s9api.XdmNode


            charset = defCharset;
        } else {
            charset = getOption(_charset).getString();
        }

        XdmNode doc = source.read();

        String escapedContent = null;
        if ("base64".equals(encoding)) {
            if (charset == null) {
                throw XProcException.stepError(10);
            }
            escapedContent = decodeBase64(doc, charset);
        } else if (encoding != null) {
            throw new XProcException(step.getNode(), "Unexpected encoding: " + encoding);
        } else {
            escapedContent = extractText(doc);
        }

        TreeWriter tree = new TreeWriter(runtime);
        tree.startDocument(doc.getBaseURI());

        XdmSequenceIterator iter = doc.axisIterator(Axis.CHILD);
        XdmNode child = (XdmNode) iter.next();
        while (child.getNodeKind() != XdmNodeKind.ELEMENT) {
            tree.addSubtree(child);
            child = (XdmNode) iter.next();
        }
        tree.addStartElement(child);
        tree.addAttributes(child);
        tree.startContent();

        if ("text/html".equals(contentType)) {
            XdmNode tagDoc = null;
            if ("tagsoup".equals(runtime.htmlParser())) {
                tagDoc = tagSoup(escapedContent);
            } else {
                tagDoc = parseHTML(escapedContent);
            }
            if (namespace == null) {
                tree.addSubtree(tagDoc);
            } else {
                remapDefaultNamespace(tree, tagDoc);
            }
        } else if ("application/json".equals(contentType) || "text/json".equals(contentType)) {
            JSONTokener jt = new JSONTokener(escapedContent);
            XdmNode jsonDoc = JSONtoXML.convert(runtime.getProcessor(), jt, runtime.jsonFlavor());
            tree.addSubtree(jsonDoc);
        } else if (!"application/xml".equals(contentType)) {
            throw XProcException.stepError(51);
        } else {
            // Put a wrapper around it so that it doesn't have to have a single root...
            escapedContent = "<wrapper>" + escapedContent + "</wrapper>";

            StringReader sr = new StringReader(escapedContent);

            // Make sure the nodes in the unescapedContent get the right base URI
            InputSource is = new InputSource(sr);
            is.setSystemId(doc.getBaseURI().toASCIIString());

            XdmNode unesc = runtime.parse(is);

            // Now ignore the wrapper that we added...
            XdmNode dummyWrapper = S9apiUtils.getDocumentElement(unesc);
            XdmSequenceIterator realNodes = dummyWrapper.axisIterator(Axis.CHILD);
            while (realNodes.hasNext()) {
                unesc = (XdmNode) realNodes.next();
                if (namespace == null) {
                    tree.addSubtree(unesc);
                } else {
View Full Code Here


            FingerprintedQName newName = new FingerprintedQName("", namespace, inode.getLocalPart());
            tree.addStartElement(newName, inode.getSchemaType(), newNS);

            XdmSequenceIterator iter = unescnode.axisIterator(Axis.ATTRIBUTE);
            while (iter.hasNext()) {
                XdmNode child = (XdmNode) iter.next();
                tree.addAttribute(child);
            }

            XdmSequenceIterator childNodes = unescnode.axisIterator(Axis.CHILD);
            while (childNodes.hasNext()) {
                XdmNode child = (XdmNode) childNodes.next();
                remapDefaultNamespace(tree, child);
            }
           
            tree.addEndElement();
        } else {
View Full Code Here

    private String extractText(XdmNode doc) {
        String content = "";
        XdmSequenceIterator iter = doc.axisIterator(Axis.CHILD);
        while (iter.hasNext()) {
            XdmNode child = (XdmNode) iter.next();
            if (child.getNodeKind() == XdmNodeKind.ELEMENT || child.getNodeKind() == XdmNodeKind.TEXT) {
                content += child.getStringValue();
            }
        }
        return content;
    }
View Full Code Here

        Parser parser = new Parser();
        parser.setEntityResolver(runtime.getResolver());
        SAXSource saxSource = new SAXSource(parser, source);
        DocumentBuilder builder = runtime.getProcessor().newDocumentBuilder();
        try {
            XdmNode doc = builder.build(saxSource);
            return doc;
        } catch (Exception e) {
            throw new XProcException(e);
        }
    }
View Full Code Here

        htmlBuilder.setEntityResolver(runtime.getResolver());
        try {
            InputSource src = new InputSource(new StringReader(text));
            Document html = htmlBuilder.parse(src);
            DocumentBuilder builder = runtime.getProcessor().newDocumentBuilder();
            XdmNode doc = builder.build(new DOMSource(html));
            return doc;
        } catch (Exception e) {
            throw new XProcException(e);
        }
    }
View Full Code Here

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

        String cookieKey = getOption(_cookies).getString();

        XdmNode doc = source.read();
        XdmNode root = S9apiUtils.getDocumentElement(doc);
        if (!c_cookies.equals(root.getNodeName())) {
            throw new XProcException(step.getNode(), "The input to cx:set-cookies must be a c:cookies document.");
        }
       
        for (XdmNode node : new AxisNodes(root, Axis.CHILD, AxisNodes.SIGNIFICANT)) {
            if (node.getNodeKind() == XdmNodeKind.ELEMENT) {
View Full Code Here

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

        while (source.moreDocuments()) {
            XdmNode doc = source.read();
            loadRdf(dataset, doc);
        }

        String href = getOption(_href).getString();
        String graphName = getOption(_graph, (String) null);
View Full Code Here

        } catch (InvocationTargetException nsme) {
            // nop; oh, well
            logger.debug(MessageFormatter.nodeMessage(step.getNode(), "Cannot reset schema cache."));
        }

        XdmNode doc = source.read();
        docBaseURI = doc.getBaseURI();

        String namespace = S9apiUtils.getDocumentElement(doc).getNodeName().getNamespaceURI();
        boolean tryNamespaces = getOption(_try_namespaces, false) && !"".equals(namespace);

        // Populate the URI cache so that URI references in schema documents will find
        // the schemas provided preferentially
        Vector<XdmNode> schemaDocuments = new Vector<XdmNode> ();
        while (schemas.moreDocuments()) {
            XdmNode schemaNode = schemas.read();
            String targetNS = schemaNode.getBaseURI().toASCIIString();
            logger.debug(MessageFormatter.nodeMessage(step.getNode(), "Caching input schema: " + targetNS));
            if (targetNS.equals(namespace)) {
                tryNamespaces = false;
            }
            schemaDocuments.add(schemaNode);
            runtime.getResolver().cache(schemaNode, schemaNode.getBaseURI());
        }

        if (tryNamespaces) {
            // Need to load one more schema
            try {
                XdmNode nsSchemaDoc = runtime.parse(namespace, doc.getBaseURI().toASCIIString(), false);
                schemaDocuments.add(nsSchemaDoc);
                runtime.getResolver().cache(nsSchemaDoc, nsSchemaDoc.getBaseURI());
            } catch (Exception e) {
                // nevermind
            }
        }


        // FIXME: HACK! Do this the right way
        for (XdmNode schemaNode : schemaDocuments) {
            InputSource schemaSource = S9apiUtils.xdmToInputSource(runtime, schemaNode);
            schemaSource.setSystemId(schemaNode.getBaseURI().toASCIIString());
            SAXSource source = new SAXSource(schemaSource);
            manager.load(source);
        }

        XdmDestination destination = new XdmDestination();
        Controller controller = new Controller(config);
        Receiver receiver = destination.getReceiver(controller.getConfiguration());
        PipelineConfiguration pipe = controller.makePipelineConfiguration();
        pipe.setRecoverFromValidationErrors(!getOption(_assert_valid,false));
        receiver.setPipelineConfiguration(pipe);

        SchemaValidator validator = manager.newSchemaValidator();
        validator.setDestination(destination);
        validator.setErrorListener(new XSDErrorHandler());

        String mode = getOption(_mode, "strict");
        validator.setLax("lax".equals(mode));

        boolean useHints = getOption(_use_location_hints, false);
        validator.setUseXsiSchemaLocation(useHints);
       
        try {
            logger.trace(MessageFormatter.nodeMessage(step.getNode(),
                    "Validating: " + doc.getBaseURI().toASCIIString()));

            validator.validate(doc.asSource());
            if (validationException != null) {
                throw (SaxonApiException) validationException;
            }
        } catch (SaxonApiException sae) {
            if (getOption(_assert_valid,false)) {
                throw new XProcException(XProcConstants.stepError(53), sae);
            }
        }
       
        XdmNode valid = destination.getXdmNode();
        result.write(valid);
    }
View Full Code Here

    private void validateWithXerces() throws SaxonApiException {
        logger.trace(MessageFormatter.nodeMessage(step.getNode(), "Validating with Xerces"));

        Vector<XdmNode> schemaDocuments = new Vector<XdmNode> ();
        while (schemas.moreDocuments()) {
            XdmNode schemaNode = schemas.read();
            schemaDocuments.add(schemaNode);
            runtime.getResolver().cache(schemaNode, schemaNode.getBaseURI());
        }

        XdmNode doc = source.read();
        docBaseURI = doc.getBaseURI();

        try {
            SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

            runtime.getConfigurer().getJaxpConfigurer().configSchemaFactory(factory);

            XdmNode schemaNode = schemaDocuments.get(0);
            InputSource is = S9apiUtils.xdmToInputSource(runtime, schemaNode);
            is.setSystemId(schemaNode.getBaseURI().toASCIIString());
            Schema schema = factory.newSchema(new SAXSource(is));
            Validator validator = schema.newValidator();
            validator.setErrorHandler(new XSDErrorHandler());

            InputSource docSource = S9apiUtils.xdmToInputSource(runtime, doc);
View Full Code Here

                logger.warn(message);
            }
        }

        while (source.moreDocuments()) {
            XdmNode doc = source.read();
            logger.trace(MessageFormatter.nodeMessage(step.getNode(),
                    "Message step " + step.getName() + " read " + doc.getDocumentURI()));
            result.write(doc);
        }
    }
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.