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.models.xpathsearch;
16:
17: import java.util.ArrayList;
18: import java.util.Arrays;
19: import java.util.Collection;
20: import java.util.Collections;
21: import java.util.Iterator;
22: import java.util.List;
23:
24: import javax.jmi.reflect.RefBaseObject;
25:
26: import org.jaxen.BaseXPath;
27: import org.jaxen.FunctionContext;
28: import org.jaxen.JaxenException;
29:
30: import com.metaboss.sdlctools.models.ModelRepositoryException;
31: import com.metaboss.sdlctools.models.ModelRepositoryXPathProcessingException;
32:
33: /** This is the main entry point for matching an XPath against a MOF Model */
34: public class MOFXPath extends BaseXPath {
35: /** Construct given an XPath expression string. */
36: public MOFXPath(MOFXPathDocumentNavigator pNavigator,
37: String pXPathExpression) throws JaxenException {
38: super (pXPathExpression, MOFXPathDocumentNavigator.getInstance());
39: }
40:
41: /** Override to create specialised function context
42: * @return Specialised function context */
43: protected FunctionContext createFunctionContext() {
44: return MOFXPathDocumentNavigator.getFunctionContextInstance();
45: }
46:
47: /** This method selects nodes using given XPath expression starting from the
48: * given model element. The reason this method
49: * is separate and not just overridden selectNodes is that we actually do not
50: * want to interfere in XPath processing itself (so we do not want to override the method
51: * which might be called recursively) */
52: public static List doSelectNodes(String pXPathExpression,
53: RefBaseObject pNode) throws ModelRepositoryException {
54: return doSelectNodes(pXPathExpression, Arrays
55: .asList(new RefBaseObject[] { pNode }));
56: }
57:
58: /** This method selects nodes using given XPath expression starting from the
59: * given collection of the model elements. The reason this method
60: * is separate and not just overridden selectNodes is that we actually do not
61: * want to interfere in XPath processing itself (so we do not want to override the method
62: * which might be called recursively) */
63: public static List doSelectNodes(String pXPathExpression,
64: Collection pNodes) throws ModelRepositoryException {
65: try {
66: MOFXPathDocumentNavigator lNavigator = MOFXPathDocumentNavigator
67: .getInstance();
68: MOFXPath lPath = new MOFXPath(lNavigator, pXPathExpression);
69: // Preprocess all input nodes
70: List lInputNodesList = new ArrayList();
71: for (Iterator lInputNodesIterator = pNodes.iterator(); lInputNodesIterator
72: .hasNext();)
73: lInputNodesList.add(lNavigator
74: .preprocessObject(lInputNodesIterator.next()));
75: List lResult = lPath.selectNodes(lInputNodesList);
76: if (lResult.isEmpty())
77: return Collections.unmodifiableList(lResult);
78: // Do postprocessing
79: List lProcessedResult = new ArrayList();
80: for (Iterator lIter = lResult.iterator(); lIter.hasNext();)
81: lProcessedResult.add(lNavigator.postprocessObject(lIter
82: .next()));
83: return Collections.unmodifiableList(lProcessedResult);
84: } catch (JaxenException e) {
85: throw new ModelRepositoryXPathProcessingException(
86: "Caught exception while trying to search for model elements by XPath. XPath expression: "
87: + pXPathExpression, e);
88: }
89: }
90: }
|