Example usage for java.io Writer getClass

List of usage examples for java.io Writer getClass

Introduction

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

Prototype

@HotSpotIntrinsicCandidate
public final native Class<?> getClass();

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:com.netspective.commons.xml.ParseContext.java

public void doExternalTransformations() throws TransformerConfigurationException, TransformerException,
        ParserConfigurationException, SAXException, IOException {
    // re-create the input source because the original stream is already closed
    InputSource inputSource = recreateInputSource();

    Source activeSource = inputSource.getByteStream() != null ? new StreamSource(inputSource.getByteStream())
            : new StreamSource(inputSource.getCharacterStream());
    activeSource.setSystemId(inputSource.getSystemId());

    Writer activeResultBuffer = new StringWriter();
    Result activeResult = new StreamResult(activeResultBuffer);
    activeResult.setSystemId(activeResultBuffer.getClass().getName());

    TransformerFactory factory = TransformerFactory.newInstance();
    int lastTransformer = transformSources.length - 1;
    for (int i = 0; i <= lastTransformer; i++) {
        Transformer transformer = factory.newTransformer(transformSources[i]);
        transformer.transform(activeSource, activeResult);

        if (i < lastTransformer) {
            activeSource = new StreamSource(new StringReader(activeResultBuffer.toString()));
            activeResultBuffer = new StringWriter();
            activeResult = new StreamResult(activeResultBuffer);
        }// w  w w.j  ava  2  s .  c om
    }

    // now that all the transformations have been performed, we want to reset our input source to the final
    // transformation
    init(createInputSource(activeResultBuffer.toString()));
}

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

@SuppressWarnings("serial")
@Override//from   ww w .jav  a2  s. c o  m
public void execute(final Environment env, @SuppressWarnings("rawtypes") Map params, TemplateModel[] loopVars,
        final TemplateDirectiveBody body) throws TemplateException, IOException {
    try {

        Writer wout = env.getOut();
        if (wout instanceof GrailsRoutablePrintWriter) {
            wout = ((GrailsRoutablePrintWriter) wout).getOut();
        }

        tagInstance.invokeMethod("pushOut", wout);

        params = unwrapParams(params, true);
        if (log.isDebugEnabled()) {
            log.debug("wout is " + wout.getClass());
            log.debug("execute(): @" + namespace + "." + tagName);
            log.debug("execute(): params " + params);
            log.debug("execute(): body " + body);
            log.debug("hasReturnValue " + hasReturnValue);
        }
        Object result = null;
        if (tagInstance.getMaximumNumberOfParameters() == 1) {

            result = tagInstance.call(params);
        } else {
            Closure bodyClosure = EMPTY_BODY;

            if (body != null) {
                bodyClosure = new Closure(this) {

                    @SuppressWarnings({ "unused", "rawtypes", "unchecked" })
                    public Object doCall(Object it) throws IOException, TemplateException {
                        ObjectWrapper objectWrapper = env.getObjectWrapper();
                        Map<String, TemplateModel> oldVariables = null;
                        TemplateModel oldIt = null;
                        StringWriter bodyOutput = new StringWriter();

                        if (log.isDebugEnabled()) {
                            log.debug("doCall it " + it);
                        }

                        boolean itIsAMap = false;
                        if (it != null) {
                            if (it instanceof Map) {
                                itIsAMap = true;
                                oldVariables = new LinkedHashMap<String, TemplateModel>();
                                Map<String, Object> itMap = (Map) it;
                                for (Map.Entry<String, Object> entry : itMap.entrySet()) {
                                    oldVariables.put(entry.getKey(), env.getVariable(entry.getKey()));
                                }
                            } else {
                                oldIt = env.getVariable("it");
                            }
                        }

                        try {
                            if (it != null) {
                                if (itIsAMap) {
                                    Map<String, Object> itMap = (Map) it;
                                    for (Map.Entry<String, Object> entry : itMap.entrySet()) {
                                        env.setVariable(entry.getKey(), objectWrapper.wrap(entry.getValue()));
                                    }
                                } else {
                                    env.setVariable("it", objectWrapper.wrap(it));
                                }
                            }
                            //Writer wout = (Writer) tagInstance.getProperty("out");

                            body.render(new GrailsPrintWriter(bodyOutput));
                        } finally {
                            if (oldVariables != null) {
                                for (Map.Entry<String, TemplateModel> entry : oldVariables.entrySet()) {
                                    env.setVariable(entry.getKey(), entry.getValue());
                                }
                            } else if (oldIt != null) {
                                env.setVariable("it", oldIt);
                            }
                        }

                        //return "";
                        return bodyOutput.getBuffer().toString();
                    }
                };
            }

            result = tagInstance.call(new Object[] { params, bodyClosure });
        }

        if (log.isDebugEnabled()) {
            log.debug("hasReturnValue " + hasReturnValue);
            //log.debug("result " + result);
        }
        //FIXME this used to check for hasReturnValue but since I can't get out passed in right then I always append the result
        if (result != null && hasReturnValue) {
            //if (result != null) {
            env.getOut().append(result.toString());
        }
    } catch (RuntimeException e) {
        log.error(e.getMessage(), e);
        throw new TemplateException(e, env);
    } finally {
        tagInstance.invokeMethod("popOut", null);
    }
}

From source file:org.apache.struts2.components.template.FreemarkerTemplateEngine.java

public void renderTemplate(TemplateRenderingContext templateContext) throws Exception {
    // get the various items required from the stack
    ValueStack stack = templateContext.getStack();
    Map context = stack.getContext();
    ServletContext servletContext = (ServletContext) context.get(ServletActionContext.SERVLET_CONTEXT);
    HttpServletRequest req = (HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST);
    HttpServletResponse res = (HttpServletResponse) context.get(ServletActionContext.HTTP_RESPONSE);

    // prepare freemarker
    Configuration config = freemarkerManager.getConfiguration(servletContext);

    // get the list of templates we can use
    List templates = templateContext.getTemplate().getPossibleTemplates(this);

    // find the right template
    freemarker.template.Template template = null;
    String templateName = null;/*  w ww  .j av  a  2s .c  om*/
    Exception exception = null;
    for (Iterator iterator = templates.iterator(); iterator.hasNext();) {
        Template t = (Template) iterator.next();
        templateName = getFinalTemplateName(t);
        try {
            // try to load, and if it works, stop at the first one
            template = config.getTemplate(templateName);
            break;
        } catch (IOException e) {
            if (exception == null) {
                exception = e;
            }
        }
    }

    if (template == null) {
        LOG.error("Could not load template " + templateContext.getTemplate());
        if (exception != null) {
            throw exception;
        } else {
            return;
        }
    }

    if (LOG.isDebugEnabled()) {
        LOG.debug("Rendering template " + templateName);
    }

    ActionInvocation ai = ActionContext.getContext().getActionInvocation();

    Object action = (ai == null) ? null : ai.getAction();
    SimpleHash model = freemarkerManager.buildTemplateModel(stack, action, servletContext, req, res,
            config.getObjectWrapper());

    model.put("tag", templateContext.getTag());
    model.put("themeProperties", getThemeProps(templateContext.getTemplate()));

    // the BodyContent JSP writer doesn't like it when FM flushes automatically --
    // so let's just not do it (it will be flushed eventually anyway)
    Writer writer = templateContext.getWriter();
    if (bodyContent != null && bodyContent.isAssignableFrom(writer.getClass())) {
        final Writer wrapped = writer;
        writer = new Writer() {
            public void write(char cbuf[], int off, int len) throws IOException {
                wrapped.write(cbuf, off, len);
            }

            public void flush() throws IOException {
                // nothing!
            }

            public void close() throws IOException {
                wrapped.close();
            }
        };
    }

    try {
        stack.push(templateContext.getTag());
        template.process(model, writer);
    } finally {
        stack.pop();
    }
}

From source file:org.codehaus.groovy.grails.web.pages.GroovyPageOutputStack.java

private void checkExistingStack(final Writer topWriter) {
    if (topWriter != null) {
        for (StackEntry item : stack) {
            if (item.originalTarget == topWriter) {
                log.warn(//from w w w  .ja v a  2 s. com
                        "Pushed a writer to stack a second time. Writer type " + topWriter.getClass().getName(),
                        new Exception());
            }
        }
    }
}