KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > bridge > jsp > taglib > XsltTag


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.bridge.jsp.taglib;
11 import org.mmbase.bridge.jsp.taglib.util.Attribute;
12 import org.mmbase.bridge.jsp.taglib.functions.Functions;
13 import javax.servlet.jsp.JspTagException JavaDoc;
14 import javax.servlet.http.HttpServletRequest JavaDoc;
15
16 import org.mmbase.util.logging.Logger;
17 import org.mmbase.util.logging.Logging;
18
19 import javax.xml.transform.stream.StreamSource JavaDoc;
20 import javax.xml.transform.OutputKeys JavaDoc;
21 import java.util.Properties JavaDoc;
22
23 /**
24  * Has to live in a formatter tag, and can provide inline XSLT to it.
25  *
26  * @author Michiel Meeuwissen
27  * @version $Id: XsltTag.java,v 1.22 2006/03/24 18:00:30 michiel Exp $
28  */

29
30 public class XsltTag extends ContextReferrerTag {
31
32
33     private static final Logger log = Logging.getLoggerInstance(XsltTag.class);
34
35     private Attribute ext = Attribute.NULL;
36     private FormatterTag formatter;
37
38
39
40     /**
41      * If you use the extends attribute in stead of inline <xsl:import />
42      * then the caches can be invalidated (without parsing of xslt beforehand)
43      *
44      * @todo This has to be implemented still
45      */

46     public void setExtends(String JavaDoc e) throws JspTagException JavaDoc {
47         ext = getAttribute(e);
48     }
49
50     public int doStartTag() throws JspTagException JavaDoc{
51         // Find the parent formatter.
52
formatter = (FormatterTag) findParentTag(FormatterTag.class, null, false);
53         if (formatter == null && getId() == null) {
54             throw new JspTagException JavaDoc("No parent formatter found");
55             // living outside a formatter tag can happen the xslttag has an id.
56
// then it can be used as a 'constant'.
57
// like this:
58
// <mm:xslt id="bla">....</mm:xslt>
59
// <mm:formatter>xxx <mm:xslt referid="bla" /></mm:formatter>
60
// <mm:formatter>yyy <mm:xslt referid="bla" /></mm:formatter>
61

62         }
63         return EVAL_BODY_BUFFERED;
64     }
65
66     /**
67      *
68      */

69     public int doEndTag() throws JspTagException JavaDoc {
70         String JavaDoc xsltString;
71         String JavaDoc body = bodyContent != null ? bodyContent.getString() : "";
72         if (getReferid() == null) {
73             xsltString = body.trim();
74         } else {
75             xsltString = getString(getReferid());
76             if (! "".equals(body)) {
77                 throw new JspTagException JavaDoc("Cannot use body when using 'referid' attribute'.");
78             }
79         }
80         if (getId() != null) {
81             getContextProvider().getContextContainer().register(getId(), xsltString);
82         }
83         if (formatter != null) {
84             String JavaDoc totalString;
85             if (xsltString.startsWith("<xsl:stylesheet")) {
86                 totalString = xsltString;
87             } else {
88                 totalString =
89                     "<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" " +
90                     " xmlns:taglib=\"" + Functions.class.getName() + "\"" +
91                     " xmlns:mm=\"" + Functions.class.getName() + "\"" +
92                     " xmlns:node=\"" + org.mmbase.bridge.util.xml.NodeFunction.class.getName() + "\""+
93                     " xmlns:o=\"" + org.mmbase.bridge.util.xml.Generator.NAMESPACE + "\"" +
94                     " xmlns:mmxf=\"http://www.mmbase.org/xmlns/mmxf\"" +
95                     " extension-element-prefixes=\"mm taglib node\"" +
96                     " exclude-result-prefixes=\"node mmxf o mm taglib node\"" +
97                     " version=\"1.0\"" +
98                     " >" +
99                     xsltString +
100                     "</xsl:stylesheet>";
101                 /* set output property method=xml omit-xml-declaration=yes */
102                 Properties JavaDoc props = new Properties JavaDoc();
103                 props.setProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
104                 formatter.setOutputProperties(props);
105             }
106             StreamSource JavaDoc src = new StreamSource JavaDoc(new java.io.StringReader JavaDoc(totalString));
107             String JavaDoc systemId = ((HttpServletRequest JavaDoc)pageContext.getRequest()).getRequestURL().append('/').append(((long) xsltString.hashCode() & 0xffff)).toString();
108             src.setSystemId(systemId);
109             if (log.isDebugEnabled()) log.debug("Found xslt " + systemId + ": " + totalString);
110             formatter.setXsltSource(src);
111         }
112         formatter = null;
113         return super.doEndTag();
114     }
115 }
116
Popular Tags