001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.xml.xpath;
043:
044: import java.util.HashMap;
045:
046: import org.apache.commons.jxpath.ri.compiler.VariableReference;
047:
048: /**
049: * XPathModel helper class.
050: *
051: * @author Enrico Lelina
052: * @version
053: */
054: public abstract class AbstractXPathModelHelper {
055:
056: /**
057: * Describe variable <code>opHash</code> here.
058: *
059: */
060: protected static HashMap opHash = new HashMap();
061: /**
062: * Describe variable <code>funcHash</code> here.
063: *
064: */
065: private static HashMap funcHash = new HashMap();
066:
067: static {
068: opHash.put("+", new Integer(XPathCoreOperation.OP_SUM));
069: opHash.put("-", new Integer(XPathCoreOperation.OP_MINUS));
070: opHash.put("*", new Integer(XPathCoreOperation.OP_MULT));
071: opHash.put("div", new Integer(XPathCoreOperation.OP_DIV));
072: opHash.put("mod", new Integer(XPathCoreOperation.OP_MOD));
073: // **FIX* opHash.put("", new Integer(XPathCoreOperation.OP_NEGATIVE));
074: opHash.put("negative", new Integer(
075: XPathCoreOperation.OP_NEGATIVE));
076: opHash.put("and", new Integer(XPathCoreOperation.OP_AND));
077: opHash.put("or", new Integer(XPathCoreOperation.OP_OR));
078: opHash.put("==", new Integer(XPathCoreOperation.OP_EQ));
079: opHash.put("!=", new Integer(XPathCoreOperation.OP_NE));
080: opHash.put("<", new Integer(XPathCoreOperation.OP_LT));
081: opHash.put("<", new Integer(XPathCoreOperation.OP_LT));
082: opHash.put("<=", new Integer(XPathCoreOperation.OP_LE));
083: opHash.put("<=", new Integer(XPathCoreOperation.OP_LE));
084: opHash.put(">", new Integer(XPathCoreOperation.OP_GT));
085: opHash.put(">", new Integer(XPathCoreOperation.OP_GT));
086: opHash.put(">=", new Integer(XPathCoreOperation.OP_GE));
087: opHash.put(">=", new Integer(XPathCoreOperation.OP_GE));
088: }
089:
090: static {
091:
092: funcHash.put("concat", new Integer(
093: XPathCoreFunction.FUNC_CONCAT));
094: funcHash.put("last", new Integer(XPathCoreFunction.FUNC_LAST));
095: funcHash.put("position", new Integer(
096: XPathCoreFunction.FUNC_POSITION));
097: funcHash
098: .put("count", new Integer(XPathCoreFunction.FUNC_COUNT));
099: funcHash.put("id", new Integer(XPathCoreFunction.FUNC_ID));
100: funcHash.put("local-name", new Integer(
101: XPathCoreFunction.FUNC_LOCAL_NAME));
102: funcHash.put("namespace-uri", new Integer(
103: XPathCoreFunction.FUNC_NAMESPACE_URI));
104: funcHash.put("name", new Integer(XPathCoreFunction.FUNC_NAME));
105: funcHash.put("string", new Integer(
106: XPathCoreFunction.FUNC_STRING));
107: funcHash.put("starts-with", new Integer(
108: XPathCoreFunction.FUNC_STARTS_WITH));
109: funcHash.put("contains", new Integer(
110: XPathCoreFunction.FUNC_CONTAINS));
111: funcHash.put("substring-before", new Integer(
112: XPathCoreFunction.FUNC_SUBSTRING_BEFORE));
113: funcHash.put("substring-after", new Integer(
114: XPathCoreFunction.FUNC_SUBSTRING_AFTER));
115: funcHash.put("substring", new Integer(
116: XPathCoreFunction.FUNC_SUBSTRING));
117: funcHash.put("string-length", new Integer(
118: XPathCoreFunction.FUNC_STRING_LENGTH));
119: funcHash.put("normalize-space", new Integer(
120: XPathCoreFunction.FUNC_NORMALIZE_SPACE));
121: funcHash.put("translate", new Integer(
122: XPathCoreFunction.FUNC_TRANSLATE));
123: funcHash.put("boolean", new Integer(
124: XPathCoreFunction.FUNC_BOOLEAN));
125: funcHash.put("not", new Integer(XPathCoreFunction.FUNC_NOT));
126: funcHash.put("true", new Integer(XPathCoreFunction.FUNC_TRUE));
127: funcHash
128: .put("false", new Integer(XPathCoreFunction.FUNC_FALSE));
129: funcHash.put("lang", new Integer(XPathCoreFunction.FUNC_LANG));
130: funcHash.put("number", new Integer(
131: XPathCoreFunction.FUNC_NUMBER));
132: funcHash.put("sum", new Integer(XPathCoreFunction.FUNC_SUM));
133: funcHash
134: .put("floor", new Integer(XPathCoreFunction.FUNC_FLOOR));
135: funcHash.put("ceiling", new Integer(
136: XPathCoreFunction.FUNC_CEILING));
137: funcHash
138: .put("round", new Integer(XPathCoreFunction.FUNC_ROUND));
139: funcHash.put("null", new Integer(XPathCoreFunction.FUNC_NULL));
140: funcHash.put("key", new Integer(XPathCoreFunction.FUNC_KEY));
141: }
142:
143: /** Singleton Castor support */
144: private static AbstractXPathModelHelper mXPathModelHelper = null;
145:
146: /**
147: * Get Castor support from Enterprise Designer context.
148: * @return AbstractXPathModelHelper The castor support object.
149: */
150: public static synchronized AbstractXPathModelHelper getInstance() {
151: if (mXPathModelHelper == null) {
152: mXPathModelHelper = loadImpl(null);
153: }
154:
155: return mXPathModelHelper;
156: }
157:
158: /**
159: * Get the Castor support using the given Class Loader.
160: * @param loader Class Loader that can find the XPathModelHelper
161: * implementation class.
162: * @return AbstractCastorSuppport The castor support object.
163: */
164: public static synchronized AbstractXPathModelHelper getInstance(
165: ClassLoader loader) {
166: if (null == mXPathModelHelper) {
167: mXPathModelHelper = loadImpl(loader);
168: }
169: return mXPathModelHelper;
170: }
171:
172: /** Loads the XPathModelHelper implementation class.
173: * @param loader ClassLoader to use.
174: * @return XPathModelHelper implementing class.
175: */
176: private static AbstractXPathModelHelper loadImpl(ClassLoader loader) {
177: String implClassName = null;
178: AbstractXPathModelHelper axmh = null;
179: try {
180: implClassName = System
181: .getProperty(
182: "org.netbeans.modules.xml.xpath.AbstractXPathModelHelper",
183: "org.netbeans.modules.xml.xpath.impl.XPathModelHelperImpl");
184: Class implClass = null;
185: if (loader != null) {
186: implClass = Class.forName(implClassName, true, loader);
187: } else {
188: implClass = Class.forName(implClassName);
189: }
190: axmh = (AbstractXPathModelHelper) implClass.newInstance();
191: } catch (Exception e) {
192: throw new IllegalArgumentException("Cannot find/load "
193: + implClassName, e);
194: }
195: return axmh;
196: }
197:
198: /**
199: * Instantiates a new XPathModel object.
200: * @return a new XPathModel object instance
201: */
202: public abstract XPathModel newXPathModel();
203:
204: /**
205: * Instantiates a new XPathStringLiteral object.
206: * @param value the value
207: * @return a new XPathStringLiteral object instance
208: */
209: public abstract XPathStringLiteral newXPathStringLiteral(
210: String value);
211:
212: /**
213: * Instantiates a new XPathNumericLiteral object.
214: * @param value the value
215: * @return a new XPathNumericLiteral object instance
216: */
217: public abstract XPathNumericLiteral newXPathNumericLiteral(
218: Number value);
219:
220: /**
221: * Instantiates a new XPathPredicateNumericLiteral object.
222: * @param value the value
223: * @return a new XPathPredicateNumericLiteral object instance
224: */
225: public abstract XPathPredicateNumericLiteral newXPathPredicateNumericLiteral(
226: Long value);
227:
228: /**
229: * Instantiates a new XPathCoreFunction object.
230: * @param function the function code
231: * @return a new XPathCoreFunction object instance
232: */
233: public abstract XPathCoreFunction newXPathCoreFunction(int function);
234:
235: /**
236: * Instantiates a new XPathExtension Function object.
237: * @param name the function name
238: * @return a new XPathExtensionFunction object instance
239: */
240: public abstract XPathExtensionFunction newXPathExtensionFunction(
241: String name);
242:
243: /**
244: * Instantiates a new XPathCoreOperation object.
245: * @param code the operation code
246: * @return a new XPathCoreOperatoin object instance
247: */
248: public abstract XPathCoreOperation newXPathCoreOperation(int code);
249:
250: /**
251: * Instantiates a new XPathLocationPath object.
252: * @param steps the steps
253: * @return a new XPathLocationPath object instance
254: */
255: public abstract XPathLocationPath newXPathLocationPath(
256: LocationStep[] steps);
257:
258: /**
259: * Instantiates a new XPathExpressionPath object.
260: * @param rootExpression root expression if any
261: * @param steps the steps
262: * @return a new XPathLocationPath object instance
263: */
264: public abstract XPathExpressionPath newXPathExpressionPath(
265: XPathExpression rootExpression, LocationStep[] steps);
266:
267: /**
268: * Instantiates a new XPathStringLiteral object of the variable type.
269: * @param steps the steps
270: * @return a new XPathStringLiteral object instance
271: */
272: public abstract XPathVariableReference newXPathVariableReference(
273: VariableReference vReference);
274:
275: /**
276: * Instantiates a new XPathPredicateExpression object for given expression.
277: * @param expression which is a predicate expression
278: * @return a new XPathPredicateExpression object instance
279: */
280: public abstract XPathPredicateExpression newXPathPredicateExpression(
281: XPathExpression expression);
282:
283: /**
284: * gives the type of the core operation given the operator string
285: * @param operator String
286: * @return int (type)
287: */
288: public Integer getOperatorType(String operator) {
289:
290: return (Integer) opHash.get(operator);
291: }
292:
293: /**
294: * gives the type of the function given the function
295: * @param function String
296: * @return int (type)
297: */
298: public Integer getFunctionType(String function) {
299:
300: return (Integer) funcHash.get(function);
301: }
302:
303: /**
304: * Determines if a function name is valid. Assumes the function name is
305: * not one of the core functions.
306: * @param functionName the name of the function
307: * @return true if the function name is valid, false otherwise
308: */
309: public abstract boolean isValidFunction(String functionName);
310:
311: /**
312: * Determines if a operator name is valid. Assumes the operatior name is
313: * built in operator.
314: * @param operatorName the name of the function
315: * @return true if the operatorName name is valid, false otherwise
316: */
317: public abstract boolean isValidOperator(String operatorName);
318: }
|