public Object eval()

in src/main/java/org/apache/bsf/engines/xslt/XSLTEngine.java [78:162]


    public Object eval(final String source, final int lineNo, final int columnNo, final Object oscript) throws BSFException {
        // get the style base URI (the place from where Xerces XSLT will
        // look for imported/included files and referenced docs): if a
        // bean named "xslt:styleBaseURI" is registered, then cvt it
        // to a string and use that. Otherwise use ".", which means the
        // base is the directory where the process is running from
        final Object sbObj = mgr.lookupBean("xslt:styleBaseURI");
        final String styleBaseURI = (sbObj == null) ? "." : sbObj.toString();

        // Locate the stylesheet.
        StreamSource styleSource;

        styleSource = new StreamSource(new StringReader(oscript.toString()));
        styleSource.setSystemId(styleBaseURI);

        try {
            transformer = tFactory.newTransformer(styleSource);
        } catch (final Exception e) {
            logger.error("Exception from Xerces XSLT:", e);
            throw new BSFException(BSFException.REASON_EXECUTION_ERROR, "Exception from Xerces XSLT: " + e, e);
        }

        // get the src to work on: if a bean named "xslt:src" is registered
        // and its a Node, then use it as the source. If its not a Node, then
        // if its a URL parse it, if not treat it as a file and make a URL and
        // parse it and go. If no xslt:src is found, use an empty document
        // (stylesheet is treated as a literal result element stylesheet)
        final Object srcObj = mgr.lookupBean("xslt:src");
        Object xis = null;
        if (srcObj != null) {
            if (srcObj instanceof Node) {
                xis = new DOMSource((Node) srcObj);
            } else {
                try {
                    String mesg = "as anything";
                    if (srcObj instanceof Reader) {
                        xis = new StreamSource((Reader) srcObj);
                        mesg = "as a Reader";
                    } else if (srcObj instanceof File) {
                        xis = new StreamSource((File) srcObj);
                        mesg = "as a file";
                    } else {
                        final String srcObjstr = srcObj.toString();
                        xis = new StreamSource(new StringReader(srcObjstr));
                        if (srcObj instanceof URL) {
                            mesg = "as a URL";
                        } else {
                            ((StreamSource) xis).setPublicId(srcObjstr);
                            mesg = "as an XML string";
                        }
                    }

                    if (xis == null) {
                        throw new Exception("Unable to get input from '" + srcObj + "' " + mesg);
                    }
                } catch (final Exception e) {
                    throw new BSFException(BSFException.REASON_EXECUTION_ERROR, "BSF:XSLTEngine: unable to get " + "input from '" + srcObj + "' as XML", e);
                }
            }
        } else {
            // create an empty document - real src must come into the
            // stylesheet using "doc(...)" [see XSLT spec] or the stylesheet
            // must be of literal result element type
            xis = new StreamSource();
        }

        // set all declared beans as parameters.
        for (int i = 0; i < declaredBeans.size(); i++) {
            final BSFDeclaredBean b = (BSFDeclaredBean) declaredBeans.elementAt(i);
            transformer.setParameter(b.name, new XObject(b.bean));
        }

        // declare a "bsf" parameter which is the BSF handle so that
        // the script can do BSF stuff if it wants to
        transformer.setParameter("bsf", new XObject(new BSFFunctions(mgr, this)));

        // do it
        try {
            final DOMResult result = new DOMResult();
            transformer.transform((StreamSource) xis, result);
            return new XSLTResultNode(result.getNode());
        } catch (final Exception e) {
            throw new BSFException(BSFException.REASON_EXECUTION_ERROR, "exception while eval'ing XSLT script" + e, e);
        }
    }