Package com.xmlcalabash.runtime

Examples of com.xmlcalabash.runtime.XPipeline


         }

         InputStreamUriParameterResolver resolver = new InputStreamUriParameterResolver(new XProcURIResolver(runtime));
         resolver.addResolver(new ClassPathUriResolver());
         runtime.setURIResolver(resolver);
         XPipeline pipeline = runtime.load(new Input(pipelineUri));
         return new CalabashPipeline(pipeline, runtime, resolver, legacySourceOutput);
      } catch (SaxonApiException ex) {
         // TODO: Should we log the exception here?
         throw new PipelineException(ex);
      }
View Full Code Here


         streamResolver.addResolver(new ClassPathUriResolver());
         for (URIResolver resolver: resolvers) {
            streamResolver.addResolver(resolver);
         }
         runtime.setURIResolver(streamResolver);
         XPipeline pipeline = runtime.load(new Input(pipelineUri));
         return new CalabashPipeline(pipeline, runtime, streamResolver);
      } catch (SaxonApiException ex) {
         // TODO: Should we log the exception here?
         throw new PipelineException(ex);
      }
View Full Code Here

    //  e.printStackTrace();
    //}
      trans.setInitialContextNode(source);
      XdmDestination pipeDoc = new XdmDestination();
      trans.setDestination(pipeDoc);
      XPipeline pipeline = null;
    //try {
            trans.transform();
      pipeline = runtime.use(pipeDoc.getXdmNode());
      pipeline.run();
    //} catch (SaxonApiException e) {
      // TODO Auto-generated catch block
    //  e.printStackTrace();
    //}
   
    //pipeline.getSerialization("result");
    //Serialization serial
   
    //wd = new GenericWritableDocument(runtime, null, serial);
      GenericWritableDocument wd = new GenericWritableDocument(runtime, null, null, output);
    //--where serial is pipeline.getSerialization(port)
      ReadablePipe rpipe = pipeline.readFrom("result");
    //while...
    //try {
      wd.write(rpipe.read());
    //} catch (SaxonApiException e) {
      // TODO Auto-generated catch block
View Full Code Here

         getResponse().setStatus(Status.SERVER_ERROR_SERVICE_UNAVAILABLE);
         return null;
      }
      try {
         final XPipelineContext xproc = cache.get(pipelineRef);
         XPipeline pipeline = xproc.getPipeline();
         if (pipeline.getInputs().size()>0) {
            // unsupported
            getLogger().severe("Inputs on GET methods are not supported: "+xproc.getPipeline().getInputs());
            getResponse().setStatus(Status.SERVER_ERROR_SERVICE_UNAVAILABLE);
            cache.release(xproc);
            return null;
         }
         if (pipeline.getOutputs().size()!=1) {
            // unsupported
            getLogger().severe("Multiple outputs are not supported.");
            getResponse().setStatus(Status.SERVER_ERROR_SERVICE_UNAVAILABLE);
            cache.release(xproc);
            return null;
         }
         //for (QName optionName : pipeline.getOptions()) {
         if (options!=null) {
            for (QName optionName : options) {
               getLogger().fine("Setting option: "+optionName);
               String value = getOptionValue(optionName);
               if (value!=null) {
                  getLogger().fine("Option: "+optionName+"="+value);
                  pipeline.passOption(optionName, new RuntimeValue(value));
               }
            }
         }
         final String outputPort = xproc.getPipeline().getOutputs().iterator().next();
         return new OutputRepresentation(MediaType.APPLICATION_XML) {
View Full Code Here

      synchronized (info) {
         if (info.compiled.size()>0) {
            return new XPipelineContext(location,info.compiled.remove(info.compiled.size()-1),info.timestamp);
         }
      }
      XPipeline pipeline = runtime.load(location.toString());
      if (info.timestamp<0) {
         synchronized (info) {
            info.timestamp = System.currentTimeMillis();
         }
      }
View Full Code Here

        XProcRuntime innerRuntime = new XProcRuntime(runtime);
        innerRuntime.resetExtensionFunctions();

        QName stepName = getOption(_step, (QName) null);
        XPipeline pipeline = null;
        if (XProcConstants.p_pipeline.equals(piperoot.getNodeName())
                || XProcConstants.p_declare_step.equals(piperoot.getNodeName())) {
            if (stepName != null) {
                throw new XProcException(step.getNode(), "Step option can only be used when loading a p:library");
            }
            pipeline = innerRuntime.use(pipedoc);
        } else if (XProcConstants.p_library.equals(piperoot.getNodeName())) {
            XLibrary library = innerRuntime.useLibrary(piperoot);
            if (stepName == null) {
                pipeline = library.getFirstPipeline();
            } else {
                pipeline = library.getPipeline(stepName);
            }
        }

        Set<String> inputports = pipeline.getInputs();
        Set<String> outputports = pipeline.getOutputs();

        int inputCount = 0;
        for (String port : inputports) {
            XInput input = pipeline.getInput(port);
            if (input.getParameters()) {
                // nop; it's ok for these to be unbound
            } else {
                inputCount++;
            }
        }

        boolean detailed = getOption(_detailed, false);

        if (!detailed && (inputCount > 1 || outputports.size() > 1)) {
            throw new XProcException(step.getNode(), "You must specify detailed='true' to eval pipelines with multiple inputs or outputs");
        }

        DeclareStep decl = pipeline.getDeclareStep();
        String primaryin = null;
        Iterator<String> portiter = inputports.iterator();
        while (portiter.hasNext()) {
            String port = portiter.next();
            Input input = decl.getInput(port);
            if (!input.getParameterInput() && ((inputports.size() == 1 && !input.getPrimarySet()) || input.getPrimary())) {
                primaryin = port;
            }
        }

        Hashtable<String,Vector<XdmNode>> inputs = new Hashtable<String,Vector<XdmNode>> ();
        for (ReadablePipe pipe : sources) {
            while (pipe.moreDocuments()) {
                String port = primaryin;
                XdmNode doc = pipe.read();
                XdmNode root = S9apiUtils.getDocumentElement(doc);
                if (detailed && cx_document.equals(root.getNodeName())) {
                    port = root.getAttributeValue(_port);
                    // FIXME: support exclude-inline-prefixes
                    boolean seenelem = false;
                    XdmDestination dest = new XdmDestination();
                    Vector<XdmValue> nodes = new Vector<XdmValue> ();
                    XdmSequenceIterator iter = root.axisIterator(Axis.CHILD);
                    while (iter.hasNext()) {
                        XdmNode child = (XdmNode) iter.next();
                        if (child.getNodeKind() == XdmNodeKind.ELEMENT) {
                            if (seenelem) {
                                throw new IllegalArgumentException("Not a well-formed inline document");
                            }
                            seenelem = true;
                        }
                        nodes.add(child);
                    }

                    S9apiUtils.writeXdmValue(runtime, nodes, dest, root.getBaseURI());
                    doc = dest.getXdmNode();
                }

                if (port == null) {
                    throw new XProcException(step.getNode(), "You must use cx:document for pipelines with no primary input port");
                }

                if (!inputs.containsKey(port)) {
                    inputs.put(port, new Vector<XdmNode> ());
                }

                inputs.get(port).add(doc);
            }
        }

        for (String port : inputs.keySet()) {
            if (inputports.contains(port)) {
                pipeline.clearInputs(port);
                for (XdmNode node : inputs.get(port)) {
                    pipeline.writeTo(port, node);
                }
            } else {
                throw new XProcException(step.getNode(), "Eval pipeline has no input port named '" + port + "'");
            }
        }

        if (params != null) {
            for (QName name : params.keySet()) {
                pipeline.setParameter(name, params.get(name));
            }
        }

        for (ReadablePipe pipe : options) {
            while (pipe.moreDocuments()) {
                XdmNode doc = pipe.read();
                XdmNode root = S9apiUtils.getDocumentElement(doc);

                if (!cx_options.equals(root.getNodeName())) {
                    throw new XProcException(step.getNode(), "Options port must be a cx:options document.");
                }

               
                for (XdmNode opt : new AxisNodes(runtime, root, Axis.CHILD, AxisNodes.SIGNIFICANT)) {
                    if (opt.getNodeKind() != XdmNodeKind.ELEMENT || !cx_option.equals(opt.getNodeName())) {
                        throw new XProcException(step.getNode(), "A cx:options document must only contain cx:option elements");
                    }

                    String name = opt.getAttributeValue(_name);
                    QName qname = new QName(name, opt);

                    String value = opt.getAttributeValue(_value);

                    if (name == null || value == null) {
                        throw new XProcException(step.getNode(), "A cx:option element must have name and value attributes");
                    }

                    RuntimeValue runtimeValue = new RuntimeValue(value);
                    pipeline.passOption(qname, runtimeValue);
                }
            }
        }

        pipeline.run();

        portiter = outputports.iterator();
        while (portiter.hasNext()) {
            String port = portiter.next();
            ReadablePipe rpipe = pipeline.readFrom(port);
            rpipe.canReadSequence(true);

            while (rpipe.moreDocuments()) {
                XdmNode doc = rpipe.read();
View Full Code Here

            InputSource is = new InputSource(entity.getStream());
            String base = getHostRef().toString();
            is.setSystemId(base + "/" + name);

            XdmNode doc = runtime.parse(is);
            XPipeline pipeline = runtime.use(doc);
            getPipelines().put(id, new PipelineConfiguration(runtime, pipeline, expires));
        } catch (Exception e) {
            throw new XProcException(e);
        }
View Full Code Here

        if (!getPipelines().containsKey(id)) {
            return badRequest(Status.CLIENT_ERROR_NOT_FOUND, "no pipeline: " + pipelineUri(id), variant.getMediaType());
        }

        PipelineConfiguration pipeconfig = getPipelines().get(id);
        XPipeline xpipeline = pipeconfig.pipeline;

        // Passing options was never documented and this is redundant with the behavior of posting
        // to the base pipeline endoing, /pipelines/{id}
        /*
        if (MediaType.MULTIPART_FORM_DATA.equals(entity.getMediaType(), true)) {
View Full Code Here

        if (userArgs.isShowVersion()) {
            XProcConfiguration.showVersion(runtime);
        }

        XPipeline pipeline = null;

        if (userArgs.getPipeline() != null) {
            pipeline = runtime.load(userArgs.getPipeline());
        } else if (userArgs.hasImplicitPipeline()) {
            XdmNode implicitPipeline = userArgs.getImplicitPipeline(runtime);

            if (debug) {
                System.err.println("Implicit pipeline:");

                Serializer serializer = new Serializer();

                serializer.setOutputProperty(Serializer.Property.INDENT, "yes");
                serializer.setOutputProperty(Serializer.Property.METHOD, "xml");

                serializer.setOutputStream(System.err);

                S9apiUtils.serialize(runtime, implicitPipeline, serializer);
            }

            pipeline = runtime.use(implicitPipeline);
        } else if (config.pipeline != null) {
            XdmNode doc = config.pipeline.read();
            pipeline = runtime.use(doc);
        } else {
            throw new UnsupportedOperationException("Either a pipeline or libraries and / or steps must be given");
        }

        // Process parameters from the configuration...
        for (String port : config.params.keySet()) {
            Map<QName, String> parameters = config.params.get(port);
            setParametersOnPipeline(pipeline, port, parameters);
        }

        // Now process parameters from the command line...
        for (String port : userArgs.getParameterPorts()) {
            Map<QName, String> parameters = userArgs.getParameters(port);
            setParametersOnPipeline(pipeline, port, parameters);
        }

        Set<String> ports = pipeline.getInputs();
        Set<String> userArgsInputPorts = userArgs.getInputPorts();
        Set<String> cfgInputPorts = config.inputs.keySet();
        Set<String> allPorts = new HashSet<String>();
        allPorts.addAll(userArgsInputPorts);
        allPorts.addAll(cfgInputPorts);

        // map a given input without port specification to the primary non-parameter input implicitly
        for (String port : ports) {
            if (!allPorts.contains(port) && allPorts.contains(null)
                && pipeline.getDeclareStep().getInput(port).getPrimary()
                && !pipeline.getDeclareStep().getInput(port).getParameterInput()) {

                if (userArgsInputPorts.contains(null)) {
                    userArgs.setDefaultInputPort(port);
                    allPorts.remove(null);
                    allPorts.add(port);
                }
                break;
            }
        }

        for (String port : allPorts) {
            if (!ports.contains(port)) {
                throw new XProcException("There is a binding for the port '" + port + "' but the pipeline declares no such port.");
            }

            pipeline.clearInputs(port);

            if (userArgsInputPorts.contains(port)) {
                XdmNode doc = null;
                for (Input input : userArgs.getInputs(port)) {
                    switch (input.getType()) {
                        case XML:
                            switch (input.getKind()) {
                                case URI:
                                    String uri = input.getUri();
                                    if ("-".equals(uri)) {
                                        doc = runtime.parse(new InputSource(System.in));
                                    } else {
                                        doc = runtime.parse(new InputSource(uri));
                                    }
                                    break;

                                case INPUT_STREAM:
                                    InputStream inputStream = input.getInputStream();
                                    try {
                                        doc = runtime.parse(new InputSource(inputStream));
                                    } finally {
                                        Closer.close(inputStream);
                                    }
                                    break;

                                default:
                                    throw new UnsupportedOperationException(format("Unsupported input kind '%s'", input.getKind()));
                            }
                            break;

                        case DATA:
                            ReadableData rd;
                            switch (input.getKind()) {
                                case URI:
                                    rd = new ReadableData(runtime, c_data, input.getUri(), input.getContentType());
                                    doc = rd.read();
                                    break;

                                case INPUT_STREAM:
                                    InputStream inputStream = input.getInputStream();
                                    try {
                                        rd = new ReadableData(runtime, c_data, inputStream, input.getContentType());
                                        doc = rd.read();
                                    } finally {
                                        Closer.close(inputStream);
                                    }
                                    break;

                                default:
                                    throw new UnsupportedOperationException(format("Unsupported input kind '%s'", input.getKind()));
                            }
                            break;

                        default:
                            throw new UnsupportedOperationException(format("Unsupported input type '%s'", input.getType()));
                    }

                    pipeline.writeTo(port, doc);
                }
            } else {
                for (ReadablePipe pipe : config.inputs.get(port)) {
                    XdmNode doc = pipe.read();
                    pipeline.writeTo(port, doc);
                }
            }
        }

        // Implicit binding for stdin?
        String implicitPort = null;
        for (String port : ports) {
            if (!allPorts.contains(port)) {
                if (pipeline.getDeclareStep().getInput(port).getPrimary()
                        && !pipeline.getDeclareStep().getInput(port).getParameterInput()) {
                    implicitPort = port;
                }
            }
        }

        if (implicitPort != null && !pipeline.hasReadablePipes(implicitPort)) {
            XdmNode doc = runtime.parse(new InputSource(System.in));
            pipeline.writeTo(implicitPort, doc);
        }

        Map<String, Output> portOutputs = new HashMap<String, Output>();

        Map<String, Output> userArgsOutputs = userArgs.getOutputs();
        for (String port : pipeline.getOutputs()) {
            // Bind to "-" implicitly
            Output output = null;

            if (userArgsOutputs.containsKey(port)) {
                output = userArgsOutputs.get(port);
            } else if (config.outputs.containsKey(port)) {
                output = new Output(config.outputs.get(port));
            } else if (userArgsOutputs.containsKey(null)
                       && pipeline.getDeclareStep().getOutput(port).getPrimary()) {
                // Bind unnamed port to primary output port
                output = userArgsOutputs.get(null);
            }

            // Look for explicit binding to "-"
            if ((output != null) && (output.getKind() == Kind.URI) && "-".equals(output.getUri())) {
                output = null;
            }

            portOutputs.put(port, output);
        }

        for (QName optname : config.options.keySet()) {
            RuntimeValue value = new RuntimeValue(config.options.get(optname), null, null);
            pipeline.passOption(optname, value);
        }

        for (QName optname : userArgs.getOptionNames()) {
            RuntimeValue value = new RuntimeValue(userArgs.getOption(optname), null, null);
            pipeline.passOption(optname, value);
        }

        pipeline.run();

        for (String port : pipeline.getOutputs()) {
            Output output;
            if (portOutputs.containsKey(port)) {
                output = portOutputs.get(port);
            } else {
                // You didn't bind it, and it isn't going to stdout, so it's going into the bit bucket.
                continue;
            }

            if ((output == null) || ((output.getKind() == OUTPUT_STREAM) && System.out.equals(output.getOutputStream()))) {
                logger.trace("Copy output from " + port + " to stdout");
            } else {
                switch (output.getKind()) {
                    case URI:
                        logger.trace("Copy output from " + port + " to " + output.getUri());
                        break;

                    case OUTPUT_STREAM:
                        String outputStreamClassName = output.getOutputStream().getClass().getName();
                        logger.trace("Copy output from " + port + " to " + outputStreamClassName + " stream");
                        break;

                    default:
                        throw new UnsupportedOperationException(format("Unsupported output kind '%s'", output.getKind()));
                }
            }

            Serialization serial = pipeline.getSerialization(port);

            if (serial == null) {
                // Use the configuration options
                // FIXME: should each of these be considered separately?
                // FIXME: should there be command-line options to override these settings?
                serial = new Serialization(runtime, pipeline.getNode()); // The node's a hack
                for (String name : config.serializationOptions.keySet()) {
                    String value = config.serializationOptions.get(name);

                    if ("byte-order-mark".equals(name)) serial.setByteOrderMark("true".equals(value));
                    if ("escape-uri-attributes".equals(name)) serial.setEscapeURIAttributes("true".equals(value));
                    if ("include-content-type".equals(name)) serial.setIncludeContentType("true".equals(value));
                    if ("indent".equals(name)) serial.setIndent("true".equals(value));
                    if ("omit-xml-declaration".equals(name)) serial.setOmitXMLDeclaration("true".equals(value));
                    if ("undeclare-prefixes".equals(name)) serial.setUndeclarePrefixes("true".equals(value));
                    if ("method".equals(name)) serial.setMethod(new QName("", value));

                    // FIXME: if ("cdata-section-elements".equals(name)) serial.setCdataSectionElements();
                    if ("doctype-public".equals(name)) serial.setDoctypePublic(value);
                    if ("doctype-system".equals(name)) serial.setDoctypeSystem(value);
                    if ("encoding".equals(name)) serial.setEncoding(value);
                    if ("media-type".equals(name)) serial.setMediaType(value);
                    if ("normalization-form".equals(name)) serial.setNormalizationForm(value);
                    if ("standalone".equals(name)) serial.setStandalone(value);
                    if ("version".equals(name)) serial.setVersion(value);
                }
            }

            // I wonder if there's a better way...
            WritableDocument wd = null;
            if (output == null) {
                wd = new WritableDocument(runtime, null, serial);
            } else {
                switch (output.getKind()) {
                    case URI:
                        URI furi = new URI(output.getUri());
                        String filename = furi.getPath();
                        FileOutputStream outfile = new FileOutputStream(filename);
                        wd = new WritableDocument(runtime, filename, serial, outfile);
                        break;

                    case OUTPUT_STREAM:
                        OutputStream outputStream = output.getOutputStream();
                        wd = new WritableDocument(runtime, null, serial, outputStream);
                        break;

                    default:
                        throw new UnsupportedOperationException(format("Unsupported output kind '%s'", output.getKind()));
                }
            }

            try {
                ReadablePipe rpipe = pipeline.readFrom(port);
                while (rpipe.moreDocuments()) {
                    wd.write(rpipe.read());
                }
            } finally {
                if (output != null) {
View Full Code Here

        if (!getPipelines().containsKey(id)) {
            return badRequest(Status.CLIENT_ERROR_NOT_FOUND, "no pipeline: " + pipelineUri(id), variant.getMediaType());
        }

        PipelineConfiguration pipeconfig = getPipelines().get(id);
        XPipeline xpipeline = pipeconfig.pipeline;

        HashMap<QName,String> options = convertForm(getQuery());

        String message = "Options added: ";
        boolean first = true;

        for (QName name : options.keySet()) {
            RuntimeValue value = new RuntimeValue(options.get(name), null, null);
            xpipeline.passOption(name, value);
            pipeconfig.setOption(name, options.get(name));
            if (!first) {
                message += ", ";
            }
            message += name;
View Full Code Here

TOP

Related Classes of com.xmlcalabash.runtime.XPipeline

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.