001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: */
019: package org.openharmonise.rm.resources.metadata.properties.ranges;
020:
021: import java.util.*;
022:
023: import org.openharmonise.rm.*;
024: import org.openharmonise.rm.metadata.ChildObjectPropertyInstance;
025: import org.openharmonise.rm.publishing.State;
026: import org.openharmonise.rm.resources.*;
027: import org.w3c.dom.Element;
028:
029: /**
030: * Class which represents a <code>Property</code> range which is
031: * restricted to <code>AbstractChildObject</code> values defined by
032: * a relative path.
033: *
034: * @author Michael Bell
035: * @version $Revision: 1.2 $
036: *
037: */
038: public class RelativeChildObjectRange extends AbstractRange implements
039: ChildObjectRange {
040:
041: /**
042: * Relative child object range tag name.
043: */
044: public final static String TAG_RELATIVECHILDOBJECT_RANGE = "RelativeChildObjectRange";
045:
046: /**
047: * Constructs a new <code>RelativeChildObjectRange</code>.
048: */
049: public RelativeChildObjectRange() {
050: super (RelativeChildObjectRange.class.getName());
051: }
052:
053: /**
054: * Constructs a <code>RelativeChildObjectRange</code> with the specified
055: * non class type restrictions.
056: *
057: * @param sDetails the relative path which will be used to determine
058: * members of this range
059: */
060: public RelativeChildObjectRange(String sDetails) {
061: super (RelativeChildObjectRange.class.getName(), sDetails);
062: }
063:
064: /**
065: * Constructs a <code>RelativeChildObjectRange</code> with the specified
066: * class and path restrictions.
067: *
068: * @param objName the class name to associated with this range
069: * @param sDetails the relative path which will be used to determine
070: * members of this range
071: */
072: protected RelativeChildObjectRange(String objName, String sDetails) {
073: super (objName, sDetails);
074: }
075:
076: /* (non-Javadoc)
077: * @see org.openharmonise.rm.resources.metadata.properties.ranges.Range#isValid(java.lang.Object)
078: */
079: public boolean isValid(Object obj) {
080: boolean bIsValid = false;
081:
082: return bIsValid;
083: }
084:
085: /**
086: * Returns the list of <code>AbstractChildObject</code> members of this range
087: * determined by the relative path restriction.
088: *
089: * @param child the <code>AbstractChildObject</code> from which to base
090: * the relative path restrictions
091: * @return the list of <code>AbstractChildObject</code> members of this range
092: * @throws DataAccessException if any errors occur
093: */
094: public List getAvailableValues(AbstractChildObject child)
095: throws DataAccessException {
096: List avails = new ArrayList();
097:
098: AbstractParentObject parent = child.getRealParent();
099:
100: StringTokenizer tokeniser = new StringTokenizer(m_sDetails, "/");
101:
102: while (tokeniser.hasMoreTokens() && parent != null) {
103: String 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 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 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(
150: AbstractParentObject parent, String sPathSegment)
151: throws DataAccessException {
152: AbstractParentObject result = null;
153:
154: if (sPathSegment != null && sPathSegment.length() > 0) {
155:
156: if (sPathSegment.equals(".")) {
157: result = parent;
158: } else if (sPathSegment.equals("..")) {
159: result = parent.getRealParent();
160: } else {
161: AbstractChildObject child = parent
162: .getChildByName(sPathSegment);
163:
164: if (child instanceof AbstractParentObject) {
165: result = (AbstractParentObject) child;
166: }
167: }
168: }
169:
170: return result;
171: }
172:
173: /**
174: * Adds all the non <code>AbstractParentObject</code> <code>AbstractChildObject</code>s
175: * of the given <code>AbstractParentObject</code> to the given <code>List</code>.
176: *
177: * @param result the list to add <code>AbstractChildObject</code>s to
178: * @param parent the <code>AbstractParentObject</code> from which the child
179: * member will be taken
180: *
181: * @throws DataAccessException if an error occurs accessing child members
182: * from the <code>AbstractParentObject</code>
183: */
184: private void addAllChildObjectsToList(List result,
185: AbstractParentObject parent) throws DataAccessException {
186: Iterator iter = parent.getChildren().iterator();
187:
188: while (iter.hasNext()) {
189: AbstractChildObject child = (AbstractChildObject) iter
190: .next();
191:
192: if ((child instanceof AbstractParentObject) == false) {
193: result.add(child);
194: }
195:
196: if (child instanceof AbstractParentObject) {
197: addAllChildObjectsToList(result,
198: (AbstractParentObject) child);
199: }
200: }
201: }
202:
203: /* (non-Javadoc)
204: * @see org.openharmonise.rm.resources.metadata.properties.ranges.ChildObjectRange#getChildObjectValueClassName(org.openharmonise.rm.resources.AbstractChildObject)
205: */
206: public String getChildObjectValueClassName(AbstractChildObject child) {
207: return child.getClass().getName();
208: }
209:
210: /* (non-Javadoc)
211: * @see org.openharmonise.rm.resources.metadata.properties.ranges.Range#getPropertyInstanceClass()
212: */
213: public Class getPropertyInstanceClass()
214: throws ClassNotFoundException {
215: return ChildObjectPropertyInstance.class;
216: }
217:
218: /* (non-Javadoc)
219: * @see org.openharmonise.rm.publishing.Publishable#getTagName()
220: */
221: public String getTagName() {
222: return TAG_RELATIVECHILDOBJECT_RANGE;
223: }
224:
225: /* (non-Javadoc)
226: * @see org.openharmonise.rm.publishing.Publishable#populate(org.w3c.dom.Element, org.openharmonise.rm.publishing.State)
227: */
228: public void populate(Element xmlElement, State state)
229: throws PopulateException {
230: String sTagname = xmlElement.getTagName();
231:
232: if (sTagname.equals(TAG_PATH_RESTRICTION)) {
233: String sPath = xmlElement.getFirstChild().getNodeValue();
234: setPathRestriction(sPath);
235: } else {
236: super .populate(xmlElement, state);
237: }
238:
239: }
240:
241: /* (non-Javadoc)
242: * @see org.openharmonise.rm.resources.metadata.properties.ranges.ChildObjectRange#getChildObjectValueClassName(java.lang.Class)
243: */
244: public String getChildObjectValueClassName(Class clss) {
245: return clss.getName();
246: }
247:
248: }
|