Search in sources :

Example 51 with TemplateModelException

use of freemarker.template.TemplateModelException in project freemarker by apache.

the class Execute method exec.

/**
 * Executes a method call.
 *
 * @param arguments a <tt>List</tt> of <tt>String</tt> objects containing the values
 * of the arguments passed to the method.
 * @return the <tt>TemplateModel</tt> produced by the method, or null.
 */
public Object exec(List arguments) throws TemplateModelException {
    String aExecute;
    StringBuilder aOutputBuffer = new StringBuilder();
    if (arguments.size() < 1) {
        throw new TemplateModelException("Need an argument to execute");
    }
    aExecute = (String) (arguments.get(0));
    try {
        Process exec = Runtime.getRuntime().exec(aExecute);
        // stdout from the process comes in here
        InputStream execOut = exec.getInputStream();
        try {
            Reader execReader = new InputStreamReader(execOut);
            char[] buffer = new char[OUTPUT_BUFFER_SIZE];
            int bytes_read = execReader.read(buffer);
            while (bytes_read > 0) {
                aOutputBuffer.append(buffer, 0, bytes_read);
                bytes_read = execReader.read(buffer);
            }
        } finally {
            execOut.close();
        }
    } catch (IOException ioe) {
        throw new TemplateModelException(ioe.getMessage());
    }
    return aOutputBuffer.toString();
}
Also used : TemplateModelException(freemarker.template.TemplateModelException) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException)

Example 52 with TemplateModelException

use of freemarker.template.TemplateModelException in project freemarker by apache.

the class CaptureOutput method getWriter.

public Writer getWriter(final Writer out, final Map args) throws TemplateModelException {
    String errmsg = "Must specify the name of the variable in " + "which to capture the output with the 'var' or 'local' or 'global' parameter.";
    if (args == null)
        throw new TemplateModelException(errmsg);
    boolean local = false, global = false;
    final TemplateModel nsModel = (TemplateModel) args.get("namespace");
    Object varNameModel = args.get("var");
    if (varNameModel == null) {
        varNameModel = args.get("local");
        if (varNameModel == null) {
            varNameModel = args.get("global");
            global = true;
        } else {
            local = true;
        }
        if (varNameModel == null) {
            throw new TemplateModelException(errmsg);
        }
    }
    if (args.size() == 2) {
        if (nsModel == null) {
            throw new TemplateModelException("Second parameter can only be namespace");
        }
        if (local) {
            throw new TemplateModelException("Cannot specify namespace for a local assignment");
        }
        if (global) {
            throw new TemplateModelException("Cannot specify namespace for a global assignment");
        }
        if (!(nsModel instanceof Environment.Namespace)) {
            throw new TemplateModelException("namespace parameter does not specify a namespace. It is a " + nsModel.getClass().getName());
        }
    } else if (args.size() != 1)
        throw new TemplateModelException("Bad parameters. Use only one of 'var' or 'local' or 'global' parameters.");
    if (!(varNameModel instanceof TemplateScalarModel)) {
        throw new TemplateModelException("'var' or 'local' or 'global' parameter doesn't evaluate to a string");
    }
    final String varName = ((TemplateScalarModel) varNameModel).getAsString();
    if (varName == null) {
        throw new TemplateModelException("'var' or 'local' or 'global' parameter evaluates to null string");
    }
    final StringBuilder buf = new StringBuilder();
    final Environment env = Environment.getCurrentEnvironment();
    final boolean localVar = local;
    final boolean globalVar = global;
    return new Writer() {

        @Override
        public void write(char[] cbuf, int off, int len) {
            buf.append(cbuf, off, len);
        }

        @Override
        public void flush() throws IOException {
            out.flush();
        }

        @Override
        public void close() throws IOException {
            SimpleScalar result = new SimpleScalar(buf.toString());
            try {
                if (localVar) {
                    env.setLocalVariable(varName, result);
                } else if (globalVar) {
                    env.setGlobalVariable(varName, result);
                } else {
                    if (nsModel == null) {
                        env.setVariable(varName, result);
                    } else {
                        ((Environment.Namespace) nsModel).put(varName, result);
                    }
                }
            } catch (java.lang.IllegalStateException ise) {
                // if somebody uses 'local' outside a macro
                throw new IOException("Could not set variable " + varName + ": " + ise.getMessage());
            }
        }
    };
}
Also used : TemplateModelException(freemarker.template.TemplateModelException) TemplateModel(freemarker.template.TemplateModel) IOException(java.io.IOException) SimpleScalar(freemarker.template.SimpleScalar) Environment(freemarker.core.Environment) TemplateScalarModel(freemarker.template.TemplateScalarModel) Writer(java.io.Writer)

Example 53 with TemplateModelException

use of freemarker.template.TemplateModelException in project freemarker by apache.

the class ObjectConstructor method exec.

public Object exec(List args) throws TemplateModelException {
    if (args.isEmpty()) {
        throw new TemplateModelException("This method must have at least one argument, the name of the class to instantiate.");
    }
    String classname = args.get(0).toString();
    Class cl = null;
    try {
        cl = ClassUtil.forName(classname);
    } catch (Exception e) {
        throw new TemplateModelException(e.getMessage());
    }
    BeansWrapper bw = BeansWrapper.getDefaultInstance();
    Object obj = bw.newInstance(cl, args.subList(1, args.size()));
    return bw.wrap(obj);
}
Also used : TemplateModelException(freemarker.template.TemplateModelException) BeansWrapper(freemarker.ext.beans.BeansWrapper) TemplateModelException(freemarker.template.TemplateModelException)

Example 54 with TemplateModelException

use of freemarker.template.TemplateModelException in project freemarker by apache.

the class StaticModelsTest method modelCaching.

@Test
public void modelCaching() throws Exception {
    BeansWrapper bw = new BeansWrapper(Configuration.VERSION_2_3_21);
    TemplateHashModel statics = bw.getStaticModels();
    TemplateHashModel s = (TemplateHashModel) statics.get(S.class.getName());
    assertNotNull(s);
    assertNotNull(s.get("F"));
    assertNotNull(s.get("m"));
    try {
        s.get("x");
        fail();
    } catch (TemplateModelException e) {
        assertThat(e.getMessage(), containsString("No such key"));
    }
    try {
        statics.get("no.such.ClassExists");
        fail();
    } catch (TemplateModelException e) {
        assertTrue(e.getCause() instanceof ClassNotFoundException);
    }
    TemplateModel f = s.get("F");
    assertTrue(f instanceof TemplateScalarModel);
    assertEquals(((TemplateScalarModel) f).getAsString(), "F OK");
    TemplateModel m = s.get("m");
    assertTrue(m instanceof TemplateMethodModelEx);
    assertEquals(((TemplateScalarModel) ((TemplateMethodModelEx) m).exec(new ArrayList())).getAsString(), "m OK");
    assertSame(s, statics.get(S.class.getName()));
    bw.clearClassIntrospecitonCache();
    TemplateHashModel sAfterClean = (TemplateHashModel) statics.get(S.class.getName());
    assertNotSame(s, sAfterClean);
    assertSame(sAfterClean, statics.get(S.class.getName()));
    assertNotNull(sAfterClean.get("F"));
    assertNotNull(sAfterClean.get("m"));
}
Also used : TemplateModelException(freemarker.template.TemplateModelException) TemplateHashModel(freemarker.template.TemplateHashModel) TemplateMethodModelEx(freemarker.template.TemplateMethodModelEx) ArrayList(java.util.ArrayList) TemplateScalarModel(freemarker.template.TemplateScalarModel) TemplateModel(freemarker.template.TemplateModel) Test(org.junit.Test)

Example 55 with TemplateModelException

use of freemarker.template.TemplateModelException in project freemarker by apache.

the class ErrorMessagesTest method markupOutputParameter.

@Test
public void markupOutputParameter() throws Exception {
    TemplateHTMLOutputModel html = HTMLOutputFormat.INSTANCE.fromMarkup("<p>a");
    for (Version ici : new Version[] { Configuration.VERSION_2_3_0, Configuration.VERSION_2_3_24 }) {
        BeansWrapper bw = new BeansWrapperBuilder(ici).build();
        TemplateHashModel thm = (TemplateHashModel) bw.wrap(new TestBean());
        {
            TemplateMethodModelEx m = (TemplateMethodModelEx) thm.get("m1");
            try {
                m.exec(Collections.singletonList(html));
                fail();
            } catch (TemplateModelException e) {
                assertThat(e.getMessage(), allOf(containsString("String"), containsString("convert"), containsString("markup_output"), containsString("Tip:"), containsString("?markup_string")));
            }
        }
        {
            TemplateMethodModelEx m = (TemplateMethodModelEx) thm.get("m2");
            try {
                m.exec(Collections.singletonList(html));
                fail();
            } catch (TemplateModelException e) {
                assertThat(e.getMessage(), allOf(containsString("Date"), containsString("convert"), containsString("markup_output"), not(containsString("?markup_string"))));
            }
        }
        for (String methodName : new String[] { "mOverloaded", "mOverloaded3" }) {
            TemplateMethodModelEx m = (TemplateMethodModelEx) thm.get(methodName);
            try {
                m.exec(Collections.singletonList(html));
                fail();
            } catch (TemplateModelException e) {
                assertThat(e.getMessage(), allOf(containsString("No compatible overloaded"), containsString("String"), containsString("markup_output"), containsString("Tip:"), containsString("?markup_string")));
            }
        }
        {
            TemplateMethodModelEx m = (TemplateMethodModelEx) thm.get("mOverloaded2");
            try {
                m.exec(Collections.singletonList(html));
                fail();
            } catch (TemplateModelException e) {
                assertThat(e.getMessage(), allOf(containsString("No compatible overloaded"), containsString("Integer"), containsString("markup_output"), not(containsString("?markup_string"))));
            }
        }
        {
            TemplateMethodModelEx m = (TemplateMethodModelEx) thm.get("mOverloaded4");
            Object r = m.exec(Collections.singletonList(html));
            if (r instanceof TemplateScalarModel) {
                r = ((TemplateScalarModel) r).getAsString();
            }
            assertEquals("<p>a", r);
        }
    }
}
Also used : TemplateModelException(freemarker.template.TemplateModelException) TemplateHashModel(freemarker.template.TemplateHashModel) Version(freemarker.template.Version) TemplateMethodModelEx(freemarker.template.TemplateMethodModelEx) TemplateScalarModel(freemarker.template.TemplateScalarModel) TemplateHTMLOutputModel(freemarker.core.TemplateHTMLOutputModel) Test(org.junit.Test)

Aggregations

TemplateModelException (freemarker.template.TemplateModelException)87 TemplateModel (freemarker.template.TemplateModel)24 IOException (java.io.IOException)21 TemplateScalarModel (freemarker.template.TemplateScalarModel)18 SimpleScalar (freemarker.template.SimpleScalar)17 BeanModel (freemarker.ext.beans.BeanModel)14 Environment (freemarker.core.Environment)13 Writer (java.io.Writer)13 ArrayList (java.util.ArrayList)13 HttpServletRequest (javax.servlet.http.HttpServletRequest)12 Map (java.util.Map)10 List (java.util.List)9 SimpleNumber (freemarker.template.SimpleNumber)7 TemplateHashModel (freemarker.template.TemplateHashModel)7 Iterator (java.util.Iterator)6 freemarker.core._TemplateModelException (freemarker.core._TemplateModelException)5 TemplateHashModelEx (freemarker.template.TemplateHashModelEx)5 TemplateMethodModelEx (freemarker.template.TemplateMethodModelEx)5 TemplateModelIterator (freemarker.template.TemplateModelIterator)5 UMAException (com.ibm.uma.UMAException)4