List of usage examples for javax.xml.transform.sax TransformerHandler startElement
public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException;
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 . ja v a 2 s . co m*/ * 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:Main.java
/** * Creates a {@link TransformerHandler}. * //from w ww . ja va 2s .c om * @param commentHeader the comment header * @param rootTag the root tag * @param streamResult stream result */ public static TransformerHandler createTransformerHandler(String commentHeader, String rootTag, StreamResult streamResult, Charset charset) throws TransformerFactoryConfigurationError, TransformerConfigurationException, SAXException { SAXTransformerFactory tf = (SAXTransformerFactory) TransformerFactory.newInstance(); try { tf.setAttribute("indent-number", new Integer(2)); } catch (Exception e) { // ignore, workaround for JDK 1.5 bug, see http://forum.java.sun.com/thread.jspa?threadID=562510 } TransformerHandler transformerHandler = tf.newTransformerHandler(); Transformer serializer = transformerHandler.getTransformer(); serializer.setOutputProperty(OutputKeys.ENCODING, charset.name()); serializer.setOutputProperty(OutputKeys.METHOD, "xml"); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); transformerHandler.setResult(streamResult); transformerHandler.startDocument(); String newline = System.getProperty("line.separator"); if (newline == null) { newline = "\n"; } commentHeader = (newline + commentHeader).replaceAll("\\n--", newline + " "); transformerHandler.characters("\n".toCharArray(), 0, 1); transformerHandler.comment(commentHeader.toCharArray(), 0, commentHeader.toCharArray().length); transformerHandler.characters("\n".toCharArray(), 0, 1); if (rootTag.length() > 0) { transformerHandler.startElement("", "", rootTag, null); } return transformerHandler; }
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 w w w . ja v a 2 s . c o m*/ 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 a v 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;//from w w w . ja v a 2s. 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 w w w .j a v a 2s . com 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.EpubInterface.java
private String produceXML(ArrayList<String[]> topics) throws TMFOutputException { LOG.debug("[produceXML] - BEGIN"); String xml;//w w 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("", "", "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 a 2 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 w w w.j av a 2 s . c o m 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:it.polito.tellmefirst.web.rest.interfaces.MapInterface.java
private String produceXML(String[] coordinates) throws TMFOutputException { LOG.debug("[produceXML] - BEGIN"); String xml;//w ww. ja v a2 s . c om 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; }