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)
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
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
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
;
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
;
74 * An implementation of <code>TransformerFactory</code> producing
75 * <code>Transformer</code> objects which use <code>libxslt</code>
78 * @author Julian Scheid
79 * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
81 public class GnomeTransformerFactory
82 extends TransformerFactory
91 * URIResolver set by user, or default implementation.
93 private URIResolver uriResolver
;
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
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
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();
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();
137 !media
.equals(parseParameter(data
, "type")))
142 !title
.equals(parseParameter(data
, "title")))
146 href
= parseParameter(data
, "href");
153 XMLReader parser
= null;
156 if (source
instanceof SAXSource
)
158 SAXSource sax
= (SAXSource
) source
;
159 input
= sax
.getInputSource();
160 parser
= sax
.getXMLReader();
164 StreamSource stream
= (StreamSource
) source
;
165 InputStream in
= stream
.getInputStream();
166 input
= new InputSource(in
);
168 input
.setSystemId(base
);
171 parser
= createXMLReader();
173 AssociatedStylesheetHandler ash
=
174 new AssociatedStylesheetHandler();
177 parser
.setContentHandler(ash
);
181 catch (SAXException e
)
183 throw new TransformerConfigurationException(e
);
185 catch (IOException e
)
187 throw new TransformerConfigurationException(e
);
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
233 public void processingInstruction(String target
, String data
)
236 if ("xml-stylesheet".equals(target
))
238 if (media
!= null && !media
.equals(parseParameter(data
, "type")))
242 if (title
!= null && !title
.equals(parseParameter(data
, "title")))
246 href
= parseParameter(data
, "href");
252 String
parseParameter(String data
, String name
)
254 int start
= data
.indexOf(name
+ "=");
257 start
+= name
.length() + 2;
258 char delim
= data
.charAt(start
- 1);
259 int end
= data
.indexOf(delim
, start
);
262 return data
.substring(start
, end
);
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 ()
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
318 public Transformer
newTransformer ()
319 throws TransformerConfigurationException
321 return newTransformer (null);
325 * Returns a new instance of class {@link Transformer} for
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
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 ();