public XQuery(CommandBuilder builder, Config config, Command parent, Command child, MorphlineContext context) throws SaxonApiException, IOException, XMLStreamException {
super(builder, config, parent, child, context);
List<? extends Config> fragmentConfigs = getConfigs().getConfigList(config, "fragments");
if (fragmentConfigs.size() == 0) {
throw new MorphlineCompilationException("At least one fragment must be defined", config);
}
if (fragmentConfigs.size() > 1) {
throw new MorphlineCompilationException("More than one fragment is not yet supported", config);
}
for (Config fragment : fragmentConfigs) {
String fragmentPath = getConfigs().getString(fragment, "fragmentPath");
if (!fragmentPath.equals("/")) {
throw new MorphlineCompilationException("Non-root fragment paths are not yet supported", config);
}
XQueryCompiler compiler = processor.newXQueryCompiler();
compiler.setErrorListener(new DefaultErrorListener());
compiler.setCompileWithTracing(isTracing);
compiler.setLanguageVersion(getConfigs().getString(config, "languageVersion", "1.0"));
XQueryExecutable executable = null;
String query = getConfigs().getString(fragment, "queryString", null);
if (query != null) {
executable = compiler.compile(query);
}
String queryFile = getConfigs().getString(fragment, "queryFile", null);
if (queryFile != null) {
executable = compiler.compile(new File(queryFile));
}
if (query == null && queryFile == null) {
throw new MorphlineCompilationException("Either query or queryFile must be defined", config);
}
if (query != null && queryFile != null) {
throw new MorphlineCompilationException("Must not define both query and queryFile at the same time", config);
}
XQueryEvaluator evaluator = executable.load();
Config variables = getConfigs().getConfig(fragment, "externalVariables", ConfigFactory.empty());
for (Map.Entry<String, Object> entry : new Configs().getEntrySet(variables)) {