1:
37:
38: package ;
39:
40: import ;
41: import ;
42: import ;
43:
44: import ;
45:
46: import ;
47: import ;
48: import ;
49: import ;
50:
51: import ;
52: import ;
53: import ;
54: import ;
55: import ;
56: import ;
57: import ;
58: import ;
59: import ;
60:
61: import ;
62: import ;
63: import ;
64: import ;
65: import ;
66:
67: import ;
68:
69: import ;
70: import ;
71:
72: import ;
73: import ;
74: import ;
75: import ;
76: import ;
77:
78:
85: public class GnomeTransformer
86: extends Transformer
87: implements Templates
88: {
89:
90:
93: private Map parameters;
94:
95:
98: private Properties outputProperties;
99:
100:
103: private URIResolver resolver;
104:
105:
108: private ErrorListener errorListener;
109:
110:
114: private Object stylesheet;
115:
116:
122: GnomeTransformer (Source source,
123: URIResolver resolver,
124: ErrorListener errorListener)
125: throws TransformerConfigurationException
126: {
127: this.resolver = resolver;
128: this.errorListener = errorListener;
129: parameters = new HashMap ();
130: outputProperties = new Properties ();
131:
132: if (source == null)
133: {
134: stylesheet = newStylesheet ();
135: }
136: else if (source instanceof StreamSource)
137: {
138: try
139: {
140: StreamSource ss = (StreamSource) source;
141: NamedInputStream in = XMLJ.getInputStream (ss);
142: String systemId = ss.getSystemId ();
143: String publicId = ss.getPublicId ();
144: String base = XMLJ.getBaseURI (systemId);
145: byte[] detectBuffer = in.getDetectBuffer ();
146: if (detectBuffer == null)
147: {
148: String msg = "No document element";
149: throw new TransformerConfigurationException (msg);
150: }
151: stylesheet = newStylesheetFromStream (in, detectBuffer, publicId,
152: systemId, base,
153: (resolver != null),
154: (errorListener != null));
155: }
156: catch (IOException e)
157: {
158: throw new TransformerConfigurationException (e);
159: }
160: }
161: else if (source instanceof DOMSource)
162: {
163: DOMSource ds = (DOMSource) source;
164: Node node = ds.getNode ();
165: if (!(node instanceof GnomeDocument))
166: {
167: String msg = "Node is not a GnomeDocument";
168: throw new TransformerConfigurationException (msg);
169: }
170: GnomeDocument doc = (GnomeDocument) node;
171: stylesheet = newStylesheetFromDoc (doc);
172: }
173: else
174: {
175: String msg = "Source type not supported (" + source + ")";
176: throw new TransformerConfigurationException (msg);
177: }
178: }
179:
180:
183: private GnomeTransformer (Object stylesheet,
184: URIResolver resolver,
185: ErrorListener errorListener,
186: Map parameters,
187: Properties outputProperties)
188: {
189: this.stylesheet = stylesheet;
190: this.resolver = resolver;
191: this.errorListener = errorListener;
192: this.parameters = parameters;
193: this.outputProperties = outputProperties;
194: }
195:
196: private native Object newStylesheet ()
197: throws TransformerConfigurationException;
198:
199: private native Object newStylesheetFromStream (InputStream in,
200: byte[] detectBuffer,
201: String publicId,
202: String systemId,
203: String base,
204: boolean entityResolver,
205: boolean errorHandler)
206: throws TransformerConfigurationException;
207:
208: private native Object newStylesheetFromDoc (GnomeDocument doc)
209: throws TransformerConfigurationException;
210:
211:
212:
213:
214:
215: public synchronized void setParameter (String parameter, Object value)
216: {
217: parameters.put (parameter, value);
218: }
219:
220: public synchronized Object getParameter (String name)
221: {
222: return parameters.get (name);
223: }
224:
225: public synchronized void clearParameters ()
226: {
227: parameters.clear ();
228: }
229:
230:
231:
232: public void setErrorListener (ErrorListener listener)
233: {
234: this.errorListener = listener;
235: }
236:
237: public ErrorListener getErrorListener ()
238: {
239: return errorListener;
240: }
241:
242:
243:
244: public void setURIResolver (URIResolver resolver)
245: {
246: this.resolver = resolver;
247: }
248:
249: public URIResolver getURIResolver ()
250: {
251: return resolver;
252: }
253:
254:
255:
256:
257:
258: public void setOutputProperties (Properties outputProperties)
259: {
260:
261: this.outputProperties = new Properties (outputProperties);
262: }
263:
264: public void setOutputProperty (String name, String value)
265: {
266: outputProperties.setProperty (name, value);
267: }
268:
269: public Properties getOutputProperties ()
270: {
271:
272: return new Properties (this.outputProperties);
273: }
274:
275: public String getOutputProperty (String name)
276: {
277: return outputProperties.getProperty (name);
278: }
279:
280:
281:
282: public Transformer newTransformer ()
283: {
284: return new GnomeTransformer (stylesheet, resolver, errorListener,
285: new HashMap (parameters),
286: new Properties (outputProperties));
287: }
288:
289:
290:
291:
295: public void transform (Source source, Result result)
296: throws TransformerException
297: {
298: if (source instanceof StreamSource)
299: {
300: try
301: {
302: StreamSource ss = (StreamSource) source;
303: NamedInputStream in = XMLJ.getInputStream (ss);
304: String publicId = ss.getPublicId ();
305: String systemId = ss.getSystemId ();
306: String base = XMLJ.getBaseURI (systemId);
307: byte[] detectBuffer = in.getDetectBuffer ();
308: if (detectBuffer == null)
309: {
310: throw new TransformerException ("No document element");
311: }
312: if (result instanceof StreamResult)
313: {
314: OutputStream out = XMLJ.getOutputStream ((StreamResult) result);
315: transformStreamToStream (in, detectBuffer, publicId, systemId,
316: base, (resolver != null),
317: (errorListener != null), out);
318: }
319: else if (result instanceof DOMResult)
320: {
321: DOMResult dr = (DOMResult) result;
322: GnomeDocument ret =
323: transformStreamToDoc (in, detectBuffer, publicId, systemId,
324: base, (resolver != null),
325: (errorListener != null));
326: dr.setNode (ret);
327: dr.setSystemId (null);
328: }
329: else if (result instanceof SAXResult)
330: {
331: SAXResult sr = (SAXResult) result;
332: transformStreamToSAX (in, detectBuffer, publicId, systemId,
333: base, (resolver != null),
334: (errorListener != null),
335: getSAXContext (sr));
336: }
337: else
338: {
339: String msg = "Result type not supported (" + result + ")";
340: throw new TransformerConfigurationException (msg);
341: }
342: }
343: catch (IOException e)
344: {
345: throw new TransformerException (e);
346: }
347: }
348: else if (source instanceof DOMSource)
349: {
350: DOMSource ds = (DOMSource) source;
351: Node node = ds.getNode ();
352: if (!(node instanceof GnomeDocument))
353: {
354: String msg = "Node is not a GnomeDocument (" + node + ")";
355: throw new TransformerException (msg);
356: }
357: GnomeDocument doc = (GnomeDocument) node;
358: if (result instanceof StreamResult)
359: {
360: try
361: {
362: OutputStream out = XMLJ.getOutputStream ((StreamResult) result);
363: transformDocToStream (doc, out);
364: }
365: catch (IOException e)
366: {
367: throw new TransformerException (e);
368: }
369: }
370: else if (result instanceof DOMResult)
371: {
372: DOMResult dr = (DOMResult) result;
373: GnomeDocument ret = transformDocToDoc (doc);
374: dr.setNode (ret);
375: dr.setSystemId (null);
376: }
377: else if (result instanceof SAXResult)
378: {
379: SAXResult sr = (SAXResult) result;
380: transformDocToSAX (doc, getSAXContext (sr));
381: }
382: else
383: {
384: String msg = "Result type not supported";
385: throw new TransformerConfigurationException (msg);
386: }
387: }
388: else
389: {
390: String msg = "Source type not supported";
391: throw new TransformerConfigurationException (msg);
392: }
393: }
394:
395: private GnomeXMLReader getSAXContext (SAXResult result)
396: {
397: GnomeXMLReader ctx = new GnomeXMLReader ();
398: ctx.setContentHandler (result.getHandler ());
399: ctx.setLexicalHandler (result.getLexicalHandler ());
400: if (errorListener != null)
401: {
402: ErrorHandler errorHandler =
403: new ErrorListenerErrorHandler (errorListener);
404: ctx.setErrorHandler (errorHandler);
405: }
406: if (resolver != null)
407: {
408: EntityResolver entityResolver =
409: new URIResolverEntityResolver (resolver);
410: ctx.setEntityResolver (entityResolver);
411: }
412: return ctx;
413: }
414:
415: private native void transformStreamToStream (InputStream in,
416: byte[] detectBuffer,
417: String publicId,
418: String systemId,
419: String base,
420: boolean entityResolver,
421: boolean errorHandler,
422: OutputStream out)
423: throws TransformerException;
424:
425: private native GnomeDocument transformStreamToDoc (InputStream in,
426: byte[] detectBuffer,
427: String publicId,
428: String systemId,
429: String base,
430: boolean entityResolver,
431: boolean errorHandler)
432: throws TransformerException;
433:
434: private native void transformStreamToSAX (InputStream in,
435: byte[] detectBuffer,
436: String publicId,
437: String systemId,
438: String base,
439: boolean entityResolver,
440: boolean errorHandler,
441: GnomeXMLReader out)
442: throws TransformerException;
443:
444: private native void transformDocToStream (GnomeDocument in,
445: OutputStream out)
446: throws TransformerException;
447:
448: private native GnomeDocument transformDocToDoc (GnomeDocument in)
449: throws TransformerException;
450:
451: private native void transformDocToSAX (GnomeDocument in,
452: GnomeXMLReader out)
453: throws TransformerException;
454:
455:
459: private String[] getParameterArray ()
460: {
461: String[] parameterArray = new String[parameters.size () * 2];
462: int index = 0;
463: for (Iterator it = parameters.keySet ().iterator ();
464: it.hasNext ();
465: ++index)
466: {
467: String parameterKey = (String) it.next ();
468: String parameterValue = (String) parameters.get (parameterKey);
469: parameterArray[index * 2 + 0] = parameterKey;
470: parameterArray[index * 2 + 1] =
471: "'" + ((parameterValue != null) ? parameterValue : "") + "'";
472:
473: }
474: return parameterArray;
475: }
476:
477:
478:
479: public void finalize ()
480: {
481: if (stylesheet != null)
482: {
483: free ();
484: stylesheet = null;
485: }
486: }
487:
488: private native void free ();
489:
490:
491:
492: private InputStream resolveEntity (String publicId, String systemId)
493: throws TransformerException
494: {
495: if (resolver != null)
496: {
497: systemId = resolver.resolve (null, systemId).getSystemId ();
498: }
499: if (systemId == null)
500: {
501: return null;
502: }
503: try
504: {
505: URL url = new URL (systemId);
506: return XMLJ.getInputStream (url);
507: }
508: catch (IOException e)
509: {
510: throw new TransformerException (e);
511: }
512: }
513:
514: private void setDocumentLocator (Object ctx, Object loc)
515: {
516: }
517:
518: private void warning (String message,
519: int lineNumber,
520: int columnNumber,
521: String publicId,
522: String systemId)
523: throws TransformerException
524: {
525: if (errorListener == null)
526: {
527: return;
528: }
529: SourceLocator l = new StandaloneLocator (lineNumber,
530: columnNumber,
531: publicId,
532: systemId);
533: errorListener.warning (new TransformerException (message, l));
534: }
535:
536: private void error (String message,
537: int lineNumber,
538: int columnNumber,
539: String publicId,
540: String systemId)
541: throws TransformerException
542: {
543: if (errorListener == null)
544: {
545: return;
546: }
547: SourceLocator l = new StandaloneLocator (lineNumber,
548: columnNumber,
549: publicId,
550: systemId);
551: errorListener.error (new TransformerException (message, l));
552: }
553:
554: private void fatalError (String message,
555: int lineNumber,
556: int columnNumber,
557: String publicId,
558: String systemId)
559: throws TransformerException
560: {
561: if (errorListener == null)
562: {
563: return;
564: }
565: SourceLocator l = new StandaloneLocator (lineNumber,
566: columnNumber,
567: publicId,
568: systemId);
569: errorListener.fatalError (new TransformerException (message, l));
570: }
571:
572: }