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.util;
043:
044: public class NameUtil {
045:
046: /** Column names separator in the column pair name. */
047: private static final char columnPairSeparator = ';';
048:
049: /** Element namess separator in the fully qualified name. */
050: public static final char dbElementSeparator = '.';
051:
052: /** Returns schema resource name.
053: * @return the schema resource name
054: */
055: public static String getSchemaResourceName(String schemaName) {
056: return (schemaName + ".dbschema"); //NOI18N
057: }
058:
059: /**
060: * Returns the table name from a given db member name.
061: * The member might be a column or a column pair.
062: * @param memberName the full qualified member name
063: * @return the table name of this member name
064: */
065: public static String getTableName(String memberName) {
066: int index = memberName.indexOf(columnPairSeparator);
067: String tempString = ((index != -1) ? memberName.substring(0,
068: index) : memberName);
069:
070: return tempString.substring(0, tempString
071: .lastIndexOf(dbElementSeparator));
072: }
073:
074: /**
075: * Returns the schema name from a given db member name.
076: * The member might be a column, a table or a column pair.
077: * @param memberName the full qualified member name
078: * @return the schema name of this member name
079: */
080: public static String getSchemaName(String memberName) {
081: if (memberName == null)
082: return null;
083: int index = memberName.indexOf(columnPairSeparator);
084: String tempString = ((index != -1) ? memberName.substring(0,
085: index) : memberName);
086:
087: return tempString.substring(0, tempString
088: .indexOf(dbElementSeparator));
089: }
090:
091: /**
092: * The method returns the relative name for the specified table name.
093: * If the specified name is already relative, the method returns it as it is.
094: * Otherwise it strips the schema name from the given table name.
095: * @param tableName a table name
096: * @return the relative table name
097: */
098: public static String getRelativeTableName(String tableName) {
099: if (tableName == null)
100: return null;
101:
102: if (isRelativeTableName(tableName))
103: return tableName;
104:
105: return tableName.substring(tableName
106: .indexOf(dbElementSeparator) + 1);
107: }
108:
109: /**
110: * The method returns the relative name for the specified member name.
111: * If the specified name is already relative, the method returns member name
112: * equal to the input argument.
113: * Otherwise it strips the schema name from the given member name.
114: * In the case of a column pair it stips the schema name from both column names.
115: * @param tableName a member name
116: * @return the relative member name
117: */
118: public static String getRelativeMemberName(String memberName) {
119: if (memberName == null)
120: return null;
121:
122: int semicolonIndex = memberName.indexOf(columnPairSeparator);
123: if (semicolonIndex != -1) {
124: String firstColumn = memberName
125: .substring(0, semicolonIndex);
126: String secondColumn = memberName
127: .substring(semicolonIndex + 1);
128: return getRelativeMemberNameInternal(firstColumn)
129: + columnPairSeparator
130: + getRelativeMemberNameInternal(secondColumn);
131: } else {
132: return getRelativeMemberNameInternal(memberName);
133: }
134: }
135:
136: /**
137: * The method returns the relative name for the specified member name.
138: * Note, this is an internal helper method which fails for a column pair name.
139: * If the specified name is already relative, the method returns it as it is.
140: * Otherwise it strips the schema name from the given member name.
141: * @param memberName a non column pair member name
142: * @return the relative member name
143: * @see #getRelativeMemberName
144: */
145: private static String getRelativeMemberNameInternal(
146: String memberName) {
147: if (memberName == null)
148: return null;
149:
150: if (isRelativeMemberName(memberName))
151: return memberName;
152:
153: return memberName.substring(memberName
154: .indexOf(dbElementSeparator) + 1);
155: }
156:
157: /**
158: * The method returns a absolute (full qualified) name for the specified table name
159: * using the specified schema name.
160: * If the specified name is already absolute, the method returns it as it is,
161: * without checking the schema part of the name beeing equal to the specified schema name.
162: * @param schemaName the schema name to be prepended
163: * @param tableName the table name
164: * @return the absolute table name
165: */
166: public static String getAbsoluteTableName(String schemaName,
167: String tableName) {
168: if (tableName == null)
169: return null;
170:
171: if (!isRelativeTableName(tableName))
172: return tableName;
173:
174: return schemaName + dbElementSeparator + tableName;
175: }
176:
177: /**
178: * The method returns a absolute (full qualified) name for the specified member name
179: * using the specified schema name.
180: * If the specified name is already absolute, the method returns a member name equal to the input
181: * argument,without checking the schema part of the name beeing equal to the specified schema name.
182: * In the case of a column pair it prepends the schemaName to both columns included in the pair.
183: * @param schemaName the schema name to be prepended
184: * @param memberName the member name
185: * @return the absolute member name
186: */
187: public static String getAbsoluteMemberName(String schemaName,
188: String memberName) {
189: if (memberName == null)
190: return null;
191:
192: int semicolonIndex = memberName.indexOf(columnPairSeparator);
193: if (semicolonIndex != -1) {
194: String firstColumn = memberName
195: .substring(0, semicolonIndex);
196: String secondColumn = memberName
197: .substring(semicolonIndex + 1);
198: return getAbsoluteMemberNameInternal(schemaName,
199: firstColumn)
200: + columnPairSeparator
201: + getAbsoluteMemberNameInternal(schemaName,
202: secondColumn);
203: } else {
204: return getAbsoluteMemberNameInternal(schemaName, memberName);
205: }
206: }
207:
208: /**
209: * The method returns a absolute (full qualified) name for the specified member name
210: * using the specified schema name.
211: * Note, this is an internal helper method, that fails if it gets a column pair name.
212: * If the specified name is already absolute, the method returns it as it is,
213: * without checking the schema part of the name beeing equal to the specified schema name.
214: * @param schemaName the schema name to be prepended
215: * @param memberName the non column pair member name
216: * @return the absolute member name
217: * @see #getAbsoluteMemberName
218: */
219: private static String getAbsoluteMemberNameInternal(
220: String schemaName, String memberName) {
221: if (memberName == null)
222: return null;
223: if (!isRelativeMemberName(memberName))
224: return memberName;
225:
226: return schemaName + dbElementSeparator + memberName;
227: }
228:
229: /**
230: * Returns true if the specified table name is relative.
231: * Note, this method fails for member names,
232: * use isRelativeMemberName instead.
233: * @param tableName relative or absolute table name
234: * @return true if tableName is relative; false otherwise
235: * @see #isRelativeMemberName
236: */
237: private static boolean isRelativeTableName(String tableName) {
238: // A relative table name does not contain a dbElementSeparator
239: return tableName.indexOf(dbElementSeparator) == -1;
240: }
241:
242: /**
243: * Returns true if the specified member name is relative.
244: * Note, the method fails for a relative table name, please use isRelativeTableName instead.
245: * Note, the method fails for a column pair name (both relative or absolute),
246: * split the column pair name first and then call this method.
247: * @param columnName relative or absolute column name
248: * @return true if columnName is relative; false otherwise
249: * @see #isRelativeTableName
250: */
251: private static boolean isRelativeMemberName(String columnName) {
252: // A relative column name contains a single dbElementSeparator
253: int first = columnName.indexOf(dbElementSeparator);
254: return columnName.indexOf(dbElementSeparator, first + 1) == -1;
255: }
256: }
|