KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > tools > OPP > OPPHelper


1 // You can redistribute this software and/or modify it under the terms of
2
// the Ozone Library License version 1 published by ozone-db.org.
3
//
4
// The original code and portions created by SMB are
5
// Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
6
//
7
// $Id: OPPHelper.java,v 1.10 2003/11/27 19:30:05 per_nyfelt Exp $
8
package org.ozoneDB.tools.OPP;
9
10 //import org.ozoneDB.DxLib.DxIterator;
11
//import org.ozoneDB.DxLib.DxTreeSet;
12

13 import java.util.regex.Matcher JavaDoc;
14 import java.util.regex.Pattern JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.Map JavaDoc;
17 import java.io.File JavaDoc;
18
19 /**
20  * @author <a HREF="http://www.softwarebuero.de/">SMB</a>
21  * @version $Revision: 1.10 $Date: 2003/11/27 19:30:05 $
22  */

23 public class OPPHelper {
24     /** @deprecated use org.ozoneDB.core.helper.ReflectionHelper.SIGNATUR_DELIMETER instead
25      * @see org.ozoneDB.core.helper.ReflectionHelper
26      */

27     public final static String JavaDoc SIGNATURE_DELIMITER = "|";
28     private static Map JavaDoc primitives;
29
30     static {
31         primitives = new HashMap JavaDoc();
32         primitives.put("byte", Byte.TYPE);
33         primitives.put("char", Character.TYPE);
34         primitives.put("double", Double.TYPE);
35         primitives.put("float", Float.TYPE);
36         primitives.put("int", Integer.TYPE);
37         primitives.put("long", Long.TYPE);
38         primitives.put("short", Short.TYPE);
39         primitives.put("boolean", Boolean.TYPE);
40     }
41
42     public static String JavaDoc typecodeForPrimitive(char ch) throws Exception JavaDoc {
43         String JavaDoc ret;
44         switch (ch) {
45             case 'B':
46                 ret = "byte";
47                 break;
48             case 'C':
49                 ret = "char";
50                 break;
51             case 'D':
52                 ret = "double";
53                 break;
54             case 'F':
55                 ret = "float";
56                 break;
57             case 'I':
58                 ret = "int";
59                 break;
60             case 'J':
61                 ret = "long";
62                 break;
63             case 'S':
64                 ret = "short";
65                 break;
66             case 'Z':
67                 ret = "boolean";
68                 break;
69             default:
70                 throw new Exception JavaDoc("Unknown type code '" + ch + "'.");
71         }
72         return ret;
73     }
74
75     public static String JavaDoc wrappercodeForPrimitive(Class JavaDoc cl) /*throws Exception*/ {
76         String JavaDoc name = cl.getName();
77         return wrappercodeForPrimitive(name);
78     }
79
80     public static String JavaDoc wrappercodeForPrimitive(String JavaDoc name) /*throws Exception*/ {
81         String JavaDoc ret;
82         if (name.equals("int")) {
83             ret = "Integer";
84         } else if (name.equals("char")) {
85             ret = "Character";
86         } else {
87             ret = name.substring(0, 1).toUpperCase() + name.substring(1);
88         }
89         return ret;
90     }
91
92     /**
93      * Checks weather a string representing a build type is a primitive.
94      * By using this method in builders and directors we don't need to have access
95      * to the classes that the strings represent. This is useful when the direct
96      * is not using reflection but rather parsing the build resolver.
97      * @param typeName The name of the type to be checked
98      * @return true if the typeName represents a primitive
99      */

100     public static boolean isPrimitive(String JavaDoc typeName) {
101         return primitives.keySet().contains(typeName);
102     }
103
104     /**
105      * For all primitive types in implementationClass return a string like '<varName>.intValue()'.
106      */

107     public static String JavaDoc returncodeForPrimitive(Class JavaDoc cl, String JavaDoc varName) {
108         String JavaDoc name = cl.getName();
109         return returncodeForPrimitive(name, varName);
110     }
111
112     /**
113      * For all primitive types in implementationClass return a string like '<varName>.intValue()'.
114      */

115     public static String JavaDoc returncodeForPrimitive(String JavaDoc name, String JavaDoc varName) {
116         String JavaDoc ret;
117         if (name.equals("int")) {
118             ret = "((Integer)" + varName + ").intValue()";
119         } else if (name.equals("boolean")) {
120             ret = "((Boolean)" + varName + ").booleanValue()";
121         } else if (name.equals("char")) {
122             ret = "((Character)" + varName + ").charValue()";
123         } else if (name.equals("long")) {
124             ret = "((Long)" + varName + ").longValue()";
125         } else if (name.equals("float")) {
126             ret = "((Float)" + varName + ").floatValue()";
127         } else if (name.equals("double")) {
128             ret = "((Double)" + varName + ").doubleValue()";
129         } else if (name.equals("byte")) {
130             ret = "((Byte)" + varName + ").byteValue()";
131         } else if (name.equals("short")) {
132             ret = "((Short)" + varName + ").shortValue()";
133         } else {
134             throw new IllegalArgumentException JavaDoc("unknown type: '" + name + "'");
135         }
136         return ret;
137     }
138
139     public static String JavaDoc packageName(Class JavaDoc c) {
140         String JavaDoc name = c.getName();
141         return OPPHelper.packageName(name);
142     }
143
144     public static String JavaDoc packageName(String JavaDoc name) {
145         int index = name.lastIndexOf('.');
146         return index != -1 ? name.substring(0, index) : "";
147     }
148
149     public static String JavaDoc simpleClassName(Class JavaDoc c) {
150         return simpleClassName(c.getName());
151     }
152
153     public static String JavaDoc simpleClassName(String JavaDoc name) {
154         int index = name.lastIndexOf('.');
155         return index != -1 ? name.substring(index + 1) : name;
156     }
157
158     public static String JavaDoc classFileBasename(Class JavaDoc c) {
159         return classFileBasename(c.getName());
160     }
161
162     public static String JavaDoc classFileBasename(String JavaDoc name) {
163         return classFileBasename(name, File.separatorChar);
164     }
165
166     public static String JavaDoc classFileBasename(String JavaDoc name, char separator) {
167         return name.replace('.', separator);
168     }
169
170
171     public static Pattern JavaDoc newRE(String JavaDoc s, boolean ignoreCase) {
172         return ignoreCase ? Pattern.compile(s, Pattern.CASE_INSENSITIVE) : Pattern.compile(s);
173     }
174
175     public static boolean reMatch(Object JavaDoc re, String JavaDoc s) {
176         Pattern JavaDoc pattern = (Pattern JavaDoc) re;
177         //System.out.println("[OPPHelper] - Pattern is " + pattern.pattern());
178
Matcher JavaDoc matcher = pattern.matcher(s);
179         return matcher.matches();
180     }
181
182     public static String JavaDoc reSearch(Object JavaDoc re, String JavaDoc s, int start, int paren) {
183         Pattern JavaDoc pattern = (Pattern JavaDoc) re;
184         Matcher JavaDoc matcher = pattern.matcher(s);
185         if (matcher.find(start)) {
186             return matcher.group(paren);
187         }
188         return null;
189     }
190
191     public static void main(String JavaDoc[] av) throws Exception JavaDoc {
192         System.out.println(av[0]);
193         Object JavaDoc re = newRE(av[0], false);
194         System.out.println(reSearch(re, av[1], 0, 0));
195     }
196
197     public static Class JavaDoc classForPrimitive(String JavaDoc type) {
198         return (Class JavaDoc) primitives.get(type);
199     }
200 }
201
Popular Tags