List of usage examples for javax.xml.transform OutputKeys METHOD
String METHOD
To view the source code for javax.xml.transform OutputKeys METHOD.
Click Source Link
From source file:com.diversityarrays.dalclient.DalUtil.java
public static void writeXmlResult(String xml, Writer w) throws IOException, TransformerException { StreamSource source = new StreamSource(new StringReader(xml)); TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); //$NON-NLS-1$ transformer.setOutputProperty(OutputKeys.METHOD, "xml"); //$NON-NLS-1$ transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$ transformer.setOutputProperty(OutputKeys.ENCODING, ENCODING_UTF_8); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); //$NON-NLS-1$ //$NON-NLS-2$ transformer.transform(source, new StreamResult(w)); }
From source file:de.betterform.xml.dom.DOMUtil.java
public static void prettyPrintDOMAsHTML(Node node, OutputStream stream) throws TransformerException { Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.METHOD, "html"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.transform(new DOMSource(node), new StreamResult(stream)); }
From source file:main.java.vasolsim.common.file.__NONUSEDREFERENCE_VaSOLSimExam.java
/** * Writes a digitized version of the VaSOLSim test, represented by an thisInstance of this class, * to a given file in//from w w w . jav a 2 s .c o m * XML format. Prior to writing, this function will (re)initialize teh Ciphers used to protect the file, and update * all protected information accordingly. * * @param simFile the file on the disk to be written * @param canOverwriteFile can this method call write over an existing file with content * * @return if the write operation was successful * * @throws VaSolSimException thrown if insufficient information to write the file is contained in VaSolSimTest * object. Please ensure a password is provided to protect answer data and potentially * email data if test statistics and notifications are reported. */ public boolean write(File simFile, boolean canOverwriteFile) throws VaSolSimException { if (simFile.isFile()) { if (!canOverwriteFile) { throw new VaSolSimException(ERROR_MESSAGE_FILE_ALREADY_EXISTS); } else { PrintWriter printWriter; try { printWriter = new PrintWriter(simFile); } catch (FileNotFoundException e) { throw new VaSolSimException(ERROR_MESSAGE_FILE_NOT_FOUND_AFTER_INTERNAL_CHECK); } printWriter.print(""); printWriter.close(); } } else { if (!simFile.getParentFile().isDirectory() && !simFile.getParentFile().mkdirs()) { throw new VaSolSimException(ERROR_MESSAGE_COULD_NOT_CREATE_DIRS); } try { if (!simFile.createNewFile()) { throw new VaSolSimException(ERROR_MESSAGE_COULD_NOT_CREATE_FILE); } } catch (IOException e) { throw new VaSolSimException(ERROR_MESSAGE_CREATE_FILE_EXCEPTION); } } //update crypto stuff updateCryptoEngine(); updateCryptoProperties(); /* * Check for any missing encrypted data that is required to read the test as specified */ if (encryptedValidationHash.length < 1) throw new VaSolSimException(ERROR_MESSAGE_VALIDATION_KEY_NOT_PROVIDED); /* * Leave out for client side information gathering */ /* if (isNotifyingCompletion && encryptedNotificationEmail.length < 1) throw new VaSolSimException("Notification requested with no email provided!"); */ if (isNotifyingCompletion && isNotifyingCompletionUsingStandaloneEmailParadigm && encryptedNotificationEmailPassword.length < 1) throw new VaSolSimException(ERROR_MESSAGE_STANDALONE_NOTIFICATION_PASSWORD_NOT_PROVIDED); /* if (isReportingStatistics && encryptedStatisticsEmail.length < 1) throw new VaSolSimException("Statistics requested with no email provided!"); */ if (isReportingStatistics && isReportingStatisticsUsingStandaloneEmailParadigm && encryptedStatisticsEmailPassword.length < 1) throw new VaSolSimException(ERROR_MESSAGE_STANDALONE_STATS_PASSWORD_NOT_PROVIDED); Document solExam; try { solExam = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); } catch (ParserConfigurationException e) { throw new VaSolSimException(ERROR_MESSAGE_INTERNAL_XML_PARSER_INITIALIZATION_EXCEPTION, e); } //create document root Element root = solExam.createElement(xmlRootElementName); solExam.appendChild(root); //append the information sub element Element information = solExam.createElement(xmlInfoElementName); root.appendChild(information); Element security = solExam.createElement(xmlSecurityElementName); root.appendChild(security); /* * Build information element tree */ createInformationElements(information, solExam); /* * Build security element tree */ createSecurityElements(security, solExam); try { Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "roles.dtd"); transformer.setOutputProperty(indentationKey, "4"); transformer.transform(new DOMSource(solExam), new StreamResult(new FileOutputStream(simFile))); } catch (FileNotFoundException e) { throw new VaSolSimException(ERROR_MESSAGE_FILE_NOT_FOUND_AFTER_INTERNAL_CHECK, e); } catch (TransformerConfigurationException e) { throw new VaSolSimException(ERROR_MESSAGE_INTERNAL_TRANSFORMER_CONFIGURATION, e); } catch (TransformerException e) { throw new VaSolSimException(ERROR_MESSAGE_INTERNAL_TRANSFORMER_EXCEPTION, e); } return true; }
From source file:com.diversityarrays.dalclient.DalUtil.java
public static void printXmlDocument(Document doc, Writer w) throws IOException, TransformerException { TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); //$NON-NLS-1$ transformer.setOutputProperty(OutputKeys.METHOD, "xml"); //$NON-NLS-1$ transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$ transformer.setOutputProperty(OutputKeys.ENCODING, ENCODING_UTF_8); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); //$NON-NLS-1$ //$NON-NLS-2$ transformer.transform(new DOMSource(doc), new StreamResult(w)); }
From source file:elh.eus.absa.CorpusReader.java
/** * print annotations in Semeval-absa 2015 format * * @param savePath string : path for the file to save the data * @throws ParserConfigurationException//from w w w .j a v a 2s.co m */ public void print2Semeval2015format(String savePath) throws ParserConfigurationException { DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); // root elements org.w3c.dom.Document doc = docBuilder.newDocument(); org.w3c.dom.Element rootElement = doc.createElement("Reviews"); doc.appendChild(rootElement); for (String rev : getReviews().keySet()) { // review elements org.w3c.dom.Element review = doc.createElement("Review"); rootElement.appendChild(review); // set id attribute to sentence element review.setAttribute("rid", rev); // Sentences element org.w3c.dom.Element sentences = doc.createElement("sentences"); review.appendChild(sentences); List<String> processed = new ArrayList<String>(); for (String sent : this.revSents.get(rev)) { if (processed.contains(sent)) { continue; } else { processed.add(sent); } //System.err.println("creating elements for sentence "+sent); // sentence elements org.w3c.dom.Element sentence = doc.createElement("sentence"); sentences.appendChild(sentence); // set attribute to sentence element sentence.setAttribute("id", sent); // text element of the current sentence org.w3c.dom.Element text = doc.createElement("text"); sentence.appendChild(text); text.setTextContent(getSentences().get(sent)); // Opinions element org.w3c.dom.Element opinions = doc.createElement("Opinions"); sentence.appendChild(opinions); for (Opinion op : getSentenceOpinions(sent)) { if (op.getCategory().equalsIgnoreCase("NULL")) { continue; } // opinion elements org.w3c.dom.Element opinion = doc.createElement("Opinion"); opinions.appendChild(opinion); // set attributes to the opinion element opinion.setAttribute("target", op.getTarget()); opinion.setAttribute("category", op.getCategory()); opinion.setAttribute("polarity", op.getPolarity()); opinion.setAttribute("from", op.getFrom().toString()); opinion.setAttribute("to", op.getTo().toString()); } } } // write the content into xml file try { TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(new File(savePath)); // Output to console for testing //StreamResult result = new StreamResult(System.out); transformer.transform(source, result); System.err.println("File saved to run.xml"); } catch (TransformerException e) { System.err.println("CorpusReader: error when trying to print generated xml result file."); e.printStackTrace(); } }
From source file:de.betterform.xml.dom.DOMUtil.java
public static String serializeToString(org.w3c.dom.Document doc) { try {/* w w w. j a va 2 s. c o m*/ DOMSource domSource = new DOMSource(doc); StringWriter writer = new StringWriter(); StreamResult result = new StreamResult(writer); TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.transform(domSource, result); writer.flush(); return writer.toString(); } catch (TransformerException ex) { ex.printStackTrace(); return null; } }
From source file:de.betterform.xml.xforms.model.Model.java
private XSModel loadSchema(Element element) throws TransformerException, IllegalAccessException, InstantiationException, ClassNotFoundException { Element copy = (Element) element.cloneNode(true); NamespaceResolver.applyNamespaces(element, copy); ByteArrayOutputStream stream = new ByteArrayOutputStream(); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.transform(new DOMSource(copy), new StreamResult(stream)); byte[] array = stream.toByteArray(); return loadSchema(new ByteArrayInputStream(array)); }
From source file:com.zacwolf.commons.wbxcon.WBXCONorg.java
/** * This is a helper method that returns a String form of a DOM {@link org.w3c.dom.Node} Object * starting at the parent/child level specified by root node * @param rootnode the DOM {@link org.w3c.dom.Node} to use as the root * @return an XML form of <code>rootnode</code> and any children * @throws IOException//from w ww. j a va2 s . c om * @throws TransformerException */ final static String documentToXMLstring(final Node rootnode) throws TransformerException { final StringWriter out = new StringWriter(); final TransformerFactory tf = TransformerFactory.newInstance(); final Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.INDENT, "no"); transformer.setOutputProperty(OutputKeys.ENCODING, org.apache.http.Consts.UTF_8.displayName()); transformer.transform(new DOMSource(rootnode), new StreamResult(out)); return out.toString(); }
From source file:com.zacwolf.commons.wbxcon.WBXCONorg.java
/** * This is a helper method that returns a String form of a {@link org.w3c.dom.Document} * object, fully indented into a "pretty print" format. * @param doc {@link org.w3c.dom.Document} to be parsed * @param out OutputStream to copy the text into * @throws IOException//from ww w . j a va 2s . c o m * @throws TransformerException */ final static void documentPrettyPrint(final Document doc, final OutputStream out) throws IOException, TransformerException { final Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, org.apache.http.Consts.UTF_8.displayName()); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); transformer.transform(new DOMSource(doc), new StreamResult(out)); }
From source file:gov.va.ds4p.ds4pmobileportal.ui.eHealthExchange.java
private String converXmlDocToString(Document xmlDocument) { String xmlString = ""; try {/* ww w. j a v a 2s . c om*/ TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); StringWriter writer = new StringWriter(); transformer.transform(new DOMSource(xmlDocument), new StreamResult(writer)); xmlString = writer.getBuffer().toString(); //.replaceAll("\n|\r", ""); } catch (Exception ex) { ex.printStackTrace(); } return xmlString; }