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: /*
043: * File : DependencyStateHandler.java
044: * Created on : Dec 10, 2003
045: * Author : Aztec
046: */
047: package org.netbeans.modules.uml.core.reverseengineering.parsingfacilities.translation.statehandlers;
048:
049: import org.dom4j.Node;
050:
051: import org.netbeans.modules.uml.core.reverseengineering.parsingfacilities.IUMLParserEventDispatcher;
052: import org.netbeans.modules.uml.core.reverseengineering.parsingfacilities.Identifier;
053: import org.netbeans.modules.uml.core.reverseengineering.reframework.DependencyEvent;
054: import org.netbeans.modules.uml.core.reverseengineering.reframework.IDependencyEvent;
055: import org.netbeans.modules.uml.core.reverseengineering.reframework.parsingframework.ITokenDescriptor;
056:
057: /**
058: * @author Aztec
059: */
060: public class DependencyStateHandler extends TopLevelStateHandler {
061: Identifier m_DependencyName = new Identifier();
062: Identifier m_FullName = new Identifier();
063: boolean m_IsClassDependency = true;
064: int m_NestedLevel;
065:
066: public DependencyStateHandler(String language) {
067: super (language);
068: m_IsClassDependency = true;
069: m_NestedLevel = 0;
070: }
071:
072: /**
073: * Create a new state handler to be added to the state mechanism. If the
074: * state is not a state that is being processed then a new state handler is
075: * not created.
076: *
077: * @param stateName [in] The state name.
078: * @param language [in] The langauge being processed.
079: *
080: * @return The handler for the state.
081: */
082: public StateHandler createSubStateHandler(String stateName,
083: String val) {
084: StateHandler retVal = null;
085:
086: if ("Identifier".equals(stateName)) {
087: m_NestedLevel++;
088: retVal = this ;
089: }
090: return retVal;
091: }
092:
093: /**
094: * Initializes the state handler. The jump action XMI node is
095: * initializes.
096: */
097: public void initialize() {
098: createTopLevelNode("UML:Dependency");
099: }
100:
101: /* (non-Javadoc)
102: * @see org.netbeans.modules.uml.core.reverseengineering.parsingfacilities.translation.statehandlers.IDependencyStateHandler#isClassDependency()
103: */
104: public boolean isClassDependency() {
105: return m_IsClassDependency;
106: }
107:
108: /**
109: * Process a new token. The tokens that are processed are in the
110: * context of an object creation.
111: *
112: * @param pToken [in] The token to be processed.
113: */
114: public void processToken(ITokenDescriptor pToken, String language) {
115: if (pToken == null)
116: return;
117:
118: String tokenType = pToken.getType();
119:
120: if ("Keyword".equals(tokenType)) {
121: handleStartPosition(pToken);
122: handleKeyword(pToken);
123: handleFilename(pToken);
124: } else if (("Identifier".equals(tokenType))
125: || ("Scope Operator".equals(tokenType))) {
126: // The FullName will also contain the OnDemand Operator
127: // while the Dependency name will only contain the
128: // name of the dependent package or class.
129: m_DependencyName.addToken(pToken);
130: m_FullName.addToken(pToken);
131: } else if ("OnDemand Operator".equals(tokenType)) {
132: setIsClassDependency(false);
133: m_FullName.addToken(pToken);
134: } else if ("Statement Terminator".equals(tokenType)) {
135: handleTerminator(pToken);
136: handleEndPostion(pToken);
137: }
138: }
139:
140: /* (non-Javadoc)
141: * @see org.netbeans.modules.uml.core.reverseengineering.parsingfacilities.translation.statehandlers.IDependencyStateHandler#setIsClassDependency(boolean)
142: */
143: public void setIsClassDependency(boolean newVal) {
144: m_IsClassDependency = newVal;
145: }
146:
147: /**
148: * Notification that the a state has completed.
149: *
150: * @param stateName [in] The name of the state.
151: */
152: public void stateComplete(String stateName) {
153: if (m_NestedLevel == 0) {
154: IDependencyEvent pEvent = buildDependencyEvent();
155: IUMLParserEventDispatcher pDispatcher = getEventDispatcher();
156:
157: if (pDispatcher != null) {
158: pDispatcher.fireDependencyFound("", pEvent, null);
159: }
160: } else {
161: m_NestedLevel--;
162:
163: // Now if I am not still in a nested state I must move back to the
164: // general state.
165: if (m_NestedLevel == 0) {
166: updateName();
167: }
168: }
169: }
170:
171: protected IDependencyEvent buildDependencyEvent() {
172: String value = isClassDependency() ? "true" : "false";
173: createTokenDescriptor("Class Dependency", -1, -1, -1, value, 0);
174:
175: IDependencyEvent pEvent = new DependencyEvent();
176: if (pEvent != null) {
177: Node pNode = getDOMNode();
178:
179: if (pNode != null) {
180: pEvent.setEventData(pNode);
181:
182: }
183: }
184:
185: return pEvent;
186: }
187:
188: /**
189: * Add the dependency name information to the XMI structure.
190: *
191: * @param pToken [in] The token that specifies the class name.
192: */
193: protected void updateName() {
194: String supplierName = m_DependencyName.getIdentifierAsUML();
195: setNodeAttribute("name", supplierName);
196:
197: String value = m_FullName.getIdentifierAsSource();
198: createTokenDescriptor("Name", m_FullName.getStartLine(),
199: m_FullName.getStartColumn(), m_FullName
200: .getStartPosition(), value, m_FullName
201: .getLength());
202:
203: setNodeAttribute("supplier", supplierName);
204: }
205: }
|