KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > websvc > jaxrpc > Utilities


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.websvc.jaxrpc;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.StringTokenizer JavaDoc;
25 import org.openide.WizardDescriptor;
26
27 /** Utility methods shared for web service modules.
28  *
29  * @author Peter Williams
30  */

31 public final class Utilities {
32     
33     private Utilities() {
34     }
35     
36     /** This method ensures the list of steps displayed in the left hand panel
37      * of the wizard is correct for any given displayed panel.
38      *
39      * Taken from web/core
40      */

41     public static String JavaDoc[] createSteps(String JavaDoc[] before, WizardDescriptor.Panel[] panels) {
42         //assert panels != null;
43
// hack to use the steps set before this panel processed
44
int diff = 0;
45         if (before == null) {
46             before = new String JavaDoc[0];
47         } else if (before.length > 0) {
48             diff = ("...".equals(before[before.length - 1])) ? 1 : 0; // NOI18N
49
}
50         String JavaDoc[] res = new String JavaDoc[ (before.length - diff) + panels.length];
51         for (int i = 0; i < res.length; i++) {
52             if (i < (before.length - diff)) {
53                 res[i] = before[i];
54             } else {
55                 res[i] = panels[i - before.length + diff].getComponent().getName();
56             }
57         }
58         return res;
59     }
60     
61     /** Class/Identifier validation
62      */

63     public static boolean isJavaIdentifier(String JavaDoc id) {
64         boolean result = true;
65         
66         if(id == null || id.length() == 0 || !Character.isJavaIdentifierStart(id.charAt(0))) {
67             result = false;
68         } else {
69             for(int i = 1, idlength = id.length(); i < idlength; i++) {
70                 if(!Character.isJavaIdentifierPart(id.charAt(i))) {
71                     result = false;
72                     break;
73                 }
74             }
75         }
76         
77         return result;
78     }
79     
80     /** Package name validation
81      */

82     public static boolean isJavaPackage(String JavaDoc pkg) {
83         boolean result = false;
84         
85         if(pkg != null && pkg.length() > 0) {
86             int state = 0;
87             for(int i = 0, pkglength = pkg.length(); i < pkglength && state < 2; i++) {
88                 switch(state) {
89                     case 0:
90                         if(Character.isJavaIdentifierStart(pkg.charAt(i))) {
91                             state = 1;
92                         } else {
93                             state = 2;
94                         }
95                         break;
96                     case 1:
97                         if(pkg.charAt(i) == '.') {
98                             state = 0;
99                         } else if(!Character.isJavaIdentifierPart(pkg.charAt(i))) {
100                             state = 2;
101                         }
102                         break;
103                 }
104             }
105             
106             if(state == 1) {
107                 result = true;
108             }
109         }
110         
111         return result;
112     }
113     
114     /** Retrieve the canonical version of a File instance, converting the possible
115      * IOException into a null (presumably for error presentation purposes).
116      */

117     public static File JavaDoc getCanonicalFile(File JavaDoc f) {
118         File JavaDoc f1;
119         try {
120             f1 = f.getCanonicalFile();
121         } catch (IOException JavaDoc e) {
122             f1 = null;
123         }
124         return f1;
125     }
126     
127     public static String JavaDoc removeSpacesFromServiceName(String JavaDoc serviceName) {
128         if (serviceName!=null) {
129             String JavaDoc result = ""; //NOI18N
130
if (serviceName.indexOf(" ") > -1) { //NOI18N
131
StringTokenizer JavaDoc serviceNameTokenizer = new StringTokenizer JavaDoc(serviceName, " ", false); //NOI18N
132
while (serviceNameTokenizer.hasMoreTokens()) {
133                     StringBuffer JavaDoc token = new StringBuffer JavaDoc(serviceNameTokenizer.nextToken());
134                     if (token != null) {
135                         token.setCharAt(0, Character.toUpperCase(token.charAt(0)));
136                         result = result.concat(token.toString());
137                     }
138                 }
139                 return result;
140             } else if (serviceName.length()>0) {
141                 result = Character.toUpperCase(serviceName.charAt(0))+serviceName.substring(1);
142             }
143             // find the digits and change the following letters to upper case
144
StringBuffer JavaDoc buf = new StringBuffer JavaDoc(result);
145             for (int i=0;i<buf.length();i++) {
146                 if (Character.isDigit(buf.charAt(i))) {
147                     if ((i+1)<buf.length() && !Character.isDigit(buf.charAt(i+1))) {
148                         buf.setCharAt(i+1, Character.toUpperCase(buf.charAt(i+1)));
149                         ++i;
150                     }
151                 }
152                 result = buf.toString();
153             }
154             return result;
155         }
156         return null;
157     }
158 }
159
Popular Tags