View Javadoc
1 /* 2 * 3 * ==================================================================== 4 * 5 * The Apache Software License, Version 1.1 6 * 7 * Copyright (c) 1999 The Apache Software Foundation. All rights 8 * reserved. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in 19 * the documentation and/or other materials provided with the 20 * distribution. 21 * 22 * 3. The end-user documentation included with the redistribution, if 23 * any, must include the following acknowlegement: 24 * "This product includes software developed by the 25 * Apache Software Foundation (http://www.apache.org/)." 26 * Alternately, this acknowlegement may appear in the software itself, 27 * if and wherever such third-party acknowlegements normally appear. 28 * 29 * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software 30 * Foundation" must not be used to endorse or promote products derived 31 * from this software without prior written permission. For written 32 * permission, please contact apache@apache.org. 33 * 34 * 5. Products derived from this software may not be called "Apache" 35 * nor may "Apache" appear in their names without prior written 36 * permission of the Apache Group. 37 * 38 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 39 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 40 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 41 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 42 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 43 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 44 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 45 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 46 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 47 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 48 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 49 * SUCH DAMAGE. 50 * ==================================================================== 51 * 52 * This software consists of voluntary contributions made by many 53 * individuals on behalf of the Apache Software Foundation. For more 54 * information on the Apache Software Foundation, please see 55 * <http://www.apache.org/>;. 56 * 57 */ 58 package org.apache.commons.jelly.tags.jsl; 59 60 import org.apache.commons.jelly.JellyContext; 61 import org.apache.commons.jelly.JellyException; 62 import org.apache.commons.jelly.XMLOutput; 63 import org.apache.commons.jelly.tags.xml.XPathSource; 64 import org.apache.commons.jelly.tags.xml.XPathTagSupport; 65 66 import org.apache.commons.logging.Log; 67 import org.apache.commons.logging.LogFactory; 68 69 import org.dom4j.Node; 70 import org.dom4j.rule.Action; 71 import org.dom4j.rule.Rule; 72 import org.dom4j.rule.Stylesheet; 73 74 import org.jaxen.XPath; 75 76 77 /*** 78 * This tag implements a JSL stylesheet which is similar to an 79 * XSLT stylesheet but can use Jelly tags inside it 80 * 81 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a> 82 * @version $Revision: 1.8 $ 83 */ 84 public class StylesheetTag extends XPathTagSupport implements XPathSource { 85 86 /*** The Log to which logging calls will be made. */ 87 private Log log = LogFactory.getLog(StylesheetTag.class); 88 89 90 /*** Holds the stylesheet which will be applied to the source context. */ 91 private Stylesheet stylesheet; 92 93 /*** Holds value of property mode. */ 94 private String mode; 95 96 /*** The variable which the stylesheet will be output as */ 97 private String var; 98 99 /*** The XPath expression to evaluate. */ 100 private XPath select; 101 102 /*** The XPath source used by TemplateTag and ApplyTemplatesTag to pass XPath contexts */ 103 private Object xpathSource; 104 105 public StylesheetTag() { 106 } 107 108 109 /*** 110 * Adds a new template rule to this stylesheet 111 */ 112 public void addTemplate( Rule rule ) { 113 getStylesheet().addRule( rule ); 114 } 115 116 // XPathSource interface 117 //------------------------------------------------------------------------- 118 119 /*** 120 * @return the current XPath iteration value 121 * so that any other XPath aware child tags to use 122 */ 123 public Object getXPathSource() { 124 return xpathSource; 125 } 126 127 128 // Tag interface 129 //------------------------------------------------------------------------- 130 public void doTag(XMLOutput output) throws Exception { 131 stylesheet = createStylesheet(output); 132 133 // run the body to add the rules 134 invokeBody(output); 135 stylesheet.setModeName(getMode()); 136 137 if (var != null) { 138 context.setVariable(var, stylesheet); 139 } 140 else { 141 Object source = getSource(); 142 143 if (log.isDebugEnabled()) { 144 log.debug("About to evaluate stylesheet on source: " + source); 145 } 146 147 stylesheet.run(source); 148 } 149 } 150 151 152 // Properties 153 //------------------------------------------------------------------------- 154 155 /*** 156 * Getter for property mode. 157 * @return Value of property mode. 158 */ 159 public String getMode() { 160 return mode; 161 } 162 163 /*** 164 * Sets the mode. 165 * @param mode New value of property mode. 166 */ 167 public void setMode(String mode) { 168 this.mode = mode; 169 } 170 171 public Stylesheet getStylesheet() { 172 return stylesheet; 173 } 174 175 /*** Sets the variable name to define for this expression 176 */ 177 public void setVar(String var) { 178 this.var = var; 179 } 180 181 /*** Sets the XPath expression to evaluate. */ 182 public void setSelect(XPath select) { 183 this.select = select; 184 } 185 186 // Implementation methods 187 //------------------------------------------------------------------------- 188 189 /*** @return the source on which the stylesheet should run 190 */ 191 protected Object getSource() throws Exception { 192 Object source = getXPathContext(); 193 if ( select != null ) { 194 return select.evaluate(source); 195 } 196 return source; 197 } 198 199 200 /*** 201 * Factory method to create a new stylesheet 202 */ 203 protected Stylesheet createStylesheet(final XMLOutput output) { 204 // add default actions 205 Stylesheet answer = new Stylesheet(); 206 answer.setValueOfAction( 207 new Action() { 208 public void run(Node node) throws Exception { 209 String text = node.getStringValue(); 210 if ( text != null && text.length() > 0 ) { 211 // #### should use an 'output' property 212 // when this variable gets reused 213 output.write( text ); 214 } 215 } 216 } 217 ); 218 return answer; 219 } 220 221 /*** 222 * Sets the xpathSource. 223 * @param xpathSource The xpathSource to set 224 */ 225 void setXPathSource(Object xpathSource) { 226 this.xpathSource = xpathSource; 227 } 228 229 }

This page was automatically generated by Maven