Merge from the pain train
[official-gcc.git] / libjava / javax / xml / transform / TransformerFactory.java
blob53d9173cddefe2183f44f7ef32df91a5fabc666b
1 /* TransformerFactory.java --
2 Copyright (C) 2004, 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 package javax.xml.transform;
40 import java.io.BufferedReader;
41 import java.io.File;
42 import java.io.FileInputStream;
43 import java.io.InputStream;
44 import java.io.InputStreamReader;
45 import java.io.IOException;
46 import java.util.Properties;
48 /**
49 * Factory for obtaining transformation contexts.
51 * @author (a href='mailto:dog@gnu.org'>Chris Burdess</a)
53 public abstract class TransformerFactory
56 protected TransformerFactory()
60 /**
61 * Creates a new factory instance.
62 * The implementation class to load is the first found in the following
63 * locations:
64 * <ol>
65 * <li>the <code>javax.xml.transform.TransformerFactory</code> system
66 * property</li>
67 * <li>the above named property value in the
68 * <code><i>$JAVA_HOME</i>/lib/jaxp.properties</code> file</li>
69 * <li>the class name specified in the
70 * <code>META-INF/services/javax.xml.parsers.DocumentBuilderFactory</code>
71 * system resource</li>
72 * <li>the default factory class</li>
73 * </ol>
75 public static TransformerFactory newInstance()
76 throws TransformerFactoryConfigurationError
78 ClassLoader loader = Thread.currentThread().getContextClassLoader();
79 if (loader == null)
81 loader = TransformerFactory.class.getClassLoader();
83 String className = null;
84 int count = 0;
87 className = getFactoryClassName(loader, count++);
88 if (className != null)
90 try
92 Class t = (loader != null) ? loader.loadClass(className) :
93 Class.forName(className);
94 return (TransformerFactory) t.newInstance();
96 catch (ClassNotFoundException e)
98 className = null;
100 catch (Exception e)
102 throw new TransformerFactoryConfigurationError(e,
103 "error instantiating class " + className);
107 while (className == null && count < 3);
110 Class t =
111 Class.forName("gnu.xml.transform.TransformerFactoryImpl");
112 return (TransformerFactory) t.newInstance();
114 catch (Exception e)
116 throw new TransformerFactoryConfigurationError(e);
120 private static String getFactoryClassName(ClassLoader loader, int attempt)
122 final String propertyName = "javax.xml.transform.TransformerFactory";
123 switch (attempt)
125 case 0:
126 return System.getProperty(propertyName);
127 case 1:
130 File file = new File(System.getProperty("java.home"));
131 file = new File(file, "lib");
132 file = new File(file, "jaxp.properties");
133 InputStream in = new FileInputStream(file);
134 Properties props = new Properties();
135 props.load(in);
136 in.close();
137 return props.getProperty(propertyName);
139 catch (IOException e)
141 return null;
143 case 2:
146 String serviceKey = "/META-INF/services/" + propertyName;
147 InputStream in = (loader != null) ?
148 loader.getResourceAsStream(serviceKey) :
149 TransformerFactory.class.getResourceAsStream(serviceKey);
150 if (in != null)
152 BufferedReader r =
153 new BufferedReader(new InputStreamReader(in));
154 String ret = r.readLine();
155 r.close();
156 return ret;
159 catch (IOException e)
162 return null;
163 default:
164 return null;
169 * Creates a new transformer using the specified stylesheet.
170 * @param source the source of an <a href='http://www.w3.org/TR/xslt'>XSLT
171 * stylesheet</a> specifying the transformation to apply
173 public abstract Transformer newTransformer(Source source)
174 throws TransformerConfigurationException;
177 * Creates a new transformer that applies the identity transform.
179 public abstract Transformer newTransformer()
180 throws TransformerConfigurationException;
183 * Creates a new compiled transformation using the specified stylesheet.
184 * @param source the source of an <a href='http://www.w3.org/TR/xslt'>XSLT
185 * stylesheet</a> specifying the transformation to apply
187 public abstract Templates newTemplates(Source source)
188 throws TransformerConfigurationException;
191 * Returns a source object representing the XML resource specified by the
192 * <a href='http://www.w3.org/TR/xml-stylesheet/'>xml-stylesheet</a>
193 * processing instruction and matching the given criteria.
194 * Note that if multiple stylesheets are selected, the source represents a
195 * stylesheet composed of a list of imports.
196 * @param source the source XML document
197 * @param media the media attribute to match, or <code>null</code> to match
198 * the preferred templates
199 * @param title the title attribute to match, or <code>null</code> to match
200 * any
201 * @param charset the charset attribute to match, or <code>null</code> to
202 * match any
204 public abstract Source getAssociatedStylesheet(Source source,
205 String media,
206 String title,
207 String charset)
208 throws TransformerConfigurationException;
211 * Set the resolver callback to be used by transformers obtained from
212 * this factory.
214 public abstract void setURIResolver(URIResolver resolver);
217 * Returns the resolver callback to be used by transformers obtained from
218 * this factory.
220 public abstract URIResolver getURIResolver();
223 * Sets a feature of transformers and templates obtained from this
224 * factory.
225 * Feature names are fully qualified URIs, and may depend on the factory
226 * implementation.
227 * @param name the name of the feature
228 * @param value the feature state
229 * @exception TransformerConfigurationException if the feature is
230 * unsupported
232 public abstract void setFeature(String name, boolean value)
233 throws TransformerConfigurationException;
236 * Returns the state of a feature in the factory implementation.
237 * Feature names are fully qualified URIs, and may depend on the factory
238 * implementation. JAXP also predefines several features, including the
239 * constants in {@link javax.xml.XMLConstants} and
240 * <ul>
241 * <li>{@link javax.xml.transform.dom.DOMSource#FEATURE}</li>
242 * <li>{@link javax.xml.transform.dom.DOMResult#FEATURE}</li>
243 * <li>{@link javax.xml.transform.sax.SAXSource#FEATURE}</li>
244 * <li>{@link javax.xml.transform.sax.SAXResult#FEATURE}</li>
245 * <li>{@link javax.xml.transform.sax.SAXTransformerFactory#FEATURE}</li>
246 * <li>{@link javax.xml.transform.sax.SAXTransformerFactory#FEATURE_XMLFILTER}</li>
247 * <li>{@link javax.xml.transform.stream.StreamSource#FEATURE}</li>
248 * <li>{@link javax.xml.transform.stream.StreamResult#FEATURE}</li>
249 * </ul>
250 * The latter expose various capabilities of the factory implementation.
252 public abstract boolean getFeature(String name);
255 * Set a named attribute on the underlying implementation.
256 * @param name the attribute name
257 * @param value the value to assign
258 * @exception IllegalArgumentException if the attribute is not supported
260 public abstract void setAttribute(String name, Object value)
261 throws IllegalArgumentException;
264 * Retrieve the specified named attribute value.
265 * @param name the attribute name
266 * @exception IllegalArgumentException if the attribute is not supported
268 public abstract Object getAttribute(String name)
269 throws IllegalArgumentException;
272 * Sets the callback to be used by transformers obtained from this factory
273 * to report transformation errors.
275 public abstract void setErrorListener(ErrorListener listener)
276 throws IllegalArgumentException;
279 * Returns the callback to be used by transformers obtained from this
280 * factory to report transformation errors.
282 public abstract ErrorListener getErrorListener();