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(); }
/** * Used from {@code console.jelly} to write annotated log to the given output. * * @since 1.349 */ public void writeLogTo(long offset, @Nonnull XMLOutput out) throws IOException { getLogText().writeHtmlTo(offset, out.asWriter()); }
output.startElement(tagName,actual); getTagBody().run(context,output); output.endElement(tagName); } catch (SAXException x) { throw new JellyTagException(x);
public static void applyTo(XMLOutput xo) { xo.setContentHandler(new TableNestChecker(xo.getContentHandler())); }
public String getHtml(ProjectConfigInfo projectConfigInfo) throws IOException, ClassNotFoundException, JellyException { String name = getClass().getName().replace('.', '/').replace('$', '/') + "/" + "index.jelly"; URL actionTemplate = getClass().getClassLoader().getResource(name); JellyContext context = new JellyContext(); context.setVariable("p", projectConfigInfo); context.setVariable("it", this); OutputStream outputStream = new ByteArrayOutputStream(); XMLOutput output = XMLOutput.createXMLOutput(outputStream); context.runScript(actionTemplate, output); output.flush(); return "<p>" + outputStream.toString() + " </p>"; }
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(); }
public void doTag(XMLOutput output) throws JellyTagException { try { output.startElement(uri, localName, qname, attributes); invokeBody(output); output.endElement(uri, localName, qname); } catch (SAXException e) { throw new JellyTagException(e); } }
/** Evaluates the body of a tag */ public void run(JellyContext context, XMLOutput output) throws JellyTagException { if ( text != null ) { try { output.write(text); } catch (SAXException e) { throw new JellyTagException("could not write to XMLOutput",e); } } } }
/** * Writes the body fo this tag to the given Writer */ protected void writeBody(Writer writer) throws SAXException, JellyTagException { XMLOutput newOutput = createXMLOutput(writer); try { // we need to avoid multiple start/end document events newOutput.setContentHandler( new SafeContentHandler(newOutput.getContentHandler()) ); newOutput.startDocument(); invokeBody(newOutput); newOutput.endDocument(); } finally { try { newOutput.close(); } catch (IOException e) {} } }
/** * Helper method to create a test suite from the given Jelly script * * @param script is the URL to the script which should create a TestSuite * @return a newly created TestSuite */ public static TestSuite createTestSuite(URL script) throws Exception { JellyContext context = new JellyContext(script); XMLOutput output = XMLOutput.createXMLOutput(System.out); context = context.runScript(script, output); TestSuite answer = (TestSuite) context.getVariable("org.apache.commons.jelly.junit.suite"); if ( answer == null ) { log.warn( "Could not find a TestSuite created by Jelly for the script:" + script ); // return an empty test suite return new TestSuite(); } return answer; } }
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)); }
public void setUp(String scriptName) throws Exception { context = new JellyContext(); xmlOutput = XMLOutput.createXMLOutput(new StringWriter()); jelly = new Jelly(); String script = scriptName; URL url = this.getClass().getResource(script); if ( url == null ) { throw new Exception( "Could not find Jelly script: " + script + " in package of class: " + this.getClass().getName() ); } jelly.setUrl(url); }
public void testOutputBadGood() throws Exception { setUpScript("outputBad.jelly"); Script script = getJelly().compileScript(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); XMLOutput ouput = XMLOutput.createXMLOutput(bos); script.run(getJellyContext(),ouput); ouput.flush(); assertEquals("<html></html>",bos.toString()); }
/** * Test simple 'raw' execution of a string. See JELLY-189. */ public void testRawExecuteAsString() throws Exception { String message = "<?xml version=\"1.0\"?>" + " <j:jelly xmlns:j=\"jelly:core\">" + "jelly-test-case" + " </j:jelly>"; ByteArrayOutputStream output = new ByteArrayOutputStream(); XMLOutput xmlOutput = XMLOutput.createXMLOutput(output); InputSource script = new InputSource( new StringReader(message.toString()) ); JellyContext context = new JellyContext(); context.runScript( script, xmlOutput); output.close(); //check that the output confirms the expected assertEquals("jelly-test-case", new String(output.toByteArray())); } }
private String compileJelly(JellyContext context, String template) { StringWriter writer = new StringWriter(); try { XMLOutput xmlOutput = XMLOutput.createXMLOutput(writer); String url = this.getClass() .getResource("MasterMailer.class").toString(); url = url.substring(0, url.lastIndexOf(".class")); context.runScript(url + "/" + template, xmlOutput); xmlOutput.flush(); } catch (Exception e) { e.printStackTrace(new PrintWriter(writer)); } return writer.toString(); }
/** * Parses the body of this tag and returns the parsed document */ protected void parseBody(XMLOutput output) throws JellyTagException { ContentHandler handler = getJellyParser(); XMLOutput newOutput = new XMLOutput(handler); try { handler.startDocument(); invokeBody(newOutput); handler.endDocument(); } catch (SAXException e) { throw new JellyTagException(e); } }
public void testDom4Xmlns() throws SAXException { StringWriter writer = new StringWriter(); OutputFormat format = new OutputFormat(); final XMLWriter xmlWriter = new HTMLWriter(writer, format); xmlWriter.setEscapeText(false); XMLOutput output = new XMLOutput(xmlWriter, xmlWriter); String golden = "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">\n"; golden += "<html>"; output.startDocument(); output.write(golden); output.endDocument(); assertEquals("output should contain the namespaces", golden, writer.toString()); }
/** * Factory method to create a new XMLOutput from an XMLWriter */ protected static XMLOutput createXMLOutput(final XMLWriter xmlWriter) { XMLOutput answer = new XMLOutput() { public void close() throws IOException { xmlWriter.close(); } }; answer.setContentHandler(xmlWriter); answer.setLexicalHandler(xmlWriter); return answer; }