001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.xsl.java;
030:
031: import com.caucho.java.JavaWriter;
032: import com.caucho.xml.QName;
033: import com.caucho.xsl.XslParseException;
034:
035: /**
036: * Represents the xsl:template node.
037: */
038: public class XslTemplate extends XslNode implements XslTopNode {
039: private String _match;
040: private String _name;
041: private String _mode;
042: private double _priority = 0.0 / 0.0;
043: private String _as;
044:
045: private String _macroName;
046:
047: /**
048: * Adds an attribute.
049: */
050: public void addAttribute(QName name, String value)
051: throws XslParseException {
052: if (name.getName().equals("match"))
053: _match = value;
054: else if (name.getName().equals("name"))
055: _name = value;
056: else if (name.getName().equals("mode"))
057: _mode = value;
058: else if (name.getName().equals("priority"))
059: _priority = Double.parseDouble(value);
060: else if (name.getName().equals("as"))
061: _as = value;
062: else
063: super .addAttribute(name, value);
064: }
065:
066: /**
067: * Ends the attributes.
068: */
069: public void endAttributes() throws XslParseException {
070: if (_match == null && _name == null)
071: throw error(L
072: .l("xsl:template needs a 'match' or a 'name' attribute."));
073:
074: if (_name != null) {
075: _macroName = ("_xsl_macro_" + _gen.toJavaIdentifier(_name)
076: + "__" + _gen.uniqueId());
077:
078: _gen.addMacro(_name, _macroName);
079: }
080: }
081:
082: /**
083: * Generates the code for the tag
084: *
085: * @param out the output writer for the generated java.
086: */
087: public void generate(JavaWriter out) throws Exception {
088: String fun = null;
089:
090: if (_match != null) {
091: fun = _gen.createTemplatePattern(_name, parseMatch(_match),
092: _mode, _priority);
093:
094: out.println();
095: out.print("// '" + _match.replace('\n', ' ') + "'");
096:
097: if (_mode != null) {
098: _gen.addMode(_mode);
099: out.println(" mode '" + _mode + "'");
100: } else
101: out.println();
102:
103: out.printJavaString("// " + getFilename() + ":"
104: + getStartLine());
105: out.println();
106:
107: out.println("private void " + fun
108: + "(XslWriter out, Node inputNode, Env env)");
109: out.println(" throws Exception");
110: out.println("{");
111: out.pushDepth();
112:
113: out.println("Object _xsl_tmp;");
114: out.println("Node node = inputNode;");
115: out.println("int _xsl_top = env.getTop();");
116:
117: boolean isRawText = _gen.getDisableOutputEscaping();
118: if (isRawText)
119: out
120: .println("boolean oldEscaping = out.disableEscaping(true);");
121: else
122: out
123: .println("boolean oldEscaping = out.disableEscaping(false);");
124:
125: String filename = getBaseURI();
126: if (filename != null) {
127: int pos = _gen.addStylesheet(filename);
128:
129: out.println("env.setStylesheetEnv(stylesheets[" + pos
130: + "]);");
131: }
132:
133: _gen.setSelectDepth(0);
134: _gen.clearUnique();
135:
136: generateChildren(out);
137: /*
138: if (node.getLocalName().equals("template") ||
139: node.getLocalName().equals("xsl:template"))
140: generateChildren(node);
141: else
142: generateChild((QAbstractNode) node);
143: */
144:
145: /*
146: if (! _isCacheable)
147: println("out.setNotCacheable();");
148: */
149:
150: out.println("out.disableEscaping(oldEscaping);");
151: out.println("env.popToTop(_xsl_top);");
152: out.popDepth();
153: out.println("}");
154: out.println();
155: }
156:
157: if (_name != null) {
158: out.println("void " + _macroName
159: + "(XslWriter out, Node inputNode, Env env)");
160: out.println(" throws Exception");
161: out.println("{");
162: out.pushDepth();
163:
164: if (_match == null) {
165: out.println("Object _xsl_tmp;");
166: out.println("Node node = inputNode;");
167: generateChildren(out);
168: } else {
169: out.println(fun + "(out, inputNode, env);");
170: }
171:
172: out.popDepth();
173: out.println("}");
174: }
175: }
176: }
|