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.websvc.jaxrpc;
043:
044: import java.io.File;
045: import java.io.IOException;
046: import java.util.StringTokenizer;
047: import org.openide.WizardDescriptor;
048:
049: /** Utility methods shared for web service modules.
050: *
051: * @author Peter Williams
052: */
053: public final class Utilities {
054:
055: private Utilities() {
056: }
057:
058: /** This method ensures the list of steps displayed in the left hand panel
059: * of the wizard is correct for any given displayed panel.
060: *
061: * Taken from web/core
062: */
063: public static String[] createSteps(String[] before,
064: WizardDescriptor.Panel[] panels) {
065: //assert panels != null;
066: // hack to use the steps set before this panel processed
067: int diff = 0;
068: if (before == null) {
069: before = new String[0];
070: } else if (before.length > 0) {
071: diff = ("...".equals(before[before.length - 1])) ? 1 : 0; // NOI18N
072: }
073: String[] res = new String[(before.length - diff)
074: + panels.length];
075: for (int i = 0; i < res.length; i++) {
076: if (i < (before.length - diff)) {
077: res[i] = before[i];
078: } else {
079: res[i] = panels[i - before.length + diff]
080: .getComponent().getName();
081: }
082: }
083: return res;
084: }
085:
086: /** Class/Identifier validation
087: */
088: public static boolean isJavaIdentifier(String id) {
089: boolean result = true;
090:
091: if (id == null || id.length() == 0
092: || !Character.isJavaIdentifierStart(id.charAt(0))) {
093: result = false;
094: } else {
095: for (int i = 1, idlength = id.length(); i < idlength; i++) {
096: if (!Character.isJavaIdentifierPart(id.charAt(i))) {
097: result = false;
098: break;
099: }
100: }
101: }
102:
103: return result;
104: }
105:
106: /** Package name validation
107: */
108: public static boolean isJavaPackage(String pkg) {
109: boolean result = false;
110:
111: if (pkg != null && pkg.length() > 0) {
112: int state = 0;
113: for (int i = 0, pkglength = pkg.length(); i < pkglength
114: && state < 2; i++) {
115: switch (state) {
116: case 0:
117: if (Character.isJavaIdentifierStart(pkg.charAt(i))) {
118: state = 1;
119: } else {
120: state = 2;
121: }
122: break;
123: case 1:
124: if (pkg.charAt(i) == '.') {
125: state = 0;
126: } else if (!Character.isJavaIdentifierPart(pkg
127: .charAt(i))) {
128: state = 2;
129: }
130: break;
131: }
132: }
133:
134: if (state == 1) {
135: result = true;
136: }
137: }
138:
139: return result;
140: }
141:
142: /** Retrieve the canonical version of a File instance, converting the possible
143: * IOException into a null (presumably for error presentation purposes).
144: */
145: public static File getCanonicalFile(File f) {
146: File f1;
147: try {
148: f1 = f.getCanonicalFile();
149: } catch (IOException e) {
150: f1 = null;
151: }
152: return f1;
153: }
154:
155: public static String removeSpacesFromServiceName(String serviceName) {
156: if (serviceName != null) {
157: String result = ""; //NOI18N
158: if (serviceName.indexOf(" ") > -1) { //NOI18N
159: StringTokenizer serviceNameTokenizer = new StringTokenizer(
160: serviceName, " ", false); //NOI18N
161: while (serviceNameTokenizer.hasMoreTokens()) {
162: StringBuffer token = new StringBuffer(
163: serviceNameTokenizer.nextToken());
164: if (token != null) {
165: token.setCharAt(0, Character.toUpperCase(token
166: .charAt(0)));
167: result = result.concat(token.toString());
168: }
169: }
170: return result;
171: } else if (serviceName.length() > 0) {
172: result = Character.toUpperCase(serviceName.charAt(0))
173: + serviceName.substring(1);
174: }
175: // find the digits and change the following letters to upper case
176: StringBuffer buf = new StringBuffer(result);
177: for (int i = 0; i < buf.length(); i++) {
178: if (Character.isDigit(buf.charAt(i))) {
179: if ((i + 1) < buf.length()
180: && !Character.isDigit(buf.charAt(i + 1))) {
181: buf.setCharAt(i + 1, Character.toUpperCase(buf
182: .charAt(i + 1)));
183: ++i;
184: }
185: }
186: result = buf.toString();
187: }
188: return result;
189: }
190: return null;
191: }
192: }
|