KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > xsl > AbstractStylesheet


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;
30
31 import com.caucho.vfs.Path;
32 import com.caucho.vfs.PersistentDependency;
33 import com.caucho.vfs.Vfs;
34 import com.caucho.xml.XMLWriter;
35
36 import org.w3c.dom.Node JavaDoc;
37
38 import javax.xml.transform.Templates JavaDoc;
39 import javax.xml.transform.URIResolver JavaDoc;
40 import java.util.ArrayList JavaDoc;
41 import java.util.Properties JavaDoc;
42
43 /**
44  * A compiled XSL stylesheet. Stylesheets use 'transform' to transform
45  * an XML tree to an XML Document.
46  *
47  * <p>The resulting document can be printed, or it can be added to another
48  * XML tree.
49  */

50 abstract public class AbstractStylesheet
51   implements CauchoStylesheet, Templates JavaDoc {
52   // The path to the stylesheet source
53
private Path _path;
54   private ArrayList JavaDoc<PersistentDependency> _depends =
55     new ArrayList JavaDoc<PersistentDependency>();
56   private ArrayList JavaDoc<String JavaDoc> _cacheDepends = new ArrayList JavaDoc<String JavaDoc>();
57   private ArrayList JavaDoc _globalParameters;
58
59   protected Properties JavaDoc _output = new Properties JavaDoc();
60   protected AbstractStylesheet _stylesheet;
61
62   protected String JavaDoc _errorPage;
63   protected URIResolver JavaDoc _uriResolver;
64   boolean _escapeEntities = true;
65
66   /**
67    * Initialize the stylesheet with the search path.
68    *
69    * @param path the path of the stylepath used to search for stylesheets.
70    */

71   public void init(Path path) throws Exception JavaDoc
72   {
73     /*
74      * Can't use best path because the stylesheets are relative to
75      * the search path.
76     if (path instanceof MergePath)
77       path = ((MergePath) path).getBestPath();
78     */

79     
80     _path = path;
81   }
82
83   public void setURIResolver(URIResolver JavaDoc resolver)
84   {
85     _uriResolver = resolver;
86   }
87
88   public URIResolver JavaDoc getURIResolver()
89   {
90     return _uriResolver;
91   }
92
93   /**
94    * Copies the current stylesheet into the new stylesheet. Used to
95    * create the transformer.
96    *
97    * @param stylesheet the new stylesheet which will contain the copied values.
98    */

99   protected void copy(AbstractStylesheet stylesheet)
100   {
101     stylesheet._stylesheet = this;
102     stylesheet._depends = (ArrayList JavaDoc) _depends.clone();
103     stylesheet._output = (Properties JavaDoc) _output.clone();
104     stylesheet._errorPage = _errorPage;
105     stylesheet._globalParameters = _globalParameters;
106     stylesheet._path = _path;
107     stylesheet._uriResolver = _uriResolver;
108   }
109
110   /**
111    * Clone the stylesheet. Used to create transformer.
112    */

113   public Object JavaDoc clone()
114   {
115     try {
116       AbstractStylesheet instance;
117       instance = (AbstractStylesheet) getClass().newInstance();
118
119       copy(instance);
120
121       if (_path != null)
122         instance.init(_path);
123       else
124         instance.init(Vfs.lookup("anonymous.xsl"));
125
126       return instance;
127     } catch (Exception JavaDoc e) {
128       return null;
129     }
130   }
131
132   /**
133    * Returns the output properties for the stylesheet.
134    */

135   public Properties JavaDoc getOutputProperties()
136   {
137     return _output;
138   }
139
140   public Path getPath()
141   {
142     return _path;
143   }
144   
145   /**
146    * Returns a stylesheet property.
147    */

148   public Object JavaDoc getProperty(String JavaDoc name)
149   {
150     if (name.equals(DEPENDS))
151       return _depends;
152     else if (name.equals(CACHE_DEPENDS))
153       return _cacheDepends;
154     else if (name.equals("caucho.path"))
155       return _path;
156     else if (name.equals(GLOBAL_PARAM))
157       return _globalParameters;
158     else
159       return null;
160   }
161
162   /**
163    * Sets a stylesheet property.
164    */

165   public void setProperty(String JavaDoc name, Object JavaDoc value)
166   {
167     if (name.equals(GLOBAL_PARAM))
168       _globalParameters = (ArrayList JavaDoc) value;
169   }
170
171   /**
172    * Creates a new transformer.
173    */

174   public javax.xml.transform.Transformer JavaDoc newTransformer()
175   {
176     return new TransformerImpl((StylesheetImpl) clone());
177   }
178
179   /**
180    * Returns true if the any of the source stylesheets have been modified
181    * since this stylesheet was compiled.
182    */

183   public boolean isModified()
184   {
185     for (int i = 0; i < _depends.size(); i++) {
186       PersistentDependency depend = _depends.get(i);
187
188       if (depend.isModified())
189         return true;
190     }
191
192     return false;
193   }
194
195   /**
196    * Add a dependency to the stylesheet. Used to keep track of source
197    * stylesheets.
198    *
199    * @param path the path of the source stylesheet.
200    */

201   protected void addDepend(PersistentDependency depend)
202   {
203     if (! _depends.contains(depend))
204       _depends.add(depend);
205   }
206
207   /**
208    * Returns the dependency list of the stylesheet.
209    */

210   public ArrayList JavaDoc<PersistentDependency> getDepends()
211   {
212     return _depends;
213   }
214
215   /**
216    * Adds a cache dependency.
217    */

218   protected void addCacheDepend(String JavaDoc path)
219   {
220     _cacheDepends.add(path);
221   }
222
223   public ArrayList JavaDoc<String JavaDoc> getCacheDepends()
224   {
225     return _cacheDepends;
226   }
227
228   /**
229    * Transforms the XML node to a new XML document based on this stylesheet.
230    *
231    * <p>Since Documents are DocumentFragments, calling functions can insert
232    * the contents using appendChild.
233    *
234    * @param xml source xml to convert
235    * @param out source xml to convert
236    * @return the converted document
237    */

238   abstract public void transform(Node JavaDoc xml,
239                                  XMLWriter out,
240                                  TransformerImpl transformer)
241     throws Exception JavaDoc;
242 }
243     
244
Popular Tags