Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
FormSetFactory |
|
| 2.6666666666666665;2.667 |
1 | /* | |
2 | * Licensed to the Apache Software Foundation (ASF) under one or more | |
3 | * contributor license agreements. See the NOTICE file distributed with | |
4 | * this work for additional information regarding copyright ownership. | |
5 | * The ASF licenses this file to You under the Apache License, Version 2.0 | |
6 | * (the "License"); you may not use this file except in compliance with | |
7 | * the License. You may obtain a copy of the License at | |
8 | * | |
9 | * http://www.apache.org/licenses/LICENSE-2.0 | |
10 | * | |
11 | * Unless required by applicable law or agreed to in writing, software | |
12 | * distributed under the License is distributed on an "AS IS" BASIS, | |
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
14 | * See the License for the specific language governing permissions and | |
15 | * limitations under the License. | |
16 | */ | |
17 | package org.apache.commons.validator; | |
18 | ||
19 | import org.xml.sax.Attributes; | |
20 | import org.apache.commons.digester.AbstractObjectCreationFactory; | |
21 | import org.apache.commons.logging.Log; | |
22 | import org.apache.commons.logging.LogFactory; | |
23 | ||
24 | /** | |
25 | * Factory class used by Digester to create FormSet's. | |
26 | * | |
27 | * @version $Revision: 1739356 $ | |
28 | * @since Validator 1.2 | |
29 | */ | |
30 | 107 | public class FormSetFactory extends AbstractObjectCreationFactory { |
31 | ||
32 | /** Logging */ | |
33 | 107 | private transient Log log = LogFactory.getLog(FormSetFactory.class); |
34 | ||
35 | /** | |
36 | * <p>Create or retrieve a <code>FormSet</code> for the specified | |
37 | * attributes.</p> | |
38 | * | |
39 | * @param attributes The sax attributes for the formset element. | |
40 | * @return The FormSet for a locale. | |
41 | * @throws Exception If an error occurs creating the FormSet. | |
42 | */ | |
43 | @Override | |
44 | public Object createObject(Attributes attributes) throws Exception { | |
45 | ||
46 | 157 | ValidatorResources resources = (ValidatorResources)digester.peek(0); |
47 | ||
48 | 157 | String language = attributes.getValue("language"); |
49 | 157 | String country = attributes.getValue("country"); |
50 | 157 | String variant = attributes.getValue("variant"); |
51 | ||
52 | 157 | return createFormSet(resources, language, country, variant); |
53 | ||
54 | } | |
55 | ||
56 | /** | |
57 | * <p>Create or retrieve a <code>FormSet</code> based on the language, country | |
58 | * and variant.</p> | |
59 | * | |
60 | * @param resources The validator resources. | |
61 | * @param language The locale's language. | |
62 | * @param country The locale's country. | |
63 | * @param variant The locale's language variant. | |
64 | * @return The FormSet for a locale. | |
65 | * @since Validator 1.2 | |
66 | */ | |
67 | private FormSet createFormSet(ValidatorResources resources, | |
68 | String language, | |
69 | String country, | |
70 | String variant) throws Exception { | |
71 | ||
72 | // Retrieve existing FormSet for the language/country/variant | |
73 | 157 | FormSet formSet = resources.getFormSet(language, country, variant); |
74 | 157 | if (formSet != null) { |
75 | 10 | if (getLog().isDebugEnabled()) { |
76 | 0 | getLog().debug("FormSet[" + formSet.displayKey() + "] found - merging."); |
77 | } | |
78 | 10 | return formSet; |
79 | } | |
80 | ||
81 | // Create a new FormSet for the language/country/variant | |
82 | 147 | formSet = new FormSet(); |
83 | 147 | formSet.setLanguage(language); |
84 | 147 | formSet.setCountry(country); |
85 | 147 | formSet.setVariant(variant); |
86 | ||
87 | // Add the FormSet to the validator resources | |
88 | 147 | resources.addFormSet(formSet); |
89 | ||
90 | 147 | if (getLog().isDebugEnabled()) { |
91 | 0 | getLog().debug("FormSet[" + formSet.displayKey() + "] created."); |
92 | } | |
93 | ||
94 | 147 | return formSet; |
95 | ||
96 | } | |
97 | ||
98 | /** | |
99 | * Accessor method for Log instance. | |
100 | * | |
101 | * The Log instance variable is transient and | |
102 | * accessing it through this method ensures it | |
103 | * is re-initialized when this instance is | |
104 | * de-serialized. | |
105 | * | |
106 | * @return The Log instance. | |
107 | */ | |
108 | private Log getLog() { | |
109 | 157 | if (log == null) { |
110 | 0 | log = LogFactory.getLog(FormSetFactory.class); |
111 | } | |
112 | 157 | return log; |
113 | } | |
114 | ||
115 | } |