KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > xpath > AbstractXPathModelHelper


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.xpath;
21
22 import java.util.HashMap JavaDoc;
23
24 import org.apache.commons.jxpath.ri.compiler.VariableReference;
25
26
27 /**
28  * XPathModel helper class.
29  *
30  * @author Enrico Lelina
31  * @version $Revision: 1.4 $
32  */

33 public abstract class AbstractXPathModelHelper {
34
35     /**
36      * Describe variable <code>opHash</code> here.
37      *
38      */

39     protected static HashMap JavaDoc opHash = new HashMap JavaDoc();
40     /**
41      * Describe variable <code>funcHash</code> here.
42      *
43      */

44     private static HashMap JavaDoc funcHash = new HashMap JavaDoc();
45
46     static {
47         opHash.put("+", new Integer JavaDoc(XPathCoreOperation.OP_SUM));
48         opHash.put("-", new Integer JavaDoc(XPathCoreOperation.OP_MINUS));
49         opHash.put("*", new Integer JavaDoc(XPathCoreOperation.OP_MULT));
50         opHash.put("div", new Integer JavaDoc(XPathCoreOperation.OP_DIV));
51         opHash.put("mod", new Integer JavaDoc(XPathCoreOperation.OP_MOD));
52         // **FIX* opHash.put("", new Integer(XPathCoreOperation.OP_NEGATIVE));
53
opHash.put("negative", new Integer JavaDoc(XPathCoreOperation.OP_NEGATIVE));
54         opHash.put("and", new Integer JavaDoc(XPathCoreOperation.OP_AND));
55         opHash.put("or", new Integer JavaDoc(XPathCoreOperation.OP_OR));
56         opHash.put("==", new Integer JavaDoc(XPathCoreOperation.OP_EQ));
57         opHash.put("!=", new Integer JavaDoc(XPathCoreOperation.OP_NE));
58         opHash.put("&lt;", new Integer JavaDoc(XPathCoreOperation.OP_LT));
59         opHash.put("<", new Integer JavaDoc(XPathCoreOperation.OP_LT));
60         opHash.put("&lt;=", new Integer JavaDoc(XPathCoreOperation.OP_LE));
61         opHash.put("<=", new Integer JavaDoc(XPathCoreOperation.OP_LE));
62         opHash.put("&gt;", new Integer JavaDoc(XPathCoreOperation.OP_GT));
63         opHash.put(">", new Integer JavaDoc(XPathCoreOperation.OP_GT));
64         opHash.put("&gt;=", new Integer JavaDoc(XPathCoreOperation.OP_GE));
65         opHash.put(">=", new Integer JavaDoc(XPathCoreOperation.OP_GE));
66     }
67     
68
69     static {
70
71         funcHash.put("concat", new Integer JavaDoc(XPathCoreFunction.FUNC_CONCAT));
72         funcHash.put("last", new Integer JavaDoc(XPathCoreFunction.FUNC_LAST));
73         funcHash.put("position", new Integer JavaDoc(XPathCoreFunction.FUNC_POSITION));
74         funcHash.put("count", new Integer JavaDoc(XPathCoreFunction.FUNC_COUNT));
75         funcHash.put("id", new Integer JavaDoc(XPathCoreFunction.FUNC_ID));
76         funcHash.put("local-name", new Integer JavaDoc(XPathCoreFunction.FUNC_LOCAL_NAME));
77         funcHash.put("namespace-uri", new Integer JavaDoc(XPathCoreFunction.FUNC_NAMESPACE_URI));
78         funcHash.put("name", new Integer JavaDoc(XPathCoreFunction.FUNC_NAME));
79         funcHash.put("string", new Integer JavaDoc(XPathCoreFunction.FUNC_STRING));
80         funcHash.put("starts-with", new Integer JavaDoc(XPathCoreFunction.FUNC_STARTS_WITH));
81         funcHash.put("contains", new Integer JavaDoc(XPathCoreFunction.FUNC_CONTAINS));
82         funcHash.put("substring-before", new Integer JavaDoc(XPathCoreFunction.FUNC_SUBSTRING_BEFORE));
83         funcHash.put("substring-after", new Integer JavaDoc(XPathCoreFunction.FUNC_SUBSTRING_AFTER));
84         funcHash.put("substring", new Integer JavaDoc(XPathCoreFunction.FUNC_SUBSTRING));
85         funcHash.put("string-length", new Integer JavaDoc(XPathCoreFunction.FUNC_STRING_LENGTH));
86         funcHash.put("normalize-space", new Integer JavaDoc(XPathCoreFunction.FUNC_NORMALIZE_SPACE));
87         funcHash.put("translate", new Integer JavaDoc(XPathCoreFunction.FUNC_TRANSLATE));
88         funcHash.put("boolean", new Integer JavaDoc(XPathCoreFunction.FUNC_BOOLEAN));
89         funcHash.put("not", new Integer JavaDoc(XPathCoreFunction.FUNC_NOT));
90         funcHash.put("true", new Integer JavaDoc(XPathCoreFunction.FUNC_TRUE));
91         funcHash.put("false", new Integer JavaDoc(XPathCoreFunction.FUNC_FALSE));
92         funcHash.put("lang", new Integer JavaDoc(XPathCoreFunction.FUNC_LANG));
93         funcHash.put("number", new Integer JavaDoc(XPathCoreFunction.FUNC_NUMBER));
94         funcHash.put("sum", new Integer JavaDoc(XPathCoreFunction.FUNC_SUM));
95         funcHash.put("floor", new Integer JavaDoc(XPathCoreFunction.FUNC_FLOOR));
96         funcHash.put("ceiling", new Integer JavaDoc(XPathCoreFunction.FUNC_CEILING));
97         funcHash.put("round", new Integer JavaDoc(XPathCoreFunction.FUNC_ROUND));
98         funcHash.put("null", new Integer JavaDoc(XPathCoreFunction.FUNC_NULL));
99         funcHash.put("key", new Integer JavaDoc(XPathCoreFunction.FUNC_KEY));
100     }
101
102     /** Singleton Castor support */
103     private static AbstractXPathModelHelper mXPathModelHelper = null;
104
105     /**
106      * Get Castor support from Enterprise Designer context.
107      * @return AbstractXPathModelHelper The castor support object.
108      */

109     public static synchronized AbstractXPathModelHelper getInstance() {
110         if (mXPathModelHelper == null) {
111             mXPathModelHelper = loadImpl(null);
112         }
113
114         return mXPathModelHelper;
115     }
116     
117     /**
118      * Get the Castor support using the given Class Loader.
119      * @param loader Class Loader that can find the XPathModelHelper
120      * implementation class.
121      * @return AbstractCastorSuppport The castor support object.
122      */

123     public static synchronized AbstractXPathModelHelper getInstance(ClassLoader JavaDoc loader) {
124         if (null == mXPathModelHelper) {
125             mXPathModelHelper = loadImpl(loader);
126         }
127         return mXPathModelHelper;
128     }
129
130     /** Loads the XPathModelHelper implementation class.
131      * @param loader ClassLoader to use.
132      * @return XPathModelHelper implementing class.
133      */

134     private static AbstractXPathModelHelper loadImpl(ClassLoader JavaDoc loader) {
135         String JavaDoc implClassName = null;
136         AbstractXPathModelHelper axmh = null;
137         try {
138             implClassName =
139                 System.getProperty
140                 ("org.netbeans.modules.xml.xpath.AbstractXPathModelHelper",
141                  "org.netbeans.modules.xml.xpath.impl.XPathModelHelperImpl");
142             Class JavaDoc implClass = null;
143             if (loader != null) {
144                 implClass = Class.forName(implClassName, true, loader);
145             } else {
146                 implClass = Class.forName(implClassName);
147             }
148             axmh = (AbstractXPathModelHelper) implClass.newInstance();
149         } catch (Exception JavaDoc e) {
150             throw new IllegalArgumentException JavaDoc("Cannot find/load " + implClassName, e);
151         }
152         return axmh;
153     }
154     
155     /**
156      * Instantiates a new XPathModel object.
157      * @return a new XPathModel object instance
158      */

159     public abstract XPathModel newXPathModel();
160     
161     /**
162      * Instantiates a new XPathStringLiteral object.
163      * @param value the value
164      * @return a new XPathStringLiteral object instance
165      */

166     public abstract XPathStringLiteral newXPathStringLiteral(String JavaDoc value);
167     
168     /**
169      * Instantiates a new XPathNumericLiteral object.
170      * @param value the value
171      * @return a new XPathNumericLiteral object instance
172      */

173     public abstract XPathNumericLiteral newXPathNumericLiteral(Number JavaDoc value);
174
175     /**
176      * Instantiates a new XPathPredicateNumericLiteral object.
177      * @param value the value
178      * @return a new XPathPredicateNumericLiteral object instance
179      */

180     public abstract XPathPredicateNumericLiteral newXPathPredicateNumericLiteral(Long JavaDoc value);
181     
182     /**
183      * Instantiates a new XPathCoreFunction object.
184      * @param function the function code
185      * @return a new XPathCoreFunction object instance
186      */

187     public abstract XPathCoreFunction newXPathCoreFunction(int function);
188     
189     /**
190      * Instantiates a new XPathExtension Function object.
191      * @param name the function name
192      * @return a new XPathExtensionFunction object instance
193      */

194     public abstract XPathExtensionFunction newXPathExtensionFunction(String JavaDoc name);
195     
196     /**
197      * Instantiates a new XPathCoreOperation object.
198      * @param code the operation code
199      * @return a new XPathCoreOperatoin object instance
200      */

201     public abstract XPathCoreOperation newXPathCoreOperation(int code);
202     
203     /**
204      * Instantiates a new XPathLocationPath object.
205      * @param steps the steps
206      * @return a new XPathLocationPath object instance
207      */

208     public abstract XPathLocationPath newXPathLocationPath(LocationStep[] steps);
209
210     /**
211      * Instantiates a new XPathExpressionPath object.
212      * @param rootExpression root expression if any
213      * @param steps the steps
214      * @return a new XPathLocationPath object instance
215      */

216     public abstract XPathExpressionPath newXPathExpressionPath(XPathExpression rootExpression,
217                                                            LocationStep[] steps);
218
219     /**
220      * Instantiates a new XPathStringLiteral object of the variable type.
221      * @param steps the steps
222      * @return a new XPathStringLiteral object instance
223      */

224     public abstract XPathVariableReference newXPathVariableReference(VariableReference vReference);
225
226     /**
227      * Instantiates a new XPathPredicateExpression object for given expression.
228      * @param expression which is a predicate expression
229      * @return a new XPathPredicateExpression object instance
230      */

231     public abstract XPathPredicateExpression newXPathPredicateExpression(XPathExpression expression);
232
233     /**
234      * gives the type of the core operation given the operator string
235      * @param operator String
236      * @return int (type)
237      */

238     public Integer JavaDoc getOperatorType(String JavaDoc operator) {
239         
240         return (Integer JavaDoc) opHash.get(operator);
241     }
242
243     /**
244      * gives the type of the function given the function
245      * @param function String
246      * @return int (type)
247      */

248     public Integer JavaDoc getFunctionType(String JavaDoc function) {
249         
250         return (Integer JavaDoc) funcHash.get(function);
251     }
252
253     /**
254      * Determines if a function name is valid. Assumes the function name is
255      * not one of the core functions.
256      * @param functionName the name of the function
257      * @return true if the function name is valid, false otherwise
258      */

259     public abstract boolean isValidFunction(String JavaDoc functionName);
260     
261     /**
262      * Determines if a operator name is valid. Assumes the operatior name is
263      * built in operator.
264      * @param operatorName the name of the function
265      * @return true if the operatorName name is valid, false otherwise
266      */

267     public abstract boolean isValidOperator(String JavaDoc operatorName);
268 }
269
Popular Tags