KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > rm > resources > metadata > properties > ranges > AbstractRange


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (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 http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19 package org.openharmonise.rm.resources.metadata.properties.ranges;
20
21 import java.util.Vector JavaDoc;
22
23 import org.openharmonise.rm.*;
24 import org.openharmonise.rm.factory.*;
25 import org.openharmonise.rm.publishing.*;
26 import org.openharmonise.rm.resources.publishing.Template;
27 import org.w3c.dom.*;
28
29
30 /**
31  * Base abstract implementation of the <code>Range</code> interface
32  * for <code>Property</code> ranges. Handles data which define the restrictions on
33  * allowed property instance values.
34  *
35  *
36  * @author Michael Bell
37  * @version $Revision: 1.2 $
38  *
39  */

40 public abstract class AbstractRange implements Range, Publishable, Cloneable JavaDoc {
41
42     //XML constants
43
/**
44      * The range XML element name
45      */

46     public static final String JavaDoc TAG_RANGE = "Range";
47     
48     /**
49      * The range details XML element name
50      */

51     public static final String JavaDoc TAG_RANGE_DETAILS = "RangeDetails";
52     
53     /**
54      * The range object XML element name
55      */

56     public static final String JavaDoc TAG_RANGE_OBJECT = "RangeObject";
57
58     /**
59      * The <code>String</code> representation of all non class type restrictions
60      * for this range
61      */

62     protected String JavaDoc m_sDetails = null;
63     
64     /**
65      * The class name of the class restriction for this range
66      */

67     protected String JavaDoc m_sObjectClassName = null;
68     
69     /**
70      * <code>boolean</code> flag which indicates that this range has been changed
71      * since being populated
72      */

73     private boolean m_bIsChanged = false;
74
75     /**
76      * Constructs a new range.
77      */

78     public AbstractRange() {
79     }
80
81     /**
82      * Constructs a range with the specified class type restriction.
83      *
84      * @param sObjectClassName the class name of objects within this range
85      */

86     public AbstractRange(String JavaDoc sObjectClassName) {
87         this(sObjectClassName, null);
88     }
89
90     /**
91      * Constructs a range with the specified class type and other restrictions.
92      *
93      * @param sObjectClassName the class name of objects within this range
94      * @param sDetails a <code>String</code> representation of the non class
95      * type restrictions
96      */

97     public AbstractRange(String JavaDoc sObjectClassName, String JavaDoc sDetails) {
98         m_sObjectClassName = sObjectClassName;
99         m_sDetails = sDetails;
100     }
101
102     /**
103      * Returns <code>true</code> if this range has restrictions other than
104      * the class type restriction.
105      *
106      * @return <code>true</code> if this range has restrictions other than
107      * the class type restriction
108      */

109     public boolean hasDetails() {
110         return m_sDetails != null;
111     }
112
113     /* (non-Javadoc)
114      * @see org.openharmonise.rm.resources.metadata.properties.ranges.Range#getDetails()
115      */

116     public String JavaDoc getDetails() {
117
118         return m_sDetails;
119     }
120
121     /* (non-Javadoc)
122      * @see org.openharmonise.rm.resources.metadata.properties.ranges.Range#getObject()
123      */

124     public String JavaDoc getObject() {
125
126         return m_sObjectClassName;
127     }
128
129     /* (non-Javadoc)
130      * @see org.openharmonise.rm.resources.metadata.properties.ranges.Range#isValid(java.lang.Object)
131      */

132     abstract public boolean isValid(Object JavaDoc obj);
133
134     /* (non-Javadoc)
135      * @see org.openharmonise.rm.resources.metadata.properties.ranges.Range#setDetails(java.lang.String)
136      */

137     public void setDetails(String JavaDoc sDetails) {
138         m_sDetails = sDetails;
139
140     }
141
142     /* (non-Javadoc)
143      * @see java.lang.Object#clone()
144      */

145     public Object JavaDoc clone() {
146         try {
147             return super.clone();
148         } catch (CloneNotSupportedException JavaDoc e) {
149             return null;
150         }
151     }
152
153     /* (non-Javadoc)
154      * @see org.openharmonise.rm.publishing.Publishable#getTagName()
155      */

156     public String JavaDoc getTagName() {
157         return TAG_RANGE;
158     }
159
160     /* (non-Javadoc)
161      * @see org.openharmonise.rm.publishing.Publishable#populate(org.w3c.dom.Element, org.openharmonise.rm.publishing.State)
162      */

163     public void populate(Element xmlElement, State state)
164         throws PopulateException {
165         String JavaDoc sTagName = xmlElement.getTagName();
166         Text txt = null;
167         String JavaDoc sTemp = null;
168
169         if (sTagName.equals(getTagName()) == true || sTagName.equals(TAG_RANGE) == true) {
170             NodeList nodes = xmlElement.getChildNodes();
171
172             for (int i = 0; i < nodes.getLength(); i++) {
173                 if (nodes.item(i).getNodeType() != Node.ELEMENT_NODE) {
174                     continue;
175                 }
176
177                 Element el = (Element) nodes.item(i);
178                 populate(el, state);
179             }
180         } else if (sTagName.equals(TAG_RANGE_OBJECT)) {
181             NodeList nodes = xmlElement.getChildNodes();
182             
183             //need to loop through to avoid whitespace etc
184
for(int i=0;i<nodes.getLength();i++) {
185                 Node node = nodes.item(i);
186                 
187                 if(node.getNodeType() == Node.TEXT_NODE) {
188                     txt = (Text) node;
189                     String JavaDoc sVal = txt.getNodeValue().trim();
190                     if(sVal.length() > 0) {
191                         this.m_sObjectClassName = sVal;
192                         break;
193                     }
194                 } else if(node.getNodeType() == Node.ELEMENT_NODE) {
195                     try {
196                         String JavaDoc sClassname = HarmoniseObjectFactory.getClassName((Element)node);
197                         this.m_sObjectClassName = sClassname;
198                         break;
199                     } catch (HarmoniseFactoryException e) {
200                         throw new PopulateException(e);
201                     }
202                 }
203             }
204             
205             
206         } else if (sTagName.equals(TAG_RANGE_DETAILS)) {
207             txt = (Text) xmlElement.getFirstChild();
208             setDetails(txt.getNodeValue());
209         }
210
211     }
212
213     /* (non-Javadoc)
214      * @see org.openharmonise.rm.publishing.Publishable#publish(org.w3c.dom.Element, org.openharmonise.rm.publishing.HarmoniseOutput, org.openharmonise.rm.publishing.State)
215      */

216     public Element publish(Element topEl, HarmoniseOutput output, State state)
217         throws PublishException {
218         Element docEl = null;
219         NodeList nodes = null;
220         Text txt = null;
221         String JavaDoc sTagName = topEl.getTagName();
222
223         try {
224
225             if (sTagName.equals(getTagName()) || sTagName.equals(TAG_RANGE)) {
226                 docEl = output.createElement(getTagName());
227
228                 nodes = topEl.getChildNodes();
229             } else if (sTagName.equals(TAG_RANGE_OBJECT)) {
230                 docEl = output.createElement(sTagName);
231                 txt = output.createTextNode(getObject());
232                 docEl.appendChild(txt);
233                 output.copyChildren(docEl, topEl, new Vector JavaDoc());
234             } else if (sTagName.equals(TAG_RANGE_DETAILS)) {
235                 docEl = output.createElement(sTagName);
236                 txt = output.createTextNode(getDetails());
237                 docEl.appendChild(txt);
238                 output.copyChildren(docEl, topEl, new Vector JavaDoc());
239             }
240
241             // recurse through the children if there are any
242
Element formEl;
243             Element el;
244
245             if (nodes != null) {
246                 for (int i = 0; i < nodes.getLength(); i++) {
247                     if (nodes.item(i).getNodeType() != Node.ELEMENT_NODE) {
248                         continue;
249                     }
250
251                     formEl = (Element) nodes.item(i);
252                     el = publish(formEl, output, state);
253
254                     if (el != null) {
255                         docEl.appendChild(el);
256                     }
257                 }
258             }
259
260         } catch (Exception JavaDoc ex) {
261             throw new PublishException(ex.getLocalizedMessage());
262         }
263
264         return docEl;
265     }
266
267     /* (non-Javadoc)
268      * @see org.openharmonise.rm.publishing.Publishable#publish(org.openharmonise.rm.resources.publishing.Template, org.openharmonise.rm.publishing.HarmoniseOutput, org.openharmonise.rm.publishing.State)
269      */

270     public Element publish(Template template, HarmoniseOutput output, State state)
271         throws PublishException {
272         Element resultEl = null;
273
274         try {
275
276             resultEl =
277                 publish(template.getTemplateRootElement(), output, state);
278         } catch (DataAccessException e) {
279             throw new PublishException(e);
280         }
281
282         return resultEl;
283     }
284
285     /**
286      * Sets the <code>boolean</code> flag which indicates a change to this range.
287      *
288      * @param bIsChanged <code>true</code> to indicate that this range has been
289      * changed since population
290      */

291     public void isChanged(boolean bIsChanged) {
292         m_bIsChanged = bIsChanged;
293     }
294
295     /**
296      * Returns <code>true</code> if this range has changed since population.
297      *
298      * @return <code>true</code> if this range has changed since population
299      */

300     public boolean isChanged() {
301         return m_bIsChanged;
302     }
303
304     /* (non-Javadoc)
305      * @see java.lang.Object#equals(java.lang.Object)
306      */

307     public boolean equals(Object JavaDoc obj) {
308         boolean bResult = false;
309
310         if (obj instanceof AbstractRange) {
311             AbstractRange range = (AbstractRange) obj;
312
313             if (this == range) {
314                 bResult = true;
315             } else {
316                 bResult = true;
317
318                 bResult = m_sObjectClassName.equals(range.getObject());
319
320                 if (bResult == true) {
321                     
322                     String JavaDoc sDetails = getDetails();
323                     String JavaDoc sOtherDetails = range.getDetails();
324     
325                     if ((sDetails == null && sOtherDetails != null)
326                         || (sOtherDetails == null && sDetails != null)
327                         || (sDetails != null && sDetails.equals(sOtherDetails) == false)) {
328                         bResult = false;
329                     }
330                 }
331
332             }
333         }
334
335         return bResult;
336     }
337
338     /* (non-Javadoc)
339      * @see java.lang.Object#toString()
340      */

341     public String JavaDoc toString() {
342         StringBuffer JavaDoc strbuf = new StringBuffer JavaDoc();
343
344         strbuf.append("range [obj - ").append(m_sObjectClassName).append("]");
345         strbuf.append("[ details - ").append(m_sDetails).append("]");
346
347         return strbuf.toString();
348     }
349     
350     
351     /* (non-Javadoc)
352      * @see org.openharmonise.rm.resources.metadata.properties.ranges.Range#getPropertyInstanceClass()
353      */

354     abstract public Class JavaDoc getPropertyInstanceClass() throws ClassNotFoundException JavaDoc;
355
356 }
357
Popular Tags