Example usage for org.dom4j.io DocumentSource DocumentSource

List of usage examples for org.dom4j.io DocumentSource DocumentSource

Introduction

In this page you can find the example usage for org.dom4j.io DocumentSource DocumentSource.

Prototype

public DocumentSource(Document document) 

Source Link

Document

Creates a JAXP SAXSource for the given Document .

Usage

From source file:bookmarks.BookmarkXML.java

License:Open Source License

/**
 * /* ww w .  jav  a 2 s  .  co m*/
 * @param xslFileName
 * */

public void styleDocument(String xslFileName, String prefLang) {
    try {
        TransformerFactory factory = TransformerFactory.newInstance();

        if (prefLang.equals("eng")) {
            Transformer transformer = factory.newTransformer(new StreamSource(
                    "file:///" + PropertyManager.CATALINA_BASE + PropertyManager.XSLPATHENG + xslFileName));

            String test2 = PropertyManager.CATALINA_BASE + PropertyManager.XSLPATHENG + xslFileName;

            transformer.setOutputProperty(OutputKeys.ENCODING, Utilities.REQUEST_ENCODING);
            DocumentSource source = new DocumentSource(xmlDoc);
            DocumentResult result = new DocumentResult();

            transformer.transform(source, result);

            xmlDoc = result.getDocument();
        } else {

            String test2 = PropertyManager.CATALINA_BASE + PropertyManager.XSLPATHGER + xslFileName;

            Transformer transformer = factory.newTransformer(new StreamSource(
                    "file:///" + PropertyManager.CATALINA_BASE + PropertyManager.XSLPATHGER + xslFileName));

            transformer.setOutputProperty(OutputKeys.ENCODING, Utilities.REQUEST_ENCODING);
            DocumentSource source = new DocumentSource(xmlDoc);
            DocumentResult result = new DocumentResult();

            transformer.transform(source, result);

            xmlDoc = result.getDocument();

        }
    } catch (TransformerConfigurationException e) {
        throw new RuntimeException(e);
    } catch (TransformerFactoryConfigurationError e) {
        throw new RuntimeException(e);
    } catch (TransformerException e) {
        throw new RuntimeException(e);
    }
}

From source file:ca.coder2000.recipes.RecipeFile.java

License:Mozilla Public License

/**
 * Returns formatted HTML from the xml file.
 * @param stylesheet The stylesheet to transform against.
 * @return The HTML to be used.//from w ww  . ja  v a 2s . co m
 */
public Document getHTML(File stylesheet) {
    Transformer transformer;
    TransformerFactory factory = TransformerFactory.newInstance();

    DocumentSource source = new DocumentSource(doc);
    DocumentResult result = new DocumentResult();

    try {
        transformer = factory.newTransformer(new StreamSource(stylesheet));
        transformer.transform(source, result);
        // Hard coded path MUST remove
        OutputFormat format = OutputFormat.createPrettyPrint();
        XMLWriter writer = new XMLWriter(new FileWriter("k:/recipe/recipe.tmp"), format);
        writer.write(result.getDocument());
    } catch (TransformerException te) {
    } catch (IOException ioe) {
    }

    return result.getDocument();
}

From source file:com.amalto.commons.core.utils.XMLUtils.java

License:Open Source License

public static org.dom4j.Document styleDocument(org.dom4j.Document document, String stylesheet)
        throws Exception {
    // load the transformer using JAXP
    Transformer transformer = saxonTransformerFactory
            .newTransformer(new StreamSource(new StringReader(stylesheet)));

    // now lets style the given document
    DocumentSource source = new DocumentSource(document);
    DocumentResult result = new DocumentResult();
    transformer.transform(source, result);

    // return the transformed document
    org.dom4j.Document transformedDoc = result.getDocument();

    if (logger.isDebugEnabled()) {
        logger.debug("The xml file style transformed successfully ");
    }//from w w  w  . ja v a 2 s  .co m
    return transformedDoc;
}

From source file:com.amalto.workbench.utils.XmlUtil.java

License:Open Source License

public static Document styleDocument(Document document, String stylesheet) throws Exception {

    // load the transformer using JAXP
    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer(new StreamSource(stylesheet));

    // now lets style the given document
    DocumentSource source = new DocumentSource(document);
    DocumentResult result = new DocumentResult();
    transformer.transform(source, result);

    // return the transformed document
    Document transformedDoc = result.getDocument();

    logger.info(Messages.XmlUtil_Loginfo2);

    return transformedDoc;
}

From source file:com.anite.maven.plugin.hivedoc.HivedocReport.java

License:Apache License

public void execute() throws MojoExecutionException, MojoFailureException {

    try {/*  w ww.  j  av  a 2  s .  c  om*/
        // Construct the registry and output the XML File 
        ModuleDescriptorProvider provider = new XmlModuleDescriptorProvider(
                new DefaultClassResolver(getClassLoader()));

        Dom4JRegistrySerializer serializer = new Dom4JRegistrySerializer();
        serializer.addModuleDescriptorProvider(provider);

        Document result = serializer.createRegistryDocument();

        // Run the XSL over it to generate Hivedoc
        File outputFolder = new File(project.getReporting().getOutputDirectory() + "/hivedoc");
        outputFolder.mkdirs();
        File outputFile = new File(outputFolder, "index.html");
        DocumentSource registryDocument = new DocumentSource(result);
        Source xsltFile = new StreamSource(this.getClass().getClassLoader().getResourceAsStream(XSLT_FILE));
        Result output = new StreamResult(new FileOutputStream(outputFile));

        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer trans = transformerFactory.newTransformer(xsltFile);
        trans.setParameter("base.dir", outputFolder.getAbsolutePath());

        trans.transform(registryDocument, output);

        // Copy the resources to the output folder
        copyResouceDir("private.png", outputFolder);
        copyResouceDir("public.png", outputFolder);
        copyResouceDir("hivemind.css", outputFolder);

    } catch (MalformedURLException e) {
        this.getLog().error(e);
        throw new MojoExecutionException("", e);
    } catch (DependencyResolutionRequiredException e) {
        this.getLog().error(e);
        throw new MojoExecutionException("", e);
    } catch (FileNotFoundException e) {
        this.getLog().error(e);
        throw new MojoExecutionException("", e);
    } catch (TransformerConfigurationException e) {
        this.getLog().error(e);
        throw new MojoExecutionException("", e);
    } catch (TransformerException e) {
        this.getLog().error(e);
        throw new MojoExecutionException("", e);
    } catch (ClassCastException e) {
        this.getLog().error(e);
        throw new MojoExecutionException("", e);
    } catch (IOException e) {
        this.getLog().error(e);
        throw new MojoExecutionException("", e);
    }
}

From source file:com.collabnet.ccf.core.transformer.DynamicXsltProcessor.java

License:Open Source License

/**
 * Tries to load the XSLT from the file defined in the properties
 * /*w w w.  j a va 2  s  .  c  om*/
 * @throws ValidationException
 *             if the XSLT file is not defined in the properties, the file
 *             cannot be found or there was an error parsing it
 */
private List<Transformer> loadXSLT(File xsltFile, Element element) {
    List<Transformer> transformerList = new ArrayList<Transformer>();
    if (xsltFile == null) {
        String cause = "xsltFile property not set";
        log.error(cause);
        XPathUtils.addAttribute(element, GenericArtifactHelper.ERROR_CODE,
                GenericArtifact.ERROR_TRANSFORMER_FILE);
        throw new CCFRuntimeException(cause);
    }

    try {
        Source source = null;
        if (isOnlyAllowWhiteListedJavaFunctionCalls()) {
            SAXReader reader = new SAXReader();
            Document originalDocument = reader.read(xsltFile);
            Document clonedDocument = (Document) originalDocument.clone();
            Element clonedRootElement = clonedDocument.getRootElement();
            // replace white listed Java functions in XPath expressions with
            // "."
            for (String functionCall : getWhiteListedJavaFunctionCalls()) {
                List<Element> nodes = findFunctionCalls(clonedRootElement, functionCall);
                for (Element e : nodes) {
                    e.addAttribute("select", ".");
                }
            }
            Transformer secureTransform = secureFactory.newTransformer(new DocumentSource(clonedDocument));
            secureTransform.setErrorListener(new XsltValidationErrorListener());
            log.debug("Loaded sanitized version of XSLT [" + xsltFile + "] successfully");
            transformerList.add(secureTransform);
            source = new DocumentSource(originalDocument);
        } else {
            source = new StreamSource(xsltFile);
        }
        Transformer transform = factory.newTransformer(source);
        log.debug("Loaded original XSLT [" + xsltFile + "] successfully");
        transformerList.add(transform);
    } catch (Exception e) {
        String cause = "Failed to load XSLT: [" + xsltFile + " ]" + e.getMessage();
        log.error(cause, e);
        XPathUtils.addAttribute(element, GenericArtifactHelper.ERROR_CODE,
                GenericArtifact.ERROR_TRANSFORMER_FILE);
        throw new CCFRuntimeException(cause, e);
    }

    return transformerList;
}

From source file:com.collabnet.ccf.core.transformer.DynamicXsltProcessor.java

License:Open Source License

/**
 * Applies the transform to the Dom4J document
 * //from  www .ja  va 2 s  .  c om
 * @param transformer
 *            List of transformers to be applied, all transformers have to
 *            execute properly, but only the result from latest one is
 *            returned
 * @param d
 *            the document to transform
 * 
 * @return an array containing a single XML string representing the
 *         transformed document
 * @throws TransformerException
 *             thrown if an XSLT runtime error happens during transformation
 */
public static Document transform(List<Transformer> transformer, Document d) throws TransformerException {
    DocumentSource source = null;
    DocumentResult result = null;
    /**
     * We will run through all transformers, so that we can have specially
     * configured transformers that check things like calls to external
     * functions Only the result from the last transformer is returned
     */
    for (Transformer trans : transformer) {
        // TODO: Allow the user to specify stylesheet parameters?
        source = new DocumentSource(d);
        result = new DocumentResult();
        trans.transform(source, result);
    }

    return result.getDocument();
}

From source file:com.collabnet.ccf.core.transformer.XsltProcessor.java

License:Open Source License

/**
 * Applies the transform to the Dom4J document
 * //from w w w. ja  v  a 2 s.c om
 * @param transformer
 * @param d
 *            the document to transform
 * 
 * @return an array containing a single XML string representing the
 *         transformed document
 * @throws TransformerException
 *             thrown if an XSLT runtime error happens during transformation
 */
public static Document transform(Transformer transformer, Document d) throws TransformerException {
    DocumentSource source = new DocumentSource(d);
    DocumentResult result = new DocumentResult();
    // TODO: Allow the user to specify stylesheet parameters?
    transformer.transform(source, result);
    return result.getDocument();
}

From source file:com.collabnet.ccf.schemageneration.CCFXSLTSchemaAndXSLTFileGenerator.java

License:Apache License

/**
 * Applies the transform to the Dom4J document
 * // w  w w.j  a va  2  s .c om
 * @param transformer
 * @param d
 *            the document to transform
 * 
 * @return an array containing a single XML string representing the
 *         transformed document
 * @throws TransformerException
 *             thrown if an XSLT runtime error happens during transformation
 */
private static Document transform(Transformer transformer, Document d) throws TransformerException {
    DocumentSource source = new DocumentSource(d);
    DocumentResult result = new DocumentResult();
    // TODO: Allow the user to specify stylesheet parameters?
    transformer.transform(source, result);
    return result.getDocument();
}

From source file:com.collabnet.ccf.schemageneration.XSLTInitialMFDGeneratorScriptGenerator.java

License:Apache License

/**
 * Applies the transform to the Dom4J document
 * /*w  w  w  .  j  ava  2  s  .  c  o m*/
 * @param transformer
 * @param d
 *            the document to transform
 * @param targetSchemaName 
 * @param sourceSchemaName 
 * 
 * @return an array containing a single XML string representing the
 *         transformed document
 * @throws TransformerException
 *             thrown if an XSLT runtime error happens during transformation
 */
private static Document transform(Transformer transformer, Document d, String sourceSchemaName,
        String targetSchemaName) throws TransformerException {
    DocumentSource source = new DocumentSource(d);
    DocumentResult result = new DocumentResult();
    transformer.setParameter("sourceSchema", sourceSchemaName);
    transformer.setParameter("targetSchema", targetSchemaName);
    transformer.transform(source, result);
    return result.getDocument();
}