KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > importer > rose > builder > UnitTreeBuilder


1 /**
2  * <copyright>
3  *
4  * Copyright (c) 2002-2004 IBM Corporation and others.
5  * All rights reserved. This program and the accompanying materials
6  * are made available under the terms of the Eclipse Public License v1.0
7  * which accompanies this distribution, and is available at
8  * http://www.eclipse.org/legal/epl-v10.html
9  *
10  * Contributors:
11  * IBM - Initial API and implementation
12  *
13  * </copyright>
14  *
15  * $Id: UnitTreeBuilder.java,v 1.2 2005/06/08 06:20:36 nickb Exp $
16  */

17 package org.eclipse.emf.importer.rose.builder;
18
19 import java.util.List JavaDoc;
20
21 import org.eclipse.emf.importer.rose.parser.RoseNode;
22 import org.eclipse.emf.importer.rose.parser.Util;
23
24
25 /**
26  * Traverses the Rose files the first time to build unit tree and class information.
27  *
28  * Unit tree contains tree UnitTreeNodes.
29  * Each tree node has name, quid and cat file name
30  *
31  * Table information is a map from String quid to TableObjects.
32  * Each table info has name, quid, xml file, uuid, model id and it parent model id.
33  */

34 public class UnitTreeBuilder
35 {
36   protected UnitTreeNode topNode;
37   protected RoseUtil roseUtil;
38
39   public UnitTreeBuilder(RoseUtil roseUtil)
40   {
41     this.roseUtil = roseUtil;
42   }
43
44   public void traverse(String JavaDoc qualifier, RoseNode tree, UnitTreeNode unitNode) throws Exception JavaDoc
45   {
46     topNode = unitNode;
47     List JavaDoc nodes = tree.getNodes();
48     for (int i = 0; i < nodes.size(); i++)
49     {
50       RoseNode node = (RoseNode)nodes.get(i);
51       if (node.getRoseNodeType() == RoseNode.OBJECT)
52       {
53         traverseObject(qualifier, node, unitNode);
54       }
55       else if (node.getRoseNodeType() == RoseNode.LIST)
56       {
57         traverseList(qualifier, node, unitNode);
58       }
59     }
60   }
61
62   private void traverseObject(String JavaDoc qualifier, RoseNode tree, UnitTreeNode unitNode) throws Exception JavaDoc
63   {
64     String JavaDoc objKey = tree.getKey();
65     String JavaDoc objType = Util.getType(tree.getValue());
66     String JavaDoc objName = Util.getName(tree.getValue());
67
68     if (objKey.equals(RoseStrings.ROOT_CATEGORY) && objType.equals(RoseStrings.CLASS_CATEGORY))
69     {
70       // For the model information.
71
//
72
String JavaDoc quid = unitNode.getQUID();
73       if (quid.equals(""))
74       {
75         quid = tree.getRoseId();
76         if (quid != null)
77         {
78           quid = quid.substring(1, quid.length() - 1);
79         }
80         else
81         {
82           quid = "";
83         }
84       }
85       if (!quid.equals(""))
86       {
87         unitNode.setQUID(quid);
88       }
89       unitNode.setRoseNode(tree);
90       traverse(qualifier, tree, unitNode);
91     }
92     else if (objKey.equals("") && objType.equals(RoseStrings.CLASS_CATEGORY))
93     {
94       // This is package,
95
// so check the package information if this is in cat file.
96
// If it is, then load the .cat file into the tree thereby traversing it;
97
// otherwise, just traverse the tree.
98
//
99
RoseNode loadingNode = tree.findNodeWithKey(RoseStrings.IS_LOADED);
100
101       String JavaDoc quid = tree.getRoseId();
102       if (quid != null)
103       {
104         quid = quid.substring(1, quid.length() - 1);
105       }
106       if (loadingNode != null)
107       {
108         // The package is in a .cat file.
109
//
110
RoseNode fileNameNode = tree.findNodeWithKey(RoseStrings.FILE_NAME);
111         String JavaDoc fileNameNodeValue = fileNameNode.getValue();
112         String JavaDoc fileName = roseUtil.resolveFileName(fileNameNodeValue);
113         UnitTreeNode unitTreeNode = new UnitTreeNode(objName, quid, fileName);
114         unitNode.addNode(unitTreeNode);
115         roseUtil.createRoseUnitTreeAndTable(fileNameNodeValue, unitTreeNode);
116       }
117       else
118       {
119         // The package not in a .cat file.
120
//
121
String JavaDoc qualifiedName = objName;
122         if (qualifier != null)
123         {
124           qualifiedName = qualifier + "." + objName;
125         }
126         TableObject obj = new TableObject(qualifiedName, quid, topNode);
127         roseUtil.quidTable.put(quid, obj);
128         if (qualifier != null)
129         {
130           roseUtil.nameTable.put(qualifiedName, obj);
131         }
132         roseUtil.nameTable.put(objName, obj);
133         unitNode.setRoseNode(tree);
134         traverse(qualifiedName, tree, unitNode);
135       }
136     }
137     else if (objType.equals(RoseStrings.CLASS))
138     {
139       String JavaDoc quid = tree.getRoseId();
140       if (quid != null)
141       {
142         quid = quid.substring(1, quid.length() - 1);
143       }
144       String JavaDoc qualifiedName = objName;
145       if (qualifier != null)
146       {
147         qualifiedName = qualifier + "." + objName;
148       }
149       TableObject obj = new TableObject(qualifiedName, quid, topNode);
150       roseUtil.quidTable.put(quid, obj);
151       roseUtil.nameTable.put(objName, obj);
152       if (qualifiedName != null)
153       {
154         roseUtil.nameTable.put(qualifiedName, obj);
155       }
156       traverse(qualifiedName, tree, unitNode);
157     }
158     else if (objType.equals(RoseStrings.OPERATION) || objType.equals(RoseStrings.CLASSATTRIBUTE)
159       || objType.equals(RoseStrings.INHERITANCE_RELATIONSHIP) || objType.equals(RoseStrings.ASSOCIATION)
160       || objType.equals(RoseStrings.ROLE) || objType.equals(RoseStrings.VISIBILITY_RELATIONSHIP)
161       || objType.equals(RoseStrings.USES_RELATIONSHIP))
162     {
163       traverse(qualifier, tree, unitNode);
164     }
165   }
166
167   private void traverseList(String JavaDoc qualifier, RoseNode tree, UnitTreeNode unitNode) throws Exception JavaDoc
168   {
169     traverse(qualifier, tree, unitNode);
170   }
171 }
172
Popular Tags