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 java.io.BufferedReader;
045: import java.io.FileNotFoundException;
046: import java.io.FileReader;
047: import java.io.IOException;
048: import java.util.Enumeration;
049: import java.util.HashMap;
050: import java.util.Iterator;
051: import java.util.Map;
052:
053: import org.dom4j.Document;
054: import org.dom4j.Node;
055:
056: import org.netbeans.modules.uml.core.support.umlsupport.XMLManip;
057:
058: /**
059: */
060: public class LanguageLibrary implements ILanguageLibrary {
061: private String m_IndexName, m_LookupFile;
062: private Document m_LookupDocument;
063: private Map<String, String> m_Index = new HashMap<String, String>();
064:
065: /**
066: * The LookupLanguageLibrary will use a configuration file to retrieve
067: * the XMI id of the class that is contained in a Describe project
068: *
069: * @param name [in] Then name of the class.
070: * @param pVal [out] The class data.
071: */
072: public IREClass findClass(String name) {
073: IREClass cl = null;
074: String fullName = name;
075: String shortName = name;
076: // NOTE: I am making the assumption that we are using the Java RT library.
077: // In java there are no JDK classes that are not in a package. Also, since
078: // classes in the java.lang package do not have to have an import statement
079: // that references the class I have to make sure that I check for classes
080: // in the java.lang package. To accomplish the java.lang check I am checking
081: // all non-scoped classes against the java.lang package.
082: //
083: // This is very Java specific and I should create a new JavaRT index file that
084: // will take this into account. Basically the index file must also take into
085: // account all global scope packages.
086: String id = null;
087:
088: //check for <*> appended to name
089: int index = -1;
090: if ((index = name.indexOf("<")) > 0) {
091: shortName = name.substring(0, index);
092: }
093:
094: if (shortName.indexOf("::") == -1
095: && shortName.indexOf(".") == -1) {
096: //try java.util.*
097: fullName = "java::util::" + shortName;
098:
099: if ((id = m_Index.get(fullName)) == null) {
100: fullName = "java::lang::" + shortName;
101:
102: if ((id = m_Index.get(fullName)) == null) {
103: fullName = resolveFullyQualifiedName(shortName);
104: }
105: }
106: }
107:
108: if (id == null)
109: id = m_Index.get(fullName);
110: if (m_LookupDocument != null && id != null) {
111: //String query = "//*[@xmi.id=\"" + id + "\"]";
112: //Node n = XMLManip.selectSingleNode(m_LookupDocument, query);
113:
114: Node n = m_LookupDocument.elementByID(id);
115:
116: if (n != null) {
117: cl = new REClass();
118: cl.setEventData(n);
119: }
120: }
121: return cl;
122: }
123:
124: /**
125: * Retrieves the XML file that is used to lookup the class information.
126: * The XMI Ids must be the same used in the index file.
127: *
128: * @param pVal [out] The name of the XML file.
129: */
130: public String getLookupFile() {
131: return m_LookupFile;
132: }
133:
134: /* (non-Javadoc)
135: * @see org.netbeans.modules.uml.core.reverseengineering.reframework.ILanguageLibrary#setLookupFile(java.lang.String)
136: */
137: public void setLookupFile(String newval) {
138: if (newval == null)
139: return;
140:
141: // AZTEC: C++ code did m_IndexName = newval, which is strange
142: m_LookupFile = newval;
143: m_LookupDocument = XMLManip.getDOMDocumentUseWeakCache(newval);
144: }
145:
146: /**
147: * Retrieves the index file used to map the class names to a XMI ID.
148: *
149: * @param pVal [out] Then name of the index file.
150: */
151: public String getIndex() {
152: return m_IndexName;
153: }
154:
155: /**
156: * Sets the index file used to map the class names to a XMI ID.
157: *
158: * @param pVal [in] Then name of the index file.
159: */
160: public void setIndex(String newVal) {
161: m_IndexName = newVal;
162: try {
163: loadIndex(m_IndexName);
164: } catch (Exception e) {
165: e.printStackTrace();
166: }
167: }
168:
169: protected void loadIndex(String file) throws FileNotFoundException,
170: IOException {
171: FileReader fr = new FileReader(file);
172: BufferedReader bufr = new BufferedReader(fr);
173: String line;
174: while ((line = bufr.readLine()) != null) {
175: int pos = line.lastIndexOf(':');
176: if (pos != -1) {
177: String key = line.substring(0, pos).trim();
178: String val = line.substring(pos + 1).trim();
179:
180: m_Index.put(key, val);
181: }
182: }
183: bufr.close();
184: fr.close();
185: }
186:
187: private String resolveFullyQualifiedName(String name) {
188: String result = null;
189: String query = "::" + name;
190: Iterator<String> iter = m_Index.keySet().iterator();
191:
192: while (iter.hasNext()) {
193: String key = iter.next();
194: if (key.endsWith(query)) {
195: result = key;
196: break;
197: }
198:
199: }
200:
201: return result;
202: }
203: }
|