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-2006 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.spring.beans.editor;
043:
044: import javax.xml.XMLConstants;
045: import org.netbeans.editor.TokenItem;
046: import org.netbeans.modules.xml.text.api.XMLDefaultTokenContext;
047: import org.netbeans.modules.xml.text.syntax.SyntaxElement;
048: import org.netbeans.modules.xml.text.syntax.dom.EmptyTag;
049: import org.netbeans.modules.xml.text.syntax.dom.StartTag;
050: import org.netbeans.modules.xml.text.syntax.dom.Tag;
051:
052: /**
053: *
054: * @author Rohan Ranade
055: */
056: public final class ContextUtilities {
057:
058: private ContextUtilities() {
059: }
060:
061: public static final String P_NAMESPACE = "http://www.springframework.org/schema/p"; // NOI18N
062:
063: public static boolean isPNamespaceName(DocumentContext context,
064: String nodeName) {
065: String prefix = ContextUtilities
066: .getPrefixFromNodeName(nodeName);
067: if (prefix != null) {
068: String namespaceUri = context.lookupNamespacePrefix(prefix);
069: if (P_NAMESPACE.equals(namespaceUri)) {
070: return true;
071: }
072: }
073: return false;
074: }
075:
076: public static boolean isValueToken(TokenItem currentToken) {
077: if (currentToken != null) {
078: if (currentToken.getTokenID().getNumericID() == XMLDefaultTokenContext.VALUE_ID) {
079: return true;
080: }
081: }
082:
083: return false;
084: }
085:
086: public static boolean isTagToken(TokenItem currentToken) {
087: if (currentToken != null) {
088: if (currentToken.getTokenID().getNumericID() == XMLDefaultTokenContext.TAG_ID) {
089: return true;
090: }
091: }
092:
093: return false;
094: }
095:
096: public static boolean isAttributeToken(TokenItem currentToken) {
097: if (currentToken != null) {
098: if (currentToken.getTokenID().getNumericID() == XMLDefaultTokenContext.ARGUMENT_ID) {
099: return true;
100: }
101: }
102:
103: return false;
104: }
105:
106: public static TokenItem getAttributeToken(TokenItem currentToken) {
107: if (isValueToken(currentToken)) {
108: TokenItem equalsToken = currentToken.getPrevious();
109: while (equalsToken.getTokenID().getNumericID() != XMLDefaultTokenContext.OPERATOR_ID) {
110: equalsToken = equalsToken.getPrevious();
111: }
112:
113: TokenItem argumentToken = equalsToken.getPrevious();
114: while (argumentToken.getTokenID().getNumericID() != XMLDefaultTokenContext.ARGUMENT_ID) {
115: argumentToken = argumentToken.getPrevious();
116: }
117:
118: return argumentToken;
119: }
120:
121: return null;
122: }
123:
124: public static Tag getCurrentTagElement(DocumentContext context) {
125: SyntaxElement element = context.getCurrentElement();
126: if (element instanceof StartTag) {
127: return (StartTag) element;
128: } else if (element instanceof EmptyTag) {
129: return (EmptyTag) element;
130: }
131:
132: return null;
133: }
134:
135: public static TokenItem getAttributeToken(DocumentContext context) {
136: if (isValueToken(context.getCurrentToken())) {
137: TokenItem equalsToken = context.getCurrentToken()
138: .getPrevious();
139: while (equalsToken.getTokenID().getNumericID() != XMLDefaultTokenContext.OPERATOR_ID) {
140: equalsToken = equalsToken.getPrevious();
141: }
142:
143: TokenItem argumentToken = equalsToken.getPrevious();
144: while (argumentToken.getTokenID().getNumericID() != XMLDefaultTokenContext.ARGUMENT_ID) {
145: argumentToken = argumentToken.getPrevious();
146: }
147:
148: return argumentToken;
149: }
150:
151: return null;
152: }
153:
154: public static String getAttributeTokenImage(DocumentContext context) {
155: TokenItem tok = getAttributeToken(context);
156: if (tok != null) {
157: return tok.getImage();
158: }
159:
160: return null;
161: }
162:
163: /**
164: * Returns the prefix from the element's tag.
165: */
166: public static String getPrefixFromTag(String tagName) {
167: if (tagName == null)
168: return null;
169: return (tagName.indexOf(":") == -1) ? null : // NOI18N
170: tagName.substring(0, tagName.indexOf(":")); // NOI18N
171: }
172:
173: /**
174: * Returns the local name from the element's tag.
175: */
176: public static String getLocalNameFromTag(String tagName) {
177: if (tagName == null)
178: return null;
179: return (tagName.indexOf(":") == -1) ? tagName : // NOI18N
180: tagName.substring(tagName.indexOf(":") + 1, tagName
181: .length()); // NOI18N
182: }
183:
184: /**
185: * Returns any prefix declared with this namespace. For example, if
186: * the namespace was declared as xmlns:po, the prefix 'po' will be returned.
187: * Returns null for declaration that contains no prefix.
188: */
189: public static String getPrefixFromNamespaceDeclaration(
190: String namespace) {
191: if (!namespace.startsWith(XMLConstants.XMLNS_ATTRIBUTE))
192: return null;
193: int xmlnsLength = XMLConstants.XMLNS_ATTRIBUTE.length();
194: if (namespace.length() == xmlnsLength) {
195: return ""; // NOI18N
196: }
197: if (namespace.charAt(xmlnsLength) == ':') {
198: return namespace.substring(xmlnsLength + 1);
199: }
200: return null;
201: }
202:
203: public static String getPrefixFromNodeName(String nodeName) {
204: int colonIndex = nodeName.indexOf(':');
205: if (colonIndex <= 0) {
206: return null;
207: }
208: return nodeName.substring(0, colonIndex);
209: }
210:
211: public static StartTag getRoot(SyntaxElement se) {
212: StartTag root = null;
213: while (se != null) {
214: if (se instanceof StartTag) {
215: root = (StartTag) se;
216: }
217: se = se.getPrevious();
218: }
219:
220: return root;
221: }
222:
223: public static boolean isPNamespaceAdded(DocumentContext dc) {
224: return dc.getDeclaredNamespaces().contains(
225: ContextUtilities.P_NAMESPACE);
226: }
227: }
|