Refine search
/** * Evaluate a Jelly script and return output as a String. * * @since 1.267 */ public static String runScript(Script script) throws JellyTagException { StringWriter out = new StringWriter(); script.run(getCurrentJellyContext(), XMLOutput.createXMLOutput(out)); return out.toString(); }
/** * Gets the System property 'java.runtime.version' and compares it with, * well, the same system property */ public void testSimpleSystemInvoke() throws Exception { setUpScript( "testInvokeStaticTag.jelly" ); Script script = getJelly().compileScript(); getJellyContext().setVariable( "test.simpleSystemInvoke",Boolean.TRUE ); getJellyContext().setVariable( "propertyName", "java.runtime.version" ); script.run( getJellyContext(),getXMLOutput() ); assertTrue( System.getProperty( "java.runtime.version" ).equals( getJellyContext().getVariable("propertyName" ) ) ); }
/** * Adds {@link XMLOutput} to the context. */ public void run(JellyContext context, XMLOutput output) throws JellyTagException { HttpServletRequest request = (HttpServletRequest)context.getVariable("request"); Set<String> executedScripts = (Set<String>) request.getAttribute(ONCE_TAG_KEY); if(executedScripts==null) request.setAttribute(ONCE_TAG_KEY,executedScripts=new HashSet<String>()); String key = getFileName()+':'+getLineNumber()+':'+getColumnNumber(); if(executedScripts.add(key)) // run it just for the first time getTagBody().run(context,output); } };
/** * Sets the System property 'TEST PROPERTY' to the value 'Jelly is cool' and compares it with, * well, the same system property */ public void testSystemInvoke() throws Exception { setUpScript( "testInvokeStaticTag.jelly" ); Script script = getJelly().compileScript(); getJellyContext().setVariable( "test.systemInvoke",Boolean.TRUE ); getJellyContext().setVariable( "propertyName", "TEST PROPERTY" ); getJellyContext().setVariable( "propertyValue", "Jelly is cool" ); script.run( getJellyContext(),getXMLOutput() ); assertTrue( System.getProperty( "TEST PROPERTY" ).equals( "Jelly is cool" ) ); }
public void testArgs() throws Exception { InputStream in = new FileInputStream("src/test/org/apache/commons/jelly/test_args.jelly"); XMLParser parser = new XMLParser(); Script script = parser.parse(in); script = script.compile(); log.debug("Found: " + script); assertTrue("Parsed a Script", script instanceof Script); String[] args = { "one", "two", "three" }; JellyContext context = new JellyContext(); context.setVariable("args", args); StringWriter buffer = new StringWriter(); script.run(context, XMLOutput.createXMLOutput(buffer)); String text = buffer.toString().trim(); if (log.isDebugEnabled()) { log.debug("Evaluated script as..."); log.debug(text); } assertEquals("Produces the correct output", "one two three", text); }
/** * Evaluates the body and obtains it as a string. * Uses standard XMLOutput.createXMLOutput() to generate output unless the JellyContext * contains a variable named org.apache.commons.jelly.XMLOutputFactory which is an object * of that type. */ protected String getBodyText(JellyContext context, boolean shouldEscape) throws JellyTagException { StringWriter writer = new StringWriter(); XMLOutputFactory xof = (XMLOutputFactory)context.getVariable(XMLOutputFactory.class.getName()); getTagBody().run(context, xof!=null ? xof.createXMLOutput(writer, shouldEscape) : XMLOutput.createXMLOutput(writer, shouldEscape)); return writer.toString(); }
output.startElement(tagName,actual); getTagBody().run(context,output); output.endElement(tagName); } catch (SAXException x) { throw new JellyTagException(x);
private String convert(Run<?, ?> build, JellyContext context, Script script) throws JellyTagException, IOException { ByteArrayOutputStream output = new ByteArrayOutputStream(16 * 1024); XMLOutput xmlOutput = XMLOutput.createXMLOutput(output); script.run(context, xmlOutput); xmlOutput.flush(); xmlOutput.close(); output.close(); return output.toString(getCharset(build)); }
/** * CDATA sections should be retained in the output. * * @throws Exception */ public void testCData() throws Exception { Jelly jelly = new Jelly(); jelly.setScript("file:src/test/org/apache/commons/jelly/test/xml/testCData.jelly"); Script script = jelly.compileScript(); JellyContext context = new JellyContext(); script.run(context, XMLOutput.createDummyXMLOutput()); String output = (String) context.getVariable("foo"); assertTrue("'foo' is not null", output != null); String golden = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; golden += "<!DOCTYPE foo [\n"; golden += " <!ELEMENT foo (#PCDATA)>\n"; golden += "]><foo></foo>"; assertEquals("output should contain the CDATA section", golden, output); }
public void testOutputData() throws Exception { setUpScript("outputData.jelly"); Script script = getJelly().compileScript(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); XMLOutput ouput = XMLOutput.createXMLOutput(bos); script.run(getJellyContext(),ouput); ouput.flush(); assertEquals("[string]",bos.toString().trim()); } }
protected void runTest() throws Throwable { // create a new child context so that each test case // will have its own variable scopes JellyContext newContext = new JellyContext( context ); // disable inheritence of variables and tag libraries newContext.setExportLibraries(false); newContext.setExport(false); // invoke the test case getBody().run(newContext, output); } };
public void doTag(XMLOutput output) throws JellyTagException { JellyContext newContext = context.newJellyContext(); getBody().run(newContext, output); } }
public void run(JellyContext context, XMLOutput output) throws JellyTagException { Functions.initPageVariables(context); s.run(context,output); } },self,new XMLOutput(new DefaultHandler()));
/** * Attempts to parse the script from the given uri using the * {@link #getResource} method then returns the compiled script. */ public Script compileScript(String uri) throws JellyException { XMLParser parser = getXMLParser(); parser.setContext(this); InputStream in = getResourceAsStream(uri); if (in == null) { throw new JellyException("Could not find Jelly script: " + uri); } Script script = null; try { script = parser.parse(in); } catch (IOException e) { throw new JellyException(JellyContext.BAD_PARSE+" : "+uri, e); } catch (SAXException e) { throw new JellyException(JellyContext.BAD_PARSE+" : "+uri, e); } return script.compile(); }
/** * Attempts to parse the script from the given InputSource using the * {@link #getResource} method then returns the compiled script. */ public Script compileScript(InputSource source) throws JellyException { XMLParser parser = getXMLParser(); parser.setContext(this); Script script = null; try { script = parser.parse(source); } catch (IOException e) { throw new JellyException(JellyContext.BAD_PARSE+" : "+source.getSystemId(), e); } catch (SAXException e) { throw new JellyException(JellyContext.BAD_PARSE+" : "+source.getSystemId(), e); } return script.compile(); }
/** * Compiles the tags body */ public Script compile() throws JellyException { if (tagBody != null) { tagBody = tagBody.compile(); } return this; }
/** * Retrieves Integer.MAX_VALUE using tag and verifies against direct * access. */ public void testGetIntegerMaxValue() throws Exception { setUpScript( "testGetStaticTag.jelly" ); Script script = getJelly().compileScript(); getJellyContext().setVariable( "test.Integer.MAX_VALUE", Boolean.TRUE ); script.run( getJellyContext(), getXMLOutput() ); assertEquals( new Integer(java.lang.Integer.MAX_VALUE), getJellyContext().getVariable("value" ) ); }
/** * Adds {@link XMLOutput} to the context. */ public void run(JellyContext context, XMLOutput output) throws JellyTagException { HttpServletRequest request = (HttpServletRequest)context.getVariable("request"); Set<String> executedScripts = (Set<String>) request.getAttribute(ONCE_TAG_KEY); if(executedScripts==null) request.setAttribute(ONCE_TAG_KEY,executedScripts=new HashSet<String>()); String key = getFileName()+':'+getLineNumber()+':'+getColumnNumber(); if(executedScripts.add(key)) // run it just for the first time getTagBody().run(context,output); } };