net.sf.saxon.s9api.XQueryCompiler Java Examples

The following examples show how to use net.sf.saxon.s9api.XQueryCompiler. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: XQueryProcessor.java    From spark-xml-utils with Apache License 2.0 5 votes vote down vote up
/**
 * Set the namespaces in the XQueryCompiler.
 * 
 * @param xqueryCompiler
 * @param namespaceMappings Namespace prefix to Namespace uri mappings
 */
private  void setPrefixNamespaceMappings(XQueryCompiler xqueryCompiler, HashMap<String,String> namespaceMappings) {

	if (namespaceMappings != null) {
		
		// Get the mappings
		Set<Entry<String, String>> mappings = namespaceMappings.entrySet();

		// If mappings exist, set the namespaces
		if (mappings != null) {
		
			Iterator<Entry<String, String>> it = mappings.iterator();
			while (it.hasNext()) {
				Entry<String, String> entry = it.next();
				xqueryCompiler.declareNamespace(entry.getKey(),entry.getValue());
			}
		
		}	
		
	}
	
	// Add in the defaults
	xqueryCompiler.declareNamespace("xml",NamespaceConstant.XML);
	xqueryCompiler.declareNamespace("xs",NamespaceConstant.SCHEMA);
	xqueryCompiler.declareNamespace("fn",NamespaceConstant.FN);
	
}
 
Example #2
Source File: SaxonRunnerApp.java    From intellij-xquery with Apache License 2.0 5 votes vote down vote up
@Override
public void runApp() throws Exception {
    doAdditionalConfiguration(processor);
    Serializer out = prepareSerializer();
    XQueryCompiler compiler = processor.newXQueryCompiler();
    XQueryExecutable executable = compiler.compile(new File(config.getMainFile()));
    setMainModule(executable.getUnderlyingCompiledQuery().getMainModule());
    XQueryEvaluator evaluator = executable.load();
    bindContextItem(evaluator);
    bindVariables(evaluator);
    evaluator.run(out);
}
 
Example #3
Source File: XQueryProcessor.java    From spark-xml-utils with Apache License 2.0 4 votes vote down vote up
/**
 * Initialization to improve performance for repetitive invocations of evaluate expressions
 * 
 * @throws XQueryException
 */
private void init() throws XQueryException {
	
	try {
		
		// Get the processor
		proc = new Processor(false);

		// Set any specified configuration properties for the processor
		if (featureMappings != null) {
			for (Entry<String, Object> entry : featureMappings.entrySet()) {
				proc.setConfigurationProperty(entry.getKey(), entry.getValue());
			}
		}
		
		// Get the XQuery compiler
		XQueryCompiler xqueryCompiler = proc.newXQueryCompiler();
		xqueryCompiler.setEncoding(CharEncoding.UTF_8);

		// Set the namespace to prefix mappings
		this.setPrefixNamespaceMappings(xqueryCompiler, namespaceMappings);

		// Compile the XQuery expression and get an XQuery evaluator
		exp = xqueryCompiler.compile(xQueryExpression);
		eval = exp.load();
		
		// Create and initialize the serializer 
		baos = new ByteArrayOutputStream();
		serializer = proc.newSerializer(baos);
		// Appears ok to always set output property to xml (even if we are just returning a text string)
		serializer.setOutputProperty(Serializer.Property.METHOD, "xml");
		serializer.setOutputProperty(Serializer.Property.OMIT_XML_DECLARATION,"yes");
		serializer.setProcessor(proc);
		
	} catch (SaxonApiException e) {
		
		log.error("Problems creating an XQueryProcessor.  " + e.getMessage(),e);
		throw new XQueryException(e.getMessage());

	}
	
}