1:
37:
38: package ;
39:
40: import ;
41: import ;
42: import ;
43: import ;
44:
45: import ;
46: import ;
47: import ;
48: import ;
49:
50: import ;
51: import ;
52: import ;
53: import ;
54: import ;
55: import ;
56: import ;
57:
58: import ;
59: import ;
60: import ;
61: import ;
62: import ;
63:
64: import ;
65: import ;
66: import ;
67: import ;
68: import ;
69: import ;
70:
71: import ;
72:
73:
81: public class GnomeTransformerFactory
82: extends TransformerFactory
83: {
84:
85: static
86: {
87: XMLJ.init ();
88: }
89:
90:
93: private URIResolver uriResolver;
94:
95:
98: private ErrorListener errorListener;
99:
100:
103: private Map attributes = new HashMap ();
104:
105:
106:
107:
108:
109:
110:
114: public Source getAssociatedStylesheet(Source source, String media,
115: String title, String charset)
116: throws TransformerConfigurationException
117: {
118: String href= null;
119: String base = source.getSystemId();
120: if (source instanceof DOMSource)
121: {
122: Node node = ((DOMSource) source).getNode();
123: Document doc = (node.getNodeType() == Node.DOCUMENT_NODE) ?
124: (Document) node : node.getOwnerDocument();
125: if (base == null)
126: {
127: base = doc.getDocumentURI();
128: }
129: for (node = doc.getFirstChild(); node != null;
130: node = node.getNextSibling())
131: {
132: if (node.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE &&
133: "xml-stylesheet".equals(node.getNodeName()))
134: {
135: String data = node.getNodeValue();
136: if (media != null &&
137: !media.equals(parseParameter(data, "type")))
138: {
139: continue;
140: }
141: if (title != null &&
142: !title.equals(parseParameter(data, "title")))
143: {
144: continue;
145: }
146: href = parseParameter(data, "href");
147: }
148: }
149: }
150: else
151: {
152: InputSource input;
153: XMLReader parser = null;
154: try
155: {
156: if (source instanceof SAXSource)
157: {
158: SAXSource sax = (SAXSource) source;
159: input = sax.getInputSource();
160: parser = sax.getXMLReader();
161: }
162: else
163: {
164: StreamSource stream = (StreamSource) source;
165: InputStream in = stream.getInputStream();
166: input = new InputSource(in);
167: }
168: input.setSystemId(base);
169: if (parser == null)
170: {
171: parser = createXMLReader();
172: }
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;
180: }
181: catch (SAXException e)
182: {
183: throw new TransformerConfigurationException(e);
184: }
185: catch (IOException e)
186: {
187: throw new TransformerConfigurationException(e);
188: }
189: }
190: if (href == null)
191: {
192: return null;
193: }
194: if (base != null)
195: {
196: base = XMLJ.getBaseURI(base);
197: }
198: href = XMLJ.getAbsoluteURI(base, href);
199: return new StreamSource(href);
200: }
201:
202: private XMLReader createXMLReader()
203: throws TransformerConfigurationException
204: {
205: try
206: {
207: SAXParserFactory factory = SAXParserFactory.newInstance();
208: SAXParser parser = factory.newSAXParser();
209: return parser.getXMLReader();
210: }
211: catch (FactoryConfigurationError e)
212: {
213: throw new TransformerConfigurationException(e);
214: }
215: catch (ParserConfigurationException e)
216: {
217: throw new TransformerConfigurationException(e);
218: }
219: catch (SAXException e)
220: {
221: throw new TransformerConfigurationException(e);
222: }
223: }
224:
225: class AssociatedStylesheetHandler
226: extends DefaultHandler
227: {
228:
229: String media;
230: String title;
231: String href;
232:
233: public void processingInstruction(String target, String data)
234: throws SAXException
235: {
236: if ("xml-stylesheet".equals(target))
237: {
238: if (media != null && !media.equals(parseParameter(data, "type")))
239: {
240: return;
241: }
242: if (title != null && !title.equals(parseParameter(data, "title")))
243: {
244: return;
245: }
246: href = parseParameter(data, "href");
247: }
248: }
249:
250: }
251:
252: String parseParameter(String data, String name)
253: {
254: int start = data.indexOf(name + "=");
255: if (start != -1)
256: {
257: start += name.length() + 2;
258: char delim = data.charAt(start - 1);
259: int end = data.indexOf(delim, start);
260: if (end != -1)
261: {
262: return data.substring(start, end);
263: }
264: }
265: return null;
266: }
267:
268:
269:
270: public synchronized void setAttribute (String name, Object value)
271: {
272: this.attributes.put (name, value);
273: }
274:
275: public synchronized Object getAttribute (String name)
276: {
277: return attributes.get (name);
278: }
279:
280: public void setErrorListener (ErrorListener errorListener)
281: {
282: this.errorListener = errorListener;
283: }
284:
285: public ErrorListener getErrorListener ()
286: {
287: return errorListener;
288: }
289:
290: public void setURIResolver (URIResolver uriResolver)
291: {
292: this.uriResolver = uriResolver;
293: }
294:
295: public URIResolver getURIResolver ()
296: {
297: return uriResolver;
298: }
299:
300: public boolean getFeature (String name)
301: {
302: return (StreamSource.FEATURE.equals (name) ||
303: StreamResult.FEATURE.equals (name) ||
304: DOMSource.FEATURE.equals (name) ||
305: DOMResult.FEATURE.equals (name));
306: }
307:
308: public void setFeature(String name, boolean value)
309: throws TransformerConfigurationException
310: {
311: throw new TransformerConfigurationException(name);
312: }
313:
314:
318: public Transformer newTransformer ()
319: throws TransformerConfigurationException
320: {
321: return newTransformer (null);
322: }
323:
324:
328: public Transformer newTransformer (Source source)
329: throws TransformerConfigurationException
330: {
331: return new GnomeTransformer (source, uriResolver, errorListener);
332: }
333:
334:
338: public Templates newTemplates (Source source)
339: throws TransformerConfigurationException
340: {
341: return new GnomeTransformer (source, uriResolver, errorListener);
342: }
343:
344:
347: public static native void freeLibxsltGlobal ();
348:
349: }