/** * Build a document from a supplied XML file * * @param file the supplied file * @return the XdmNode representing the root of the document tree * @throws SaxonApiException if any failure occurs retrieving or parsing the document */ public XdmNode build(File file) throws SaxonApiException { return build(new StreamSource(file)); }
@Override public void process(final InputStream rawIn) throws IOException { try (final InputStream in = new BufferedInputStream(rawIn)) { XQueryEvaluator qe = slashExpression.load(); qe.setSource(new SAXSource(xmlReader, new InputSource(in))); DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance(); dfactory.setNamespaceAware(true); Document dom = dfactory.newDocumentBuilder().newDocument(); qe.run(new DOMDestination(dom)); XdmNode rootNode = proc.newDocumentBuilder().wrap(dom); sourceRef.set(rootNode); } catch (final Exception e) { error.set(e); } } });
/** * Create a DocumentBuilder. A DocumentBuilder is used to load source XML documents. * * @return a newly created DocumentBuilder */ /*@NotNull*/ public DocumentBuilder newDocumentBuilder() { return new DocumentBuilder(config); }
private XdmNode buildXdmNode(Source s, URI baseURI) throws SaxonApiException { DocumentBuilder builder = processor.newDocumentBuilder(); builder.setBaseURI(baseURI); return builder.build(s); }
/** * As xdm node. * * @param processor * the processor * @param source * the source * @return the xdm node */ private static XdmNode asXdmNode(Processor processor, Source source) { DocumentBuilder builder = processor.newDocumentBuilder(); builder.setDTDValidation(false); builder.setLineNumbering(true); try { return builder.build(source); } catch (SaxonApiException sae) { // TODO better exception handling throw new RuntimeException(sae.getMessage(), sae); } }
/** * Set the source document for the query. * <p>If the source is an instance of {@link net.sf.saxon.om.NodeInfo}, the supplied node is used * directly as the context node of the query.</p> * <p>If the source is an instance of {@link javax.xml.transform.dom.DOMSource}, the DOM node identified * by the DOMSource is wrapped as a Saxon node, and this is then used as the context item</p> * <p>In all other cases a new Saxon tree is built, by calling * {@link net.sf.saxon.s9api.DocumentBuilder#build(javax.xml.transform.Source)}, and the document * node of this tree is then used as the context item for the query.</p> * * @param source the source document to act as the initial context item for the query. */ public void setSource(Source source) throws SaxonApiException { if (source instanceof NodeInfo) { setContextItem(new XdmNode((NodeInfo) source)); } else if (source instanceof DOMSource) { setContextItem(processor.newDocumentBuilder().wrap(source)); } else { setContextItem(processor.newDocumentBuilder().build(source)); } }
documentBuilder.setDTDValidation(false); XdmNode xdmNode = documentBuilder.build(source);
public PiperackApplication(XProcConfiguration config, XProcRuntime runtime) throws SaxonApiException { this.config = config; globalRuntime = runtime; InputStream instream = PiperackApplication.class.getResourceAsStream("/etc/serializepr.xsl"); if (instream == null) { System.err.println("Error: cannot load /etc/serializepr.xsl from jar file"); System.exit(-1); } else { SAXSource source = new SAXSource(new InputSource(instream)); DocumentBuilder builder = config.getProcessor().newDocumentBuilder(); builder.setLineNumbering(true); builder.setBaseURI(URI.create("http://xmlcalabash.com/ns/piperack")); xsl = builder.build(source); } for (String name : config.piperackDefaultPipelines.keySet()) { PipelineSource src = config.piperackDefaultPipelines.get(name); loadPipeline(src.uri, name, src.expires); } setStatusService(new BaseStatus(globalRuntime)); }
private XdmNode parse(InputStream instream, URI baseURI) throws SaxonApiException { try { XMLReader reader = XMLReaderFactory.createXMLReader(); reader.setEntityResolver(runtime.getResolver()); SAXSource source = new SAXSource(reader, new InputSource(instream)); DocumentBuilder builder = runtime.getProcessor().newDocumentBuilder(); builder.setLineNumbering(true); builder.setDTDValidation(false); builder.setBaseURI(baseURI); return builder.build(source); } catch (SAXException se) { throw new XProcException(se); } }
DocumentBuilder builder = processor.newDocumentBuilder(); if (exp.isUpdateQuery()) { builder.setTreeModel(TreeModel.LINKED_TREE); builder.setDocumentProjectionQuery(exp); if (explain) { exp.getUnderlyingCompiledQuery().explainPathMap(); builder.setDTDValidation(getConfiguration().getBooleanProperty(Feature.DTD_VALIDATION)); if (getConfiguration().getBooleanProperty(Feature.DTD_VALIDATION_RECOVERABLE)) { sourceInput = new AugmentedSource(sourceInput, getConfiguration().getParseOptions()); XdmNode doc = builder.build(sourceInput); evaluator.setContextItem(doc);
int validationMode = getConfiguration().getSchemaValidationMode(); if (validationMode == Validation.STRICT) { builder.setSchemaValidator(processor.getSchemaManager().newSchemaValidator()); } else if (validationMode == Validation.LAX) { SchemaValidator validator = processor.getSchemaManager().newSchemaValidator(); validator.setLax(true); builder.setSchemaValidator(validator); builder.setDTDValidation(getConfiguration().getBooleanProperty(Feature.DTD_VALIDATION)); builder.setWhitespaceStrippingPolicy(sheet.getWhitespaceStrippingPolicy()); if (getConfiguration().getBooleanProperty(Feature.DTD_VALIDATION_RECOVERABLE)) { source = new AugmentedSource(source, getConfiguration().getParseOptions()); XdmNode node = builder.build(source); transformer.setGlobalContextItem(node, true); source = node.asSource();
public XdmNode readXML(String href, String base) { Source source = null; href = URIUtils.encode(href); try { URI baseURI = new URI(base); source = new SAXSource(new InputSource(baseURI.resolve(href).toASCIIString())); } catch (URISyntaxException use) { throw new XProcException(use); } // No resolver, we don't have one yet DocumentBuilder builder = cfgProcessor.newDocumentBuilder(); builder.setLineNumbering(true); try { return builder.build(source); } catch (SaxonApiException sae) { throw new XProcException(XProcConstants.dynamicError(11), sae); } }
@Override public void reset() { try { writer = builder.newBuildingStreamWriter(); } catch (SaxonApiException e) { // unlikely to happen since this already succeeded once, and we can't throw a checked exception here throw new LuxException(e); } }
public XdmNode parse(InputSource isource) { try { // Make sure the builder uses our entity resolver XMLReader reader = XMLReaderFactory.createXMLReader(); reader.setEntityResolver(this); SAXSource source = new SAXSource(reader, isource); DocumentBuilder builder = runtime.getProcessor().newDocumentBuilder(); builder.setLineNumbering(true); builder.setDTDValidation(false); return builder.build(source); } catch (SaxonApiException sae) { String msg = sae.getMessage(); if (msg.contains("validation")) { throw XProcException.stepError(27, sae); } else if (msg.contains("HTTP response code: 403 ")) { throw XProcException.dynamicError(21); } else { throw XProcException.dynamicError(11, sae); } } catch (SAXException e) { throw new XProcException(e); } }
/** * Set the source document for the query. * <p>If the source is an instance of {@link net.sf.saxon.om.NodeInfo}, the supplied node is used * directly as the context node of the query.</p> * <p>If the source is an instance of {@link javax.xml.transform.dom.DOMSource}, the DOM node identified * by the DOMSource is wrapped as a Saxon node, and this is then used as the context item</p> * <p>In all other cases a new Saxon tree is built, by calling * {@link net.sf.saxon.s9api.DocumentBuilder#build(javax.xml.transform.Source)}, and the document * node of this tree is then used as the context item for the query.</p> * * @param source the source document to act as the initial context item for the query. */ public void setSource(Source source) throws SaxonApiException { if (source instanceof NodeInfo) { setContextItem(new XdmNode((NodeInfo) source)); } else if (source instanceof DOMSource) { setContextItem(processor.newDocumentBuilder().wrap(source)); } else { setContextItem(processor.newDocumentBuilder().build(source)); } }
documentBuilder.setDTDValidation(false); XdmNode xdmNode = documentBuilder.build(source);
public PiperackApplication(XProcConfiguration config, XProcRuntime runtime) throws SaxonApiException { this.config = config; globalRuntime = runtime; InputStream instream = PiperackApplication.class.getResourceAsStream("/etc/serializepr.xsl"); if (instream == null) { System.err.println("Error: cannot load /etc/serializepr.xsl from jar file"); System.exit(-1); } else { SAXSource source = new SAXSource(new InputSource(instream)); DocumentBuilder builder = config.getProcessor().newDocumentBuilder(); builder.setLineNumbering(true); builder.setBaseURI(URI.create("http://xmlcalabash.com/ns/piperack")); xsl = builder.build(source); } for (String name : config.piperackDefaultPipelines.keySet()) { PipelineSource src = config.piperackDefaultPipelines.get(name); loadPipeline(src.uri, name, src.expires); } setStatusService(new BaseStatus(globalRuntime)); }
private XdmNode parse(InputStream instream, URI baseURI) throws SaxonApiException { try { XMLReader reader = XMLReaderFactory.createXMLReader(); reader.setEntityResolver(runtime.getResolver()); SAXSource source = new SAXSource(reader, new InputSource(instream)); DocumentBuilder builder = runtime.getProcessor().newDocumentBuilder(); builder.setLineNumbering(true); builder.setDTDValidation(false); builder.setBaseURI(baseURI); return builder.build(source); } catch (SAXException se) { throw new XProcException(se); } }
DocumentBuilder builder = processor.newDocumentBuilder(); if (exp.isUpdateQuery()) { builder.setTreeModel(TreeModel.LINKED_TREE); builder.setDocumentProjectionQuery(exp); if (explain) { exp.getUnderlyingCompiledQuery().explainPathMap(); builder.setDTDValidation(getConfiguration().getBooleanProperty(Feature.DTD_VALIDATION)); if (getConfiguration().getBooleanProperty(Feature.DTD_VALIDATION_RECOVERABLE)) { sourceInput = new AugmentedSource(sourceInput, getConfiguration().getParseOptions()); XdmNode doc = builder.build(sourceInput); evaluator.setContextItem(doc);
int validationMode = getConfiguration().getSchemaValidationMode(); if (validationMode == Validation.STRICT) { builder.setSchemaValidator(processor.getSchemaManager().newSchemaValidator()); } else if (validationMode == Validation.LAX) { SchemaValidator validator = processor.getSchemaManager().newSchemaValidator(); validator.setLax(true); builder.setSchemaValidator(validator); builder.setDTDValidation(getConfiguration().getBooleanProperty(Feature.DTD_VALIDATION)); builder.setWhitespaceStrippingPolicy(sheet.getWhitespaceStrippingPolicy()); if (getConfiguration().getBooleanProperty(Feature.DTD_VALIDATION_RECOVERABLE)) { source = new AugmentedSource(source, getConfiguration().getParseOptions()); XdmNode node = builder.build(source); transformer.setGlobalContextItem(node, true); source = node.asSource();