Example usage for java.io CharArrayWriter CharArrayWriter

List of usage examples for java.io CharArrayWriter CharArrayWriter

Introduction

In this page you can find the example usage for java.io CharArrayWriter CharArrayWriter.

Prototype

public CharArrayWriter() 

Source Link

Document

Creates a new CharArrayWriter.

Usage

From source file:org.grails.plugin.freemarker.TagLibToDirectiveAndFunction.java

@SuppressWarnings("serial")
@Override//from  w  ww.j  a  va  2s . c  o  m
public Object exec(@SuppressWarnings("rawtypes") List arguments) throws TemplateModelException {
    if (log.isDebugEnabled()) {
        log.debug("exec(): @" + namespace + "." + tagName);
    }
    try {
        CharArrayWriter writer = new CharArrayWriter();
        tagInstance.invokeMethod("pushOut", writer);

        Object args = null;
        Object body = null;
        if (arguments != null) {
            if (arguments.size() > 0) {
                args = arguments.get(0);
            }
            if (arguments.size() > 1) {
                body = arguments.get(1);
            }
        }

        Object result = null;
        args = args != null ? unwrapParams((TemplateHashModelEx) args, Boolean.TRUE)
                : unwrapParams(Collections.EMPTY_MAP, Boolean.TRUE);
        if (log.isDebugEnabled()) {
            log.debug("exec(): args " + args);
            log.debug("exec(): body " + body);
        }

        if (tagInstance.getMaximumNumberOfParameters() == 1) {
            result = tagInstance.call(args);
        } else {
            Closure bodyClosure = EMPTY_BODY;

            if (body != null || hasReturnValue) {
                final Object fBody = body;
                bodyClosure = new Closure(this) {

                    @SuppressWarnings("unused")
                    public Object doCall(Object it) throws IOException, TemplateException {
                        return fBody;
                    }

                };
            }

            result = tagInstance.call(new Object[] { args, bodyClosure });
            if (result == null) {
                // writer.flush();
                result = writer.toString();
            }
        }

        return result;
    } catch (RuntimeException e) {
        throw new TemplateModelException(e);
    } finally {
        tagInstance.invokeMethod("popOut", null);
    }
}

From source file:grails.plugin.freemarker.TagLibToDirectiveAndFunction.java

@SuppressWarnings("serial")
@Override/*www .  jav  a  2 s.  c o m*/
public Object exec(@SuppressWarnings("rawtypes") List arguments) throws TemplateModelException {
    if (log.isDebugEnabled()) {
        log.debug("exec(): @" + namespace + "." + tagName);
    }
    try {
        CharArrayWriter writer = new CharArrayWriter();
        tagInstance.invokeMethod("pushOut", writer);

        Object args = null;
        Object body = null;
        if (arguments != null) {
            if (arguments.size() > 0) {
                args = arguments.get(0);
            }
            if (arguments.size() > 1) {
                body = arguments.get(1);
            }
        }

        Object result = null;
        args = args != null ? unwrapParams((TemplateHashModelEx) args, true)
                : unwrapParams(Collections.EMPTY_MAP, true);

        if (tagInstance.getMaximumNumberOfParameters() == 1) {
            result = tagInstance.call(args);
        } else {
            Closure bodyClosure = EMPTY_BODY;

            if (body != null || hasReturnValue) {
                final Object fBody = body;
                bodyClosure = new Closure(this) {
                    @SuppressWarnings("unused")
                    public Object doCall(Object it) throws IOException, TemplateException {
                        return fBody;
                    }
                };
            }

            result = tagInstance.call(new Object[] { args, bodyClosure });
            if (result == null) {
                // writer.flush();
                result = writer.toString();
            }
        }

        return result;
    } catch (RuntimeException e) {
        throw new TemplateModelException(e);
    } finally {
        tagInstance.invokeMethod("popOut", null);
    }
}

From source file:org.milyn.delivery.dom.SmooksVisitorPhaseTest.java

public void test_filtering() throws IOException, SAXException {
    Smooks smooks = new Smooks();
    BasicExecutionEventListener eventListener = new BasicExecutionEventListener();

    smooks.addConfigurations("config2.xml", getClass().getResourceAsStream("config2.xml"));
    // Create an exec context - no profiles....
    ExecutionContext executionContext = smooks.createExecutionContext();
    CharArrayWriter outputWriter = new CharArrayWriter();

    // Filter the input message to the outputWriter, using the execution context...
    executionContext.setEventListener(eventListener);
    smooks.filterSource(executionContext, new StreamSource(getClass().getResourceAsStream("testxml1.xml")),
            new StreamResult(outputWriter));

    log.debug(outputWriter.toString());/*w  w  w . j  a  v a  2s. co  m*/
    byte[] expected = StreamUtils.readStream(getClass().getResourceAsStream("testxml1-expected.xml"));
    assertTrue(StreamUtils.compareCharStreams(new ByteArrayInputStream(expected),
            new ByteArrayInputStream(outputWriter.toString().getBytes())));
    assertEquals(32, eventListener.getEvents().size());
}

From source file:org.dhatim.delivery.dom.SmooksVisitorPhaseTest.java

@Test
public void test_filtering() throws IOException, SAXException {
    Smooks smooks = new Smooks();
    BasicExecutionEventListener eventListener = new BasicExecutionEventListener();

    smooks.addConfigurations("config2.xml", getClass().getResourceAsStream("config2.xml"));
    // Create an exec context - no profiles....
    ExecutionContext executionContext = smooks.createExecutionContext();
    CharArrayWriter outputWriter = new CharArrayWriter();

    // Filter the input message to the outputWriter, using the execution context...
    executionContext.setEventListener(eventListener);
    smooks.filterSource(executionContext, new StreamSource(getClass().getResourceAsStream("testxml1.xml")),
            new StreamResult(outputWriter));

    log.debug(outputWriter.toString());/*from   w  w  w.j  a va 2s  .  c o m*/
    byte[] expected = StreamUtils.readStream(getClass().getResourceAsStream("testxml1-expected.xml"));
    assertTrue(StreamUtils.compareCharStreams(new ByteArrayInputStream(expected),
            new ByteArrayInputStream(outputWriter.toString().getBytes())));
    assertEquals(32, eventListener.getEvents().size());
}

From source file:org.jbpm.bpel.wsdl.xml.WsdlUtil.java

public static Definition writeAndRead(Definition definition) throws WSDLException {
    // write the definition to an in-memory sink
    CharArrayWriter memorySink = new CharArrayWriter();
    factory.newWSDLWriter().writeWSDL(definition, memorySink);
    // read the definition back from memory
    try {/*w  ww  .ja  v  a  2  s . c  om*/
        return factory.newWSDLReader().readWSDL(definition.getDocumentBaseURI(),
                XmlUtil.parseText(memorySink.toString()));
    } catch (SAXException e) {
        throw new WSDLException(WSDLException.PARSER_ERROR, "could not read the WSDL definitions back", e);
    }
}

From source file:org.dita.dost.TestUtils.java

/**
 * Read XML file contents into a string.
 * /* w ww . jav  a 2s .c  o m*/
 * @param file XML file to read
 * @param normalize normalize whitespace
 * @return contents of the file
 * @throws Exception if parsing the file failed
 */
public static String readXmlToString(final File file, final boolean normalize, final boolean clean)
        throws Exception {
    final Writer std = new CharArrayWriter();
    InputStream in = null;
    try {
        in = new BufferedInputStream(new FileInputStream(file));
        final Transformer t = TransformerFactory.newInstance().newTransformer();
        XMLReader p = XMLReaderFactory.createXMLReader();
        p.setEntityResolver(CatalogUtils.getCatalogResolver());
        if (normalize) {
            t.setOutputProperty(OutputKeys.INDENT, "yes");
            p = new NormalizingXMLFilterImpl(p);
        }
        if (clean) {
            p = new CleaningXMLFilterImpl(p);
        }
        t.transform(new SAXSource(p, new InputSource(in)), new StreamResult(std));
    } finally {
        if (in != null) {
            in.close();
        }
    }
    return std.toString();
}

From source file:org.apereo.portal.portlets.sitemap.SitemapTest.java

@Test
public void testStylesheetCompilation() throws IOException {
    final MockHttpServletRequest request = new MockHttpServletRequest();
    Resource resource = new ClassPathResource(STYLESHEET_LOCATION);
    Source source = new StreamSource(resource.getInputStream(), resource.getURI().toASCIIString());
    try {/* w ww  .  j  a va  2 s  .  c om*/
        Transformer transformer = TransformerFactory.newInstance().newTransformer(source);
        transformer.setParameter(SitemapPortletController.USE_TAB_GROUPS, useTabGroups);
        transformer.setParameter(SitemapPortletController.USER_LANG, "en_US");
        transformer.setParameter(XsltPortalUrlProvider.CURRENT_REQUEST, request);
        transformer.setParameter(XsltPortalUrlProvider.XSLT_PORTAL_URL_PROVIDER, this.xsltPortalUrlProvider);

        Source xmlSource = new StreamSource(new ClassPathResource(XML_LOCATION).getFile());
        CharArrayWriter buffer = new CharArrayWriter();
        TransformerUtils.enableIndenting(transformer);
        transformer.transform(xmlSource, new StreamResult(buffer));
        if (logger.isTraceEnabled()) {
            logger.trace("XML: " + new String(buffer.toCharArray()));
        }
    } catch (TransformerConfigurationException e) {
        logger.error(e.getMessage(), e);
        throw new RuntimeException(e);
    } catch (TransformerException e) {
        logger.error(e.getMessage(), e);
        throw new RuntimeException(e);
    }
}

From source file:com.silverpeas.jcrutil.model.impl.AbstractJcrTestCase.java

protected String readFile(String path) throws IOException {
    CharArrayWriter writer = null;
    InputStream in = null;//ww w.  j av a2s  . com
    Reader reader = null;
    try {
        in = new FileInputStream(path);
        writer = new CharArrayWriter();
        reader = new InputStreamReader(in);
        char[] buffer = new char[8];
        int c = 0;
        while ((c = reader.read(buffer)) != -1) {
            writer.write(buffer, 0, c);
        }
        return new String(writer.toCharArray());
    } catch (IOException ioex) {
        return null;
    } finally {
        if (reader != null) {
            reader.close();
        }
        if (in != null) {
            in.close();
        }
        if (writer != null) {
            writer.close();
        }
    }
}

From source file:org.sakaiproject.search.component.adapter.contenthosting.XLContentDigester.java

public Reader getContentReader(ContentResource contentResource) {
    CharArrayWriter writer = new CharArrayWriter();
    loadContent(writer, contentResource);
    return new CharArrayReader(writer.toCharArray());
}

From source file:org.apache.jasper.compiler.JspUtil.java

public static char[] removeQuotes(char[] chars) {
    CharArrayWriter caw = new CharArrayWriter();
    for (int i = 0; i < chars.length; i++) {
        if (chars[i] == '%' && chars[i + 1] == '\\' && chars[i + 2] == '>') {
            caw.write('%');
            caw.write('>');
            i = i + 2;/*  ww w  .j  av a 2 s . co  m*/
        } else {
            caw.write(chars[i]);
        }
    }
    return caw.toCharArray();
}