List of usage examples for java.io StringWriter flush
public void flush()
From source file:nz.co.senanque.base.ObjectTest.java
@SuppressWarnings("unused") @Test// ww w . ja v a2 s . c om public void test1() throws Exception { ValidationSession validationSession = m_validationEngine.createSession(); // create a customer Customer customer = m_customerDAO.createCustomer(); validationSession.bind(customer); Invoice invoice = new Invoice(); invoice.setDescription("test invoice"); customer.getInvoices().add(invoice); boolean exceptionFound = false; try { customer.setName("ttt"); } catch (ValidationException e) { exceptionFound = true; } assertTrue(exceptionFound); final ObjectMetadata customerMetadata = validationSession.getMetadata(customer); final FieldMetadata customerTypeMetadata = customerMetadata.getFieldMetadata(Customer.CUSTOMERTYPE); assertFalse(customerTypeMetadata.isActive()); assertFalse(customerTypeMetadata.isReadOnly()); assertFalse(customerTypeMetadata.isRequired()); customer.setName("aaaab"); assertTrue(customerTypeMetadata.isActive()); assertTrue(customerTypeMetadata.isReadOnly()); assertTrue(customerTypeMetadata.isRequired()); exceptionFound = false; try { customer.setCustomerType("B"); } catch (Exception e) { exceptionFound = true; } assertTrue(exceptionFound); exceptionFound = false; try { customer.setCustomerType("XXX"); } catch (Exception e) { exceptionFound = true; } assertTrue(exceptionFound); customer.setBusiness(IndustryType.AG); customer.setAmount(new Double(500.99)); final long id = m_customerDAO.save(customer); log.info(id); // fetch customer back customer = m_customerDAO.getCustomer(id); final int invoiceCount = customer.getInvoices().size(); validationSession.bind(customer); invoice = new Invoice(); invoice.setDescription("test invoice2"); customer.getInvoices().add(invoice); m_customerDAO.save(customer); // fetch customer again customer = m_customerDAO.getCustomer(id); customer.toString(); validationSession.bind(customer); final Invoice inv = customer.getInvoices().get(0); customer.getInvoices().remove(inv); //ObjectMetadata metadata = validationSession.getMetadata(customer); ObjectMetadata metadata = customer.getMetadata(); org.junit.Assert.assertEquals("this is a description", metadata.getFieldMetadata(Customer.NAME).getDescription()); List<ChoiceBase> choices = metadata.getFieldMetadata(Customer.BUSINESS).getChoiceList(); assertEquals(1, choices.size()); List<ChoiceBase> choices2 = metadata.getFieldMetadata(Customer.CUSTOMERTYPE).getChoiceList(); assertEquals(2, choices2.size()); choices2 = metadata.getFieldMetadata(Customer.CUSTOMERTYPE).getChoiceList(); assertEquals(2, choices2.size()); customer.setName("aab"); choices2 = metadata.getFieldMetadata(Customer.CUSTOMERTYPE).getChoiceList(); assertEquals(6, choices2.size()); // Convert customer to XML QName qname = new QName("http://www.example.org/sandbox", "Session"); JAXBElement<Session> sessionJAXB = new JAXBElement<Session>(qname, Session.class, new Session()); sessionJAXB.getValue().getCustomers().add(customer); //??This fails to actually add StringWriter marshallWriter = new StringWriter(); Result marshallResult = new StreamResult(marshallWriter); m_marshaller.marshal(sessionJAXB, marshallResult); marshallWriter.flush(); String result = marshallWriter.getBuffer().toString().trim(); String xml = result.replaceAll("\\Qhttp://www.example.org/sandbox\\E", "http://www.example.org/sandbox"); log.info(xml); // Convert customer back to objects SAXBuilder builder = new SAXBuilder(); org.jdom.Document resultDOM = builder.build(new StringReader(xml)); @SuppressWarnings("unchecked") JAXBElement<Session> request = (JAXBElement<Session>) m_unmarshaller.unmarshal(new JDOMSource(resultDOM)); validationSession = m_validationEngine.createSession(); validationSession.bind(request.getValue()); assertEquals(3, validationSession.getProxyCount()); List<Customer> customers = request.getValue().getCustomers(); assertEquals(1, customers.size()); customers.clear(); assertEquals(1, validationSession.getProxyCount()); request.toString(); validationSession.close(); }
From source file:org.openhab.binding.samsungtv.internal.protocol.RemoteController.java
private String createKeyDataPayload(KeyCode key) throws IOException { /* @formatter:off//w w w . j a v a2 s .c o m * * Payload: * * offset value and description * ------ --------------------- * 0x18 three 0x00 bytes * 0x1b 0x000c - key code size (little endian) * 0x1d key code encoded as base64 string * * @formatter:on */ StringWriter writer = new StringWriter(); writer.append((char) 0x00); writer.append((char) 0x00); writer.append((char) 0x00); writeBase64String(writer, key.getValue()); writer.flush(); return writer.toString(); }
From source file:groovyx.net.http.EncoderRegistry.java
/** * Default handler used for a plain text content-type. Acceptable argument * types are://from w w w. j a v a 2s. co m * <ul> * <li>Closure</li> * <li>Writable</li> * <li>Reader</li> * </ul> * For Closure argument, a {@link PrintWriter} is passed as the single * argument to the closure. Any data sent to the writer from the * closure will be sent to the request content body. * @param data * @return an {@link HttpEntity} encapsulating this request data * @throws IOException */ public HttpEntity encodeText(Object data, Object contentType) throws IOException { if (data instanceof Closure) { StringWriter out = new StringWriter(); PrintWriter writer = new PrintWriter(out); ((Closure) data).call(writer); writer.close(); out.flush(); data = out; } else if (data instanceof Writable) { StringWriter out = new StringWriter(); ((Writable) data).writeTo(out); out.flush(); data = out; } else if (data instanceof Reader && !(data instanceof BufferedReader)) data = new BufferedReader((Reader) data); if (data instanceof BufferedReader) { StringWriter out = new StringWriter(); DefaultGroovyMethods.leftShift(out, (BufferedReader) data); data = out; } // if data is a String, we are already covered. if (contentType == null) contentType = ContentType.TEXT; return createEntity(contentType, data.toString()); }
From source file:org.openhab.binding.samsungtv.internal.protocol.RemoteController.java
private String createRegistrationPayload(String ip) throws IOException { /*/*w w w . j a va2s . c om*/ * Payload starts with 2 bytes: 0x64 and 0x00, then comes 3 strings * encoded with base64 algorithm. Every string is preceded by 2-bytes * field containing encoded string length. * * These three strings are as follow: * * remote control device IP, unique ID value to distinguish * controllers, name it will be displayed as controller name. */ StringWriter w = new StringWriter(); w.append((char) 0x64); w.append((char) 0x00); writeBase64String(w, ip); writeBase64String(w, uniqueId); writeBase64String(w, appName); w.flush(); return w.toString(); }
From source file:de.betterform.xml.dom.DOMUtil.java
public static String serializeToString(org.w3c.dom.Document doc) { try {/*from w w w .ja v a 2 s . c om*/ 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:edu.wisc.my.portlets.dmp.web.CachingXsltView.java
@SuppressWarnings("unchecked") @Override/* w w w . ja v a2s. com*/ protected void doTransform(Map model, Source source, HttpServletRequest request, HttpServletResponse response) throws Exception { final Map parameters = getParameters(model, request); final Serializable cacheKey = this.getCacheKey(model, source, parameters); String cachedData; synchronized (this.xsltResultCache) { cachedData = this.xsltResultCache.get(cacheKey); if (cachedData == null) { final StringWriter writer = new StringWriter(); final String encoding = response.getCharacterEncoding(); this.doTransform(source, parameters, new StreamResult(writer), encoding); cachedData = writer.getBuffer().toString(); this.xsltResultCache.put(cacheKey, cachedData); } } if (useWriter()) { final PrintWriter writer = response.getWriter(); writer.write(cachedData); writer.flush(); } else { final OutputStream outputStream = new BufferedOutputStream(response.getOutputStream()); outputStream.write(cachedData.getBytes()); outputStream.flush(); } }
From source file:nz.co.senanque.sandbox.ObjectTest.java
@Test public void test1() throws Exception { @SuppressWarnings("unused") Object en = IndustryType.fromValue("Ag"); ValidationSession validationSession = m_validationEngine.createSession(); // create a customer Customer customer = m_customerDAO.createCustomer(); validationSession.bind(customer);/*from ww w . j av a 2 s. c om*/ Invoice invoice = new Invoice(); invoice.setDescription("test invoice"); invoice.setTestBoolean(true); customer.getInvoices().add(invoice); boolean exceptionFound = false; try { customer.setName("ttt"); } catch (ValidationException e) { exceptionFound = true; } assertTrue(exceptionFound); final ObjectMetadata customerMetadata = validationSession.getMetadata(customer); @SuppressWarnings("unused") final FieldMetadata customerTypeMetadata = customerMetadata.getFieldMetadata(Customer.CUSTOMERTYPE); // assertFalse(customerTypeMetadata.isActive()); // assertFalse(customerTypeMetadata.isReadOnly()); // assertFalse(customerTypeMetadata.isRequired()); customer.setName("aaaab"); exceptionFound = false; try { customer.setCustomerType("B"); } catch (Exception e) { exceptionFound = true; } // assertTrue(exceptionFound); exceptionFound = false; try { customer.setCustomerType("XXX"); } catch (Exception e) { exceptionFound = true; } assertTrue(exceptionFound); customer.setBusiness(IndustryType.AG); customer.setAmount(new Double(500.99)); final long id = m_customerDAO.save(customer); log.info("{}", id); // fetch customer back customer = m_customerDAO.getCustomer(id); @SuppressWarnings("unused") final int invoiceCount = customer.getInvoices().size(); validationSession.bind(customer); invoice = new Invoice(); ValidationUtils.setDefaults(invoice); invoice.setDescription("test invoice2"); invoice.setTestBoolean(true); customer.getInvoices().add(invoice); assertEquals("xyz", invoice.getTestDefault()); assertEquals("Ag", invoice.getTestEnumDefault().value()); m_customerDAO.save(customer); // fetch customer again customer = m_customerDAO.getCustomer(id); customer.toString(); final Invoice inv0 = customer.getInvoices().get(0); assertTrue(inv0.isTestBoolean()); validationSession.bind(customer); final Invoice inv = customer.getInvoices().get(0); assertTrue(inv.isTestBoolean()); customer.getInvoices().remove(inv); //ObjectMetadata metadata = validationSession.getMetadata(customer); ObjectMetadata metadata = customer.getMetadata(); assertEquals("this is a description", metadata.getFieldMetadata(Customer.NAME).getDescription()); assertEquals("ABC", metadata.getFieldMetadata(Customer.NAME).getPermission()); @SuppressWarnings("unused") List<ChoiceBase> choices = metadata.getFieldMetadata(Customer.BUSINESS).getChoiceList(); // assertEquals(1,choices.size()); List<ChoiceBase> choices2 = metadata.getFieldMetadata(Customer.CUSTOMERTYPE).getChoiceList(); // assertEquals(1,choices2.size()); choices2 = metadata.getFieldMetadata(Customer.CUSTOMERTYPE).getChoiceList(); // assertEquals(1,choices2.size()); for (ChoiceBase choice : choices2) { System.out.println(choice.getDescription()); } customer.setName("aab"); choices2 = metadata.getFieldMetadata(Customer.CUSTOMERTYPE).getChoiceList(); // assertEquals(6,choices2.size()); // Convert customer to XML QName qname = new QName("http://www.example.org/sandbox", "Session"); JAXBElement<Session> sessionJAXB = new JAXBElement<Session>(qname, Session.class, new Session()); sessionJAXB.getValue().getCustomers().add(customer); //??This fails to actually add StringWriter marshallWriter = new StringWriter(); Result marshallResult = new StreamResult(marshallWriter); m_marshaller.marshal(sessionJAXB, marshallResult); marshallWriter.flush(); String result = marshallWriter.getBuffer().toString().trim(); String xml = result.replaceAll("\\Qhttp://www.example.org/sandbox\\E", "http://www.example.org/sandbox"); log.info("{}", xml); // Convert customer back to objects SAXBuilder builder = new SAXBuilder(); org.jdom.Document resultDOM = builder.build(new StringReader(xml)); @SuppressWarnings("unchecked") JAXBElement<Session> request = (JAXBElement<Session>) m_unmarshaller.unmarshal(new JDOMSource(resultDOM)); validationSession = m_validationEngine.createSession(); validationSession.bind(request.getValue()); assertEquals(3, validationSession.getProxyCount()); List<Customer> customers = request.getValue().getCustomers(); assertEquals(1, customers.size()); assertTrue(customers.get(0).getInvoices().get(0).isTestBoolean()); customers.clear(); assertEquals(1, validationSession.getProxyCount()); request.toString(); validationSession.close(); }
From source file:org.openhab.binding.samsungtv.internal.protocol.RemoteControllerLegacy.java
private String createKeyDataPayload(KeyCode key) throws IOException { /* @formatter:off/*from w w w . ja v a 2 s. c om*/ * * Payload: * * offset value and description * ------ --------------------- * 0x18 three 0x00 bytes * 0x1b 0x000c - key code size (little endian) * 0x1d key code encoded as base64 string * * @formatter:on */ StringWriter writer = new StringWriter(); writer.append((char) 0x00); writer.append((char) 0x00); writer.append((char) 0x00); writeBase64String(writer, key.getValue()); writer.flush(); return writer.toString(); }
From source file:com.jayway.restassured.internal.http.EncoderRegistry.java
/** * Default handler used for a plain text content-type. Acceptable argument * types are:/*from ww w . j a v a2s . c o m*/ * <ul> * <li>Closure</li> * <li>Writable</li> * <li>Reader</li> * </ul> * For Closure argument, a {@link PrintWriter} is passed as the single * argument to the closure. Any data sent to the writer from the * closure will be sent to the request content body. * * @param data * @return an {@link HttpEntity} encapsulating this request data * @throws IOException */ public HttpEntity encodeText(Object contentType, Object data) throws IOException { if (data instanceof Closure) { StringWriter out = new StringWriter(); PrintWriter writer = new PrintWriter(out); ((Closure) data).call(writer); writer.close(); out.flush(); data = out; } else if (data instanceof Writable) { StringWriter out = new StringWriter(); ((Writable) data).writeTo(out); out.flush(); data = out; } else if (data instanceof Reader && !(data instanceof BufferedReader)) { data = new BufferedReader((Reader) data); } if (data instanceof BufferedReader) { StringWriter out = new StringWriter(); DefaultGroovyMethods.leftShift(out, (BufferedReader) data); data = out; } // if data is a String, we are already covered. return createEntity(contentTypeToString(contentType), data); }
From source file:org.usapi.BaseSeleniumTest.java
private String getStacktraceAsString(Throwable t) { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw, true); t.printStackTrace(pw);// w ww . j a va2 s.com pw.flush(); sw.flush(); return sw.toString(); }