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;
29  
30  import org.apache.commons.logging.Log;
31  import org.apache.commons.logging.LogFactory;
32  import org.sovt.xml.Loader;
33  
34  import java.util.HashMap;
35  import java.util.Map;
36  
37  /***
38   * This class holds all transformers, validators and inspectors. Different
39   * factories register instances of them which have "id" attribute specified.
40   *
41   * @author Vilmantas Baranauskas (vilmantas_baranauskas@yahoo.com)
42   */
43  public final class SovtLibrary {
44     /***
45      * Logger.
46      */
47     private static Log log = LogFactory.getLog(SovtLibrary.class);
48  
49     //-- constants -------------------------------------------------------------
50     /***
51      * Name of the default Sovt xml library.
52      */
53     public static final String DEFAULT_LIB = "sovt.xml";
54  
55     //-- variables -------------------------------------------------------------
56     /***
57      * Contains named inspectors.
58      */
59     private Map inspectors = new HashMap();
60  
61     /***
62      * Contains named transformer.
63      */
64     private Map transformers = new HashMap();
65     /***
66      * Contains named validators.
67      */
68     private Map validators = new HashMap();
69  
70     /***
71      * Instance of this class.
72      */
73     private static SovtLibrary instance = new SovtLibrary();
74  
75     //-- constructors ----------------------------------------------------------
76     static {
77        // load default SOVT library
78        try {
79           Loader.loadLibrary(DEFAULT_LIB);
80        } catch (InstantiationException e) {
81           log.error(
82                 "Cannot load default SOVT library [" + DEFAULT_LIB + "]",
83                 e
84           );
85        }
86     }
87  
88     /***
89      * Creates instance of library. SovtLibrary is empty after creation.
90      */
91     private SovtLibrary() {
92     }
93  
94     /***
95      * Returns instance of library.
96      *
97      * @return instance of library.
98      */
99     public static SovtLibrary getInstance() {
100       return instance;
101    }
102 
103    //-- methods ---------------------------------------------------------------
104    /***
105     * Adds named inspector.
106     *
107     * @param id ID (name) of inspector
108     * @param inspector Inspector to add.
109     */
110    public void addInspector(String id, Inspector inspector) {
111       log.debug("Registering inspector [" + id + "]");
112       inspectors.put(id, inspector);
113    }
114 
115    /***
116     * Adds named validator.
117     *
118     * @param id ID (name) of validator
119     * @param validator Validator to add.
120     */
121    public void addValidator(String id, Validator validator) {
122       log.debug("Registering validator [" + id + "]");
123       validators.put(id, validator);
124    }
125 
126    /***
127     * Adds named transformer.
128     *
129     * @param id ID (name) of transformer
130     * @param transformer Transformer to add.
131     */
132    public void addTransformer(String id, Transformer transformer) {
133       log.debug("Registering transformer [" + id + "]");
134       transformers.put(id, transformer);
135    }
136 
137    /***
138     * Returns named inspector.
139     *
140     * @param id ID (name) of inspector.
141     * @return Inspector or null if there is no such inspector.
142     */
143    public Inspector getInspector(String id) {
144       return (Inspector) inspectors.get(id);
145    }
146 
147    /***
148     * Returns named validator.
149     *
150     * @param id ID (name) of validator.
151     * @return Validator or null if there is no such validator.
152     */
153    public Validator getValidator(String id) {
154       return (Validator) validators.get(id);
155    }
156 
157    /***
158     * Returns named transformer.
159     *
160     * @param id ID (name) of transformer.
161     * @return Transformer or null if there is no such transformer.
162     */
163    public Transformer getTransformer(String id) {
164       return (Transformer) transformers.get(id);
165    }
166 
167 }