01: // THIS SOFTWARE IS PROVIDED BY SOFTARIS PTY.LTD. AND OTHER METABOSS
02: // CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
03: // BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
04: // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOFTARIS PTY.LTD.
05: // OR OTHER METABOSS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
06: // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
07: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
08: // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
09: // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
10: // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
11: // EVEN IF SOFTARIS PTY.LTD. OR OTHER METABOSS CONTRIBUTORS ARE ADVISED OF THE
12: // POSSIBILITY OF SUCH DAMAGE.
13: //
14: // Copyright 2000-2005 © Softaris Pty.Ltd. All Rights Reserved.
15: package com.metaboss.sdlctools.applications.anttasks;
16:
17: import java.util.Arrays;
18: import java.util.HashSet;
19: import java.util.Set;
20:
21: import org.apache.tools.ant.BuildException;
22:
23: import com.metaboss.sdlctools.models.metabossmodel.ModelElement;
24:
25: /** This class contains one or more definitions of the model elements. It can be
26: * initialised with ref attribute or xpath attribute. In order to find out
27: * which elements are included the ref or xpath will be executed on the underlying model */
28: public class ModelElementRefType {
29: // The task, which is an owner of this element
30: private MetaBossModelToolTask mOwnerTask = null; // The owner task
31: private Class[] mAllowedClasses = null; // The array of classes this element is allowed to reference
32: private String mRef = null; // Optional model element reference
33: private String mXPath = null; // Optional model element xpath
34:
35: /** The only available constructor */
36: public ModelElementRefType(MetaBossModelToolTask pOwnerTask,
37: Class[] pAllowedClasses) {
38: mOwnerTask = pOwnerTask;
39: mAllowedClasses = pAllowedClasses;
40: }
41:
42: /** Returns the owner of this element */
43: public final MetaBossModelToolTask getOwnerTask() {
44: return mOwnerTask;
45: }
46:
47: // The setter for the "ref" attribute
48: public void setRef(String pRef) {
49: mRef = pRef;
50: }
51:
52: // The setter for the "xpath" attribute
53: public void setXPath(String pXpath) {
54: mXPath = pXpath;
55: }
56:
57: /** Returns all model elements referenced by this ref in the given context */
58: public ModelElement[] getReferencedModelElements(
59: ModelElement pContextModelElement) throws BuildException {
60: Set lReferencedModelElements = new HashSet();
61: // First look at the ref
62: if (mRef != null) {
63: ModelElement lModelElement = getOwnerTask()
64: .getModelElement(mRef, mAllowedClasses);
65: if (pContextModelElement != null) {
66: if (lModelElement.equals(pContextModelElement) == false
67: && lModelElement
68: .isContainedWithin(pContextModelElement) == false)
69: throw new BuildException("Element '" + mRef
70: + " is not contained within '"
71: + pContextModelElement.getRef()
72: + "' element.");
73: }
74: lReferencedModelElements.add(lModelElement);
75: }
76: // Now look at the xpath
77: if (mXPath != null) {
78: lReferencedModelElements.addAll(Arrays
79: .asList(getOwnerTask().findModelElementsByXPath(
80: pContextModelElement, mXPath,
81: mAllowedClasses)));
82: }
83: // Return what we have
84: return (ModelElement[]) lReferencedModelElements
85: .toArray(new ModelElement[lReferencedModelElements
86: .size()]);
87: }
88: }
|