001: /*
002: * Copyright 2006-2007 Dan Shellman
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.iscreen.impl.xml;
017:
018: import java.util.Set;
019: import java.util.Iterator;
020: import java.util.Map;
021: import java.util.HashMap;
022:
023: import org.iscreen.ValidationFactory;
024: import org.iscreen.ValidationService;
025: import org.iscreen.ConfigurationException;
026: import org.iscreen.impl.ConfiguredResource;
027:
028: /**
029: * This is a base class used by the parser for registering configuration
030: * events with a ValidationFactory. It's meant to be subclassed by
031: * a service factory that understands the XML configuration file and
032: * can construct the internal configuration of it.
033: *
034: * @author Shellman, Dan
035: */
036: public abstract class XmlServiceFactory extends ValidationFactory {
037: protected Map resourceMap = new HashMap();
038: protected Map validatorMap = new HashMap();
039: protected Map setMap = new HashMap();
040:
041: /**
042: * Default constructor.
043: */
044: public XmlServiceFactory() {
045: } //end XmlServiceFactor()
046:
047: /**
048: * Retrieves a validation service with the given service name.
049: *
050: * @param serviceName The name of the validation service.
051: *
052: * @return Returns the validation service with the given name.
053: */
054: public ValidationService getValidationService(String serviceName) {
055: ValidationService service;
056:
057: service = (ValidationService) setMap.get(serviceName);
058: if (service == null) {
059: throw new ConfigurationException(
060: "No validation service defined with the service named "
061: + serviceName);
062: }
063:
064: return service;
065: } //end getValidationService()
066:
067: /**
068: * Registers a resource with its id (and parent ref, if there is one),
069: * hard-coded messages, and resource bundle locations. Note that the
070: * Set of messages and resource files MUST NOT be null (if there are
071: * none, just pass in an empty Set).
072: *
073: * @param id The unique id of the resource.
074: * @param ref The unique id of the parent resource this resource is referencing.
075: * @param messages Set of XmlConfigMessage objects (can't be null)
076: * @param resourceFiles Set of Strings representing resource bundle locations.
077: */
078: public void registerResource(String id, String ref, Set messages,
079: Set resourceFiles) {
080: ConfiguredResource resource;
081: Iterator it;
082:
083: resource = getResource(id);
084:
085: it = messages.iterator();
086: while (it.hasNext()) {
087: XmlConfigMessage msg;
088:
089: msg = (XmlConfigMessage) it.next();
090: resource.addMessage(msg.getKey(), msg.getValue());
091: }
092:
093: resource.addResourceFiles(resourceFiles);
094: resource.setRef(getResource(ref));
095: } //end registerResource()
096:
097: /**
098: * Registers an included file (for parsing). The location defines
099: * a classpath-based location of the XML file.
100: *
101: * @param location The location of the XML file.
102: */
103: public void registerInclude(String location) {
104: XmlParser parser;
105:
106: parser = new XmlParser(this );
107: parser.parse(location);
108: } //end registerInclude()
109:
110: /**
111: * Registers an individual Validator configuration. This will create
112: * a configured Validator that can be referenced by other Validators or
113: * by adding a Validator to a Validation Set.
114: *
115: * @param globalDefaultResource The resource id, when all else fails.
116: * @param id The unique id of the Validator
117: * @param ref The unique id of the Validator this Validator references.
118: * This can be null and is optional.
119: * @param className The class name of the Validator (optional, but if
120: * there MUST be a valid ref).
121: * @param defaultResource The resource id if no id is defined locally.
122: * @param label The Label for the Validator. This is optional.
123: * @param doc The documentation for the Validator. This is optional.
124: * @param mappings The Set of mappings (can't be null, but can be empty).
125: * @param constraints The Set of constraints (can't be null, but can be empty).
126: * @param failures The Set of failures (can't be null, but can be empty).
127: */
128: public abstract void registerValidator(
129: String globalDefaultResource, String id, String ref,
130: String className, String defaultResource,
131: XmlConfigLabel label, XmlConfigDoc doc, Set mappings,
132: Set constraints, Set failures);
133:
134: /**
135: * Registers a Validation Set.
136: *
137: * @param id The validation set's unique id.
138: */
139: public abstract void registerValidationSet(String id);
140:
141: /**
142: * Adds a 'use-validator' to a Validation Set. The 'use-validator' must
143: * reference a Validator.
144: *
145: * @param setId The Validation Set id.
146: * @param globalDefaultResource The configuration file's default resource
147: * (can be null/empty).
148: * @param defaultResource The Validation Set's default resource (optional).
149: * @param validatorRef The reference to a Validator (required).
150: * @param failFastFlag Whether to stop validations if this validator fails.
151: * @param label The label for this validator.
152: * @param doc The documentation for this validator.
153: * @param mappings The mappings for this validator.
154: * @param constraints The constraints for this validator.
155: * @param failures The failures for this validator.
156: */
157: public abstract void addValidatorToSet(String setId,
158: String globalDefaultResource, String defaultResource,
159: String validatorRef, boolean failFastFlag,
160: String validatorName, XmlConfigLabel label,
161: XmlConfigDoc doc, Set mappings, Set constraints,
162: Set failures);
163:
164: /**
165: * Adds a validation set reference call to a validation set.
166: *
167: * @param setId The id of the containing Validation Set.
168: * @param setRefId The id of the Validation Set being referenced.
169: * @param failFastFlag Whether to continue validations if the set
170: * reports a failure.
171: * @param ifExp Whether to execute the validations in the referenced set.
172: * @param iterateExp Whether to iterate over the objects being mapped
173: * and validate each one.
174: * @param mapExp The mapping expression.
175: */
176: public abstract void addValidationSetToSet(String setId,
177: String setRefId, boolean failFastFlag, String name,
178: String ifExp, String iterateExp, String mapExp);
179:
180: /**
181: * Registers a service with the factory. This service can then be
182: * referenced by validators via configuration.
183: *
184: * @param serviceId The service id
185: * @param service The service
186: */
187: public void registerService(String serviceId, Object service) {
188: serviceMap.put(serviceId, service);
189: } //end registerService()
190:
191: /**
192: * Retrieves a ConfiguredResource for the given id. If no resource
193: * exists with that id, then a blank one is created and returned.
194: *
195: * @param id The id of the resource
196: *
197: * @return Returns a ConfiguredResource for the given id.
198: */
199: public ConfiguredResource getResource(String id) {
200: ConfiguredResource resource;
201:
202: resource = (ConfiguredResource) resourceMap.get(id);
203: if (resource == null) {
204: resource = new ConfiguredResource();
205: resource.setId(id);
206: resourceMap.put(id, resource);
207: }
208:
209: return resource;
210: } //end getResource()
211:
212: /**
213: * Retrieves a resource based upon a set of id's. If the 'id1' is a
214: * valid resource id, then return that one. If not, and 'id2' is a valid
215: * resource id, then return that one. If not, and 'id3' is a valid
216: * resource id, then return that one. If none of the id's are valid,
217: * then throw an exception.
218: *
219: * @param id1 The first resource id to look for.
220: * @param id2 The second resource id to look for.
221: * @param id3 The third resource id to look for.
222: *
223: * @return Returns the ConfiguredResource of the first valid resource id.
224: */
225: public ConfiguredResource getResource(String id1, String id2,
226: String id3) {
227: if (id1 != null && !id1.trim().equals("")) {
228: return getResource(id1);
229: } else if (id2 != null && !id2.trim().equals("")) {
230: return getResource(id2);
231: } else if (id3 != null && !id3.trim().equals("")) {
232: return getResource(id3);
233: }
234:
235: throw new ConfigurationException(
236: "No valid resource reference was given.");
237: } //end getResource()
238:
239: // ***
240: // Protected methods
241: // ***
242:
243: /**
244: * Called to initialize the configuration (loads the XML config file).
245: */
246: protected void loadConfig() {
247: registerInclude(getConfigLocation());
248: } //end loadConfig()
249: } //end XmlServiceFactory
|