View Javadoc

1   package net.sf.tomp.xml.include;
2   
3   import net.sf.tomp.xml.type.Type;
4   import net.sf.tomp.xml.type.TypeDatabase;
5   import net.sf.tomp.xml.type.TypeDetectorFilter;
6   import net.sf.tomp.xml.type.TypeTransformation;
7   import net.sf.tomp.xml.type.TypeTransformationException;
8   import net.sf.tomp.xml.type.Variant;
9   import net.sf.tomp.xml.type.VariantImpl;
10  import net.sf.tomp.xtcl.filter.DynamicFilterImpl;
11  
12  import org.apache.commons.logging.Log;
13  import org.apache.commons.logging.LogFactory;
14  import org.xml.sax.SAXException;
15  import org.xml.sax.XMLFilter;
16  import org.xml.sax.XMLReader;
17  
18  /***
19   * DOCUMENT ME!
20   * 
21   * @author tomp
22   */
23  public class AdaptiveFilterImpl extends DynamicFilterImpl {
24      ////////////////////////////////////////////////////////////////////
25      // Constructors.
26      ////////////////////////////////////////////////////////////////////
27      private Log log = LogFactory.getLog(AdaptiveFilterImpl.class);
28  
29      /*** DOCUMENT ME! */
30      protected TypeDetectorFilter typeDetectorFilter;
31  
32      /*** DOCUMENT ME! */
33      protected Type targetType;
34  
35      /*** DOCUMENT ME! */
36      protected Variant variant = Variant.NO_VARIANT;
37  
38      /***
39       * Creates a new AdaptiveFilterImpl object.
40       */
41      public AdaptiveFilterImpl() {
42          super();
43          typeDetectorFilter = new TypeDetectorFilter();
44          parent = typeDetectorFilter;
45      }
46  
47      /***
48       * Construct an XML filter with the specified parent.
49       * 
50       * @param p DOCUMENT ME!
51       * @see #setParent
52       * @see #getParent
53       */
54      public AdaptiveFilterImpl(XMLReader p) {
55          super();
56          typeDetectorFilter = new TypeDetectorFilter(p);
57          parent = typeDetectorFilter;
58      }
59  
60      /***
61       * DOCUMENT ME!
62       * 
63       * @return DOCUMENT ME!
64       */
65      public TypeDatabase getTypeDatabase() {
66          return typeDetectorFilter.getTypeDatabase();
67      }
68  
69      /***
70       * DOCUMENT ME!
71       * 
72       * @param td DOCUMENT ME!
73       */
74      public void setTypeDatabase(TypeDatabase td) {
75          typeDetectorFilter.setTypeDatabase(td);
76      }
77  
78      /***
79       * DOCUMENT ME!
80       * 
81       * @return DOCUMENT ME!
82       */
83      public Type getSourceType() {
84          return typeDetectorFilter.getType();
85      }
86  
87      /***
88       * DOCUMENT ME!
89       * 
90       * @return DOCUMENT ME!
91       */
92      public Type getSourceTypeForCurrentContext() {
93          return typeDetectorFilter.getTypeForCurrentContext();
94      }
95  
96      /***
97       * DOCUMENT ME!
98       * 
99       * @return DOCUMENT ME!
100      */
101     public Type getTargetType() {
102         return targetType;
103     }
104 
105     /***
106      * DOCUMENT ME!
107      * 
108      * @param t DOCUMENT ME!
109      */
110     public void setTargetType(Type t) {
111         targetType = t;
112     }
113 
114     /***
115      * DOCUMENT ME!
116      * 
117      * @return DOCUMENT ME!
118      */
119     public Variant getVariant() {
120         return variant;
121     }
122 
123     /***
124      * DOCUMENT ME!
125      * 
126      * @param t DOCUMENT ME!
127      */
128     public void setVariant(Variant t) {
129         variant = t;
130     }
131 
132     /***
133      * DOCUMENT ME!
134      * 
135      * @param identification DOCUMENT ME!
136      */
137     public void setTargetType(String identification) {
138         Type targetType = getTypeDatabase().getType(identification);
139 
140         setTargetType(targetType);
141     }
142 
143     // begin of the XTFilter impl. (overriden)
144     public void setParameter(String k, Object v) {
145         //System.out.println("Setting param "+k+"="+v);
146         if ((k != null) && k.startsWith("target-")) {
147             String tt = k.substring("target-".length()) + ":" + (String) v;
148 
149             setTargetType(tt);
150 
151             //System.out.println("*** Target type was set
152             // to="+getTargetType());
153         } else if ((k != null) && k.equals("variant")) {
154             setVariant(new VariantImpl((String) v));
155         } else {
156             super.setParameter(k, v);
157         }
158     }
159 
160     // end of the XTFilter impl.
161     // begin of XMLFilter impl.
162     public void setParent(XMLReader parent) {
163         super.setParent(typeDetectorFilter);
164 
165         //System.out.println(" $$$ "+this+" setParent to
166         // "+typeDetectorFilter+"... ");
167         if (typeDetectorFilter != null) {
168             //  System.out.println(" set to
169             // typeDetectorFilter="+typeDetectorFilter);
170             //  System.out.println(" own parent is="+getParent());
171             typeDetectorFilter.setParent(parent);
172 
173             //        } else {
174             //            System.out.println(" $$$ AdaptiveFilter.setParent to "+parent+"
175             // but typeDetector is null in"+this);
176         }
177     }
178 
179     // end of XMLFilter impl.
180 
181     /***
182      * Filter method startDocument finds and activate the filter for the
183      * transformation
184      * 
185      * @throws SAXException DOCUMENT ME!
186      */
187     public void startDocument() throws SAXException {
188         //System.out.println(""+this+".startDocument");
189         TypeDatabase tdb = getTypeDatabase();
190         Type srcType = getSourceType();
191         Type targetType = getTargetType();
192 
193         log.info("\nDETECTED src" + srcType + "\nDETECTED tgt" + targetType);
194 
195         if (variant != null) {
196             log.info("VARIANT  " + variant + " tdb=" + tdb);
197         }
198 
199         if (targetType != null) {
200             TypeTransformation tt = tdb.getTransformation(srcType, targetType,
201                     variant);
202 
203             log.info("TRANSFORM " + tt);
204 
205             if (tt != null) {
206                 try {
207                     XMLFilter tf = tt.newFilter();
208 
209                     setFilter(tf);
210                 } catch (TypeTransformationException tte) {
211                     throw new SAXException(tte);
212                 }
213 
214                 //System.out.println("&&&&&& filter="+tf);
215 
216                 /*
217                  * if(tf instanceof TransformerFilterImpl) {
218                  * ((TransformerFilterImpl)tf).getResult().setSystemId("file:something");
219                  * System.out.println("&&&&&& filter="+tf+" has
220                  * Result="+((TransformerFilterImpl)tf).getResult());
221                  * System.out.println("&&&&&& filter="+tf+" has
222                  * Result.getSystemId()="+((TransformerFilterImpl)tf).getResult().getSystemId()); }
223                  */
224 
225                 //System.out.println("*** filter has been set");
226             }
227         }
228 
229         super.startDocument();
230     }
231 }
232 
233 /*
234  * The contents of this file are subject to the Mozilla Public License Version
235  * 1.1 (the "License"); you may not use this file except in compliance with the
236  * License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
237  * Software distributed under the License is distributed on an "AS IS" basis,
238  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
239  * the specific language governing rights and limitations under the License. The
240  * Original Code is: all this file. The Initial Developer of the Original Code
241  * is: Tomas Pitner, Masaryk University in Brno, Czech Republic. Contributor(s):
242  */