001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019:
020: package org.netbeans.modules.xml.xpath.ext;
021:
022: import java.util.ArrayList;
023: import java.util.Collections;
024: import java.util.List;
025: import javax.swing.Icon;
026: import javax.xml.namespace.QName;
027: import org.netbeans.modules.xml.xpath.ext.metadata.AbstractArgument;
028: import org.netbeans.modules.xml.xpath.ext.metadata.ExtFunctionMetadata;
029: import org.netbeans.modules.xml.xpath.ext.metadata.ResultTypeCalculator;
030: import org.netbeans.modules.xml.xpath.ext.metadata.StubExtFunction;
031: import org.netbeans.modules.xml.xpath.ext.metadata.XPathType;
032: import org.netbeans.modules.xml.xpath.ext.metadata.impl.images.IconLoader;
033: import org.netbeans.modules.xml.xpath.ext.visitor.XPathVisitor;
034: import org.netbeans.modules.xml.xpath.ext.visitor.impl.ExpressionWriter;
035:
036: /**
037: * Represents a extension XPath function.
038: *
039: * This class can be extended in external modules.
040: * That is why it is not an interface but a class as distinct from other XPath
041: * entities. It has its own implementation of all base interfaces.
042: * Other XPath entities has hidden implementation. Only theirs interfaces are opened.
043: *
044: * @author Enrico Lelina
045: * @version
046: */
047: public class XPathExtensionFunction implements
048: XPathOperationOrFuntion<QName> {
049:
050: public XPathModel mModel;
051: public ExtFunctionMetadata mMetadata;
052:
053: /** List of child expressions. */
054: public List<XPathExpression> mChildren = new ArrayList<XPathExpression>();
055:
056: public XPathExtensionFunction(XPathModel model) {
057: this (model, NULL_METADATA_STUB);
058: }
059:
060: public XPathExtensionFunction(XPathModel model,
061: ExtFunctionMetadata metadata) {
062: mModel = model;
063: mMetadata = metadata;
064: }
065:
066: public XPathModel getModel() {
067: return mModel;
068: }
069:
070: public ExtFunctionMetadata getMetadata() {
071: return mMetadata;
072: }
073:
074: public QName getName() {
075: return mMetadata.getName();
076: }
077:
078: public List<XPathExpression> getChildren() {
079: return mChildren;
080: }
081:
082: public int getChildCount() {
083: return mChildren.size();
084: }
085:
086: public XPathExpression getChild(int index)
087: throws IndexOutOfBoundsException {
088: return mChildren.get(index);
089: }
090:
091: public void addChild(XPathExpression child) {
092: mChildren.add(child);
093: }
094:
095: public void insertChild(int index, XPathExpression child) {
096: mChildren.add(index, child);
097: }
098:
099: public void populateWithStub(int count) {
100: XPathModelFactory factory = getModel().getFactory();
101: for (int index = 0; index < count; index++) {
102: XPathExtensionFunction newStub = factory
103: .newXPathExtensionFunction(StubExtFunction.STUB_FUNC_NAME);
104: mChildren.add(newStub);
105: }
106: }
107:
108: public boolean removeChild(XPathExpression child) {
109: return mChildren.remove(child);
110: }
111:
112: public void clearChildren() {
113: mChildren.clear();
114: }
115:
116: public String getExpressionString() {
117: XPathVisitor visitor = new ExpressionWriter(mModel);
118: accept(visitor);
119: return ((ExpressionWriter) visitor).getString();
120: }
121:
122: public void accept(XPathVisitor visitor) {
123: visitor.visit(this );
124: }
125:
126: @Override
127: public String toString() {
128: return getExpressionString();
129: }
130:
131: /**
132: * This metadata is a stub. It can be used as a temporary empty implementation.
133: */
134: public static final ExtFunctionMetadata NULL_METADATA_STUB = new ExtFunctionMetadata() {
135:
136: public boolean isContextItemRequired(
137: XPathOperationOrFuntion func) {
138: return false;
139: }
140:
141: public QName getName() {
142: return null;
143: }
144:
145: public Icon getIcon() {
146: return IconLoader.UNKNOWN_ICON;
147: }
148:
149: public String getDisplayName() {
150: return "";
151: }
152:
153: public String getShortDescription() {
154: return ""; // NOI18N
155: }
156:
157: public String getLongDescription() {
158: return ""; // NOI18N
159: }
160:
161: public FunctionType getFunctionType() {
162: return FunctionType.EXT_FUNCTION;
163: }
164:
165: public List<AbstractArgument> getArguments() {
166: return null;
167: }
168:
169: public XPathType getResultType() {
170: return XPathType.ANY_TYPE;
171: }
172:
173: public ResultTypeCalculator getResultTypeCalculator() {
174: return null;
175: }
176:
177: public String toStrign() {
178: return "NULL metadata stub"; // NOI18N
179: }
180: };
181:
182: }
|