1 package net.sf.tomp.xtcl;
2
3 import net.sf.joost.trax.TransformerFactoryImpl;
4 import net.sf.tomp.xml.sax.XTResolver;
5 import net.sf.tomp.xtcl.filter.XTFilterFactory;
6 import net.sf.tomp.xtcl.filter.XTFilterFactoryImpl;
7 import net.sf.tomp.xtcl.result.HasResult;
8 import net.sf.tomp.xtcl.source.XTSourceFactory;
9 import net.sf.tomp.xtcl.source.XTSourceFactoryImpl;
10
11 import org.apache.xml.resolver.tools.ResolvingXMLReader;
12 import org.w3c.dom.Document;
13 import org.xml.sax.InputSource;
14 import org.xml.sax.XMLFilter;
15
16 import javax.xml.parsers.DocumentBuilder;
17 import javax.xml.parsers.DocumentBuilderFactory;
18 import javax.xml.parsers.ParserConfigurationException;
19 import javax.xml.parsers.SAXParserFactory;
20 import javax.xml.transform.Result;
21 import javax.xml.transform.Source;
22 import javax.xml.transform.Templates;
23 import javax.xml.transform.Transformer;
24 import javax.xml.transform.TransformerConfigurationException;
25 import javax.xml.transform.URIResolver;
26 import javax.xml.transform.dom.DOMResult;
27 import javax.xml.transform.dom.DOMSource;
28 import javax.xml.transform.sax.SAXTransformerFactory;
29 import javax.xml.transform.stream.StreamResult;
30 import javax.xml.transform.stream.StreamSource;
31
32 import java.io.ByteArrayInputStream;
33 import java.io.File;
34 import java.io.IOException;
35 import java.io.OutputStream;
36 import java.io.PrintStream;
37
38 import java.util.HashMap;
39 import java.util.Iterator;
40 import java.util.LinkedList;
41 import java.util.List;
42 import java.util.Map;
43 import java.util.Map.Entry;
44
45 /***
46 * DOCUMENT ME!
47 *
48 * @author tomp
49 */
50 public class Context
51 {
52 protected static Class uriResolverClass = XTResolver.class;
53 protected static Class resolvingXMLReaderClass = ResolvingXMLReader.class;
54 private Map c;
55 private List compilers;
56 private DocumentBuilder db;
57 private int depth;
58 private Transformer empty;
59 private TransformerFactoryImpl jtf;
60 private PrintStream out;
61 private Context previous;
62 private File realPath;
63 private SAXParserFactory spf;
64 private boolean strictPassing;
65 private SAXTransformerFactory tf;
66
67 /*** DOCUMENT ME! */
68 private boolean verbose;
69 private XTFilterFactory xff;
70 private XTSourceFactory xrf;
71
72 /***
73 * Creates a new instance of Context from scratch
74 *
75 * @throws ParserConfigurationException DOCUMENT ME!
76 * @throws TransformerConfigurationException DOCUMENT ME!
77 * @throws IOException DOCUMENT ME!
78 * @throws ClassNotFoundException
79 * @throws IllegalAccessException
80 * @throws InstantiationException
81 */
82 public Context()
83 throws ParserConfigurationException, TransformerConfigurationException,
84 IOException, InstantiationException, IllegalAccessException,
85 ClassNotFoundException
86 {
87 verbose = true;
88 depth = 0;
89 c = new HashMap();
90 db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
91 tf = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
92 spf = SAXParserFactory.newInstance();
93 jtf = new TransformerFactoryImpl();
94 empty = tf.newTransformer();
95 realPath = new File(".");
96 strictPassing = true;
97 xff = new XTFilterFactoryImpl(tf, jtf);
98 xrf = new XTSourceFactoryImpl();
99 compilers = new LinkedList();
100 CompilerFactory.createDefaultCompilers(this);
101
102 setResolverToFactories();
103 }
104
105 /***
106 * Creates a new instance of Context, copying all data from the given parent
107 * Context.
108 *
109 * @param p parent Context
110 * @throws ParserConfigurationException DOCUMENT ME!
111 * @throws TransformerConfigurationException DOCUMENT ME!
112 * @throws ClassNotFoundException
113 * @throws IllegalAccessException
114 * @throws InstantiationException
115 */
116 public Context(Context p)
117 throws ParserConfigurationException, TransformerConfigurationException,
118 InstantiationException, IllegalAccessException,
119 ClassNotFoundException
120 {
121 verbose = p.isVerbose();
122 previous = p;
123 c = new HashMap();
124 db = p.getDocumentBuilder();
125 tf = p.getTransformerFactory();
126 jtf = p.getJoostTransformerFactory();
127 empty = p.getEmptyTransformer();
128 out = p.getOut();
129 depth = p.depth + 1;
130 realPath = p.getRealPath();
131 strictPassing = p.isStrictPassing();
132 xff = p.getXTFilterFactory();
133 xrf = p.getXTSourceFactory();
134 compilers = new LinkedList();
135 CompilerFactory.cloneCompilers(p, this);
136 setResolverToFactories();
137 }
138
139 /***
140 *
141 */
142 private void setResolverToFactories()
143 {
144 try
145 {
146 URIResolver ur = (URIResolver) uriResolverClass.newInstance();
147
148 if (ur instanceof XTResolver)
149 {
150 ((XTResolver) ur).setContext(this);
151 }
152
153 tf.setURIResolver(ur);
154 jtf.setURIResolver(ur);
155 }
156 catch (Exception e)
157 {
158 e.printStackTrace();
159 }
160 }
161
162 public void addCompiler(Compiler comp)
163 {
164 addCompiler(0, comp);
165 }
166
167 protected void addCompiler(int index, Compiler comp)
168 {
169 compilers.add(index, comp);
170 comp.setContext(this);
171 }
172
173 public void removeCompiler()
174 {
175 removeCompiler(0);
176 }
177
178 protected void removeCompiler(int index)
179 {
180 if (index < compilers.size())
181 {
182 compilers.remove(index);
183 }
184 else
185 {
186 return;
187 }
188 }
189
190 /***
191 * DOCUMENT ME!
192 *
193 * @param deep DOCUMENT ME!
194 * @throws Exception DOCUMENT ME!
195 */
196 public void dump(boolean deep)
197 throws Exception
198 {
199 out.println("--- context " + depth + " ---");
200
201 for (Iterator i = c.entrySet().iterator(); i.hasNext();)
202 {
203 Entry me = (Entry) i.next();
204
205 out.println("\nVAR " + me.getKey() + "->" + me.getValue());
206
207 if (deep)
208 {
209 dumpVariable(me.getKey());
210 }
211 }
212
213 out.println("---------------");
214 }
215
216 /***
217 * DOCUMENT ME!
218 *
219 * @param deep DOCUMENT ME!
220 * @throws Exception DOCUMENT ME!
221 */
222 public void dumpAll(boolean deep)
223 throws Exception
224 {
225 out.println("=== all contexts ===");
226
227 Context context = this;
228
229 while (context != null)
230 {
231 context.dump(deep);
232 context = context.getPrevious();
233 }
234
235 out.println("====================");
236 }
237
238 /***
239 * DOCUMENT ME!
240 *
241 * @param k DOCUMENT ME!
242 * @throws Exception DOCUMENT ME!
243 */
244 public void dumpVariable(Object k)
245 throws Exception
246 {
247 Object v = c.get(k);
248
249 if (v instanceof Document)
250 {
251 empty.transform(new DOMSource((Document) v), new StreamResult(out));
252
253
254
255 }
256 else
257 {
258 out.println(v);
259 }
260 }
261
262 /***
263 * Executes a command in the current context
264 *
265 * @param command to be executed
266 * @return return code from execution
267 * @throws Exception if the command fails, an unspecified Exception is
268 * thrown
269 */
270 public int execute(Command command)
271 throws Exception
272 {
273 if (command == null)
274 {
275 return 0;
276 }
277 else
278 {
279 return command.execute(this);
280 }
281 }
282
283 /***
284 * Gets an object from Context - if not found here, recursively recursively
285 * calls get on previous Context
286 *
287 * @param ref the key
288 * @return the object found or null
289 */
290 public Object get(Object ref)
291 {
292 Object o = c.get(ref);
293
294 if ((o == null) && (previous != null))
295 {
296 return previous.get(ref);
297 }
298 else
299 {
300 return o;
301 }
302 }
303
304 public int getCompilerCount()
305 {
306 return compilers.size();
307 }
308
309 public Compiler getCompiler()
310 {
311 if (compilers.isEmpty())
312 {
313 return null;
314 }
315 else
316 {
317 return getCompiler(0);
318 }
319 }
320
321 protected Compiler getCompiler(int index)
322 {
323 return (Compiler) compilers.get(index);
324 }
325
326 /***
327 * returns the context's depth
328 *
329 * @return DOCUMENT ME!
330 */
331 public int getDepth()
332 {
333 return depth;
334 }
335
336 /***
337 * DOCUMENT ME!
338 *
339 * @return DOCUMENT ME!
340 */
341 public DocumentBuilder getDocumentBuilder()
342 {
343 return db;
344 }
345
346 /***
347 * DOCUMENT ME!
348 *
349 * @return DOCUMENT ME!
350 */
351 public Transformer getEmptyTransformer()
352 {
353 return empty;
354 }
355
356 /***
357 * Get a file relative to the realPath (current directory) of this Context
358 *
359 * @param f the (relative or absolute) file name
360 * @return the File
361 */
362 public File getFile(String n)
363 {
364 File f = new File(n);
365
366
367 if (f.isAbsolute() || f.getPath().startsWith("//")
368 || f.getPath().startsWith("/"))
369 {
370 return f;
371 }
372 else
373 {
374 return new File(getRealPath(), n);
375 }
376 }
377
378 /***
379 * DOCUMENT ME!
380 *
381 * @return DOCUMENT ME!
382 */
383 public TransformerFactoryImpl getJoostTransformerFactory()
384 {
385 return jtf;
386 }
387
388 public Compiler getNextCompiler(Compiler c)
389 {
390 int compilerIndex = compilers.indexOf(c);
391
392 if ((compilerIndex >= 0) && (compilerIndex < (compilers.size() - 1)))
393 {
394 return getCompiler(compilerIndex + 1);
395 }
396 else
397 {
398 return null;
399 }
400 }
401
402 /***
403 * DOCUMENT ME!
404 *
405 * @return DOCUMENT ME!
406 */
407 public PrintStream getOut()
408 {
409 return out;
410 }
411
412 /***
413 * returns the previous context
414 *
415 * @return DOCUMENT ME!
416 */
417 public Context getPrevious()
418 {
419 return previous;
420 }
421
422 /***
423 * DOCUMENT ME!
424 *
425 * @return DOCUMENT ME!
426 */
427 public File getRealPath()
428 {
429 return realPath;
430 }
431
432 /***
433 * DOCUMENT ME!
434 *
435 * @return DOCUMENT ME!
436 */
437 public SAXParserFactory getSAXParserFactory()
438 {
439 return spf;
440 }
441
442 /***
443 * DOCUMENT ME!
444 *
445 * @return DOCUMENT ME!
446 */
447 public SAXTransformerFactory getTransformerFactory()
448 {
449 return tf;
450 }
451
452 /***
453 * DOCUMENT ME!
454 *
455 * @return DOCUMENT ME!
456 */
457 public XTFilterFactory getXTFilterFactory()
458 {
459 return xff;
460 }
461
462 /***
463 * DOCUMENT ME!
464 *
465 * @return DOCUMENT ME!
466 */
467 public XTSourceFactory getXTSourceFactory()
468 {
469 return xrf;
470 }
471
472 /***
473 * Is the parameter passing strict?
474 *
475 * @return true iff parameter passing is strict
476 */
477 public boolean isStrictPassing()
478 {
479 return strictPassing;
480 }
481
482 /***
483 * @return Returns the verbose.
484 */
485 public boolean isVerbose()
486 {
487 return verbose;
488 }
489
490 /***
491 * DOCUMENT ME!
492 *
493 * @param ref DOCUMENT ME!
494 * @param value DOCUMENT ME!
495 */
496 public void put(Object ref, Object value)
497 {
498 c.put(ref, value);
499 }
500
501 /***
502 * DOCUMENT ME!
503 *
504 * @param o DOCUMENT ME!
505 */
506 public void setOut(PrintStream o)
507 {
508 out = o;
509 }
510
511 /***
512 * DOCUMENT ME!
513 *
514 * @param f DOCUMENT ME!
515 */
516 public void setRealPath(File f)
517 {
518 realPath = f;
519 }
520
521 /***
522 * Sets the parameter passing to be strict
523 *
524 * @param b DOCUMENT ME!
525 */
526 public void setStrictPassing(boolean b)
527 {
528 strictPassing = b;
529 }
530
531 /***
532 * @param verbose The verbose to set.
533 */
534 public void setVerbose(boolean verbose)
535 {
536 this.verbose = verbose;
537 }
538
539 /***
540 * DOCUMENT ME!
541 *
542 * @param f DOCUMENT ME!
543 */
544 public void setXTFilterFactory(XTFilterFactory f)
545 {
546 xff = f;
547 }
548
549 /***
550 * DOCUMENT ME!
551 *
552 * @param ref DOCUMENT ME!
553 * @param context DOCUMENT ME!
554 * @return DOCUMENT ME!
555 * @throws IllegalArgumentException DOCUMENT ME!
556 */
557 public File refToFile(Object ref)
558 {
559 Object o = get(ref);
560
561 if (o instanceof File)
562 {
563 return (File) o;
564 }
565 else if (o instanceof String)
566 {
567 return new File(getRealPath(), (String) o);
568 }
569 else
570 {
571 throw new IllegalArgumentException("refToFile=" + ref
572 + " has not pointed to File/String but to " + o
573 + " in Context=" + this);
574 }
575 }
576
577 /***
578 * DOCUMENT ME!
579 *
580 * @param ref DOCUMENT ME!
581 * @param context DOCUMENT ME!
582 * @return DOCUMENT ME!
583 * @throws IllegalArgumentException DOCUMENT ME!
584 */
585 public XMLFilter refToXMLFilter(Object ref)
586 {
587 Object o = get(ref);
588
589 if (o instanceof XMLFilter)
590 {
591 return (XMLFilter) o;
592 }
593 else
594 {
595 throw new IllegalArgumentException("refToXMLFilter=" + ref
596 + " has not pointed to XMLFilter but to " + o + " in Context="
597 + this);
598 }
599 }
600
601 /***
602 * DOCUMENT ME!
603 *
604 * @param ref DOCUMENT ME!
605 * @param context DOCUMENT ME!
606 * @return DOCUMENT ME!
607 * @throws IllegalArgumentException DOCUMENT ME!
608 */
609 public Source refToSource(Object ref)
610 {
611 Object o = get(ref);
612
613
614 if (o instanceof Source)
615 {
616 return (Source) o;
617 }
618 else if (o instanceof Document)
619 {
620 return new DOMSource((Document) o);
621 }
622 else if (o instanceof File)
623 {
624 return new StreamSource((File) o);
625 }
626 else if (o instanceof String)
627 {
628 return new StreamSource(new ByteArrayInputStream(
629 ((String) o).getBytes()));
630 }
631 else
632 {
633 throw new IllegalArgumentException("refToSource=" + ref
634 + " has not pointed to Source/Document/String/File but to " + o
635 + " in Context=" + this);
636 }
637 }
638
639 /***
640 * DOCUMENT ME!
641 *
642 * @param ref DOCUMENT ME!
643 * @param context DOCUMENT ME!
644 * @return DOCUMENT ME!
645 * @throws IOException DOCUMENT ME!
646 * @throws IllegalArgumentException DOCUMENT ME!
647 */
648 public InputSource refToInputSource(Object ref)
649 throws IOException
650 {
651 Object o = get(ref);
652
653
654 if (o instanceof InputSource)
655 {
656 return (InputSource) o;
657 }
658 else if (o instanceof File)
659 {
660 return new InputSource(((File) o).getCanonicalPath());
661
662
663 }
664 else if (o instanceof String)
665 {
666 return new InputSource(new ByteArrayInputStream(
667 ((String) o).getBytes()));
668 }
669 else
670 {
671 throw new IllegalArgumentException("refToInputSource=" + ref
672 + " has not pointed to InputSource/String/File but to " + o
673 + " in Context=" + this);
674 }
675 }
676
677 /***
678 * DOCUMENT ME!
679 *
680 * @param ref DOCUMENT ME!
681 * @param context DOCUMENT ME!
682 * @return DOCUMENT ME!
683 * @throws TransformerConfigurationException DOCUMENT ME!
684 */
685 public Templates refToStyle(Object ref)
686 throws TransformerConfigurationException
687 {
688 Object o = get(ref);
689
690
691 if (o instanceof Templates)
692 {
693 return (Templates) o;
694 }
695
696 Source templatesSource = refToSource(ref);
697
698 return getTransformerFactory().newTemplates(templatesSource);
699 }
700
701 /***
702 * DOCUMENT ME!
703 *
704 * @param ref DOCUMENT ME!
705 * @param context DOCUMENT ME!
706 * @return DOCUMENT ME!
707 * @throws TransformerConfigurationException DOCUMENT ME!
708 */
709 public Templates refToJoostStyle(Object ref)
710 throws TransformerConfigurationException
711 {
712 Object o = get(ref);
713
714
715 if (o instanceof net.sf.joost.trax.TemplatesImpl)
716 {
717 return (net.sf.joost.trax.TemplatesImpl) o;
718 }
719
720 Source templatesSource = refToSource(ref);
721
722 return getJoostTransformerFactory().newTemplates(templatesSource);
723 }
724
725 /***
726 * public Object refToTransformParam(Object ref, Context context) { Object o =
727 * context.get(ref); //System.out.println("refToTransformParam="+o); if (o
728 * instanceof Document) { return new DOMSource((Document)o); } else if (o
729 * instanceof File) { return new StreamSource((File)o); } return o; }
730 *
731 * @param ref DOCUMENT ME!
732 * @param context DOCUMENT ME!
733 * @return DOCUMENT ME!
734 * @throws IllegalArgumentException DOCUMENT ME!
735 */
736 public Result refToResult(Object ref)
737 {
738 Object o = get(ref);
739
740 if (o == null)
741 {
742 DocumentBuilder db = getDocumentBuilder();
743 Document doc = db.newDocument();
744
745 put(ref, doc);
746
747 return new DOMResult(doc);
748 }
749 else if (o instanceof Document)
750 {
751 return new DOMResult((Document) o);
752 }
753 else if (o instanceof File)
754 {
755
756
757 return new StreamResult((File) o);
758 }
759 else if (o instanceof OutputStream)
760 {
761 return new StreamResult((OutputStream) o);
762 }
763 else if (o instanceof HasResult)
764 {
765
766
767 return ((HasResult) o).getResult();
768
769
770
771
772
773
774
775
776
777
778 }
779 else if (o instanceof Result)
780 {
781 return (Result) o;
782 }
783 else
784 {
785 throw new IllegalArgumentException("refToResult=" + ref
786 + " has not pointed to null/Document/File/OutputStream/DerivationFilter/Result but to "
787 + o + " in Context=" + this);
788 }
789 }
790
791 /***
792 * @return Returns the resolvingXMLReaderClass.
793 */
794 public static Class getResolvingXMLReaderClass()
795 {
796 return resolvingXMLReaderClass;
797 }
798
799 /***
800 * @param resolvingXMLReaderClass The resolvingXMLReaderClass to set.
801 */
802 public static void setResolvingXMLReaderClass(Class resolvingXMLReaderClass)
803 {
804 Context.resolvingXMLReaderClass = resolvingXMLReaderClass;
805 }
806
807 /***
808 * @return Returns the uriResolverClass.
809 */
810 public static Class getUriResolverClass()
811 {
812 return uriResolverClass;
813 }
814
815 /***
816 * @param uriResolverClass The uriResolverClass to set.
817 */
818 public static void setUriResolverClass(Class uriResolverClass)
819 {
820 Context.uriResolverClass = uriResolverClass;
821 }
822 }
823
824
825
826
827
828
829
830
831
832
833
834