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-2006 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.dbschema;
043:
044: /** Placeholder to represent a database identifier - not really implemented
045: * yet.
046: */
047: public final class DBIdentifier {
048: private String name;
049: transient private String fullName = null;
050:
051: /** Default constructor
052: */
053: public DBIdentifier() {
054: }
055:
056: /** Creates a new identifier with a given name.
057: * @param name the name
058: */
059: private DBIdentifier(String name) {
060: this .name = name;
061: }
062:
063: /** Creates an identifier with the supplied fully qualified name.
064: * @param name the name of the identifier to create
065: * @return the identifier
066: */
067: public static DBIdentifier create(String name) {
068: String shortName = name.intern();
069: String longName = null;
070: int semicolonIndex = name.indexOf(';');
071: DBIdentifier returnId = null;
072:
073: if (semicolonIndex == -1) {
074: String testName = findShortName(name);
075:
076: if (!testName.equals(name)) {
077: shortName = testName.intern();
078: longName = name;
079: } else {
080: int index = name.lastIndexOf('/');
081: if (index != -1) {
082: shortName = name.substring(index + 1).intern();
083: longName = name;
084: }
085: }
086: } else {
087: String firstHalf = name.substring(0, semicolonIndex);
088: String secondHalf = name.substring(semicolonIndex + 1);
089: String testFirstName = findShortName(firstHalf);
090: String testSecondName = findShortName(secondHalf);
091:
092: if (!testFirstName.equals(firstHalf)
093: && !testSecondName.equals(secondHalf)) {
094: shortName = testFirstName + ';' + testSecondName;
095: longName = name;
096: }
097: }
098:
099: returnId = new DBIdentifier(shortName);
100:
101: if (longName != null)
102: returnId.setFullName(longName);
103:
104: return returnId;
105: }
106:
107: /** Returns a short name.
108: * @param name the fully qualified name.
109: * @return a short name.
110: */
111: private static String findShortName(String name) {
112: int index = name.lastIndexOf('.');
113:
114: if (index != -1)
115: return name.substring(index + 1);
116:
117: return name;
118: }
119:
120: /** Gets the simple name within a package.
121: * @return the simple name
122: */
123: public String getName() {
124: return name;
125: }
126:
127: /** Sets the simple name.
128: * @param name the simple name
129: */
130: public void setName(String name) {
131: this .name = name;
132: }
133:
134: /** Gets the fully qualified name with the schema/table prefix (if any).
135: * @return the fully qualified name
136: */
137: public String getFullName() {
138: return fullName;
139: }
140:
141: /** Sets the fully qualified name.
142: * @param fullName the fully qualified name
143: */
144: public void setFullName(String fullName) {
145: this .fullName = fullName;
146: }
147:
148: /** Returns a string representation of the object.
149: * @return a string representation of the object.
150: */
151: public String toString() {
152: return name;
153: }
154:
155: /** Compare the specified Identifier with this Identifier for equality.
156: * @param id Identifier to be compared with this
157: * @return true if the specified object equals to specified Identifier otherwise false.
158: */
159: public boolean compareTo(DBIdentifier id, boolean source) {
160: if (id.fullName != null && fullName != null)
161: if (id.fullName.equals(fullName))
162: return true;
163: else
164: return false;
165:
166: if (id.name.equals(name))
167: return true;
168:
169: return false;
170: }
171: }
|