KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > xsl > java > XslTemplate


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.xsl.java;
30
31 import com.caucho.java.JavaWriter;
32 import com.caucho.xml.QName;
33 import com.caucho.xsl.XslParseException;
34
35 /**
36  * Represents the xsl:template node.
37  */

38 public class XslTemplate extends XslNode implements XslTopNode {
39   private String JavaDoc _match;
40   private String JavaDoc _name;
41   private String JavaDoc _mode;
42   private double _priority = 0.0/0.0;
43   private String JavaDoc _as;
44
45   private String JavaDoc _macroName;
46
47   /**
48    * Adds an attribute.
49    */

50   public void addAttribute(QName name, String JavaDoc value)
51     throws XslParseException
52   {
53     if (name.getName().equals("match"))
54       _match = value;
55     else if (name.getName().equals("name"))
56       _name = value;
57     else if (name.getName().equals("mode"))
58       _mode = value;
59     else if (name.getName().equals("priority"))
60       _priority = Double.parseDouble(value);
61     else if (name.getName().equals("as"))
62       _as = value;
63     else
64       super.addAttribute(name, value);
65   }
66
67   /**
68    * Ends the attributes.
69    */

70   public void endAttributes()
71     throws XslParseException
72   {
73     if (_match == null && _name == null)
74       throw error(L.l("xsl:template needs a 'match' or a 'name' attribute."));
75
76     if (_name != null) {
77       _macroName = ("_xsl_macro_" + _gen.toJavaIdentifier(_name) + "__" +
78             _gen.uniqueId());
79       
80       _gen.addMacro(_name, _macroName);
81     }
82   }
83
84   /**
85    * Generates the code for the tag
86    *
87    * @param out the output writer for the generated java.
88    */

89   public void generate(JavaWriter out)
90     throws Exception JavaDoc
91   {
92     String JavaDoc fun = null;
93     
94     if (_match != null) {
95       fun = _gen.createTemplatePattern(_name, parseMatch(_match),
96                        _mode, _priority);
97
98       out.println();
99       out.print("// '" + _match.replace('\n', ' ') + "'");
100       
101       if (_mode != null) {
102     _gen.addMode(_mode);
103         out.println(" mode '" + _mode + "'");
104       }
105       else
106         out.println();
107       
108       out.printJavaString("// " + getFilename() + ":" + getStartLine());
109       out.println();
110       
111       out.println("private void " + fun +
112           "(XslWriter out, Node inputNode, Env env)");
113       out.println(" throws Exception");
114       out.println("{");
115       out.pushDepth();
116
117       out.println("Object _xsl_tmp;");
118       out.println("Node node = inputNode;");
119       out.println("int _xsl_top = env.getTop();");
120
121       boolean isRawText = _gen.getDisableOutputEscaping();
122       if (isRawText)
123     out.println("boolean oldEscaping = out.disableEscaping(true);");
124       else
125     out.println("boolean oldEscaping = out.disableEscaping(false);");
126
127       String JavaDoc filename = getBaseURI();
128       if (filename != null) {
129         int pos = _gen.addStylesheet(filename);
130         
131         out.println("env.setStylesheetEnv(stylesheets[" + pos + "]);");
132       }
133
134       _gen.setSelectDepth(0);
135       _gen.clearUnique();
136
137       generateChildren(out);
138       /*
139       if (node.getLocalName().equals("template") ||
140       node.getLocalName().equals("xsl:template"))
141         generateChildren(node);
142       else
143         generateChild((QAbstractNode) node);
144       */

145
146       /*
147       if (! _isCacheable)
148     println("out.setNotCacheable();");
149       */

150
151       out.println("out.disableEscaping(oldEscaping);");
152       out.println("env.popToTop(_xsl_top);");
153       out.popDepth();
154       out.println("}");
155       out.println();
156     }
157
158     if (_name != null) {
159       out.println("void " + _macroName +
160           "(XslWriter out, Node inputNode, Env env)");
161       out.println(" throws Exception");
162       out.println("{");
163       out.pushDepth();
164
165       if (_match == null) {
166     out.println("Object _xsl_tmp;");
167     out.println("Node node = inputNode;");
168     generateChildren(out);
169       }
170       else {
171     out.println(fun + "(out, inputNode, env);");
172       }
173       
174       out.popDepth();
175       out.println("}");
176     }
177   }
178 }
179
Popular Tags