Dead
[official-gcc.git] / gomp-20050608-branch / libjava / classpath / gnu / xml / libxmlj / transform / GnomeTransformerFactory.java
blob78dfe2148b664205d482c6ca4a30c3601346f23b
1 /* GnomeTransformerFactory.java -
2 Copyright (C) 2004 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., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 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 gnu.xml.libxmlj.transform;
40 import java.io.InputStream;
41 import java.io.IOException;
42 import java.util.HashMap;
43 import java.util.Map;
45 import javax.xml.parsers.FactoryConfigurationError;
46 import javax.xml.parsers.ParserConfigurationException;
47 import javax.xml.parsers.SAXParser;
48 import javax.xml.parsers.SAXParserFactory;
50 import javax.xml.transform.ErrorListener;
51 import javax.xml.transform.Source;
52 import javax.xml.transform.Templates;
53 import javax.xml.transform.Transformer;
54 import javax.xml.transform.TransformerFactory;
55 import javax.xml.transform.TransformerConfigurationException;
56 import javax.xml.transform.URIResolver;
58 import javax.xml.transform.dom.DOMResult;
59 import javax.xml.transform.dom.DOMSource;
60 import javax.xml.transform.sax.SAXSource;
61 import javax.xml.transform.stream.StreamResult;
62 import javax.xml.transform.stream.StreamSource;
64 import org.w3c.dom.Document;
65 import org.w3c.dom.Node;
66 import org.xml.sax.InputSource;
67 import org.xml.sax.SAXException;
68 import org.xml.sax.XMLReader;
69 import org.xml.sax.helpers.DefaultHandler;
71 import gnu.xml.libxmlj.util.XMLJ;
73 /**
74 * An implementation of <code>TransformerFactory</code> producing
75 * <code>Transformer</code> objects which use <code>libxslt</code>
76 * for transformation.
78 * @author Julian Scheid
79 * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
81 public class GnomeTransformerFactory
82 extends TransformerFactory
85 static
87 XMLJ.init ();
90 /**
91 * URIResolver set by user, or default implementation.
93 private URIResolver uriResolver;
95 /**
96 * ErrorListener set by user, or default implementation.
98 private ErrorListener errorListener;
101 * Attributes set by user.
103 private Map attributes = new HashMap ();
105 //--- Implementation of javax.xml.transform.TransformerFactory
106 //--- follows.
108 // -- begin getAssociatedStylesheet implementation --
111 * Returns the stylesheet associated with the specified XML source, or
112 * <code>null</code> if no associated stylesheet could be found.
114 public Source getAssociatedStylesheet(Source source, String media,
115 String title, String charset)
116 throws TransformerConfigurationException
118 String href= null;
119 String base = source.getSystemId();
120 if (source instanceof DOMSource)
122 Node node = ((DOMSource) source).getNode();
123 Document doc = (node.getNodeType() == Node.DOCUMENT_NODE) ?
124 (Document) node : node.getOwnerDocument();
125 if (base == null)
127 base = doc.getDocumentURI();
129 for (node = doc.getFirstChild(); node != null;
130 node = node.getNextSibling())
132 if (node.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE &&
133 "xml-stylesheet".equals(node.getNodeName()))
135 String data = node.getNodeValue();
136 if (media != null &&
137 !media.equals(parseParameter(data, "type")))
139 continue;
141 if (title != null &&
142 !title.equals(parseParameter(data, "title")))
144 continue;
146 href = parseParameter(data, "href");
150 else
152 InputSource input;
153 XMLReader parser = null;
156 if (source instanceof SAXSource)
158 SAXSource sax = (SAXSource) source;
159 input = sax.getInputSource();
160 parser = sax.getXMLReader();
162 else
164 StreamSource stream = (StreamSource) source;
165 InputStream in = stream.getInputStream();
166 input = new InputSource(in);
168 input.setSystemId(base);
169 if (parser == null)
171 parser = createXMLReader();
173 AssociatedStylesheetHandler ash =
174 new AssociatedStylesheetHandler();
175 ash.media = media;
176 ash.title = title;
177 parser.setContentHandler(ash);
178 parser.parse(input);
179 href = ash.href;
181 catch (SAXException e)
183 throw new TransformerConfigurationException(e);
185 catch (IOException e)
187 throw new TransformerConfigurationException(e);
190 if (href == null)
192 return null;
194 if (base != null)
196 base = XMLJ.getBaseURI(base);
198 href = XMLJ.getAbsoluteURI(base, href);
199 return new StreamSource(href);
202 private XMLReader createXMLReader()
203 throws TransformerConfigurationException
207 SAXParserFactory factory = SAXParserFactory.newInstance();
208 SAXParser parser = factory.newSAXParser();
209 return parser.getXMLReader();
211 catch (FactoryConfigurationError e)
213 throw new TransformerConfigurationException(e);
215 catch (ParserConfigurationException e)
217 throw new TransformerConfigurationException(e);
219 catch (SAXException e)
221 throw new TransformerConfigurationException(e);
225 class AssociatedStylesheetHandler
226 extends DefaultHandler
229 String media;
230 String title;
231 String href;
233 public void processingInstruction(String target, String data)
234 throws SAXException
236 if ("xml-stylesheet".equals(target))
238 if (media != null && !media.equals(parseParameter(data, "type")))
240 return;
242 if (title != null && !title.equals(parseParameter(data, "title")))
244 return;
246 href = parseParameter(data, "href");
252 String parseParameter(String data, String name)
254 int start = data.indexOf(name + "=");
255 if (start != -1)
257 start += name.length() + 2;
258 char delim = data.charAt(start - 1);
259 int end = data.indexOf(delim, start);
260 if (end != -1)
262 return data.substring(start, end);
265 return null;
268 // -- end getAssociatedStylesheet implementation --
270 public synchronized void setAttribute (String name, Object value)
272 this.attributes.put (name, value);
275 public synchronized Object getAttribute (String name)
277 return attributes.get (name);
280 public void setErrorListener (ErrorListener errorListener)
282 this.errorListener = errorListener;
285 public ErrorListener getErrorListener ()
287 return errorListener;
290 public void setURIResolver (URIResolver uriResolver)
292 this.uriResolver = uriResolver;
295 public URIResolver getURIResolver ()
297 return uriResolver;
300 public boolean getFeature (String name)
302 return (StreamSource.FEATURE.equals (name) ||
303 StreamResult.FEATURE.equals (name) ||
304 DOMSource.FEATURE.equals (name) ||
305 DOMResult.FEATURE.equals (name));
308 public void setFeature(String name, boolean value)
309 throws TransformerConfigurationException
311 throw new TransformerConfigurationException(name);
315 * Returns a new instance of class {@link Transformer} for a
316 * null souce.
318 public Transformer newTransformer ()
319 throws TransformerConfigurationException
321 return newTransformer (null);
325 * Returns a new instance of class {@link Transformer} for
326 * the given souce.
328 public Transformer newTransformer (Source source)
329 throws TransformerConfigurationException
331 return new GnomeTransformer (source, uriResolver, errorListener);
335 * Returns a new instance of class {@link Templates} for
336 * the given souce.
338 public Templates newTemplates (Source source)
339 throws TransformerConfigurationException
341 return new GnomeTransformer (source, uriResolver, errorListener);
345 * Perform native cleanup.
347 public static native void freeLibxsltGlobal ();