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.cnd.makeproject.api.configurations;
043:
044: import java.util.StringTokenizer;
045: import org.netbeans.api.project.Project;
046:
047: public class ConfigurationSupport {
048: public static String appendConfName(String oldConfs,
049: Configuration newConf) {
050: return oldConfs + "," + newConf.getDisplayName(); // NOI18N
051: }
052:
053: public static String removeConfName(String oldConfs,
054: Configuration oldConf) {
055: StringBuilder newConfs = new StringBuilder();
056: StringTokenizer st = new StringTokenizer(oldConfs, ","); // NOI18N
057: while (st.hasMoreTokens()) {
058: // Strip "'s
059: String token = st.nextToken();
060: String displayName = token; //token.substring(1, token.length()-1);
061: if (displayName.equals(oldConf.getDisplayName()))
062: continue;
063: if (newConfs.length() > 0)
064: newConfs.append(","); // NOI18N
065: newConfs.append(displayName);
066: }
067: return newConfs.toString();
068: }
069:
070: public static String renameConfName(String oldConfs,
071: String oldDisplayName, String newDisplayName) {
072: int i = oldConfs.indexOf(oldDisplayName);
073: if (i < 0) {
074: // Error FIXUP; should be there!
075: }
076: String newConfs = oldConfs.substring(0, i) + newDisplayName
077: + oldConfs.substring(i + oldDisplayName.length());
078: return newConfs;
079:
080: }
081:
082: public static String makeNameLegal(String displayName) {
083: StringBuilder tmp = new StringBuilder();
084: for (int i = 0; i < displayName.length(); i++) {
085: if (i == 0
086: && (isLetterOrDigit(displayName.charAt(i)) || displayName
087: .charAt(i) == '_')) {
088: tmp.append(displayName.charAt(i));
089: } else if (i != 0
090: && (isLetterOrDigit(displayName.charAt(i))
091: || displayName.charAt(i) == '_'
092: || displayName.charAt(i) == '-' || displayName
093: .charAt(i) == '.')) {
094: tmp.append(displayName.charAt(i));
095: } else {
096: tmp.append("_"); // NOI18N
097: }
098: }
099: if (tmp.length() == 0)
100: return "Configuration"; // NOI18N
101: else
102: return tmp.toString();
103: }
104:
105: private static boolean isLetterOrDigit(char ch) {
106: if (ch < '0' || ch > 'z')
107: return false;
108: else
109: return Character.isLetterOrDigit(ch);
110: }
111:
112: public static String getNameFromDisplayName(String displayName) {
113: /*
114: StringBuffer tmp = new StringBuffer();
115: for (int i = 0; i < displayName.length(); i++) {
116: if (!Character.isWhitespace(displayName.charAt(i)) &&
117: displayName.charAt(i) != '(' &&
118: displayName.charAt(i) != ')' &&
119: displayName.charAt(i) != ',') {
120: tmp.append(displayName.charAt(i));
121: }
122: }
123: return tmp.toString();
124: */
125: return displayName; // FIXUP: are thay always identical????
126: }
127:
128: // Unique names
129: public static String getUniqueName(Configuration[] cs,
130: String baseName) {
131: int number = 1;
132: String newDisplayName;
133: while (true) {
134: if (number == 1)
135: newDisplayName = baseName;
136: else
137: newDisplayName = baseName + "-" + number; // NOI18N
138: if (isNameUnique(cs, newDisplayName))
139: break;
140: number++;
141: }
142: return newDisplayName;
143: }
144:
145: public static String getUniqueNewName(Configuration[] cs) {
146: return getUniqueName(cs, "NewConfiguration"); // NOI18N
147: }
148:
149: public static String getUniqueCopyName(Configuration[] cs,
150: Configuration copy) {
151: int number = 1;
152: String newBaseName = "Copy"; // NOI18N
153: String newName;
154: while (true) {
155: if (number == 1)
156: newName = newBaseName + "_of_" + copy.getName(); // NOI18N
157: else
158: newName = newBaseName + "-" + number + "_of_"
159: + copy.getName(); // NOI18N
160: if (isNameUnique(cs, newName))
161: break;
162: number++;
163: }
164: return newName;
165: }
166:
167: public static boolean isNameUnique(Configuration[] cs,
168: String displayName) {
169: boolean unique = true;
170: String name = getNameFromDisplayName(displayName);
171: for (int i = 0; i < cs.length; i++) {
172: if (cs[i].getName().equals(name)) {
173: unique = false;
174: break;
175: }
176: }
177: return unique;
178: }
179:
180: // property values
181: public static String getConfsPropertyValue(Configuration[] cs) {
182: StringBuilder configurationProperty = new StringBuilder();
183: for (int i = 0; i < cs.length; i++) {
184: if (configurationProperty.length() > 0)
185: configurationProperty.append(","); // NOI18N
186: configurationProperty.append(cs[i].getDisplayName());
187: }
188: return configurationProperty.toString();
189: }
190:
191: public static String getDefaultConfPropertyValue(Configuration conf) {
192: return conf.getDisplayName();
193: }
194:
195: public static ConfigurationDescriptor getProjectDescriptor(
196: Project project) {
197: ConfigurationDescriptorProvider pdp = project.getLookup()
198: .lookup(ConfigurationDescriptorProvider.class);
199: return pdp.getConfigurationDescriptor();
200: }
201: }
|