ctx = new JellyContext(); ctx.setVariable("resURL", req.getContextPath() + Jenkins.RESOURCE_PATH); } else { ctx = null;
public RenderOnDemandClosure(JellyContext context, String attributesToCapture) { List<Script> bodyStack = new ArrayList<Script>(); for (JellyContext c = context; c!=null; c=c.getParent()) { Script script = (Script) c.getVariables().get("org.apache.commons.jelly.body"); if(script!=null) bodyStack.add(script); } this.bodyStack = bodyStack.toArray(new Script[bodyStack.size()]); assert !bodyStack.isEmpty(); // there must be at least one, which is the direct child of <l:renderOnDemand> Map<String,Object> variables = new HashMap<String, Object>(); for (String v : Util.fixNull(attributesToCapture).split(",")) variables.put(v.intern(),context.getVariable(v)); // capture the current base of context for descriptors currentDescriptorByNameUrl = Descriptor.getCurrentDescriptorByNameUrl(); this.variables = PackedMap.of(variables); Set<String> _adjuncts = AdjunctsInPage.get().getIncluded(); this.adjuncts = new String[_adjuncts.size()]; int i = 0; for (String adjunct : _adjuncts) { this.adjuncts[i++] = adjunct.intern(); } }
/** * Evaluates the Jelly script submitted by the client. * * This is useful for system administration as well as unit testing. */ @RequirePOST public void doEval(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException { checkPermission(RUN_SCRIPTS); try { MetaClass mc = WebApp.getCurrent().getMetaClass(getClass()); Script script = mc.classLoader.loadTearOff(JellyClassLoaderTearOff.class).createContext().compileScript(new InputSource(req.getReader())); new JellyRequestDispatcher(this,script).forward(req,rsp); } catch (JellyException e) { throw new ServletException(e); } }
/** * 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" ) ) ); }
public void doTag(XMLOutput output) throws JellyTagException { if ( var == null ) { throw new JellyTagException( "<define:script> must have a var attribute" ); } context.setVariable( var, getBody() ); }
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>"; }
/** * 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 testInvokeThatDoesNotHandleException() throws Exception { setUpScript( "testInvokeStaticTag.jelly" ); Script script = getJelly().compileScript(); getJellyContext().setVariable("test.invokeThatDoesNotHandleException",Boolean.TRUE); script.run(getJellyContext(),getXMLOutput()); String exceptionMessage = (String) getJellyContext().getVariable("exceptionMessage"); assertNotNull( exceptionMessage ); JellyException jellyException = (JellyException) getJellyContext().getVariable("jellyException"); assertNotNull( jellyException ); assertTrue( "messages are the same", ! exceptionMessage.equals(jellyException.getMessage()) ); assertTrue( "exception '" + jellyException.getMessage() + "' does not ends with '" + exceptionMessage+"'", jellyException.getMessage().endsWith(exceptionMessage) ); assertNotNull( jellyException.getCause() ); assertEquals( exceptionMessage, jellyException.getCause().getMessage() ); }
/** Sets the value of the named variable */ public void setVariable(String name, Object value) { if ( isExport() ) { getParent().setVariable( name, value ); return; } variables.put(name, value); }
/** * Parses the script from the given file then compiles it and runs it. * * @return the new child context that was used to run the script */ public JellyContext runScript(File file, XMLOutput output, boolean export, boolean inherit) throws JellyException { try { return runScript(file.toURL(), output, export, inherit); } catch (MalformedURLException e) { throw new JellyException(e.toString()); } }
public void doTag(XMLOutput output) throws BreakException { boolean broken = false; if (test == null || test.evaluateAsBoolean(context)) { broken = true; } if ( var != null ) { context.setVariable( this.var, String.valueOf(broken)); } if ( broken ) { throw new BreakException(); } }
public void setUp() throws Exception { super.setUp(); jelly = new Jelly(); context = new JellyContext(); xmlOutput = XMLOutput.createDummyXMLOutput(); }
public Object evaluate(JellyContext context) { Object answer = jexlExpression.evaluate(context); if ( answer == null ) { answer = context.getVariable(text); } return answer; }
public void testOutermost() throws Exception { // performs one nested include setUp("a.jelly"); Script script = jelly.compileScript(); script.run(context,xmlOutput); assertTrue("should have set 'c' variable to 'true'", context.getVariable("c").equals("true")); assertTrue("should have set 'b' variable to 'true'", context.getVariable("b").equals("true")); assertTrue("should have set 'a' variable to 'true'", context.getVariable("a").equals("true")); }
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); }
/** * Adds {@link XMLOutput} to the context. */ public void run(JellyContext context, XMLOutput output) throws JellyTagException { context.setVariable(getAttribute("var").evaluateAsString(context),output); } };
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 MissingAttributeException, JellyTagException { if (uri == null && file == null) { throw new MissingAttributeException("uri"); } // we need to create a new JellyContext of the URI // take off the script name from the URL String text = null; try { if (uri != null) { text = uri; context.runScript(uri, output, isExport(), isInherit()); } else { text = file.toString(); context.runScript(file, output, isExport(), isInherit()); } } catch (JellyException e) { throw new JellyTagException("could not include jelly script: " + text + ". Reason: " + e, e); } }
public void testSetVariablesAndRetainContextEntry() { final JellyContext jc = new JellyContext(); assertNotNull( "Initial variable of context", jc.getVariable( "context" ) ); jc.setVariables( new HashMap() ); assertNotNull( "Value after setVariables()", jc.getVariable( "context" ) ); } }
@Override protected Script resolveDefinition(JellyContext context) throws JellyTagException { Object it = context.getVariable("it"); if (it==null) throw new JellyTagException("'it' was not defined"); try { MetaClass c = WebApp.getCurrent().getMetaClass(it.getClass()); // prefer 'foo.jellytag' to avoid tags from showing up as views, // but for backward compatibility, support the plain .jelly extention as well. Script tag = c.loadTearOff(JellyClassTearOff.class).findScript(tagName+".jellytag"); if (tag==null) tag = c.loadTearOff(JellyClassTearOff.class).findScript(tagName+".jelly"); return tag; } catch (JellyException e) { throw new JellyTagException("Failed to load "+tagName+".jelly from "+it,e); } } };