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 : GeneralizationStateHandler.java
044: * Created on : Dec 11, 2003
045: * Author : Aztec
046: */
047: package org.netbeans.modules.uml.core.reverseengineering.parsingfacilities.translation.statehandlers;
048:
049: import org.dom4j.Element;
050: import org.dom4j.Node;
051:
052: import org.netbeans.modules.uml.core.reverseengineering.parsingfacilities.IUMLParserEventDispatcher;
053: import org.netbeans.modules.uml.core.reverseengineering.parsingfacilities.Identifier;
054: import org.netbeans.modules.uml.core.reverseengineering.reframework.IREGeneralization;
055: import org.netbeans.modules.uml.core.reverseengineering.reframework.REGeneralization;
056: import org.netbeans.modules.uml.core.reverseengineering.reframework.parsingframework.ITokenDescriptor;
057: import org.netbeans.modules.uml.core.support.umlsupport.XMLManip;
058:
059: /**
060: * @author Aztec
061: */
062: public class GeneralizationStateHandler extends StateHandler {
063: private Identifier m_SuperClass = new Identifier();
064: private int m_NestedLevel = 0;
065: private long m_EndPosition = -1;
066: private long m_EndLine = -1;
067: private long m_EndColumn = -1;
068: private Node m_pClassNode;
069:
070: public StateHandler createSubStateHandler(String stateName,
071: String language) {
072: StateHandler retVal = null;
073:
074: if ("Identifier".equals(stateName)) {
075: // Check if we found a new identifier. If the nesting level is greater
076: // than zero we are current processing an identifier.
077: if (m_NestedLevel <= 0) {
078: m_SuperClass.clear();
079: }
080:
081: m_NestedLevel++;
082:
083: retVal = this ;
084: } else if ("Template Instantiation".equals(stateName)) {
085: retVal = new TemplateInstantiationStateHandler();
086:
087: Node pNode = getDOMNode();
088:
089: if (pNode != null) {
090: retVal.setDOMNode(pNode);
091: }
092: }
093: return retVal;
094: }
095:
096: public void initialize() {
097: m_pClassNode = getDOMNode();
098:
099: // The generalization token descriptor is really a
100: // sub node of the UML:Class XML node. Therefore, I
101: // will create the TGeneralization under the passed
102: // in node. The TGeneralization node will then be
103: // passed to StateHandler to use in the helper methods.
104: Node pDescriptors = ensureElementExists("TokenDescriptors",
105: "TokenDescriptors");
106:
107: if (pDescriptors != null) {
108: Node pRelationship = ensureElementExists(pDescriptors,
109: getRelationshipGroupTagName(),
110: getRelationshipGroupTagName());
111:
112: if (pRelationship != null) {
113: setDOMNode(pRelationship);
114: }
115: }
116: }
117:
118: public void processToken(ITokenDescriptor pToken, String language) {
119: if (pToken == null)
120: return;
121:
122: String tokenType = pToken.getType();
123:
124: if ("Keyword".equals(tokenType)) {
125: handleRelationshipKeyword(pToken);
126: } else if (("Identifier".equals(tokenType))
127: || ("Scope Operator".equals(tokenType))) {
128: m_SuperClass.addToken(pToken);
129: }
130: }
131:
132: public void stateComplete(String stateName) {
133: if (m_NestedLevel > 0) {
134: m_NestedLevel--;
135:
136: // Now if I am not still in a nested state I must move back to the
137: // general state.
138: if (m_NestedLevel == 0) {
139: createRelationship(getRelationshipTagName(),
140: m_SuperClass);
141: }
142: } else {
143: updateEndPosition();
144: }
145: }
146:
147: public String getRelationshipTagName() {
148: return "SuperClass";
149: }
150:
151: public String getRelationshipGroupTagName() {
152: return "TGeneralization";
153: }
154:
155: public Identifier getIdentifier() {
156: return m_SuperClass;
157: }
158:
159: protected void handleRelationshipKeyword(ITokenDescriptor pToken) {
160: if (pToken == null)
161: return;
162:
163: long line = pToken.getLine();
164: long column = pToken.getColumn();
165: long position = pToken.getPosition();
166: long length = pToken.getLength();
167: String value = pToken.getValue();
168:
169: createDescriptor("Keyword", line, column, position, value,
170: length);
171: }
172:
173: protected void createRelationship(String tagName,
174: Identifier generalization) {
175: Node pRelationshipNode = getDOMNode();
176: if (pRelationshipNode != null) {
177: Node pGeneralization = XMLManip.createElement(
178: (Element) pRelationshipNode, tagName);
179:
180: if (pGeneralization != null) {
181: Element element = (pGeneralization instanceof Element) ? (Element) pGeneralization
182: : null;
183: if (element != null) {
184: m_EndLine = generalization.getStartLine();
185: m_EndColumn = generalization.getStartColumn();
186: long pos = generalization.getStartPosition();
187: long length = generalization.getLength();
188: String value = generalization.getIdentifierAsUML();
189:
190: XMLManip.setAttributeValue(element, "line", Long
191: .toString(m_EndLine));
192: XMLManip.setAttributeValue(element, "column", Long
193: .toString(m_EndColumn));
194: XMLManip.setAttributeValue(element, "position",
195: Long.toString(pos));
196: XMLManip.setAttributeValue(element, "value", value);
197: XMLManip.setAttributeValue(element, "length", Long
198: .toString(length));
199:
200: m_EndPosition = generalization.getEndPosition();
201: }
202: }
203: }
204: sendAtomicEvent(pRelationshipNode);
205: }
206:
207: protected void updateEndPosition() {
208: if (m_pClassNode != null && m_EndLine >= 0 && m_EndColumn >= 0
209: && m_EndPosition >= 0) {
210: Node pEndPos = XMLManip
211: .selectSingleNode(m_pClassNode,
212: "TokenDescriptors/TDescriptor[@type=\"ClassHeadEndPosition\"]");
213:
214: if (pEndPos != null) {
215: Element pElement = (pEndPos instanceof Element) ? (Element) pEndPos
216: : null;
217: if (pElement != null) {
218: XMLManip.setAttributeValue(pElement, "line", Long
219: .toString(m_EndLine));
220: XMLManip.setAttributeValue(pElement, "column", Long
221: .toString(m_EndColumn));
222: XMLManip.setAttributeValue(pElement, "position",
223: Long.toString(m_EndPosition));
224: }
225: } else {
226: createTokenDescriptor(m_pClassNode,
227: "ClassHeadEndPosition", m_EndLine, m_EndColumn,
228: m_EndPosition, "", 0);
229: }
230: }
231: }
232:
233: protected void sendAtomicEvent(Node pRelationshipNode) {
234: if (pRelationshipNode == null)
235: return;
236:
237: IREGeneralization pEvent = new REGeneralization();
238:
239: if (pEvent != null) {
240: pEvent.setDOMNode(pRelationshipNode);
241:
242: IUMLParserEventDispatcher pDispatcher = getEventDispatcher();
243:
244: if (pDispatcher != null) {
245: pDispatcher.fireGeneralizationFound(pEvent, null);
246: }
247: }
248: }
249: }
|