List of usage examples for java.io StringWriter flush
public void flush()
From source file:com.zimbra.cs.service.AutoDiscoverServlet.java
private static String createResponseDoc(String displayName, String email, String serviceUrl) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true);//from w w w . jav a 2 s .c o m DocumentBuilder builder = factory.newDocumentBuilder(); Document xmlDoc = builder.newDocument(); Element root = xmlDoc.createElementNS(NS, "Autodiscover"); root.setAttribute("xmlns", NS); root.setAttribute("xmlns:xsi", XSI_NS); root.setAttribute("xmlns:xsd", XSD_NS); xmlDoc.appendChild(root); //Add the response element. Element response = xmlDoc.createElementNS(NS_MOBILE, "Response"); root.appendChild(response); //Add culture to to response Element culture = xmlDoc.createElement("Culture"); culture.appendChild(xmlDoc.createTextNode("en:en")); response.appendChild(culture); //User Element user = xmlDoc.createElement("User"); Element displayNameElm = xmlDoc.createElement("DisplayName"); displayNameElm.appendChild(xmlDoc.createTextNode(displayName)); user.appendChild(displayNameElm); Element emailAddr = xmlDoc.createElement("EMailAddress"); emailAddr.appendChild(xmlDoc.createTextNode(email)); user.appendChild(emailAddr); response.appendChild(user); //Action Element action = xmlDoc.createElement("Action"); Element settings = xmlDoc.createElement("Settings"); Element server = xmlDoc.createElement("Server"); Element type = xmlDoc.createElement("Type"); type.appendChild(xmlDoc.createTextNode("MobileSync")); server.appendChild(type); Element url = xmlDoc.createElement("Url"); url.appendChild(xmlDoc.createTextNode(serviceUrl)); server.appendChild(url); Element name = xmlDoc.createElement("Name"); name.appendChild(xmlDoc.createTextNode(serviceUrl)); server.appendChild(name); settings.appendChild(server); action.appendChild(settings); response.appendChild(action); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); DOMSource source = new DOMSource(xmlDoc); StringWriter writer = new StringWriter(); StreamResult result = new StreamResult(writer); transformer.transform(source, result); writer.flush(); String xml = writer.toString(); writer.close(); //manually generate xmlns for Autodiscover and Response element, this works //for testexchangeconnectivity.com, but iOS and Android don't like Response's xmlns // StringBuilder str = new StringBuilder(); // str.append("<?xml version=\"1.0\"?>\n"); // str.append("<Autodiscover xmlns:xsd=\"").append(XSD_NS).append("\""); // str.append(" xmlns:xsi=\"").append(XSI_NS).append("\""); // str.append(" xmlns=\"").append(NS).append("\">\n"); // int respIndex = xml.indexOf("<Response>"); // str.append("<Response xmlns=\"").append(NS_MOBILE).append("\">"); // str.append(xml.substring(respIndex + "<Response>".length(), xml.length())); // return str.toString(); return "<?xml version=\"1.0\"?>\n" + xml; }
From source file:com.zimbra.cs.service.AutoDiscoverServlet.java
private static String createResponseDocForEws(String displayName, String email, String serviceUrl, Account acct) throws Exception { Provisioning prov = Provisioning.getInstance(); Server server = prov.getServer(acct); String cn = server.getCn();/*from w w w . j av a 2s .co m*/ String name = server.getName(); String acctId = acct.getId(); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); DocumentBuilder builder = factory.newDocumentBuilder(); Document xmlDoc = builder.newDocument(); Element root = xmlDoc.createElementNS(NS, "Autodiscover"); root.setAttribute("xmlns", NS); root.setAttribute("xmlns:xsi", XSI_NS); root.setAttribute("xmlns:xsd", XSD_NS); xmlDoc.appendChild(root); //Add the response element. Element response = xmlDoc.createElementNS(NS_OUTLOOK, "Response"); root.appendChild(response); //User Element user = xmlDoc.createElement("User"); Element displayNameElm = xmlDoc.createElement("DisplayName"); displayNameElm.appendChild(xmlDoc.createTextNode(displayName)); user.appendChild(displayNameElm); Element emailAddr = xmlDoc.createElement("EmailAddress"); emailAddr.appendChild(xmlDoc.createTextNode(email)); user.appendChild(emailAddr); Element depId = xmlDoc.createElement("DeploymentId"); depId.appendChild(xmlDoc.createTextNode(acctId)); user.appendChild(depId); response.appendChild(user); //Action Element account = xmlDoc.createElement("Account"); Element acctType = xmlDoc.createElement("AccountType"); acctType.appendChild(xmlDoc.createTextNode("email")); account.appendChild(acctType); response.appendChild(account); Element action = xmlDoc.createElement("Action"); action.appendChild(xmlDoc.createTextNode("settings")); account.appendChild(action); Element protocol = xmlDoc.createElement("Protocol"); account.appendChild(protocol); Element type = xmlDoc.createElement("Type"); type.appendChild(xmlDoc.createTextNode("EXCH")); protocol.appendChild(type); Element ews = xmlDoc.createElement("EwsUrl"); protocol.appendChild(ews); ews.appendChild(xmlDoc.createTextNode(serviceUrl)); Element as = xmlDoc.createElement("ASUrl"); protocol.appendChild(as); as.appendChild(xmlDoc.createTextNode(serviceUrl)); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); DOMSource source = new DOMSource(xmlDoc); StringWriter writer = new StringWriter(); StreamResult result = new StreamResult(writer); transformer.transform(source, result); writer.flush(); String xml = writer.toString(); writer.close(); return "<?xml version=\"1.0\"?>\n" + xml; }
From source file:Main.java
/** * Convert XML DOM document to a XML string representation * * @param doc/*from w w w. j a va 2 s.c om*/ * XML DOM document * @return XML string * @throws Exception * in error case */ public static String xmlDOMDocumentToString(Document doc) throws Exception { if (doc == null) { throw new RuntimeException("No XML DOM document (null)!"); } StringWriter stringWriter = new StringWriter(); String strDoc = null; try { StreamResult streamResult = new StreamResult(stringWriter); TransformerFactory transformerFactory = TransformerFactory.newInstance(); // transformerFactory.setAttribute("nIndent-number", new Integer(4)); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.transform(new DOMSource(doc.getDocumentElement()), streamResult); stringWriter.flush(); strDoc = stringWriter.toString(); } catch (Exception e) { // Logger.XMLEval.logState("Parsing of XML DOM document failed: " + e.getMessage(), LogLevel.Error); throw e; } finally { if (stringWriter != null) { stringWriter.close(); stringWriter = null; } } return strDoc; }
From source file:org.sakaiproject.util.StorageUtils.java
/** * Write a DOM Document to an output stream. * //from w w w .ja va2s . co m * @param doc * The DOM Document to write. * @param out * The output stream. */ public static String writeDocumentToString(Document doc) { try { StringWriter sw = new StringWriter(); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); DOMImplementation impl = builder.getDOMImplementation(); DOMImplementationLS feature = (DOMImplementationLS) impl.getFeature("LS", "3.0"); LSSerializer serializer = feature.createLSSerializer(); LSOutput output = feature.createLSOutput(); output.setCharacterStream(sw); output.setEncoding("UTF-8"); serializer.write(doc, output); sw.flush(); return sw.toString(); } catch (Exception any) { M_log.warn("writeDocumentToString: " + any.toString()); return null; } }
From source file:com.rockagen.commons.util.JsonUtil.java
/** * Bean to json string/* ww w .j av a 2s . c o m*/ * * @param obj obj * bean object * @param <T> t * @return json string */ public static <T> String toJson(T obj) { StringWriter writer = new StringWriter(); String jsonStr = ""; JsonGenerator gen = null; try { gen = getJsonFactory().createGenerator(writer); getMapper().writeValue(gen, obj); writer.flush(); jsonStr = writer.toString(); } catch (IOException e) { log.error("{}", e.getMessage(), e); } finally { if (gen != null) { try { gen.close(); } catch (IOException e) { } } } return jsonStr; }
From source file:framework.GlobalHelpers.java
public static String call(Closure closure) { Writer currentWriter = FrontController.threadData.get().getOut(); StringWriter stringWriter = new StringWriter(); FrontController.threadData.get().setOut(stringWriter); closure.call();//ww w. j a v a 2 s.co m stringWriter.flush(); FrontController.threadData.get().setOut(currentWriter); return stringWriter.toString(); }
From source file:com.adito.util.Utils.java
/** * Prepares a string for output inside a JavaScript string, * e.g. for use inside a document.write("") command. * * Example:/* ww w . j a v a2 s .c om*/ * <pre> * input string: He didn't say, "Stop!" * output string: He didn\'t say, \"Stop!\" * </pre> * * Deals with quotes and control-chars (tab, backslash, cr, ff, etc.) * Bug: does not yet properly escape Unicode / high-bit characters. * * @see #jsEscape(String, Writer) **/ public static String jsEscape(String source) { try { StringWriter sw = new StringWriter(); jsEscape(source, sw); sw.flush(); return sw.toString(); } catch (IOException ioe) { // should never happen writing to a StringWriter ioe.printStackTrace(); return null; } }
From source file:com.adito.util.Utils.java
/** * @see #javaEscape(String, Writer)//from w w w .ja va 2 s . co m **/ public static String javaEscape(String source) { try { StringWriter sw = new StringWriter(); javaEscape(source, sw); sw.flush(); return sw.toString(); } catch (IOException ioe) { // should never happen writing to a StringWriter ioe.printStackTrace(); return null; } }
From source file:org.newcashel.meta.model.NCClass.java
public static String getPrimaryKeyValue(String[] keys, Inst inst) throws Exception { try {//from w w w. j av a2s. c om StringWriter sw = new StringWriter(); for (int i = 0; i < keys.length; i++) { Object value = PropertyUtils.getProperty(inst, keys[i]); if (value == null) { return null; // primary key part may not be available, ES will assign id } sw.write(value.toString()); if ((i + 1) < keys.length) { sw.write("_"); } } sw.flush(); return sw.toString(); } catch (Exception e) { String msg = "primary key build failed on this class: " + inst.getType(); logger.error(msg); throw new Exception(msg); } }
From source file:com.intuit.tank.transform.scriptGenerator.ConverterUtil.java
public static String getWorkloadXML(HDWorkload hdWorkload) { StringWriter sw; try {// w ww . ja v a 2 s . co m JAXBContext context = JAXBContext.newInstance(HDWorkload.class.getPackage().getName()); Marshaller createMarshaller = context.createMarshaller(); createMarshaller.setProperty("jaxb.formatted.output", Boolean.TRUE); sw = new StringWriter(); createMarshaller.marshal(hdWorkload, sw); sw.flush(); sw.close(); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } return sw.toString(); }