KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
22
23 import org.openharmonise.rm.*;
24 import org.openharmonise.rm.metadata.ChildObjectPropertyInstance;
25 import org.openharmonise.rm.publishing.State;
26 import org.openharmonise.rm.resources.*;
27 import org.w3c.dom.Element JavaDoc;
28
29
30 /**
31  * Class which represents a <code>Property</code> range which is
32  * restricted to <code>AbstractChildObject</code> values defined by
33  * a relative path.
34  *
35  * @author Michael Bell
36  * @version $Revision: 1.2 $
37  *
38  */

39 public class RelativeChildObjectRange extends AbstractRange implements ChildObjectRange {
40
41
42     /**
43      * Relative child object range tag name.
44      */

45     public final static String JavaDoc TAG_RELATIVECHILDOBJECT_RANGE = "RelativeChildObjectRange";
46
47     /**
48      * Constructs a new <code>RelativeChildObjectRange</code>.
49      */

50     public RelativeChildObjectRange() {
51         super(RelativeChildObjectRange.class.getName());
52     }
53
54     /**
55      * Constructs a <code>RelativeChildObjectRange</code> with the specified
56      * non class type restrictions.
57      *
58      * @param sDetails the relative path which will be used to determine
59      * members of this range
60      */

61     public RelativeChildObjectRange(String JavaDoc sDetails) {
62         super(RelativeChildObjectRange.class.getName(),sDetails);
63     }
64     
65     /**
66      * Constructs a <code>RelativeChildObjectRange</code> with the specified
67      * class and path restrictions.
68      *
69      * @param objName the class name to associated with this range
70      * @param sDetails the relative path which will be used to determine
71      * members of this range
72      */

73     protected RelativeChildObjectRange(String JavaDoc objName, String JavaDoc sDetails) {
74         super(objName,sDetails);
75     }
76
77     /* (non-Javadoc)
78      * @see org.openharmonise.rm.resources.metadata.properties.ranges.Range#isValid(java.lang.Object)
79      */

80     public boolean isValid(Object JavaDoc obj) {
81         boolean bIsValid = false;
82         
83         return bIsValid;
84     }
85
86     /**
87      * Returns the list of <code>AbstractChildObject</code> members of this range
88      * determined by the relative path restriction.
89      *
90      * @param child the <code>AbstractChildObject</code> from which to base
91      * the relative path restrictions
92      * @return the list of <code>AbstractChildObject</code> members of this range
93      * @throws DataAccessException if any errors occur
94      */

95     public List getAvailableValues(AbstractChildObject child) throws DataAccessException {
96         List avails = new ArrayList();
97         
98         AbstractParentObject parent = child.getRealParent();
99         
100         StringTokenizer tokeniser = new StringTokenizer(m_sDetails,"/");
101         
102         while(tokeniser.hasMoreTokens() && parent != null) {
103             String JavaDoc sToken = tokeniser.nextToken();
104             
105             if(sToken != null && sToken.length() > 0) {
106                 parent = getParentFromPath(parent, sToken);
107             }
108         }
109         
110         if(parent != null) {
111             addAllChildObjectsToList(avails, parent);
112         }
113         
114         return avails;
115     }
116     
117     /**
118      * Returns the relative path restriction for this <code>Range</code>.
119      *
120      * @return the relative path restriction for this <code>Range</code>
121      */

122     public String JavaDoc getPathRestriction() {
123         return m_sDetails;
124     }
125     
126     /**
127      * Sets the relative path restriction for this <code>Range</code>.
128      *
129      * @param sPath the relative path restriction for this <code>Range</code>
130      */

131     public void setPathRestriction(String JavaDoc sPath) {
132         setDetails(sPath);
133         
134     }
135     
136     /**
137      * Returns the <code>AbstractParentObject</code> related to the given
138      * <code>AbstractParentObject</code> by the specified relative path
139      * segment.
140      *
141      * @param parent the parent object from which to resolve the relative path
142      * @param sPathSegment the relative path segment. May be '.','..' or a
143      * child member's name
144      *
145      * @return the related <code>AbstractParentObject</code>
146      * @throws DataAccessException if an error occurs accessing the relative
147      * object
148      */

149     private AbstractParentObject getParentFromPath(AbstractParentObject parent, String JavaDoc sPathSegment) throws DataAccessException {
150         AbstractParentObject result = null;
151         
152         if(sPathSegment != null && sPathSegment.length() > 0) {
153         
154             if(sPathSegment.equals(".")) {
155                 result = parent;
156             } else if(sPathSegment.equals("..")) {
157                 result = parent.getRealParent();
158             } else {
159                 AbstractChildObject child = parent.getChildByName(sPathSegment);
160                 
161                 if(child instanceof AbstractParentObject) {
162                     result = (AbstractParentObject) child;
163                 }
164             }
165         }
166         
167         return result;
168     }
169     
170     /**
171      * Adds all the non <code>AbstractParentObject</code> <code>AbstractChildObject</code>s
172      * of the given <code>AbstractParentObject</code> to the given <code>List</code>.
173      *
174      * @param result the list to add <code>AbstractChildObject</code>s to
175      * @param parent the <code>AbstractParentObject</code> from which the child
176      * member will be taken
177      *
178      * @throws DataAccessException if an error occurs accessing child members
179      * from the <code>AbstractParentObject</code>
180      */

181     private void addAllChildObjectsToList(
182         List result,
183         AbstractParentObject parent)
184         throws DataAccessException {
185         Iterator iter = parent.getChildren().iterator();
186
187         while (iter.hasNext()) {
188             AbstractChildObject child = (AbstractChildObject) iter.next();
189
190             if((child instanceof AbstractParentObject) == false) {
191                 result.add(child);
192             }
193
194             if (child instanceof AbstractParentObject) {
195                 addAllChildObjectsToList(result, (AbstractParentObject) child);
196             }
197         }
198     }
199
200     /* (non-Javadoc)
201      * @see org.openharmonise.rm.resources.metadata.properties.ranges.ChildObjectRange#getChildObjectValueClassName(org.openharmonise.rm.resources.AbstractChildObject)
202      */

203     public String JavaDoc getChildObjectValueClassName(AbstractChildObject child) {
204         return child.getClass().getName();
205     }
206
207     /* (non-Javadoc)
208      * @see org.openharmonise.rm.resources.metadata.properties.ranges.Range#getPropertyInstanceClass()
209      */

210     public Class JavaDoc getPropertyInstanceClass() throws ClassNotFoundException JavaDoc {
211         return ChildObjectPropertyInstance.class;
212     }
213     
214     /* (non-Javadoc)
215      * @see org.openharmonise.rm.publishing.Publishable#getTagName()
216      */

217     public String JavaDoc getTagName() {
218         return TAG_RELATIVECHILDOBJECT_RANGE;
219     }
220     
221     /* (non-Javadoc)
222      * @see org.openharmonise.rm.publishing.Publishable#populate(org.w3c.dom.Element, org.openharmonise.rm.publishing.State)
223      */

224     public void populate(Element JavaDoc xmlElement, State state)
225         throws PopulateException {
226         String JavaDoc sTagname = xmlElement.getTagName();
227     
228         if(sTagname.equals(TAG_PATH_RESTRICTION)) {
229             String JavaDoc sPath = xmlElement.getFirstChild().getNodeValue();
230             setPathRestriction(sPath);
231         } else {
232             super.populate(xmlElement, state);
233         }
234     
235     }
236
237     /* (non-Javadoc)
238      * @see org.openharmonise.rm.resources.metadata.properties.ranges.ChildObjectRange#getChildObjectValueClassName(java.lang.Class)
239      */

240     public String JavaDoc getChildObjectValueClassName(Class JavaDoc clss) {
241         return clss.getName();
242     }
243
244 }
245
Popular Tags