KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.ArrayList JavaDoc;
36 import java.util.regex.Pattern JavaDoc;
37
38 /**
39  * Represents the top-level xsl:stylesheet node.
40  */

41 public class XslStylesheet extends XslNode {
42   private String JavaDoc _version;
43   private String JavaDoc _id;
44   private String JavaDoc _extensionElementPrefixes;
45   private String JavaDoc _excludeResultPrefixes;
46   private String JavaDoc _xpathDefaultNamespace;
47   private String JavaDoc _defaultValidation;
48
49   private boolean _isStyleScript;
50   private boolean _isDisableOutputEscaping;
51
52   private ArrayList JavaDoc<XslNode> _init = new ArrayList JavaDoc<XslNode>();
53
54   private ArrayList JavaDoc<XslNode> _imports = new ArrayList JavaDoc<XslNode>();
55
56   /**
57    * Returns the tag name.
58    */

59   public String JavaDoc getTagName()
60   {
61     return "xsl:stylesheet";
62   }
63
64   /**
65    * Set true if the output escaping should be disabled.
66    */

67   public void setDisableOutputEscaping(boolean disable)
68   {
69     _isDisableOutputEscaping = disable;
70   }
71
72   /**
73    * Adds an import directive.
74    */

75   public void addImport(XslNode node)
76   {
77     _imports.add(node);
78   }
79
80   /**
81    * Adds an attribute.
82    */

83   public void addAttribute(QName name, String JavaDoc value)
84     throws XslParseException
85   {
86     if (name.getName().equals("version"))
87       _version = value;
88     else if (name.getName().equals("extension-element-prefixes"))
89       _extensionElementPrefixes = value;
90     else if (name.getName().equals("exclude-result-prefixes"))
91       _excludeResultPrefixes = value;
92     else if (name.getName().equals("xpath-default-namespace"))
93       _xpathDefaultNamespace = value;
94     else if (name.getName().equals("default-validation"))
95       _defaultValidation = value;
96     else if (name.getName().equals("resin:stylescript"))
97       _gen.setStyleScript(true);
98     else
99       super.addAttribute(name, value);
100   }
101
102   /**
103    * Ends the attributes.
104    */

105   public void endAttributes()
106     throws XslParseException
107   {
108     if (_excludeResultPrefixes != null)
109       addExcludeResultPrefixes(_excludeResultPrefixes);
110     /*
111     if (_version == null)
112       throw error(L.l("xsl:stylesheet needs a 'version' attribute."));
113     else if (! _version.equals("1.0"))
114       throw error(L.l("'{0}' is an unknown xsl:stylesheet version.",
115               _version));
116     */

117   }
118   
119   /**
120    * Adds a child node.
121    */

122   public void addChild(XslNode node)
123     throws XslParseException
124   {
125     if (node instanceof XslVariable) {
126       ((XslVariable) node).setGlobal(true);
127       _gen.addInit(node);
128     }
129     else if (node instanceof XslParam) {
130       ((XslParam) node).setGlobal(true);
131       _gen.addInit(node);
132     }
133     else if (node instanceof TextNode) {
134       TextNode text = (TextNode) node;
135
136       if (! text.isWhitespace())
137     throw error(L.l("text not allowed in the top level."));
138     }
139     else if (node instanceof XslElementNode) {
140     }
141     else if (! (node instanceof XslTopNode)) {
142       throw error(L.l("<{0}> is not allowed in the top level.",
143               node.getTagName()));
144     }
145     else
146       super.addChild(node);
147   }
148
149   /**
150    * Generates the code for the tag
151    *
152    * @param out the output writer for the generated java.
153    */

154   public void generate(JavaWriter out)
155     throws Exception JavaDoc
156   {
157     for (int i = 0; i < _imports.size(); i++) {
158       int oldMinImportance = _gen.getMinImportance();
159       _gen.setMinImportance(_gen.getImportance());
160       _imports.get(i).generate(out);
161       _gen.setMinImportance(oldMinImportance);
162       _gen.incrementImportance();
163     }
164     
165     generateChildren(out);
166   }
167
168   /**
169    * Generates the code for the tag
170    *
171    * @param out the output writer for the generated java.
172    */

173   public void generateDeclaration(JavaWriter out)
174     throws Exception JavaDoc
175   {
176     for (int i = 0; i < _imports.size(); i++)
177       _imports.get(i).generateDeclaration(out);
178     
179     super.generateDeclaration(out);
180   }
181
182   private void addExcludeResultPrefixes(String JavaDoc prefixes)
183     throws XslParseException
184   {
185     if (prefixes == null)
186       return;
187     
188     Pattern JavaDoc regexp = Pattern.compile("[,\\s]+");
189     String JavaDoc []strings = regexp.split(prefixes);
190     for (int i = 0; i < strings.length; i++) {
191       String JavaDoc prefix = strings[i];
192       String JavaDoc ns = getNamespace(prefix);
193       if (ns == null)
194     throw error(L.l("`{0}' must be a namespace prefix", prefix));
195       _gen.addExcludedNamespace(ns);
196     }
197   }
198
199   protected void printPopScope(JavaWriter out)
200     throws Exception JavaDoc
201   {
202   }
203 }
204
Popular Tags