Example usage for javax.xml.transform.sax TransformerHandler endDocument

List of usage examples for javax.xml.transform.sax TransformerHandler endDocument

Introduction

In this page you can find the example usage for javax.xml.transform.sax TransformerHandler endDocument.

Prototype

public void endDocument() throws SAXException;

Source Link

Document

Receive notification of the end of a document.

Usage

From source file:IOUtils.java

/**
 * Checks if the used Trax implementation correctly handles namespaces set using
 * <code>startPrefixMapping()</code>, but wants them also as 'xmlns:' attributes.
 * <p>//from  w  ww  .  ja v a  2 s  . c  om
 * The check consists in sending SAX events representing a minimal namespaced document
 * with namespaces defined only with calls to <code>startPrefixMapping</code> (no
 * xmlns:xxx attributes) and check if they are present in the resulting text.
 */
protected static boolean needsNamespacesAsAttributes(Properties format)
        throws TransformerException, SAXException {
    // Serialize a minimal document to check how namespaces are handled.
    final StringWriter writer = new StringWriter();

    final String uri = "namespaceuri";
    final String prefix = "nsp";
    final String check = "xmlns:" + prefix + "='" + uri + "'";

    final TransformerHandler handler = FACTORY.newTransformerHandler();

    handler.getTransformer().setOutputProperties(format);
    handler.setResult(new StreamResult(writer));

    // Output a single element
    handler.startDocument();
    handler.startPrefixMapping(prefix, uri);
    handler.startElement(uri, "element", "element", new AttributesImpl());
    handler.endElement(uri, "element", "element");
    handler.endPrefixMapping(prefix);
    handler.endDocument();

    final String text = writer.toString();

    // Check if the namespace is there (replace " by ' to be sure of what we search in)
    boolean needsIt = (text.replace('"', '\'').indexOf(check) == -1);

    return needsIt;
}

From source file:it.polito.tellmefirst.web.rest.interfaces.TextInterface.java

private String produceXML(String title) throws TMFOutputException {
    LOG.debug("[produceXML] - BEGIN");
    String xml;//from ww  w .  j  a  v a 2 s. com
    try {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        TransformerHandler hd = initXMLDoc(out);
        AttributesImpl atts = new AttributesImpl();
        atts.addAttribute("", "", "title", "", title);
        hd.startElement("", "", "Enhancement", null);
        hd.startElement("", "", "Result", atts);
        hd.endElement("", "", "Result");
        hd.endElement("", "", "Enhancement");
        hd.endDocument();
        xml = out.toString("utf-8");
    } catch (Exception e) {
        throw new TMFOutputException("Error creating XML output.", e);

    }
    LOG.debug("[produceXML] - END");
    return xml;
}

From source file:it.polito.tellmefirst.web.rest.interfaces.VideoInterface.java

private String produceXML(String videoURL) throws TMFOutputException {
    LOG.debug("[produceXML] - BEGIN");
    String xml;//from  w  w w. j  ava 2  s.c om
    try {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        TransformerHandler hd = initXMLDoc(out);
        AttributesImpl atts = new AttributesImpl();
        atts.addAttribute("", "", "videoURL", "", videoURL);
        hd.startElement("", "", "Enhancement", null);
        hd.startElement("", "", "Result", atts);
        hd.endElement("", "", "Result");
        hd.endElement("", "", "Enhancement");
        hd.endDocument();
        xml = out.toString("utf-8");
    } catch (Exception e) {
        throw new TMFOutputException("Error creating XML output.", e);
    }
    LOG.debug("[produceXML] - END");
    return xml;
}

From source file:it.polito.tellmefirst.web.rest.interfaces.ImageInterface.java

private String produceXML(String imageURL) throws TMFOutputException {
    LOG.debug("[produceXML] - BEGIN");
    String xml;//from ww  w. j a v  a2 s  .c o m
    try {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        TransformerHandler hd = initXMLDoc(out);
        AttributesImpl atts = new AttributesImpl();
        atts.addAttribute("", "", "imageURL", "", imageURL);
        hd.startElement("", "", "Enhancement", null);
        hd.startElement("", "", "Result", atts);
        hd.endElement("", "", "Result");
        hd.endElement("", "", "Enhancement");
        hd.endDocument();
        xml = out.toString("utf-8");
    } catch (Exception e) {
        throw new TMFOutputException("Error creating XML output.", e);
    }
    LOG.debug("[produceXML] - END");
    return xml;
}

From source file:it.polito.tellmefirst.web.rest.interfaces.AbstractInterface.java

private String produceXML(String abstracto) throws TMFOutputException {
    String xml;// w w  w .  j  a  v a  2 s . c om
    LOG.debug("[produceXML] - BEGIN");
    try {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        TransformerHandler hd = initXMLDoc(out);
        AttributesImpl atts = new AttributesImpl();
        atts.addAttribute("", "", "abstract", "", abstracto);
        hd.startElement("", "", "Enhancement", null);
        hd.startElement("", "", "Result", atts);
        hd.endElement("", "", "Result");
        hd.endElement("", "", "Enhancement");
        hd.endDocument();
        xml = out.toString("utf-8");
        System.out.println(xml);
    } catch (Exception e) {
        throw new TMFOutputException("Error creating XML output.", e);
    }
    LOG.debug("[produceXML] - END");
    return xml;
}

From source file:it.polito.tellmefirst.web.rest.interfaces.MapInterface.java

private String produceXML(String[] coordinates) throws TMFOutputException {
    LOG.debug("[produceXML] - BEGIN");
    String xml;//w ww.  j ava  2  s . com
    String res1 = "";
    String res2 = "";
    try {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        TransformerHandler hd = initXMLDoc(out);
        AttributesImpl atts = new AttributesImpl();
        if (coordinates[0] != null) {
            res1 = coordinates[0];
        }
        if (coordinates[1] != null) {
            res2 = coordinates[1];
        }
        atts.addAttribute("", "", "lat", "", res1);
        atts.addAttribute("", "", "long", "", res2);
        hd.startElement("", "", "Enhancement", null);
        hd.startElement("", "", "Map", atts);
        hd.endElement("", "", "Map");
        hd.endElement("", "", "Enhancement");
        hd.endDocument();
        xml = out.toString("utf-8");
    } catch (Exception e) {
        throw new TMFOutputException("Error creating XML output.", e);
    }
    LOG.debug("[produceXML] - END");
    return xml;
}

From source file:it.polito.tellmefirst.web.rest.interfaces.EpubInterface.java

private String produceXML(ArrayList<String[]> topics) throws TMFOutputException {
    LOG.debug("[produceXML] - BEGIN");
    String xml;//from   w  ww  . ja va2  s  .  c  o  m
    try {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        TransformerHandler hd = initXMLDoc(out);
        AttributesImpl atts = new AttributesImpl();
        atts.addAttribute("", "", "service", "", "ClassifyEpub");
        hd.startElement("", "", "Classification", atts);
        int i = 0;
        for (String[] topic : topics) {
            if (i == 0) {
                atts.clear();
                hd.startElement("", "", "Resources", atts);
            }
            atts.addAttribute("", "", "uri", "", topic[0]);
            atts.addAttribute("", "", "label", "", topic[1]);
            atts.addAttribute("", "", "title", "", topic[2]);
            atts.addAttribute("", "", "score", "", topic[3]);
            atts.addAttribute("", "", "mergedTypes", "", topic[4]);
            atts.addAttribute("", "", "image", "", topic[5]);
            hd.startElement("", "", "Resource", atts);
            hd.endElement("", "", "Resource");
            i++;
        }
        if (i > 0)
            hd.endElement("", "", "Resources");
        hd.endElement("", "", "Classification");
        hd.endDocument();
        xml = out.toString("utf-8");
        System.out.println(xml);
    } catch (Exception e) {
        throw new TMFOutputException("Error creating XML output.", e);
    }
    LOG.debug("[produceXML] - END");
    return xml;
}

From source file:it.polito.tellmefirst.web.rest.interfaces.ClassifyInterface.java

private String produceXML(List<String[]> topics) throws TMFOutputException {
    LOG.debug("[produceXML] - BEGIN");
    String xml;/*from   w w w .j  a  v  a2  s .co m*/
    try {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        TransformerHandler hd = initXMLDoc(out);
        AttributesImpl atts = new AttributesImpl();
        atts.addAttribute("", "", "service", "", "Classify");
        hd.startElement("", "", "Classification", atts);
        int i = 0;
        for (String[] topic : topics) {
            if (i == 0) {
                atts.clear();
                hd.startElement("", "", "Resources", atts);
            }
            atts.addAttribute("", "", "uri", "", topic[0]);
            atts.addAttribute("", "", "label", "", topic[1]);
            atts.addAttribute("", "", "title", "", topic[2]);
            atts.addAttribute("", "", "score", "", topic[3]);
            atts.addAttribute("", "", "mergedTypes", "", topic[4]);
            atts.addAttribute("", "", "image", "", ""); // We should remove this parameter in favour of Wikimedia/Wikidata API
            hd.startElement("", "", "Resource", atts);
            hd.endElement("", "", "Resource");
            i++;
        }
        if (i > 0)
            hd.endElement("", "", "Resources");
        hd.endElement("", "", "Classification");
        hd.endDocument();
        xml = out.toString("utf-8");
        System.out.println(xml);
    } catch (Exception e) {
        throw new TMFOutputException("Error creating XML output.", e);
    }
    LOG.debug("[produceXML] - END");
    return xml;
}

From source file:it.polito.tellmefirst.web.rest.interfaces.EpubChaptersInterface.java

private String produceXML(HashMap<String, List<String[]>> chapters) throws TMFOutputException {

    LOG.debug("[produceXML] - BEGIN");

    String xml;//from   www .  ja  v  a2  s  .  c  om
    try {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        TransformerHandler hd = initXMLDoc(out);
        AttributesImpl atts = new AttributesImpl();
        atts.addAttribute("", "", "service", "", "ClassifyEpubChapters");
        hd.startElement("", "", "Classification", atts);
        int i = 0;
        Set set = chapters.entrySet();
        Iterator iterator = set.iterator();
        while (iterator.hasNext()) {
            Map.Entry me = (Map.Entry) iterator.next();
            atts.clear();
            if (i == 0) {
                hd.startElement("", "", "Chapters", atts);
            }
            atts.addAttribute("", "", "toc", "", me.getKey().toString());
            hd.startElement("", "", "Chapter", atts);
            atts.clear();
            ArrayList<String[]> topics = (ArrayList<String[]>) me.getValue();
            int j = 0;
            for (String[] topic : topics) {
                atts.clear();
                if (j == 0) {
                    hd.startElement("", "", "Resources", atts);
                }
                atts.addAttribute("", "", "uri", "", topic[0]);
                atts.addAttribute("", "", "label", "", topic[1]);
                atts.addAttribute("", "", "title", "", topic[2]);
                atts.addAttribute("", "", "score", "", topic[3]);
                atts.addAttribute("", "", "mergedTypes", "", topic[4]);
                atts.addAttribute("", "", "image", "", topic[5]);
                hd.startElement("", "", "Resource", atts);
                hd.endElement("", "", "Resource");
                j++;
            }
            if (j > 0)
                hd.endElement("", "", "Resources");
            hd.endElement("", "", "Chapter");
            i++;
        }
        if (i > 0)
            hd.endElement("", "", "Chapters");
        hd.endElement("", "", "Classification");
        hd.endDocument();
        xml = out.toString("utf-8");
        System.out.println(xml);
    } catch (Exception e) {
        throw new TMFOutputException("Error creating XML output.", e);
    }

    LOG.debug("[produceXML] - END");

    return xml;
}

From source file:org.syncope.core.util.ImportExport.java

public void export(final OutputStream os)
        throws SAXException, TransformerConfigurationException, CycleInMultiParentTreeException {

    StreamResult streamResult = new StreamResult(os);
    SAXTransformerFactory transformerFactory = (SAXTransformerFactory) SAXTransformerFactory.newInstance();

    TransformerHandler handler = transformerFactory.newTransformerHandler();
    Transformer serializer = handler.getTransformer();
    serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    serializer.setOutputProperty(OutputKeys.INDENT, "yes");
    handler.setResult(streamResult);//www .  j ava 2s  . c  om
    handler.startDocument();
    handler.startElement("", "", ROOT_ELEMENT, new AttributesImpl());

    Connection conn = DataSourceUtils.getConnection(dataSource);
    ResultSet rs = null;
    try {
        // first read all tables...
        rs = conn.getMetaData().getTables(null, null, null, new String[] { "TABLE" });
        Set<String> tableNames = new HashSet<String>();
        while (rs.next()) {
            String tableName = rs.getString("TABLE_NAME");
            // these tables must be ignored
            if (!tableName.toUpperCase().startsWith("QRTZ_")
                    && !tableName.toUpperCase().equals("ACT_GE_PROPERTY")) {

                tableNames.add(tableName);
            }
        }
        // then sort tables based on foreign keys and dump
        for (String tableName : sortByForeignKeys(conn, tableNames)) {

            doExportTable(handler, conn, tableName);
        }
    } catch (SQLException e) {
        LOG.error("While exporting database content", e);
    } finally {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                LOG.error("While closing tables result set", e);
            }
        }
        DataSourceUtils.releaseConnection(conn, dataSource);
    }

    handler.endElement("", "", ROOT_ELEMENT);
    handler.endDocument();
}