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.Validator;
33 import org.sovt.impl.GroupValidator;
34 import org.xml.sax.Attributes;
35
36 import java.util.NoSuchElementException;
37
38 /***
39 * Factory of Validator objects for digister.
40 *
41 * @author Vilmantas Baranauskas (vilmantas_baranauskas@yahoo.com)
42 */
43 public class ValidatorFactory extends AbstractObjectCreationFactory {
44 //-- constants -------------------------------------------------------------
45 //-- variables -------------------------------------------------------------
46
47 //-- constructors ----------------------------------------------------------
48 /***
49 * Creates instance of ValidatorFactory.
50 */
51 public ValidatorFactory() {
52 }
53
54 //-- methods ---------------------------------------------------------------
55 /***
56 * Creates Validator for given attributes.
57 * Validator 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 GroupValidator.</li>
61 * </ol>
62 *
63 * @param attributes Attribute of xml validator definition.
64 * @return instance of Validator.
65 * @throws Exception If unable to instantiate Validator class.
66 */
67 public Object createObject(
68 Attributes attributes
69 ) throws Exception {
70 Validator validator;
71 String ref = attributes.getValue("ref");
72 if (ref != null) {
73 validator = SovtLibrary.getInstance().getValidator(ref);
74 if (validator == null) {
75 throw new NoSuchElementException("Unknown validator [" + ref + "]");
76 } else {
77 validator = (Validator) validator.clone();
78 }
79 } else if (attributes.getValue("class") != null) {
80 validator = (Validator) Class.forName(attributes.getValue("class")).
81 newInstance();
82 } else {
83 // return instance of default validator class
84 validator = new GroupValidator();
85 }
86
87 if (attributes.getValue("id") != null) {
88 // this is named validator which has to be saved for later reference
89 SovtLibrary.getInstance().addValidator(
90 attributes.getValue("id"),
91 validator
92 );
93 }
94
95 return validator;
96 }
97
98 }