View Javadoc

1   package net.sf.tomp.xtcl.command;
2   
3   import net.sf.tomp.xtcl.Context;
4   import net.sf.tomp.xtcl.filter.HasOutputProperties;
5   import net.sf.tomp.xtcl.filter.HasOutputPropertiesWrapper;
6   
7   import org.xml.sax.XMLFilter;
8   import org.xml.sax.XMLReader;
9   import org.xml.sax.helpers.LocatorImpl;
10  
11  import javax.xml.parsers.SAXParserFactory;
12  import javax.xml.transform.Result;
13  import javax.xml.transform.Source;
14  import javax.xml.transform.Templates;
15  import javax.xml.transform.Transformer;
16  import javax.xml.transform.TransformerException;
17  import javax.xml.transform.TransformerFactory;
18  import javax.xml.transform.sax.SAXSource;
19  import javax.xml.transform.sax.SAXTransformerFactory;
20  import javax.xml.transform.sax.TransformerHandler;
21  import javax.xml.transform.stream.StreamResult;
22  import javax.xml.transform.stream.StreamSource;
23  
24  import java.io.PrintStream;
25  
26  import java.util.ArrayList;
27  import java.util.Iterator;
28  import java.util.List;
29  
30  /***
31   * DOCUMENT ME!
32   * 
33   * @author tomp
34   */
35  public class Chain extends AbstractCommand {
36      /*** DOCUMENT ME! */
37      protected String sourceRef;
38  
39      /*** DOCUMENT ME! */
40      protected List filterStyleRefs = new ArrayList();
41  
42      /*** DOCUMENT ME! */
43      protected String resultRef;
44  
45      /*
46       * protected Map params = new HashMap(); protected Map realParams = new
47       * HashMap(); protected Source source; protected Templates templates;
48       * protected Result result;
49       */
50      public void setSourceRef(String s) {
51          sourceRef = s;
52      }
53  
54      /***
55       * DOCUMENT ME!
56       * 
57       * @param s DOCUMENT ME!
58       */
59      public void addFilterStyleRef(String s) {
60          filterStyleRefs.add(s);
61      }
62  
63      /***
64       * DOCUMENT ME!
65       * 
66       * @param s DOCUMENT ME!
67       */
68      public void setResultRef(String s) {
69          resultRef = s;
70      }
71  
72      /***
73       * DOCUMENT ME!
74       * 
75       * @param context DOCUMENT ME!
76       * @return DOCUMENT ME!
77       * @throws Exception DOCUMENT ME!
78       */
79      public int execute(Context context) throws Exception {
80          try {
81              TransformerFactory tf = context.getTransformerFactory();
82  
83              if (!tf.getFeature(SAXSource.FEATURE)) {
84                  throw new TransformerException("Not a SAXTransformerFactory");
85              }
86  
87              SAXTransformerFactory stf = (SAXTransformerFactory) tf;
88  
89              SAXParserFactory spf = context.getSAXParserFactory();
90  
91              spf.setNamespaceAware(true);
92  
93              XMLReader xr = spf.newSAXParser().getXMLReader();
94  
95              Source source = sourceRef.startsWith("$") ? context
96                      .refToSource(sourceRef) : new StreamSource(context
97                      .getFile(sourceRef));
98  
99              Result result = resultRef.startsWith("$") ? context
100                     .refToResult(resultRef) : new StreamResult(context
101                     .getFile(resultRef));
102 
103             XMLReader f = xr;
104             XMLReader prev = xr;
105 
106             Iterator i = filterStyleRefs.iterator();
107 
108             if (i.hasNext()) {
109                 // nonempty chain, at least one style/filter
110                 while (i.hasNext()) {
111                     Templates t = null;
112                     TransformerHandler th = null;
113 
114                     String ref = (String) i.next();
115                     Object value = null;
116 
117                     if (ref.startsWith("$")) {
118                         value = context.get(ref);
119 
120                         //System.out.println("\nREF
121                         // "+ref+"=>"+context.get(ref));
122                         if (!(value instanceof XMLFilter)) {
123                             t = context.refToStyle(ref);
124                         }
125                     } else {
126                         // style filename
127                         t = stf.newTemplates(new StreamSource(context
128                                 .getFile(ref)));
129                     }
130 
131                     // FIXME: what if XMLFilter is the last
132                     if (i.hasNext()) {
133                         f = (t == null) ? context.refToXMLFilter(ref) : stf
134                                 .newXMLFilter(t);
135 
136                         //System.out.println("XMLFilter="+f);
137                         ((XMLFilter) f).setParent(prev);
138 
139                         /*
140                          * if(f instanceof ContentHandler) { Locator loc = new
141                          * SimpleLocator(source.getSystemId());
142                          * System.out.println(f+".setDocumentLocator="+loc);
143                          * ((ContentHandler)f).setDocumentLocator(loc); }
144                          */
145                     } else {
146                         // last style/filter
147                         if (value instanceof XMLFilter) {
148                             f = context.refToXMLFilter(ref);
149 
150                             //System.out.println(">>> SET PARENT TO "+f+"
151                             // "+prev);
152                             ((XMLFilter) f).setParent(prev);
153 
154                             // FIXME changed 4/2/04
155                             /*
156                              * if(f instanceof ContentHandler) { Locator loc =
157                              * new SimpleLocator(source.getSystemId());
158                              * System.out.println(f+".setDocumentLocator="+loc);
159                              * //((ContentHandler)f).setDocumentLocator(loc); }
160                              */
161                             th = stf.newTransformerHandler();
162 
163                             //System.out.println("newTransformerHandler="+th);
164                             if (f instanceof HasOutputProperties) {
165                                 ((HasOutputProperties) f)
166                                         .setOutputPropertiesListener(new HasOutputPropertiesWrapper(
167                                                 th));
168                             }
169                         } else {
170                             //System.out.println("XMLFilter="+f);
171                             th = stf.newTransformerHandler(t);
172 
173                             /*
174                              * Locator loc = new
175                              * SimpleLocator(source.getSystemId());
176                              * System.out.println(th+".setDocumentLocator="+loc);
177                              * th.setDocumentLocator(loc);
178                              */
179                         }
180 
181                         f.setContentHandler(th);
182                         f
183                                 .setProperty(
184                                         "http://xml.org/sax/properties/lexical-handler",
185                                         th);
186                         th.setResult(result);
187 
188                         //System.out.println("NOW PARSING source="+source);
189                         f.parse(SAXSource.sourceToInputSource(source));
190 
191                         //System.out.println("NOW PARSING to result="+result);
192                         //System.out.println("END OF PARSING");
193                     }
194 
195                     prev = f;
196                 }
197             } else {
198                 //System.out.println("in CHAIN: empty transformation");
199                 Transformer transf = tf.newTransformer();
200 
201                 transf.transform(source, result);
202             }
203 
204             return done(context, 0);
205         } catch (TransformerException te) {
206             PrintStream out = context.getOut();
207 
208             te.printStackTrace(out);
209 
210             return done(context, 1);
211         }
212     }
213 
214     /***
215      * DOCUMENT ME!
216      * 
217      * @return DOCUMENT ME!
218      */
219     public String toString() {
220         return "CHAIN " + sourceRef + " -> " + resultRef + " STYLES/FILTERS ("
221                 + listList(filterStyleRefs) + ")";
222     }
223 
224     static class SimpleLocator extends LocatorImpl {
225         public SimpleLocator(String systemId) {
226             setSystemId(systemId);
227             setPublicId(systemId);
228             setColumnNumber(-1);
229             setLineNumber(-1);
230         }
231 
232         public String toString() {
233             return "SimpleLocator.systemId=" + getSystemId();
234         }
235     }
236 }
237 
238 /*
239  * The contents of this file are subject to the Mozilla Public License Version
240  * 1.1 (the "License"); you may not use this file except in compliance with the
241  * License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
242  * Software distributed under the License is distributed on an "AS IS" basis,
243  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
244  * the specific language governing rights and limitations under the License. The
245  * Original Code is: all this file. The Initial Developer of the Original Code
246  * is: Tomas Pitner, Masaryk University in Brno, Czech Republic. Contributor(s):
247  */