KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > dbschema > DBIdentifier


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.dbschema;
21
22 /** Placeholder to represent a database identifier - not really implemented
23 * yet.
24 */

25 public final class DBIdentifier {
26     private String JavaDoc name;
27     transient private String JavaDoc fullName = null;
28
29     /** Default constructor
30      */

31     public DBIdentifier() {
32     }
33
34     /** Creates a new identifier with a given name.
35      * @param name the name
36      */

37     private DBIdentifier(String JavaDoc name) {
38         this.name = name;
39     }
40
41     /** Creates an identifier with the supplied fully qualified name.
42      * @param name the name of the identifier to create
43      * @return the identifier
44      */

45     public static DBIdentifier create(String JavaDoc name) {
46         String JavaDoc shortName = name.intern();
47         String JavaDoc longName = null;
48         int semicolonIndex = name.indexOf(';');
49         DBIdentifier returnId = null;
50
51         if (semicolonIndex == -1) {
52             String JavaDoc testName = findShortName(name);
53
54             if (!testName.equals(name)) {
55                 shortName = testName.intern();
56                 longName = name;
57             } else {
58                 int index = name.lastIndexOf('/');
59                 if (index != -1) {
60                     shortName = name.substring(index + 1).intern();
61                     longName = name;
62                 }
63             }
64         } else {
65             String JavaDoc firstHalf = name.substring(0, semicolonIndex);
66             String JavaDoc secondHalf = name.substring(semicolonIndex + 1);
67             String JavaDoc testFirstName = findShortName(firstHalf);
68             String JavaDoc testSecondName = findShortName(secondHalf);
69
70             if (!testFirstName.equals(firstHalf) && !testSecondName.equals(secondHalf)) {
71                 shortName = testFirstName + ';' + testSecondName;
72                 longName = name;
73             }
74         }
75         
76         returnId = new DBIdentifier(shortName);
77
78         if (longName != null)
79             returnId.setFullName(longName);
80
81         return returnId;
82     }
83     
84     /** Returns a short name.
85      * @param name the fully qualified name.
86      * @return a short name.
87      */

88     private static String JavaDoc findShortName(String JavaDoc name) {
89         int index = name.lastIndexOf('.');
90
91         if (index != -1)
92             return name.substring(index + 1);
93
94         return name;
95     }
96
97     /** Gets the simple name within a package.
98      * @return the simple name
99      */

100     public String JavaDoc getName() {
101         return name;
102     }
103     
104     /** Sets the simple name.
105      * @param name the simple name
106      */

107     public void setName (String JavaDoc name) {
108         this.name = name;
109     }
110
111     /** Gets the fully qualified name with the schema/table prefix (if any).
112      * @return the fully qualified name
113      */

114     public String JavaDoc getFullName () {
115         return fullName;
116     }
117     
118     /** Sets the fully qualified name.
119      * @param fullName the fully qualified name
120      */

121     public void setFullName (String JavaDoc fullName) {
122         this.fullName = fullName;
123     }
124   
125     /** Returns a string representation of the object.
126      * @return a string representation of the object.
127      */

128     public String JavaDoc toString() {
129         return name;
130     }
131
132     /** Compare the specified Identifier with this Identifier for equality.
133      * @param id Identifier to be compared with this
134      * @return true if the specified object equals to specified Identifier otherwise false.
135      */

136     public boolean compareTo(DBIdentifier id, boolean source) {
137         if (id.fullName != null && fullName != null)
138             if (id.fullName.equals(fullName))
139                 return true;
140             else
141                 return false;
142
143         if (id.name.equals(name))
144             return true;
145     
146         return false;
147     }
148 }
149
Popular Tags