KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > tools > generator > GenerateSupportUtils


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 package org.netbeans.modules.xml.tools.generator;
20
21 import java.awt.event.ActionEvent JavaDoc;
22 import java.util.*;
23 import java.text.DateFormat JavaDoc;
24
25 import org.openide.filesystems.FileObject;
26 import org.openide.cookies.OpenCookie;
27 import org.openide.cookies.EditCookie;
28 import org.openide.loaders.*;
29 import org.openide.nodes.Node;
30 import org.openide.util.Utilities;
31 import org.openide.util.actions.SystemAction;
32
33 public class GenerateSupportUtils {
34
35     /**
36      * Use some heuristics and create valid java name.
37      */

38     public static String JavaDoc getJavaName (String JavaDoc name) {
39         StringTokenizer st = new StringTokenizer (name, " -.:"); // NOI18N
40
StringBuffer JavaDoc sb = new StringBuffer JavaDoc ();
41         if (st.hasMoreTokens()) {
42             sb.append (st.nextToken());
43             while (st.hasMoreTokens()) {
44                 sb.append ("_").append (st.nextToken()); // NOI18N
45
}
46         }
47         return sb.toString();
48     }
49
50
51     /*
52      * Generate default java header.
53      * @param primFile a model used for generated code or null
54      */

55     public static String JavaDoc getJavaFileHeader (String JavaDoc name, FileObject primFile) {
56         Date now = new Date();
57
58         StringBuffer JavaDoc sb = new StringBuffer JavaDoc ();
59         sb.append("/*\n * File: ").append (name).append (".java"); // NOI18N
60
if (primFile != null) {
61             sb.append ("\n * Generated from: ").append (primFile.getName()).append (".").append (primFile.getExt()); // NOI18N
62
}
63         sb.append("\n * Date: ").append (DateFormat.getDateInstance (DateFormat.LONG).format (now)); // NOI18N
64
sb.append (" ").append (DateFormat.getTimeInstance (DateFormat.SHORT).format (now)); // NOI18N
65
sb.append("\n *"); // NOI18N
66
sb.append("\n * @author ").append(System.getProperty ("user.name")); // NOI18N
67
sb.append("\n * @version generated by NetBeans XML module"); // NOI18N
68
sb.append("\n */"); // NOI18N
69
return sb.toString();
70     }
71     
72 // public static final void debug(String msg, Throwable ex) {
73
// if (Boolean.getBoolean("netbeans.debug.xml")) { // NOI18N
74
// System.err.println(msg);
75
// ex.printStackTrace();
76
// }
77
// }
78

79     
80     /**
81      * Test whether given identifier is valid Java return type
82      */

83     public static boolean isValidReturnType(String JavaDoc value) {
84         
85         if (value == null || "".equals(value)) return false; // NOI18N
86
int length = value.length();
87         
88         // check for arrays e.g. "String[][]"
89
while (length > 2 && value.charAt(length-2) == '[' && value.charAt(length-1) == ']') {
90             value = value.substring(0, length-2);
91             length -= 2;
92         }
93         
94         if (value.charAt(0) == '.' || value.charAt(value.length()-1) == '.')
95             return false;
96         
97         for(int i=1; i<length-2; i++) {
98             if (value.charAt(i) == '.' && value.charAt(i+1) == '.')
99                 return false;
100         }
101         
102                     
103         StringTokenizer st = new StringTokenizer(value, "."); // NOI18N
104
while (st.hasMoreTokens()) {
105             String JavaDoc token = st.nextToken();
106             if (!!!isPrimitiveType(token) && !!!Utilities.isJavaIdentifier(token))
107                 return false;
108         }
109         return true;
110     }
111
112     /**
113      * @return true if given value does represent Java primitive type.
114      */

115     public static boolean isPrimitiveType(String JavaDoc value) {
116
117         String JavaDoc val[] = {"int", "char", "boolean", "long", "float", "double", "void"}; // NOI18N
118
for(int i=0;i<val.length;i++) {
119             if(val[i].equals(value))
120                 return true;
121         }
122         return false;
123     }
124     
125 }
126
Popular Tags