1:
37:
38: package ;
39:
40: import ;
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49: import ;
50: import ;
51: import ;
52: import ;
53: import ;
54: import ;
55: import ;
56: import ;
57: import ;
58: import ;
59: import ;
60: import ;
61: import ;
62: import ;
63: import ;
64: import ;
65:
66:
71: public class TransformerFactoryImpl
72: extends TransformerFactory
73: {
74:
75: final XPathFactory xpathFactory;
76: final XSLURIResolver resolver;
77: ErrorListener userListener;
78: URIResolver userResolver;
79:
80: public TransformerFactoryImpl()
81: {
82: xpathFactory = new gnu.xml.xpath.XPathFactoryImpl();
83: resolver = new XSLURIResolver();
84: }
85:
86: public Transformer newTransformer(Source source)
87: throws TransformerConfigurationException
88: {
89: Stylesheet stylesheet = newStylesheet(source, 0, null);
90: Properties outputProperties =
91: new TransformerOutputProperties(stylesheet);
92: TransformerImpl transformer =
93: new TransformerImpl(this, stylesheet, outputProperties);
94: stylesheet.transformer = transformer;
95: return transformer;
96: }
97:
98: public Transformer newTransformer()
99: throws TransformerConfigurationException
100: {
101: return new TransformerImpl(this, null, new Properties());
102: }
103:
104: public Templates newTemplates(Source source)
105: throws TransformerConfigurationException
106: {
107: Stylesheet stylesheet = newStylesheet(source, 0, null);
108: return new TemplatesImpl(this, stylesheet);
109: }
110:
111: Stylesheet newStylesheet(Source source, int precedence, Stylesheet parent)
112: throws TransformerConfigurationException
113: {
114: Document doc = null;
115: String systemId = null;
116: if (source != null)
117: {
118: try
119: {
120: DOMSource ds;
121: synchronized (resolver)
122: {
123: resolver.setUserResolver(userResolver);
124: resolver.setUserListener(userListener);
125: ds = resolver.resolveDOM(source, null, null);
126: }
127: Node node = ds.getNode();
128: if (node == null)
129: {
130: throw new TransformerConfigurationException("no source document");
131: }
132: doc = (node instanceof Document) ? (Document) node :
133: node.getOwnerDocument();
134: systemId = ds.getSystemId();
135: }
136: catch (TransformerException e)
137: {
138: throw new TransformerConfigurationException(e);
139: }
140: }
141: return new Stylesheet(this, parent, doc, systemId, precedence);
142: }
143:
144: public Source getAssociatedStylesheet(Source source,
145: String media,
146: String title,
147: String charset)
148: throws TransformerConfigurationException
149: {
150: try
151: {
152: DOMSource ds;
153: synchronized (resolver)
154: {
155: resolver.setUserResolver(userResolver);
156: resolver.setUserListener(userListener);
157: ds = resolver.resolveDOM(source, null, null);
158: }
159: Node node = ds.getNode();
160: if (node == null)
161: {
162: throw new TransformerConfigurationException("no source document");
163: }
164: Document doc = (node instanceof Document) ? (Document) node :
165: node.getOwnerDocument();
166: LinkedList matches = new LinkedList();
167: for (node = doc.getFirstChild();
168: node != null;
169: node = node.getNextSibling())
170: {
171: if (node.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE &&
172: "xml-stylesheet".equals(node.getNodeName()))
173: {
174: Map params = parseParameters(node.getNodeValue());
175: if (media != null && !media.equals(params.get("media")))
176: {
177: continue;
178: }
179: if (title != null && !title.equals(params.get("title")))
180: {
181: continue;
182: }
183: if (charset != null && !charset.equals(params.get("charset")))
184: {
185: continue;
186: }
187: String href = (String) params.get("href");
188: URL url = resolver.resolveURL(null, node.getBaseURI(), href);
189: matches.add(url);
190: }
191: }
192: switch (matches.size())
193: {
194: case 0:
195: return null;
196: case 1:
197: return new StreamSource(((URL) matches.getFirst()).toString());
198: default:
199:
200:
201: DomDocument ssDoc = new DomDocument();
202: ssDoc.setBuilding(true);
203:
204: Node root =
205: ssDoc.createElementNS(Stylesheet.XSL_NS, "stylesheet");
206: Node version =
207: ssDoc.createAttributeNS(null, "version");
208: version.setNodeValue("1.0");
209: root.getAttributes().setNamedItemNS(version);
210: ssDoc.appendChild(root);
211:
212: for (Iterator i = matches.iterator(); i.hasNext(); )
213: {
214: URL url = (URL) i.next();
215: Node imp =
216: ssDoc.createElementNS(Stylesheet.XSL_NS, "import");
217: Node href =
218: ssDoc.createAttributeNS(null, "href");
219: href.setNodeValue(url.toString());
220: imp.getAttributes().setNamedItemNS(href);
221: root.appendChild(imp);
222: }
223: ssDoc.setBuilding(false);
224: return new DOMSource(ssDoc);
225: }
226: }
227: catch (IOException e)
228: {
229: throw new TransformerConfigurationException(e);
230: }
231: catch (TransformerException e)
232: {
233: throw new TransformerConfigurationException(e);
234: }
235: }
236:
237: Map parseParameters(String data)
238: {
239: Map ret = new LinkedHashMap();
240: int len = data.length();
241: String key = null;
242: int start = 0;
243: char quoteChar = '\u0000';
244: for (int i = 0; i < len; i++)
245: {
246: char c = data.charAt(i);
247: if (quoteChar == '\u0000' && c == ' ')
248: {
249: if (key == null && start < i)
250: {
251: key = data.substring(start, i);
252: }
253: else
254: {
255: String val = unquote(data.substring(start, i).trim());
256: ret.put(key, val);
257: key = null;
258: }
259: start = i + 1;
260: }
261: else if (c == '"')
262: {
263: quoteChar = (quoteChar == c) ? '\u0000' : c;
264: }
265: else if (c == '\'')
266: {
267: quoteChar = (quoteChar == c) ? '\u0000' : c;
268: }
269: }
270: if (start < len && key != null)
271: {
272: String val = unquote(data.substring(start, len).trim());
273: ret.put(key, val);
274: }
275: return ret;
276: }
277:
278: String unquote(String text)
279: {
280: int end = text.length() - 1;
281: if (text.charAt(0) == '\'' && text.charAt(end) == '\'')
282: {
283: return text.substring(1, end);
284: }
285: if (text.charAt(0) == '"' && text.charAt(end) == '"')
286: {
287: return text.substring(1, end);
288: }
289: return text;
290: }
291:
292: public void setURIResolver(URIResolver resolver)
293: {
294: userResolver = resolver;
295: }
296:
297: public URIResolver getURIResolver()
298: {
299: return userResolver;
300: }
301:
302: public void setFeature(String name, boolean value)
303: throws TransformerConfigurationException
304: {
305: throw new TransformerConfigurationException("not supported");
306: }
307:
308: public boolean getFeature(String name)
309: {
310: if (SAXSource.FEATURE.equals(name) ||
311: SAXResult.FEATURE.equals(name) ||
312: StreamSource.FEATURE.equals(name) ||
313: StreamResult.FEATURE.equals(name) ||
314: DOMSource.FEATURE.equals(name) ||
315: DOMResult.FEATURE.equals(name))
316: {
317: return true;
318: }
319: return false;
320: }
321:
322: public void setAttribute(String name, Object value)
323: throws IllegalArgumentException
324: {
325: throw new IllegalArgumentException("not supported");
326: }
327:
328: public Object getAttribute(String name)
329: throws IllegalArgumentException
330: {
331: throw new IllegalArgumentException("not supported");
332: }
333:
334: public void setErrorListener(ErrorListener listener)
335: throws IllegalArgumentException
336: {
337: userListener = listener;
338: }
339:
340: public ErrorListener getErrorListener()
341: {
342: return userListener;
343: }
344:
345: }