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 : MethodLoopStateHandler.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.Node;
050:
051: import org.netbeans.modules.uml.core.reverseengineering.parsingfacilities.IOpParserOptions;
052: import org.netbeans.modules.uml.core.reverseengineering.parsingfacilities.IUMLParserEventDispatcher;
053: import org.netbeans.modules.uml.core.reverseengineering.parsingfacilities.translation.expression.Expression;
054: import org.netbeans.modules.uml.core.reverseengineering.reframework.IInitializeEvent;
055: import org.netbeans.modules.uml.core.reverseengineering.reframework.IPostProcessingEvent;
056: import org.netbeans.modules.uml.core.reverseengineering.reframework.IRELoop;
057: import org.netbeans.modules.uml.core.reverseengineering.reframework.ITestEvent;
058: import org.netbeans.modules.uml.core.reverseengineering.reframework.InitializeEvent;
059: import org.netbeans.modules.uml.core.reverseengineering.reframework.PostProcessingEvent;
060: import org.netbeans.modules.uml.core.reverseengineering.reframework.RELoop;
061: import org.netbeans.modules.uml.core.reverseengineering.reframework.TestEvent;
062: import org.netbeans.modules.uml.core.reverseengineering.reframework.parsingframework.ITokenDescriptor;
063: import org.netbeans.modules.uml.core.support.umlsupport.XMLManip;
064:
065: /**
066: * @author Aztec
067: */
068: public class MethodLoopStateHandler extends MethodCompositeStateHandler {
069: private boolean m_IsInTestCondition;
070: private boolean m_IsInInitialize;
071: private boolean m_IsInPostProcessing;
072: private boolean m_IsInBody;
073:
074: private Node m_TestNode;
075: private Node m_InitializeNode;
076: private Node m_PostProcessingNode;
077: private Node m_BodyNode;
078:
079: private ITokenDescriptor m_pKeyword;
080: private ITokenDescriptor m_pConditionalSeparator;
081: private ITokenDescriptor m_pPostSeparator;
082:
083: private String m_StringRepresentation = "";
084:
085: private Expression m_BodyExpression;
086:
087: public MethodLoopStateHandler(String language) {
088: super (language);
089: }
090:
091: public StateHandler createSubStateHandler(String stateName,
092: String language) {
093: MethodDetailStateHandler retVal = null;
094:
095: if ("Test Condition".equals(stateName)) {
096: m_IsInTestCondition = true;
097: m_IsInBody = false;
098: m_IsInPostProcessing = false;
099: m_IsInInitialize = false;
100:
101: beginTestCondition();
102: retVal = this ;
103: } else if ("Loop Initializer".equals(stateName)) {
104: m_IsInTestCondition = false;
105: m_IsInBody = false;
106: m_IsInPostProcessing = false;
107: m_IsInInitialize = true;
108:
109: beginInitialize();
110: retVal = this ;
111: } else if ("Loop PostProcess".equals(stateName)) {
112: m_IsInTestCondition = false;
113: m_IsInBody = false;
114: m_IsInPostProcessing = true;
115: m_IsInInitialize = false;
116:
117: beginPostProcessing();
118: retVal = this ;
119: } else if ("Body".equals(stateName)) {
120: m_IsInTestCondition = false;
121: m_IsInPostProcessing = false;
122: m_IsInInitialize = false;
123: m_IsInBody = true;
124:
125: beginBody();
126: retVal = this ;
127: } else if (m_IsInTestCondition) {
128: addTestConditionState(stateName, language);
129: //m_TestConditionExpression.AddState(stateName, language);
130: retVal = this ;
131: } else if (m_IsInInitialize) {
132: //Kris - issue 78409 - the pOptions was a dead call. The addInitializerState
133: //seemed to correctly add the new StateHandler to the stack, but then
134: //this (MehodLoopStateHandler) was returned. So added the call to
135: //retrieveStatementHandler to retrieve the handler created to handle the
136: //variable definition. Also, of course, removed the retVal=this line.
137: IOpParserOptions pOptions = getOpParserOptions();
138:
139: addInitializerState(stateName, language);
140: retVal = StatementFactory.retrieveStatementHandler(
141: stateName, language, pOptions, getSymbolTable());
142:
143: } else if (m_IsInPostProcessing) {
144: //m_PostProcessingExpression.AddState(stateName, language);
145: addPostProcessingState(stateName, language);
146: retVal = this ;
147: } else if (m_IsInBody) {
148: IOpParserOptions pOptions = getOpParserOptions();
149:
150: retVal = StatementFactory.retrieveStatementHandler(
151: stateName, language, pOptions, getSymbolTable());
152: initializeHandler(retVal, m_BodyNode);
153: }
154: return retVal;
155: }
156:
157: public void initialize() {
158: Node pLoopNode = createNode("UML:LoopAction");
159: setDOMNode(pLoopNode);
160:
161: IUMLParserEventDispatcher pDispatcher = getEventDispatcher();
162:
163: if (pDispatcher != null) {
164: pDispatcher.fireBeginLoop(null);
165: }
166: beginScope();
167: }
168:
169: public void processToken(ITokenDescriptor pToken, String language) {
170: if (pToken == null)
171: return;
172:
173: String type = pToken.getType();
174:
175: String value = pToken.getValue();
176:
177: if ("Keyword".equals(type)) {
178: m_pKeyword = pToken;
179:
180: // I do not care about the HRESULT.
181: handleKeyword(pToken);
182: } else if ("Conditional Separator".equals(type)) {
183: m_pConditionalSeparator = pToken;
184: } else if ("PostProcessor Separator".equals(type)) {
185: m_pPostSeparator = pToken;
186: } else if (m_IsInTestCondition) {
187: //m_TestConditionExpression.AddToken(pToken, language);
188: addTestConditionToken(pToken, language);
189: } else if (m_IsInInitialize) {
190: //m_InitializerExpression.AddToken(pToken, language);
191: addInitializerToken(pToken, language);
192: } else if (m_IsInPostProcessing) {
193: //m_PostProcessingExpression.AddToken(pToken, language);
194: addPostProcessingToken(pToken, language);
195: } else if (m_IsInBody) {
196: if (m_BodyExpression != null)
197: m_BodyExpression.addToken(pToken, language);
198: }
199: }
200:
201: public void stateComplete(String stateName) {
202: if (m_IsInTestCondition) {
203: //m_TestConditionExpression.EndState(stateName);
204: endTestConditionState(stateName);
205: } else if (m_IsInInitialize) {
206: //m_InitializerExpression.EndState(stateName);
207: endInitializerState(stateName);
208: } else if (m_IsInPostProcessing) {
209: //m_PostProcessingExpression.EndState(stateName);
210: endPostProcessingState(stateName);
211: }
212:
213: if ("Test Condition".equals(stateName)) {
214: endTestCondition();
215: } else if ("Loop Initializer".equals(stateName)) {
216: endInitialize();
217: } else if ("Loop PostProcess".equals(stateName)) {
218: endPostProcessing();
219: } else if ("Body".equals(stateName)) {
220: endBody();
221: } else if ("Loop".equals(stateName)) {
222: endCondtional();
223: }
224: }
225:
226: protected void beginTestCondition() {
227: IUMLParserEventDispatcher pDispatcher = getEventDispatcher();
228:
229: if (pDispatcher != null) {
230: pDispatcher.fireBeginTest(null);
231: }
232: m_TestNode = createNode("UML:LoopAction.test");
233: }
234:
235: protected void beginInitialize() {
236: IUMLParserEventDispatcher pDispatcher = getEventDispatcher();
237:
238: if (pDispatcher != null) {
239: pDispatcher.fireBeginInitialize(null);
240: }
241:
242: m_InitializeNode = createNode("UML:LoopAction.initialize");
243: }
244:
245: protected void beginPostProcessing() {
246: IUMLParserEventDispatcher pDispatcher = getEventDispatcher();
247:
248: if (pDispatcher != null) {
249: pDispatcher.fireBeginPostProcessing(null);
250: }
251:
252: m_PostProcessingNode = createNode("UML:LoopAction.postprocess");
253:
254: }
255:
256: protected void beginBody() {
257: m_BodyNode = createNode("UML:LoopAction.body");
258: }
259:
260: protected void endTestCondition() {
261: String data = writeTestXMI(m_TestNode);
262:
263: // Fire the end initialization event events to all listeners.
264: IUMLParserEventDispatcher dispatcher = getEventDispatcher();
265:
266: if (dispatcher != null) {
267: sendTestEvents();
268: ITestEvent event = new TestEvent();
269:
270: // _VH(pEvent->put_StringRepresentation(dataBSTR) );
271: Node node = getDOMNode();
272:
273: if (node != null) {
274: event.setEventData(node);
275: dispatcher.fireEndTest(event, null);
276: }
277: }
278:
279: if (m_pConditionalSeparator != null) {
280: String value = m_pConditionalSeparator.getValue();
281:
282: // TODO: In the future we will refactor the state handlers
283: // to be dynamically loaded. At that time I need to
284: // make a VB and Java MethodLoopStateHandler to correctly
285: // format the string. For now I will go by the length
286: // of the seperator. Java and C++ seperator will be ';'
287: // so its length will be 1, while seperator for VB will be
288: // "to".
289: if (value.length() > 1) {
290: m_StringRepresentation += " ";
291: }
292: m_StringRepresentation += value;
293: m_StringRepresentation += " ";
294: }
295:
296: m_StringRepresentation += data;
297: }
298:
299: protected void endInitialize() {
300: String data = writeInitializeXMI(m_InitializeNode);
301:
302: // Fire the end initialization event events to all listeners.
303: IUMLParserEventDispatcher pDispatcher = getEventDispatcher();
304:
305: if (pDispatcher != null) {
306: sendInitializationEvents();
307: IInitializeEvent pEvent = new InitializeEvent();
308:
309: if (pEvent != null) {
310: pEvent.setStringRepresentation(data);
311: pDispatcher.fireEndInitialize(pEvent, null);
312: }
313: }
314:
315: m_StringRepresentation += data;
316:
317: }
318:
319: protected void endPostProcessing() {
320: String data = writeInitializeXMI(m_PostProcessingNode);
321:
322: // Fire the end initialization event events to all listeners.
323: IUMLParserEventDispatcher pDispatcher = getEventDispatcher();
324:
325: if (pDispatcher != null) {
326: sendPostProcessingEvents();
327: IPostProcessingEvent pEvent = new PostProcessingEvent();
328:
329: if (pEvent != null) {
330: pEvent.setStringRepresentation(data);
331: pDispatcher.fireEndPostProcessing(pEvent, null);
332: }
333: }
334:
335: if (m_pPostSeparator != null) {
336: String value = m_pPostSeparator.getValue();
337:
338: // TODO: In the future we will refactor the state handlers
339: // to be dynamically loaded. At that time I need to
340: // make a VB and Java MethodLoopStateHandler to correctly
341: // format the string. For now I will go by the length
342: // of the seperator. Java and C++ seperator will be ';'
343: // so its length will be 1, while seperator for VB will be
344: // "step".
345: if (value != null && value.length() > 1) {
346: m_StringRepresentation += " ";
347: }
348:
349: m_StringRepresentation += value;
350: m_StringRepresentation += " ";
351: }
352: m_StringRepresentation += data;
353: }
354:
355: protected void endBody() {
356: // No valid implementation in the C++ code base.
357: }
358:
359: protected void endCondtional() {
360: IUMLParserEventDispatcher pDispatcher = getEventDispatcher();
361: endScope();
362:
363: IRELoop pEvent = new RELoop();
364:
365: if (pEvent != null) {
366: Node pNode = getDOMNode();
367:
368: if (pNode != null) {
369: XMLManip.setAttributeValue(pNode, "representation",
370: m_StringRepresentation);
371: pEvent.setEventData(pNode);
372: if (pDispatcher != null)
373: pDispatcher.fireEndLoop(pEvent, null);
374: }
375: }
376: }
377:
378: }
|