KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > dbschema > util > NameUtil


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.util;
21
22 public class NameUtil {
23
24     /** Column names separator in the column pair name. */
25     private static final char columnPairSeparator = ';';
26
27     /** Element namess separator in the fully qualified name. */
28     public static final char dbElementSeparator = '.';
29
30     /** Returns schema resource name.
31      * @return the schema resource name
32      */

33     public static String JavaDoc getSchemaResourceName(String JavaDoc schemaName) {
34         return (schemaName + ".dbschema"); //NOI18N
35
}
36
37     /**
38      * Returns the table name from a given db member name.
39      * The member might be a column or a column pair.
40      * @param memberName the full qualified member name
41      * @return the table name of this member name
42      */

43     public static String JavaDoc getTableName (String JavaDoc memberName) {
44         int index = memberName.indexOf(columnPairSeparator);
45         String JavaDoc tempString = ((index != -1) ? memberName.substring(0, index) : memberName);
46
47         return tempString.substring(0, tempString.lastIndexOf(dbElementSeparator));
48     }
49
50     /**
51      * Returns the schema name from a given db member name.
52      * The member might be a column, a table or a column pair.
53      * @param memberName the full qualified member name
54      * @return the schema name of this member name
55      */

56     public static String JavaDoc getSchemaName(String JavaDoc memberName)
57     {
58         if (memberName == null)
59             return null;
60         int index = memberName.indexOf(columnPairSeparator);
61         String JavaDoc tempString = ((index != -1) ? memberName.substring(0, index) : memberName);
62
63         return tempString.substring(0, tempString.indexOf(dbElementSeparator));
64     }
65
66     /**
67      * The method returns the relative name for the specified table name.
68      * If the specified name is already relative, the method returns it as it is.
69      * Otherwise it strips the schema name from the given table name.
70      * @param tableName a table name
71      * @return the relative table name
72      */

73     public static String JavaDoc getRelativeTableName(String JavaDoc tableName)
74     {
75         if (tableName == null)
76             return null;
77
78         if (isRelativeTableName(tableName))
79             return tableName;
80
81         return tableName.substring(tableName.indexOf(dbElementSeparator) + 1);
82     }
83
84     /**
85      * The method returns the relative name for the specified member name.
86      * If the specified name is already relative, the method returns member name
87      * equal to the input argument.
88      * Otherwise it strips the schema name from the given member name.
89      * In the case of a column pair it stips the schema name from both column names.
90      * @param tableName a member name
91      * @return the relative member name
92      */

93     public static String JavaDoc getRelativeMemberName(String JavaDoc memberName)
94     {
95         if (memberName == null)
96             return null;
97
98         int semicolonIndex = memberName.indexOf(columnPairSeparator);
99         if (semicolonIndex != -1)
100         {
101             String JavaDoc firstColumn = memberName.substring(0, semicolonIndex);
102             String JavaDoc secondColumn = memberName.substring(semicolonIndex + 1);
103             return getRelativeMemberNameInternal(firstColumn) + columnPairSeparator +
104                 getRelativeMemberNameInternal(secondColumn);
105         }
106         else
107         {
108             return getRelativeMemberNameInternal(memberName);
109         }
110     }
111
112     /**
113      * The method returns the relative name for the specified member name.
114      * Note, this is an internal helper method which fails for a column pair name.
115      * If the specified name is already relative, the method returns it as it is.
116      * Otherwise it strips the schema name from the given member name.
117      * @param memberName a non column pair member name
118      * @return the relative member name
119      * @see #getRelativeMemberName
120      */

121     private static String JavaDoc getRelativeMemberNameInternal(String JavaDoc memberName)
122     {
123         if (memberName == null)
124             return null;
125         
126         if (isRelativeMemberName(memberName))
127             return memberName;
128         
129         return memberName.substring(memberName.indexOf(dbElementSeparator) + 1);
130     }
131     
132     /**
133      * The method returns a absolute (full qualified) name for the specified table name
134      * using the specified schema name.
135      * If the specified name is already absolute, the method returns it as it is,
136      * without checking the schema part of the name beeing equal to the specified schema name.
137      * @param schemaName the schema name to be prepended
138      * @param tableName the table name
139      * @return the absolute table name
140      */

141     public static String JavaDoc getAbsoluteTableName(String JavaDoc schemaName, String JavaDoc tableName)
142     {
143         if (tableName == null)
144             return null;
145
146         if (!isRelativeTableName(tableName))
147             return tableName;
148         
149         return schemaName + dbElementSeparator + tableName;
150     }
151
152     /**
153      * The method returns a absolute (full qualified) name for the specified member name
154      * using the specified schema name.
155      * If the specified name is already absolute, the method returns a member name equal to the input
156      * argument,without checking the schema part of the name beeing equal to the specified schema name.
157      * In the case of a column pair it prepends the schemaName to both columns included in the pair.
158      * @param schemaName the schema name to be prepended
159      * @param memberName the member name
160      * @return the absolute member name
161      */

162     public static String JavaDoc getAbsoluteMemberName(String JavaDoc schemaName, String JavaDoc memberName)
163     {
164         if (memberName == null)
165             return null;
166
167         int semicolonIndex = memberName.indexOf(columnPairSeparator);
168         if (semicolonIndex != -1)
169         {
170             String JavaDoc firstColumn = memberName.substring(0, semicolonIndex);
171             String JavaDoc secondColumn = memberName.substring(semicolonIndex + 1);
172             return getAbsoluteMemberNameInternal(schemaName, firstColumn) +
173                    columnPairSeparator +
174                    getAbsoluteMemberNameInternal(schemaName, secondColumn);
175         }
176         else
177         {
178             return getAbsoluteMemberNameInternal(schemaName, memberName);
179         }
180     }
181
182     /**
183      * The method returns a absolute (full qualified) name for the specified member name
184      * using the specified schema name.
185      * Note, this is an internal helper method, that fails if it gets a column pair name.
186      * If the specified name is already absolute, the method returns it as it is,
187      * without checking the schema part of the name beeing equal to the specified schema name.
188      * @param schemaName the schema name to be prepended
189      * @param memberName the non column pair member name
190      * @return the absolute member name
191      * @see #getAbsoluteMemberName
192      */

193     private static String JavaDoc getAbsoluteMemberNameInternal(String JavaDoc schemaName, String JavaDoc memberName)
194     {
195         if (memberName == null)
196             return null;
197         if (!isRelativeMemberName(memberName))
198             return memberName;
199
200         return schemaName + dbElementSeparator + memberName;
201     }
202
203     /**
204      * Returns true if the specified table name is relative.
205      * Note, this method fails for member names,
206      * use isRelativeMemberName instead.
207      * @param tableName relative or absolute table name
208      * @return true if tableName is relative; false otherwise
209      * @see #isRelativeMemberName
210      */

211     private static boolean isRelativeTableName(String JavaDoc tableName)
212     {
213         // A relative table name does not contain a dbElementSeparator
214
return tableName.indexOf(dbElementSeparator) == -1;
215     }
216     
217     /**
218      * Returns true if the specified member name is relative.
219      * Note, the method fails for a relative table name, please use isRelativeTableName instead.
220      * Note, the method fails for a column pair name (both relative or absolute),
221      * split the column pair name first and then call this method.
222      * @param columnName relative or absolute column name
223      * @return true if columnName is relative; false otherwise
224      * @see #isRelativeTableName
225      */

226     private static boolean isRelativeMemberName(String JavaDoc columnName)
227     {
228         // A relative column name contains a single dbElementSeparator
229
int first = columnName.indexOf(dbElementSeparator);
230         return columnName.indexOf(dbElementSeparator, first + 1) == -1;
231     }
232 }
233
Popular Tags