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;
043:
044: import org.dom4j.Document;
045: import org.dom4j.Element;
046: import org.dom4j.Node;
047:
048: import org.netbeans.modules.uml.core.reverseengineering.reframework.parsingframework.ITokenDescriptor;
049: import org.netbeans.modules.uml.core.support.umlsupport.XMLManip;
050: import org.netbeans.modules.uml.core.support.umlutils.ETList;
051:
052: /**
053: */
054: public class REClass extends REClassElement implements IREClass {
055: /**
056: * Retrieves the name of the package that contains the class. If the
057: * class does not belong to a package the name will be an empty string.
058: * @param pVal [out] The package that owns the class.
059: */
060: public String getPackage() {
061: String pack = null;
062: ITokenDescriptor desc = getTokenDescriptor("Package");
063: if (desc != null)
064: pack = desc.getValue();
065: // Get then out class name as well (if there is an outer
066: // class). The package name is
067: // <Package Structure>::<Outer Class Strucuter>
068: Node node = getEventData();
069: Node parent = node != null ? node.getParent() : null;
070: if (parent != null) {
071: // When the parent is a package we do not want to continue to search for
072: // the name. The package token descriptor has all of the information
073: // that we need.
074: if (!isPackageNode(parent)) {
075: String packageStr = getPackageName(parent);
076: if ((packageStr != null) && (packageStr.length() > 0)) {
077: if ((pack != null) && (pack.length() > 0)) {
078: pack = pack + "::" + packageStr;
079: } else {
080: pack = packageStr;
081: }
082: }
083: }
084: }
085: return pack;
086: }
087:
088: /**
089: * Retrieves the name of a class' package by traversing the parent nodes.
090: * If the parent node has a <em>name</em> attribute, the name will be
091: * appended to the package name.
092: * @param pNode [in] The start node used to find the package name.
093: * @param name [out] The name of the package.
094: */
095: private String getPackageName(Node node) {
096: String name = "";
097: if (node != null && !(node instanceof Document)) {
098: Node parent = node.getParent();
099: if (parent != null)
100: name = getPackageName(parent);
101:
102: String type = ((Element) node).getQualifiedName();
103: if ("UML:Class".equals(type)) {
104: String nameS = XMLManip.getAttributeValue(node, "name");
105: name = name.length() > 0 ? (name + "::" + nameS)
106: : nameS;
107: }
108: }
109: return name;
110: }
111:
112: /**
113: * @param parent
114: * @return
115: */
116: private boolean isPackageNode(Node parent) {
117: if (parent != null) {
118: String nodename = ((Element) parent).getQualifiedName();
119: if ("UML:Package".equals(nodename))
120: return true;
121: else if ("UML:Element.ownedElement".equals(nodename))
122: return isPackageNode(parent.getParent());
123: }
124: return false;
125: }
126:
127: /**
128: * Specifies whether the element may not have a direct instance. True
129: * indicates that an instance of the element must be an instance of a
130: * child of the element. False indicates that there may an instance of
131: * the element that is not an instance of a child.
132: * @param pVal [out] true if abstract, false otherwise.
133: */
134: public boolean getIsAbstract() {
135: return XMLManip.getAttributeBooleanValue(getEventData(),
136: "isAbstract");
137: }
138:
139: /* (non-Javadoc)
140: * @see org.netbeans.modules.uml.core.reverseengineering.reframework.IREClass#getIsLeaf()
141: */
142: public boolean getIsLeaf() {
143: return XMLManip.getAttributeBooleanValue(getEventData(),
144: "isLeaf");
145: }
146:
147: /* (non-Javadoc)
148: * @see org.netbeans.modules.uml.core.reverseengineering.reframework.IREClass#getOperations()
149: */
150: public ETList<IREOperation> getOperations() {
151: REXMLCollection<IREOperation> coll = new REXMLCollection<IREOperation>(
152: REOperation.class,
153: "UML:Element.ownedElement/UML:Operation");
154: try {
155: coll.setDOMNode(getEventData());
156: } catch (Exception e) {
157: e.printStackTrace();
158: }
159: return coll;
160: }
161:
162: /* (non-Javadoc)
163: * @see org.netbeans.modules.uml.core.reverseengineering.reframework.IREClass#getAttributes()
164: */
165: public ETList<IREAttribute> getAttributes() {
166: REXMLCollection<IREAttribute> coll = new REXMLCollection<IREAttribute>(
167: REAttribute.class,
168: "UML:Element.ownedElement/UML:Attribute");
169: try {
170: coll.setDOMNode(getEventData());
171: } catch (Exception e) {
172: e.printStackTrace();
173: }
174: return coll;
175: }
176:
177: /**
178: * Retrieves the collection of super classes for the class..
179: * @param pVal [out] The class's super classes.
180: */
181: public IREGeneralization getGeneralizations() {
182: IREGeneralization gen = new REGeneralization();
183: gen.setDOMNode(getGeneralization(getEventData()));
184: return gen;
185: }
186:
187: /**
188: * Retrieves the token descriptors for the parser event. The token descriptors will
189: * be contained by the specified node.
190: * @param node The DOM node to search.
191: * @param pVal The token descriptors for the parser data.
192: * @return S_OK if successful.
193: */
194: private Node getGeneralization(Node node) {
195: Node s = XMLManip.selectSingleNode(node,
196: "TokenDescriptors/TGeneralization");
197: if (s == null) {
198: // The library file is screwed up. So until we formalize the library structure
199: // the library file will remain screwed up.
200: s = XMLManip.selectSingleNode(node, "TGeneralization");
201: }
202: return s;
203: }
204:
205: /**
206: * Retrieves the token descriptors for the parser event. The token descriptors will
207: * be contained by the specified node.
208: * @param node The DOM node to search.
209: * @param pVal The token descriptors for the parser data.
210: * @return S_OK if successful.
211: */
212: private Node getRealization(Node node) {
213: Node s = XMLManip.selectSingleNode(node,
214: "TokenDescriptors/TRealization");
215: if (s == null) {
216: // The library file is screwed up. So until we formalize the library structure
217: // the library file will remain screwed up.
218: s = XMLManip.selectSingleNode(node, "TRealization");
219: }
220: return s;
221: }
222:
223: /**
224: * Retrieves the collection of implemented interfaces.
225: * @param pVal [out] The interfaces that are implemented by he class.
226: */
227: public IRERealization getRealizations() {
228: IRERealization real = new RERealization();
229: real.setDOMNode(getRealization(getEventData()));
230: return real;
231: }
232:
233: /**
234: * Retrieves all inner classes and interfaces.
235: * @param pVal [out] A collection of inner classes.
236: */
237: public ETList<IREClass> getAllInnerClasses() {
238: REClasses c = new REClasses();
239: try {
240: c.setDOMNode(getEventData());
241: } catch (Exception e) {
242: e.printStackTrace();
243: }
244: return c;
245: }
246:
247: /* (non-Javadoc)
248: * @see org.netbeans.modules.uml.core.reverseengineering.reframework.IREClass#getIsInterface()
249: */
250: public boolean getIsInterface() {
251: return "UML:Interface".equals(getEventData().getName());
252: }
253: }
|