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.uml.core.reverseengineering.reframework.parsingframework;
043:
044: import org.netbeans.modules.uml.core.reverseengineering.reframework.IDataTypeKind;
045: import org.netbeans.modules.uml.core.reverseengineering.reframework.ITokenKind;
046: import org.netbeans.modules.uml.core.support.umlutils.ETList;
047: import org.netbeans.modules.uml.core.support.umlutils.ETArrayList;
048:
049: /**
050: * @author sumitabhk
051: *
052: */
053: public class LanguageSyntax implements ILanguageSyntax {
054: ETList<ISyntaxToken> m_Tokens = new ETArrayList<ISyntaxToken>();
055:
056: /**
057: * Retrieves the token information that defines a string literal.
058: *
059: * @param pVal [out] The token that delimites a string
060: */
061: public ISyntaxToken getStringDelimiter() {
062: ISyntaxToken retToken = null;
063:
064: // The string delimiter should always have the token type kind and a category of
065: // string.
066: ETList<ISyntaxToken> pTokenList = getTokensByCategory(
067: ITokenKind.DELIMITER, "String");
068: if (pTokenList != null) {
069: // There should only be one syntax token that matches the specified category.
070: // However, even if there is more than one I will only use the first syntax token
071: // that was defined.
072: int count = pTokenList.size();
073: if (count > 0) {
074: retToken = (ISyntaxToken) pTokenList.get(0);
075: }
076: }
077:
078: return retToken;
079: }
080:
081: /**
082: * Retrieve the token information that is used to define a character literal.
083: *
084: * @param pVal [out] The token information
085: */
086: public ISyntaxToken getCharacterDelimiter() {
087: ISyntaxToken retToken = null;
088:
089: // The string delimiter should always have the token type kind and a category of
090: // string.
091: ETList<ISyntaxToken> pTokenList = getTokensByCategory(
092: ITokenKind.DELIMITER, "Character");
093: if (pTokenList != null) {
094: // There should only be one syntax token that matches the specified category.
095: // However, even if there is more than one I will only use the first syntax token
096: // that was defined.
097: int count = pTokenList.size();
098: if (count > 0) {
099: retToken = (ISyntaxToken) pTokenList.get(0);
100: }
101: }
102:
103: return retToken;
104: }
105:
106: /**
107: * Gets the set of tokens that define the syntax of a language.
108: *
109: * @param pVal [out] The set of tokens
110: */
111: public ETList<ISyntaxToken> getSyntaxTokens() {
112: return m_Tokens;
113: }
114:
115: /**
116: * Sets the set of tokens that define the syntax of a language.
117: *
118: * @param newVal [in] The set of tokens
119: */
120: public void setSyntaxTokens(ETList<ISyntaxToken> newVal) {
121: m_Tokens = newVal;
122: }
123:
124: /**
125: * Retrieves all the tokens that have the specified token type.
126: *
127: * @param wantedKind [in] The token type to retrieve
128: * @param pVal [out] A token list of matching tokens
129: */
130: public ETList<ISyntaxToken> getTokensByKind(int wantedKind) {
131: ETList<ISyntaxToken> retVal = new ETArrayList<ISyntaxToken>();
132:
133: int count = m_Tokens.size();
134:
135: // Go through all of the syntax tokens and check if the token is the one
136: // that we want. If it is add it to the return list.
137: for (int i = 0; i < count; i++) {
138: ISyntaxToken pToken = (ISyntaxToken) m_Tokens.get(i);
139: int pKind = pToken.getKind();
140: if (pKind == wantedKind) {
141: retVal.add(pToken);
142: }
143: }
144:
145: return retVal;
146: }
147:
148: /**
149: * Retrieves all the syntax tokens that have the specified token type and category.
150: *
151: * @param wantedKind [in] The token type to retrieve
152: * @param wantedCategory [in] The token category to retrieve
153: * @param pVal [out] A token list of matching tokens
154: */
155: public ETList<ISyntaxToken> getTokensByCategory(int wantedKind,
156: String wantedCategory) {
157: ETList<ISyntaxToken> retVal = new ETArrayList<ISyntaxToken>();
158:
159: // I could just call GetTokensByKind then filter the returned collection based on the category.
160: // However, I would then be checking two list. So, to optimize I will only check the
161: // list once and check both the kind and the category as I go.
162: int count = m_Tokens.size();
163:
164: // Go through all of the syntax tokens and check if the token is the one
165: // that we want. If it is add it to the return list.
166: for (int i = 0; i < count; i++) {
167: ISyntaxToken pToken = (ISyntaxToken) m_Tokens.get(i);
168: int pKind = pToken.getKind();
169: if (pKind == wantedKind) {
170: // Only retrieve the category if we have a node that matches the desired kind.
171: // This is again another optimization. Only retrieve what we need to determine
172: // if the node is a desired node.
173: String pCategory = pToken.getCategory();
174: if (pCategory.equals(wantedCategory)) {
175: retVal.add(pToken);
176: }
177: }
178: }
179:
180: return retVal;
181: }
182:
183: }
|