/** * Convert the given execution plan into an HTML document * * @param execPlan * @return url of the resulting html file * @throws Exception */ public String convert(String execPlan) throws Exception { InputStream xslStream = null; try { xslStream = FileLocator.openStream(bundle, new Path(EXECUTION_PLAN_XSLT), true); TransformerFactoryImpl factory = new TransformerFactoryImpl(); StreamSource xslSource = new StreamSource(xslStream); InputStream execPlanStream = new ByteArrayInputStream(execPlan.getBytes("UTF-8")); // $NON-NLS-1$ File destination = getDestination(); File outHTMLPlan = new File(destination, createOutFileName()); StreamSource in = new StreamSource(execPlanStream); StreamResult out = new StreamResult(outHTMLPlan); Transformer transformer = factory.newTransformer(xslSource); transformer.transform(in, out); return outHTMLPlan.getAbsolutePath(); } catch (Exception ex) { throw ex; } finally { if (xslStream != null) xslStream.close(); } }
private String importXMI(File sourceFile) throws FileNotFoundException, TransformerConfigurationException, TransformerException { BufferedReader styledata; StreamSource sourcedata; Templates stylesheet; Transformer trans; // Get the stylesheet file stream styledata = new BufferedReader( new FileReader( new File( getClass().getClassLoader().getResource("verifier/xmi2lem.xsl").getFile()))); sourcedata = new StreamSource(new FileReader(sourceFile)); // Initialize Saxon TransformerFactory factory = TransformerFactoryImpl.newInstance(); stylesheet = factory.newTemplates(new StreamSource(styledata)); // Apply the transformation trans = stylesheet.newTransformer(); StringWriter sw = new StringWriter(); trans.transform(sourcedata, new StreamResult(sw)); // return sw.toString(); return sw.getBuffer().substring(sw.getBuffer().indexOf("model")); }
private TransformerHandler createTransformerHandler() { try { TransformerHandlerImpl transformerHandler = (TransformerHandlerImpl) GENERIC_FACTORY.newTransformerHandler(templates); TransformerImpl impl = (TransformerImpl) transformerHandler.getTransformer(); XsltTransformer xslt = impl.getUnderlyingXsltTransformer(); Controller controller = (Controller) xslt.getUnderlyingController(); if (configuration != null) { for (Entry<String, Object> entry : configuration.entrySet()) { String name = entry.getKey(); // is valid XSLT parameter name if (XSLT_PARAMETER_NAME_PATTERN.matcher(name).matches()) { xslt.setParameter(new QName(name), new XdmExternalObject(entry.getValue())); } } } controller.setErrorListener(new TraxErrorListener(LOG, source.toExternalForm())); if (context != null) { xslt.setParameter(CoreConstants.APPLICATION_CONTEXT_PARAM, new XdmExternalObject(context)); } Map<String, Object> model = HttpContextHelper.getModel(parameters); xslt.setParameter(CoreConstants.MODEL_PARAM, new XdmExternalObject(model)); if (parameters != null) try { ServletContext servletContext = HttpContextHelper.getServletContext(parameters); xslt.setParameter( CoreConstants.SERVLET_CONTEXT_PARAM, new XdmExternalObject(servletContext)); HttpServletRequest servletRequest = HttpContextHelper.getRequest(parameters); HttpServletResponse servletResponse = HttpContextHelper.getResponse(parameters); if (servletRequest != null) { controller.setURIResolver(new SaxonResolver(servletRequest, servletResponse)); xslt.setParameter( Message.LOCALE_PARAM, new XdmExternalObject(RequestContextUtils.getLocale(servletRequest))); xslt.setParameter( CoreConstants.SERVLET_REQUEST_PARAM, new XdmExternalObject(servletRequest)); HttpSession session = servletRequest.getSession(false); if (session != null) { xslt.setParameter( CoreConstants.SERVLET_SESSION_PARAM, new XdmExternalObject(session)); } // add support for form binding xslt.setParameter( CoreConstants.REQUEST_CONTEXT_PARAM, new XdmExternalObject( new RequestContext(servletRequest, servletResponse, servletContext, model))); } if (servletResponse != null) { xslt.setParameter( CoreConstants.SERVLET_RESPONSE_PARAM, new XdmExternalObject(servletResponse)); } } catch (IllegalStateException e) { LOG.debug("Not a Servlet request!", e); // @SuppressWarnings({"rawtypes", "unchecked"}) // HashMap<String, Object> model = (HashMap) parameters.get("model"); // // HttpServletRequest servletRequest = new VirtualRequest(); // servletRequest.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, // context); // RequestContext requestContext = new RequestContext(servletRequest, model); // // transformer.setParameter(CoreConstants.REQUEST_CONTEXT_PARAM, requestContext); } return transformerHandler; } catch (TransformerConfigurationException ex) { throw new SetupException("Could not initialize transformer handler.", ex); } }
private void loadXSLT(URL source, Map<String, Object> attributes) { if (source == null) { throw new IllegalArgumentException("The parameter 'source' mustn't be null."); } this.source = source; templates = null; long lastModified = 0; try { URLConnection connection = source.openConnection(); connection.setDoInput(false); lastModified = connection.getLastModified(); } catch (IOException e) { } TransformerTracker tracker = null; // check the XSLT is in the cache first if (XSLT_CACHE.containsKey(source.toExternalForm())) { // get the XSLT directly from the cache ValidityValue<TransformerTracker> cacheEntry = XSLT_CACHE.get(source.toExternalForm()); if (cacheEntry.getLastModified() == lastModified) { tracker = cacheEntry.getValue(); if (tracker.hasChanged()) { tracker = null; } } if (tracker == null) { XSLT_CACHE.remove(source.toExternalForm()); } } if (tracker == null) { // XSLT has to be parsed Source urlSource = new StreamSource(source.toExternalForm()); TransformerFactoryImpl transformerFactory = createNewSAXTransformerFactory(); if (attributes != null && !attributes.isEmpty()) { for (Entry<String, Object> attribute : attributes.entrySet()) { transformerFactory.setAttribute(attribute.getKey(), attribute.getValue()); } } try { tracker = new TransformerTracker(context, transformerFactory, urlSource); // store the XSLT into the cache for future reuse ValidityValue<TransformerTracker> cacheEntry = new ValidityValue<TransformerTracker>(tracker, lastModified); XSLT_CACHE.put(source.toExternalForm(), cacheEntry); } catch (TransformerConfigurationException e) { throw new SetupException( "Impossible to read XSLT from '" + source.toExternalForm() + "', see nested exception", e); } } templates = tracker.templates; }