Example usage for org.jdom2.transform JDOMSource JDOMSource

List of usage examples for org.jdom2.transform JDOMSource JDOMSource

Introduction

In this page you can find the example usage for org.jdom2.transform JDOMSource JDOMSource.

Prototype

public JDOMSource(Element source) 

Source Link

Document

Creates a JDOM TrAX source wrapping a JDOM element.

Usage

From source file:com.dontocsata.xmltv.mxf.MxfValidator.java

License:Open Source License

public void validate(Document doc) throws SAXException, IOException {
    validate(new JDOMSource(doc));
}

From source file:com.hack23.cia.service.external.common.impl.XmlAgentImpl.java

License:Apache License

/**
 * Sets the name space on xml stream.//from  w ww  .ja  va 2  s .  c  o m
 *
 * @param in
 *            the in
 * @param nameSpace
 *            the name space
 * @return the source
 * @throws Exception
 *             the exception
 */
private static Source setNameSpaceOnXmlStream(final InputStream in, final String nameSpace) throws Exception {
    final SAXBuilder sb = new SAXBuilder(new XMLReaderSAX2Factory(false));
    final Document doc = sb.build(in);
    doc.getRootElement().setNamespace(Namespace.getNamespace(nameSpace));
    return new JDOMSource(doc);
}

From source file:de.rallye.api.LandingPage.java

License:Open Source License

@GET
@Produces("image/png")
@Path("timetable/{groupsCode}")
public StreamingOutput getTimeTable(@PathParam("groupsCode") final String groupsCode) {

    StreamingOutput stream = new StreamingOutput() {
        @Override/*from w  w  w .j a va 2s . c om*/
        public void write(OutputStream os) throws IOException, WebApplicationException {
            try {
                Document doc = new SAXBuilder()
                        .build(StadtRallye.class.getResourceAsStream("timetable/stundenplan.xml"));
                replaceKgRooms(doc, groupsCode);
                Source xmlFile = new JDOMSource(doc);
                final JDOMResult htmlResult = new JDOMResult();
                Transformer transformer = TransformerFactory.newInstance().newTransformer(
                        new StreamSource(StadtRallye.class.getResourceAsStream("timetable/stundenplan.xsl")));
                transformer.transform(xmlFile, htmlResult);

                PipedInputStream svgIn = new PipedInputStream();
                final PipedOutputStream svgOut = new PipedOutputStream(svgIn);

                new Thread(new Runnable() {
                    public void run() {

                        XMLOutputter xmlOutputter = new XMLOutputter();
                        try {
                            xmlOutputter.output(htmlResult.getDocument(), svgOut);
                            svgOut.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        } catch (Throwable e) {
                            e.printStackTrace();
                        }
                    }
                }).start();

                // Create a PNG transcoder
                PNGTranscoder t = new PNGTranscoder();

                // Create the transcoder input.
                //String svgURI = new File(args[0]).toURL().toString();
                TranscoderInput input = new TranscoderInput(svgIn);

                // Create the transcoder output.
                TranscoderOutput output = new TranscoderOutput(os);

                SVGConverter x;

                // Save the image.
                t.transcode(input, output);
                svgIn.close();

                // Flush and close the stream.
                os.flush();
                os.close();

            } catch (JDOMException e) {
                e.printStackTrace();
            } catch (TransformerConfigurationException e) {
                e.printStackTrace();
            } catch (TransformerException e) {
                e.printStackTrace();
            } catch (TranscoderException e) {
                e.printStackTrace();
            } catch (Throwable e) {
                e.printStackTrace();
            }
        }
    };
    return stream;

    //return StadtRallye.class.getResourceAsStream("timetable/stundenplan.xml");
}

From source file:de.tu_dortmund.ub.api.daia.DaiaEndpoint.java

License:Open Source License

private String htmlOutputter(Document doc, String xslt, HashMap<String, String> params) throws IOException {

    String result = null;/*from   w ww. j  a va2  s. c om*/

    try {

        // Init XSLT-Transformer
        Processor processor = new Processor(false);
        XsltCompiler xsltCompiler = processor.newXsltCompiler();
        XsltExecutable xsltExecutable = xsltCompiler.compile(new StreamSource(xslt));

        XdmNode source = processor.newDocumentBuilder().build(new JDOMSource(doc));
        Serializer out = new Serializer();
        out.setOutputProperty(Serializer.Property.METHOD, "html");
        out.setOutputProperty(Serializer.Property.INDENT, "yes");

        StringWriter buffer = new StringWriter();
        out.setOutputWriter(new PrintWriter(buffer));

        XsltTransformer trans = xsltExecutable.load();
        trans.setInitialContextNode(source);
        trans.setDestination(out);

        if (params != null) {
            for (String p : params.keySet()) {
                trans.setParameter(new QName(p), new XdmAtomicValue(params.get(p).toString()));
            }
        }

        trans.transform();

        result = buffer.toString();

    } catch (SaxonApiException e) {

        this.logger.error("SaxonApiException: " + e.getMessage());
    }

    return result;
}

From source file:de.tu_dortmund.ub.api.daia.DaiaEndpoint.java

License:Open Source License

private String xmlOutputter(Document doc, String xslt, HashMap<String, String> params) throws IOException {

    String result = null;//  www . j av  a2s .  c  o m

    try {

        // Init XSLT-Transformer
        Processor processor = new Processor(false);
        XsltCompiler xsltCompiler = processor.newXsltCompiler();
        XsltExecutable xsltExecutable = xsltCompiler.compile(new StreamSource(xslt));

        XdmNode source = processor.newDocumentBuilder().build(new JDOMSource(doc));
        Serializer out = new Serializer();
        out.setOutputProperty(Serializer.Property.METHOD, "xml");
        out.setOutputProperty(Serializer.Property.INDENT, "yes");

        StringWriter buffer = new StringWriter();
        out.setOutputWriter(new PrintWriter(buffer));

        XsltTransformer trans = xsltExecutable.load();
        trans.setInitialContextNode(source);
        trans.setDestination(out);

        if (params != null) {
            for (String p : params.keySet()) {
                trans.setParameter(new QName(p), new XdmAtomicValue(params.get(p).toString()));
            }
        }

        trans.transform();

        result = buffer.toString();

    } catch (SaxonApiException e) {

        this.logger.error("SaxonApiException: " + e.getMessage());
    }

    return result;
}

From source file:de.tu_dortmund.ub.api.daia.DaiaOpenUrlEndpoint.java

License:Open Source License

private String htmlOutputter(org.jdom2.Document doc, String xslt, HashMap<String, String> params)
        throws IOException {

    String result = null;/*  w w w  .  j  a v  a  2 s  . co  m*/

    try {

        // Init XSLT-Transformer
        Processor processor = new Processor(false);
        XsltCompiler xsltCompiler = processor.newXsltCompiler();
        XsltExecutable xsltExecutable = xsltCompiler.compile(new StreamSource(xslt));

        XdmNode source = processor.newDocumentBuilder().build(new JDOMSource(doc));
        Serializer out = new Serializer();
        out.setOutputProperty(Serializer.Property.METHOD, "html");
        out.setOutputProperty(Serializer.Property.INDENT, "yes");

        StringWriter buffer = new StringWriter();
        out.setOutputWriter(new PrintWriter(buffer));

        XsltTransformer trans = xsltExecutable.load();
        trans.setInitialContextNode(source);
        trans.setDestination(out);

        if (params != null) {
            for (String p : params.keySet()) {
                trans.setParameter(new QName(p), new XdmAtomicValue(params.get(p).toString()));
            }
        }

        trans.transform();

        result = buffer.toString();

    } catch (SaxonApiException e) {

        e.printStackTrace();
        this.logger.error("SaxonApiException: " + e.getMessage());
    }

    return result;
}

From source file:de.tu_dortmund.ub.api.daia.DaiaOpenUrlEndpoint.java

License:Open Source License

private String xmlOutputter(org.jdom2.Document doc, String xslt, HashMap<String, String> params)
        throws IOException {

    String result = null;/*from w ww.  j a  v  a2s  .c  o  m*/

    try {

        // Init XSLT-Transformer
        Processor processor = new Processor(false);
        XsltCompiler xsltCompiler = processor.newXsltCompiler();
        XsltExecutable xsltExecutable = xsltCompiler.compile(new StreamSource(xslt));

        XdmNode source = processor.newDocumentBuilder().build(new JDOMSource(doc));
        Serializer out = new Serializer();
        out.setOutputProperty(Serializer.Property.METHOD, "xml");
        out.setOutputProperty(Serializer.Property.INDENT, "yes");

        StringWriter buffer = new StringWriter();
        out.setOutputWriter(new PrintWriter(buffer));

        XsltTransformer trans = xsltExecutable.load();
        trans.setInitialContextNode(source);
        trans.setDestination(out);

        if (params != null) {
            for (String p : params.keySet()) {
                trans.setParameter(new QName(p), new XdmAtomicValue(params.get(p).toString()));
            }
        }

        trans.transform();

        result = buffer.toString();

    } catch (SaxonApiException e) {

        e.printStackTrace();
        this.logger.error("SaxonApiException: " + e.getMessage());
    }

    return result;
}

From source file:de.tu_dortmund.ub.data.util.XmlTransformer.java

License:Open Source License

public static String xmlOutputter(Document doc, String xslt, HashMap<String, String> params)
        throws IOException {

    String result = null;/*  w ww  .  j a  v a  2s.  c om*/

    try {

        Processor processor = new Processor(false);

        XdmNode source = processor.newDocumentBuilder().build(new JDOMSource(doc));
        Serializer out = new Serializer();
        out.setOutputProperty(Serializer.Property.METHOD, "xml");
        out.setOutputProperty(Serializer.Property.INDENT, "yes");

        StringWriter buffer = new StringWriter();
        out.setOutputWriter(new PrintWriter(buffer));

        XsltCompiler xsltCompiler = processor.newXsltCompiler();
        XsltExecutable exp = xsltCompiler.compile(new StreamSource(xslt));
        XsltTransformer trans = exp.load();
        trans.setInitialContextNode(source);
        trans.setDestination(out);

        if (params != null) {
            for (String p : params.keySet()) {
                trans.setParameter(new QName(p), new XdmAtomicValue(params.get(p)));
            }
        }

        trans.transform();

        result = buffer.toString();

    } catch (SaxonApiException e) {

        e.printStackTrace();
    }

    return result;
}

From source file:de.tu_dortmund.ub.hb_ng.SolRDF.java

License:Open Source License

private String htmlOutputter(Document doc, String xslt, HashMap<String, String> params) throws IOException {

    String result = null;//from w  ww.  j  av  a2 s . c  o  m

    try {

        Processor processor = new Processor(false);
        XsltCompiler xsltCompiler = processor.newXsltCompiler();
        XsltExecutable xsltExecutable = xsltCompiler.compile(new StreamSource(xslt));

        XdmNode source = processor.newDocumentBuilder().build(new JDOMSource(doc));
        Serializer out = new Serializer();
        out.setOutputProperty(Serializer.Property.METHOD, "html");
        out.setOutputProperty(Serializer.Property.INDENT, "yes");

        StringWriter buffer = new StringWriter();
        out.setOutputWriter(new PrintWriter(buffer));

        XsltTransformer trans = xsltExecutable.load();
        trans.setInitialContextNode(source);
        trans.setDestination(out);

        if (params != null) {
            for (String p : params.keySet()) {
                trans.setParameter(new QName(p), new XdmAtomicValue(params.get(p)));
            }
        }

        trans.transform();

        result = buffer.toString();

    } catch (SaxonApiException e) {

        this.logger.error("SaxonApiException: " + e.getMessage());
    }

    return result;
}

From source file:edu.unc.lib.deposit.normalize.Proquest2N3BagJob.java

License:Apache License

/**
 * Transform the given root element from the data document into MODS and stores it as the metadata for the object
 * being ingested//from  w w w.  ja v a 2 s  . c  om
 *
 * @param primaryPID
 * @param dataRoot
 * @param modified
 * @throws TransformerException
 * @throws FileNotFoundException
 * @throws IOException
 */
private Document extractMods(PID primaryPID, Element dataRoot, DateTime modified)
        throws TransformerException, FileNotFoundException, IOException {

    int month = modified.getMonthOfYear();
    String gradSemester;

    if (month >= 2 && month <= 6) {
        gradSemester = "Spring";
    } else if (month >= 7 && month <= 9) {
        gradSemester = "Summer";
    } else {
        gradSemester = "Winter";
    }

    JDOMResult mods = new JDOMResult();
    // Transform the metadata into MODS
    synchronized (proquest2ModsTransformer) {
        proquest2ModsTransformer.setParameter("graduationSemester", gradSemester + " " + modified.getYear());
        proquest2ModsTransformer.transform(new JDOMSource(dataRoot), mods);
    }

    // Create the description folder and write the MODS out to it
    final File modsFolder = getDescriptionDir();
    modsFolder.mkdir();

    File modsFile = new File(modsFolder, primaryPID.getUUID() + ".xml");

    try (FileOutputStream fos = new FileOutputStream(modsFile)) {
        new XMLOutputter(Format.getPrettyFormat()).output(mods.getDocument(), fos);
    }

    return mods.getDocument();
}