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

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

Introduction

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

Prototype

public void endElement(String uri, String localName, String qName) throws SAXException;

Source Link

Document

Receive notification of the end of an element.

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>/*  w w  w  .j  a  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;// w ww.  j  a v a 2  s  . c om
    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  ww w  .j av  a 2  s . c  o  m
    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;//  ww w  . j  a v  a  2 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;/*from   www .  ja  va2  s. c  o m*/
    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  w  w  .  ja va 2  s  . c o  m
    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  w w .ja  v a 2s.co 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 .  ja  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:org.syncope.core.util.ImportExport.java

private void doExportTable(final TransformerHandler handler, final Connection conn, final String tableName)
        throws SQLException, SAXException {

    AttributesImpl atts = new AttributesImpl();

    PreparedStatement stmt = null;
    ResultSet rs = null;/*from ww w .j a  v  a2  s . c  o m*/
    try {
        stmt = conn.prepareStatement("SELECT * FROM " + tableName + " a");
        rs = stmt.executeQuery();
        for (int rowNo = 0; rs.next(); rowNo++) {
            atts.clear();

            ResultSetMetaData metaData = rs.getMetaData();
            for (int i = 0; i < metaData.getColumnCount(); i++) {
                String columnName = metaData.getColumnName(i + 1);
                String value = rs.getString(columnName);
                if (value != null) {
                    atts.addAttribute("", "", columnName, "CDATA", value);
                }
            }

            handler.startElement("", "", tableName, atts);
            handler.endElement("", "", tableName);
        }
    } finally {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                LOG.error("While closing result set", e);
            }
        }
        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException e) {
                LOG.error("While closing result set", e);
            }
        }
    }
}

From source file:com.smartitengineering.exim.impl.xml.GenericDataTypeExporter.java

public void exportElement(AssociationType type, Object object, TransformerHandler handler) throws IOException {
    if (type == null || object == null || handler == null) {
        throw new IOException("All parameters are required!");
    }//from  ww w.  j ava  2s  . c  o  m
    final String elementName = type.getSimpleName();
    final String value;
    switch (type) {
    case TYPE_DATE:
        value = DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.format((Date) object);
        break;
    case TYPE_STRING:
    case TYPE_INTEGER:
    case TYPE_LONG:
    case TYPE_FLOAT:
    case TYPE_DOUBLE:
    case TYPE_BOOLEAN:
    default:
        value = object == null ? null : object.toString();
        break;
    }
    try {
        AttributesImpl atts = new AttributesImpl();
        handler.startElement(EXIM_COLLECN_URI, EXIM_COLLECTION_NS, elementName, atts);
        if (AssociationType.TYPE_STRING.equals(type)) {
            handler.startCDATA();
        }
        final char[] chars;
        if (StringUtils.isBlank(value)) {
            chars = "".toCharArray();
        } else {
            chars = value.toCharArray();
        }
        handler.characters(chars, 0, chars.length);
        if (AssociationType.TYPE_STRING.equals(type)) {
            handler.endCDATA();
        }
        handler.endElement(EXIM_COLLECN_URI, EXIM_COLLECTION_NS, elementName);
    } catch (Exception exception) {
        throw new IOException(exception);
    }
}