View Javadoc

1   package net.sf.tomp.xml.type;
2   
3   import net.sf.tomp.general.Parametrized;
4   
5   import org.xml.sax.XMLFilter;
6   
7   import java.util.*;
8   
9   /***
10   * DOCUMENT ME!
11   *
12   * @author $author$
13   * @version $Revision$
14   */
15  public class FilterTypeTransformation implements TypeTransformation
16  {
17      /*** DOCUMENT ME! */
18      public static final double FILTER_COSTS = 1;
19  
20      /*** DOCUMENT ME! */
21      protected Class filterClass;
22  
23      /*** DOCUMENT ME! */
24      protected Map params = new HashMap();
25  
26      /***
27       * Creates a new FilterTypeTransformation object.
28       *
29       * @param filterClassName DOCUMENT ME!
30       * @throws ClassNotFoundException DOCUMENT ME!
31       * @throws InstantiationException DOCUMENT ME!
32       * @throws IllegalAccessException DOCUMENT ME!
33       */
34      public FilterTypeTransformation(String filterClassName)
35          throws ClassNotFoundException, InstantiationException, 
36              IllegalAccessException
37      {
38          filterClass = Class.forName(filterClassName);
39      }
40  
41      /***
42       * DOCUMENT ME!
43       *
44       * @param k DOCUMENT ME!
45       * @param v DOCUMENT ME!
46       * @throws UnsupportedOperationException DOCUMENT ME!
47       */
48      public void setParameter(String k, Object v)
49      {
50          if (Parametrized.class.isAssignableFrom(filterClass))
51          {
52              params.put(k, v);
53          }
54          else
55          {
56              throw new UnsupportedOperationException(
57                  "Cannot set parameters to non-parametrized filter " + this);
58          }
59  
60          //System.out.println("Setting param "+k+"="+v);
61      }
62  
63      /***
64       * DOCUMENT ME!
65       *
66       * @return DOCUMENT ME!
67       * @throws TypeTransformationException DOCUMENT ME!
68       */
69      public XMLFilter newFilter()
70          throws TypeTransformationException
71      {
72          try
73          {
74              XMLFilter f = (XMLFilter) filterClass.newInstance();
75  
76              // fill params
77              //System.out.println(" @"+Integer.toHexString(hashCode())+"
78              // newFilter()=>"+f);
79              if (f instanceof Parametrized)
80              {
81                  for (Iterator i = params.entrySet().iterator(); i.hasNext();)
82                  {
83                      Map.Entry e = (Map.Entry) i.next();
84                      String k = (String) e.getKey();
85                      Object v = e.getValue();
86  
87                      ((Parametrized) f).setParameter(k, v);
88                  }
89              }
90  
91              return f;
92          }
93          catch (InstantiationException ie)
94          {
95              throw new TypeTransformationException(ie);
96          }
97          catch (IllegalAccessException ie)
98          {
99              throw new TypeTransformationException(ie);
100         }
101     }
102 
103     /***
104      * DOCUMENT ME!
105      *
106      * @return DOCUMENT ME!
107      */
108     public double getCosts()
109     {
110         return FILTER_COSTS;
111     }
112 
113     /***
114      * DOCUMENT ME!
115      *
116      * @return DOCUMENT ME!
117      */
118     public String toString()
119     {
120         return "FilterTypeTransformation@" + Integer.toHexString(hashCode())
121         + "(filterClass=" + filterClass + ")";
122     }
123 
124     /***
125      * DOCUMENT ME!
126      *
127      * @return DOCUMENT ME!
128      */
129     public boolean isIdentity()
130     {
131         return false;
132     }
133 }
134 
135 
136 /*
137  * The contents of this file are subject to the Mozilla Public License Version
138  * 1.1 (the "License"); you may not use this file except in compliance with the
139  * License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
140  * Software distributed under the License is distributed on an "AS IS" basis,
141  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
142  * the specific language governing rights and limitations under the License. The
143  * Original Code is: all this file. The Initial Developer of the Original Code
144  * is: Tomas Pitner, Masaryk University in Brno, Czech Republic. Contributor(s):
145  */