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: import java.util.ArrayList;
036: import java.util.regex.Pattern;
037:
038: /**
039: * Represents the top-level xsl:stylesheet node.
040: */
041: public class XslStylesheet extends XslNode {
042: private String _version;
043: private String _id;
044: private String _extensionElementPrefixes;
045: private String _excludeResultPrefixes;
046: private String _xpathDefaultNamespace;
047: private String _defaultValidation;
048:
049: private boolean _isStyleScript;
050: private boolean _isDisableOutputEscaping;
051:
052: private ArrayList<XslNode> _init = new ArrayList<XslNode>();
053:
054: private ArrayList<XslNode> _imports = new ArrayList<XslNode>();
055:
056: /**
057: * Returns the tag name.
058: */
059: public String getTagName() {
060: return "xsl:stylesheet";
061: }
062:
063: /**
064: * Set true if the output escaping should be disabled.
065: */
066: public void setDisableOutputEscaping(boolean disable) {
067: _isDisableOutputEscaping = disable;
068: }
069:
070: /**
071: * Adds an import directive.
072: */
073: public void addImport(XslNode node) {
074: _imports.add(node);
075: }
076:
077: /**
078: * Adds an attribute.
079: */
080: public void addAttribute(QName name, String value)
081: throws XslParseException {
082: if (name.getName().equals("version"))
083: _version = value;
084: else if (name.getName().equals("extension-element-prefixes"))
085: _extensionElementPrefixes = value;
086: else if (name.getName().equals("exclude-result-prefixes"))
087: _excludeResultPrefixes = value;
088: else if (name.getName().equals("xpath-default-namespace"))
089: _xpathDefaultNamespace = value;
090: else if (name.getName().equals("default-validation"))
091: _defaultValidation = value;
092: else if (name.getName().equals("resin:stylescript"))
093: _gen.setStyleScript(true);
094: else
095: super .addAttribute(name, value);
096: }
097:
098: /**
099: * Ends the attributes.
100: */
101: public void endAttributes() throws XslParseException {
102: if (_excludeResultPrefixes != null)
103: addExcludeResultPrefixes(_excludeResultPrefixes);
104: /*
105: if (_version == null)
106: throw error(L.l("xsl:stylesheet needs a 'version' attribute."));
107: else if (! _version.equals("1.0"))
108: throw error(L.l("'{0}' is an unknown xsl:stylesheet version.",
109: _version));
110: */
111: }
112:
113: /**
114: * Adds a child node.
115: */
116: public void addChild(XslNode node) throws XslParseException {
117: if (node instanceof XslVariable) {
118: ((XslVariable) node).setGlobal(true);
119: _gen.addInit(node);
120: } else if (node instanceof XslParam) {
121: ((XslParam) node).setGlobal(true);
122: _gen.addInit(node);
123: } else if (node instanceof TextNode) {
124: TextNode text = (TextNode) node;
125:
126: if (!text.isWhitespace())
127: throw error(L.l("text not allowed in the top level."));
128: } else if (node instanceof XslElementNode) {
129: } else if (!(node instanceof XslTopNode)) {
130: throw error(L.l("<{0}> is not allowed in the top level.",
131: node.getTagName()));
132: } else
133: super .addChild(node);
134: }
135:
136: /**
137: * Generates the code for the tag
138: *
139: * @param out the output writer for the generated java.
140: */
141: public void generate(JavaWriter out) throws Exception {
142: for (int i = 0; i < _imports.size(); i++) {
143: int oldMinImportance = _gen.getMinImportance();
144: _gen.setMinImportance(_gen.getImportance());
145: _imports.get(i).generate(out);
146: _gen.setMinImportance(oldMinImportance);
147: _gen.incrementImportance();
148: }
149:
150: generateChildren(out);
151: }
152:
153: /**
154: * Generates the code for the tag
155: *
156: * @param out the output writer for the generated java.
157: */
158: public void generateDeclaration(JavaWriter out) throws Exception {
159: for (int i = 0; i < _imports.size(); i++)
160: _imports.get(i).generateDeclaration(out);
161:
162: super .generateDeclaration(out);
163: }
164:
165: private void addExcludeResultPrefixes(String prefixes)
166: throws XslParseException {
167: if (prefixes == null)
168: return;
169:
170: Pattern regexp = Pattern.compile("[,\\s]+");
171: String[] strings = regexp.split(prefixes);
172: for (int i = 0; i < strings.length; i++) {
173: String prefix = strings[i];
174: String ns = getNamespace(prefix);
175: if (ns == null)
176: throw error(L.l("`{0}' must be a namespace prefix",
177: prefix));
178: _gen.addExcludedNamespace(ns);
179: }
180: }
181:
182: protected void printPopScope(JavaWriter out) throws Exception {
183: }
184: }
|