View Javadoc

1   /*
2     Copyright (c) 2003, Vilmantas Baranauskas
3     All rights reserved.
4   
5     Redistribution and use in source and binary forms, with or without
6     modification, are permitted provided that the following conditions are met:
7       * Redistributions of source code must retain the above copyright notice,
8         this list of conditions and the following disclaimer.
9       * Redistributions in binary form must reproduce the above copyright notice,
10        this list of conditions and the following disclaimer in the documentation
11        and/or other materials provided with the distribution.
12      * The names of its contributors may not be used to endorse or promote
13        products derived from this software without specific prior written
14        permission.
15  
16    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17    AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19    ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20    LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21    CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26    POSSIBILITY OF SUCH DAMAGE.
27  */
28  package org.sovt.xml;
29  
30  import org.apache.commons.digester.AbstractObjectCreationFactory;
31  import org.sovt.SovtLibrary;
32  import org.sovt.Transformer;
33  import org.sovt.impl.GroupTransformer;
34  import org.xml.sax.Attributes;
35  
36  import java.util.NoSuchElementException;
37  
38  /***
39   * Factory of Transformer objects for digister.
40   *
41   * @author Vilmantas Baranauskas (vilmantas_baranauskas@yahoo.com)
42   */
43  public class TransformerFactory extends AbstractObjectCreationFactory {
44     //-- constants -------------------------------------------------------------
45     //-- variables -------------------------------------------------------------
46  
47     //-- constructors ----------------------------------------------------------
48     /***
49      * Creates instence of TransformerFactory.
50      */
51     public TransformerFactory() {
52     }
53  
54     //-- methods ---------------------------------------------------------------
55     /***
56      * Creates Transformer for given attributes.
57      * Transformer creation order is following:<ol>
58      * <li>Named transforer is returned if "ref" attribute is present.</li>
59      * <li>Class instance is returned if "class" attribute is present.</li>
60      * <li>Instance of GroupTransformer.</li>
61      * </ol>
62      *
63      * @param attributes Attribute of xml transformer definition.
64      * @return instance of Transformer.
65      * @throws Exception If unable to instantiate Transformer class.
66      */
67     public Object createObject(
68           Attributes attributes
69           ) throws Exception {
70        Transformer transformer;
71        String ref = attributes.getValue("ref");
72        if (ref != null) {
73           transformer = SovtLibrary.getInstance().getTransformer(ref);
74           if (transformer == null) {
75              throw new NoSuchElementException("Unknown transformer [" + ref  + "]");
76           } else {
77              transformer = (Transformer) transformer.clone();
78           }
79        } else if (attributes.getValue("class") != null) {
80           transformer = (Transformer)
81                 Class.forName(attributes.getValue("class")).newInstance();
82        } else {
83           // return instance of default transformer class
84           transformer = new GroupTransformer();
85        }
86  
87        if (attributes.getValue("id") != null) {
88           // this is named transformer which has to be saved for later reference
89           SovtLibrary.getInstance().addTransformer(
90                 attributes.getValue("id"),
91                 transformer
92           );
93        }
94  
95        return transformer;
96     }
97  
98  }