KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > parse > ConversionDescriptor


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.hivemind.parse;
16
17 import java.util.ArrayList JavaDoc;
18 import java.util.HashMap JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.ListIterator JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.apache.hivemind.Element;
27 import org.apache.hivemind.ErrorHandler;
28 import org.apache.hivemind.schema.AttributeModel;
29 import org.apache.hivemind.schema.ElementModel;
30 import org.apache.hivemind.schema.Rule;
31 import org.apache.hivemind.schema.SchemaProcessor;
32 import org.apache.hivemind.schema.rules.BaseRule;
33 import org.apache.hivemind.schema.rules.CreateObjectRule;
34 import org.apache.hivemind.schema.rules.InvokeParentRule;
35 import org.apache.hivemind.schema.rules.ReadAttributeRule;
36
37 /**
38  * Descriptor for the <conversion> module descriptor element. This descriptor implements the
39  * {@link Rule}interface and is added as a standard rule to the containing {@link ElementModel}.
40  * When processed it delegates to a {@link CreateObjectRule}, a bunch of {@link ReadAttributeRule},
41  * and finally an {@link InvokeParentRule}.
42  *
43  * @author Howard Lewis Ship
44  */

45 public class ConversionDescriptor extends BaseRule
46 {
47     private static final String JavaDoc DEFAULT_PARENT_METHOD_NAME = "add";
48
49     private static final Log LOG = LogFactory.getLog(ConversionDescriptor.class);
50
51     private ErrorHandler _errorHandler;
52
53     private String JavaDoc _className;
54
55     private String JavaDoc _parentMethodName = DEFAULT_PARENT_METHOD_NAME;
56
57     private Map JavaDoc _attributeNameMappingMap = new HashMap JavaDoc();
58
59     /** @since 1.1 */
60     private List JavaDoc _attributeMappings = new ArrayList JavaDoc();
61
62     private List JavaDoc _rules;
63
64     private ElementModel _elementModel;
65
66     public ConversionDescriptor(ErrorHandler errorHandler, ElementModel elementModel)
67     {
68         _errorHandler = errorHandler;
69         _elementModel = elementModel;
70     }
71
72     /**
73      * @since 1.1
74      */

75     public List JavaDoc getAttributeMappings()
76     {
77         return _attributeMappings;
78     }
79
80     /**
81      * Adds a mapping for an attribute; these come from <map> elements nested within the
82      * <conversion> element. A check for duplicate attribute mappings (that is, duplicated
83      * attribute name), and an error is logged (and the duplicate ignored).
84      */

85     public void addAttributeMapping(AttributeMappingDescriptor descriptor)
86     {
87         String JavaDoc attributeName = descriptor.getAttributeName();
88
89         AttributeMappingDescriptor existing = (AttributeMappingDescriptor) _attributeNameMappingMap
90                 .get(attributeName);
91
92         if (existing != null)
93         {
94             _errorHandler.error(
95                     LOG,
96                     ParseMessages.dupeAttributeMapping(descriptor, existing),
97                     descriptor.getLocation(),
98                     null);
99
100             return;
101         }
102
103         _attributeNameMappingMap.put(attributeName, descriptor);
104
105         _attributeMappings.add(descriptor);
106     }
107
108     /**
109      * @since 1.1
110      */

111     public String JavaDoc getClassName()
112     {
113         return _className;
114     }
115
116     public void setClassName(String JavaDoc string)
117     {
118         _className = string;
119     }
120
121     /**
122      * @since 1.1
123      */

124     public String JavaDoc getParentMethodName()
125     {
126         return _parentMethodName;
127     }
128
129     public void setParentMethodName(String JavaDoc string)
130     {
131         _parentMethodName = string;
132     }
133
134     /**
135      * @since 1.1
136      */

137     public void begin(SchemaProcessor processor, Element element)
138     {
139         for (Iterator JavaDoc i = _rules.iterator(); i.hasNext();)
140         {
141             Rule rule = (Rule) i.next();
142
143             rule.begin(processor, element);
144         }
145     }
146
147     /**
148      * @since 1.1
149      */

150     public void end(SchemaProcessor processor, Element element)
151     {
152         for (ListIterator JavaDoc i = _rules.listIterator(_rules.size()); i.hasPrevious();)
153         {
154             Rule rule = (Rule) i.previous();
155
156             rule.end(processor, element);
157         }
158     }
159
160     public void addRulesForModel()
161     {
162         _rules = new ArrayList JavaDoc();
163
164         _rules.add(new CreateObjectRule(_className));
165
166         addAttributeRules();
167
168         _rules.add(new InvokeParentRule(_parentMethodName));
169     }
170
171     private void addAttributeRules()
172     {
173         Iterator JavaDoc i = _elementModel.getAttributeModels().iterator();
174
175         while (i.hasNext())
176         {
177             AttributeModel am = (AttributeModel) i.next();
178             String JavaDoc attributeName = am.getName();
179
180             AttributeMappingDescriptor amd = (AttributeMappingDescriptor) _attributeNameMappingMap
181                     .get(attributeName);
182
183             if (amd == null)
184             {
185                 _rules.add(new ReadAttributeRule(attributeName,
186                         constructPropertyName(attributeName), null, getLocation()));
187             }
188             else
189             {
190                 String JavaDoc propertyName = amd.getPropertyName();
191                 if (propertyName == null)
192                     propertyName = constructPropertyName(attributeName);
193
194                 _rules.add(new ReadAttributeRule(attributeName, propertyName, null, amd
195                         .getLocation()));
196
197                 _attributeNameMappingMap.remove(attributeName);
198             }
199         }
200
201         if (!_attributeNameMappingMap.isEmpty())
202             _errorHandler.error(LOG, ParseMessages.extraMappings(
203                     _attributeNameMappingMap.keySet(),
204                     _elementModel), _elementModel.getLocation(), null);
205     }
206
207     private String JavaDoc constructPropertyName(String JavaDoc attributeName)
208     {
209         int dashx = attributeName.indexOf('-');
210         if (dashx < 0)
211             return attributeName;
212
213         int length = attributeName.length();
214         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(length);
215
216         buffer.append(attributeName.substring(0, dashx));
217         boolean toUpper = true;
218
219         for (int i = dashx + 1; i < length; i++)
220         {
221             char ch = attributeName.charAt(i);
222
223             if (ch == '-')
224             {
225                 toUpper = true;
226                 continue;
227             }
228
229             if (toUpper)
230                 ch = Character.toUpperCase(ch);
231
232             buffer.append(ch);
233
234             toUpper = false;
235         }
236
237         return buffer.toString();
238     }
239 }
Popular Tags