public Configuration buildFromItem(Item item) throws SAXException { SAXConfigurationHandler handler = getHandler(); handler.clear(); item.toSAX(broker, handler, new Properties()); return handler.getConfiguration(); }
/* * Actual implementation of the rendering process. When a function in this * module is called, this method is executed with the given inputs. @param * Sequence[] args (XSL-FO, mime-type, parameters) @param Sequence * contextSequence (default sequence) * * @see org.exist.xquery.BasicFunction#eval(org.exist.xquery.value.Sequence[], * org.exist.xquery.value.Sequence) */ public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException { // gather input XSL-FO document // if no input document (empty), return empty result as we need data to // process if (args[0].isEmpty()) return Sequence.EMPTY_SEQUENCE; Item inputNode = args[0].itemAt(0); // get mime-type String mimeType = args[1].getStringValue(); // get parameters Properties parameters = new Properties(); if (!args[2].isEmpty()) { parameters = ModuleUtils.parseParameters(((NodeValue) args[2].itemAt(0)).getNode()); } try { // setup a transformer handler TransformerHandler handler = TransformerFactoryAllocator.getTransformerFactory(context.getBroker()) .newTransformerHandler(); Transformer transformer = handler.getTransformer(); // set the parameters if any if (parameters.size() > 0) { Enumeration keys = parameters.keys(); while (keys.hasMoreElements()) { String name = (String) keys.nextElement(); String value = parameters.getProperty(name); transformer.setParameter(name, value); } } // setup the FopFactory FopFactory fopFactory = FopFactory.newInstance(); if (args.length == 4 && args[3] != null && !args[3].isEmpty()) { FopConfigurationBuilder cfgBuilder = new FopConfigurationBuilder(context.getBroker()); Configuration cfg = cfgBuilder.buildFromItem(args[3].itemAt(0)); fopFactory.setUserConfig(cfg); } // setup the foUserAgent, using given parameters held in the // transformer handler FOUserAgent foUserAgent = setupFOUserAgent(fopFactory.newFOUserAgent(), parameters, transformer); // create new instance of FOP using the mimetype, the created user // agent, and the output stream ByteArrayOutputStream baos = new ByteArrayOutputStream(); Fop fop = fopFactory.newFop(mimeType, foUserAgent, baos); // Obtain FOP's DefaultHandler DefaultHandler dh = fop.getDefaultHandler(); // process the XSL-FO dh.startDocument(); inputNode.toSAX(context.getBroker(), dh, new Properties()); dh.endDocument(); // return the result return new Base64Binary(baos.toByteArray()); } catch (TransformerException te) { throw new XPathException(te); } catch (SAXException se) { throw new XPathException(se); } }