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
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
144 public void setParameter(String k, Object v) {
145
146 if ((k != null) && k.startsWith("target-")) {
147 String tt = k.substring("target-".length()) + ":" + (String) v;
148
149 setTargetType(tt);
150
151
152
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
161
162 public void setParent(XMLReader parent) {
163 super.setParent(typeDetectorFilter);
164
165
166
167 if (typeDetectorFilter != null) {
168
169
170
171 typeDetectorFilter.setParent(parent);
172
173
174
175
176 }
177 }
178
179
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
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
215
216
217
218
219
220
221
222
223
224
225
226 }
227 }
228
229 super.startDocument();
230 }
231 }
232
233
234
235
236
237
238
239
240
241
242