View Javadoc

1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.geronimo.ews.ws4j2ee.utils;
18  
19  import java.io.File;
20  import java.io.IOException;
21  import java.io.InputStream;
22  import java.lang.reflect.Method;
23  import java.util.ArrayList;
24  import java.util.StringTokenizer;
25  
26  import javax.xml.namespace.QName;
27  import javax.xml.parsers.DocumentBuilder;
28  import javax.xml.parsers.DocumentBuilderFactory;
29  
30  import org.apache.axis.AxisFault;
31  import org.apache.axis.utils.ClassUtils;
32  import org.apache.axis.utils.JavaUtils;
33  import org.apache.geronimo.ews.ws4j2ee.context.J2EEWebServiceContext;
34  import org.apache.geronimo.ews.ws4j2ee.toWs.GenerationFault;
35  import org.w3c.dom.Document;
36  import org.w3c.dom.Node;
37  import org.w3c.dom.NodeList;
38  import org.w3c.dom.Text;
39  import org.xml.sax.EntityResolver;
40  import org.xml.sax.InputSource;
41  import org.xml.sax.SAXException;
42  
43  /***
44   * This class was taken from the axis XMLUtils. It should be properly adopted if to be used permanantly
45   */
46  public class Utils {
47  //    protected static Log log =
48  //        LogFactory.getLog(XMLUtils.class.getName());
49          
50      public static final String charEncoding = "ISO-8859-1";
51      private static final String saxParserFactoryProperty =
52              "javax.xml.parsers.SAXParserFactory";
53  
54      private static DocumentBuilderFactory dbf = getDOMFactory();
55  
56      public static String getClassNameFromQuallifiedName(String qualifiedName) {
57          int index = qualifiedName.lastIndexOf('.');
58          if (index > 0)
59              qualifiedName = qualifiedName.substring(index + 1);
60          return qualifiedName;
61      }
62  
63      public static String getPackageNameFromQuallifiedName(String qualifiedName) {
64          int index = qualifiedName.lastIndexOf('.');
65          if (index > 0)
66              return qualifiedName.substring(0, index);
67          else
68              return "";
69      }
70  
71      public static String getAbsolutePath(String path, String confFileLocation) throws GenerationFault {
72          if (path != null) {
73              if (path.indexOf(":/") > -1 || path.indexOf("://") > -1 || path.startsWith("/"))
74                  return path;
75              return confFileLocation + "/" + path;
76          } else {
77              throw new GenerationFault("the path can not be null");
78          }
79      }
80  
81      public static String firstCharacterToLowerCase(String name) {
82          char[] charName = name.toCharArray();
83          if (charName.length > 0)
84              charName[0] = Character.toLowerCase(charName[0]);
85          return new String(charName);
86      }
87  
88      public static String firstCharacterToUpperCase(String name) {
89          char[] charName = name.toCharArray();
90          if (charName.length > 0)
91              charName[0] = Character.toUpperCase(charName[0]);
92          return new String(charName);
93      }
94  
95      public static String qName2JavaName(QName qname) {
96          return org.apache.axis.wsdl.toJava.Utils.makePackageName(qname.getNamespaceURI())
97                  + "." + firstCharacterToUpperCase(JavaUtils.xmlNameToJava(qname.getLocalPart()));
98      }
99  
100     public static Method getJavaMethod(String className, String methodName) throws AxisFault {
101         String primKey = null;
102         Class sei;
103         try {
104             sei = ClassUtils.forName(className);
105             java.lang.reflect.Method callMethod = null;
106             Method[] methods = sei.getMethods();
107             for (int i = 0; i < methods.length; i++) {
108                 if (methods[i].getName().equals(methodName)) {
109                     callMethod = methods[i];
110                 }
111             }
112             if (callMethod == null)
113                 throw new org.apache.geronimo.ews.ws4j2ee.toWs.UnrecoverableGenerationFault("Method " + methodName + " not found in the class" + className);
114             return callMethod;
115         } catch (ClassNotFoundException e) {
116             throw AxisFault.makeFault(e);
117         }
118     }
119 
120     public static Object createParameter(Object obj) {
121         return obj;
122     }
123 
124     public static Object createParameter(int in) {
125         return new Integer(in);
126     }
127 
128     public static Object createParameter(long in) {
129         return new Long(in);
130     }
131 
132     public static Object createParameter(float in) {
133         return new Float(in);
134     }
135 
136     public static Object createParameter(byte in) {
137         return new Byte(in);
138     }
139 
140     public static Object createParameter(short in) {
141         return new Short(in);
142     }
143 
144     public static Object createParameter(boolean in) {
145         return new Boolean(in);
146     }
147 
148     public static Object createParameter(double in) {
149         return new Double(in);
150     }
151 
152     public static String getParameter(String type, String name) {
153         if ("int".equals(type)) {
154             return "new Integer(" + name + ")";
155         } else if ("float".equals(type)) {
156             return "new Float(" + name + ")";
157         } else if ("double".equals(type)) {
158             return "new Double(" + name + ")";
159         } else if ("short".equals(type)) {
160             return "new Short(" + name + ")";
161         } else if ("boolean".equals(type)) {
162             return "new Boolean(" + name + ")";
163         } else if ("byte".equals(type)) {
164             return "new Byte(" + name + ")";
165         } else if ("long".equals(type)) {
166             return "new Long(" + name + ")";
167         } else if ("char".equals(type)) {
168             return "new Character(" + name + ")";
169         } else {
170             return name;
171         }
172     }
173 
174     public static String getReturnCode(String type, String name) {
175         if ("java.lang.Integer".equals(type) || "int".equals(type)) {
176             return "((java.lang.Integer)" + name + ").intValue()";
177         } else if ("java.lang.Float".equals(type) || "float".equals(type)) {
178             return "((java.lang.Float)" + name + ").floatValue()";
179         } else if ("java.lang.Double".equals(type) || "double".equals(type)) {
180             return "((java.lang.Double)" + name + ").doubleValue()";
181         } else if ("java.lang.Short".equals(type) || "short".equals(type)) {
182             return "((java.lang.Short)" + name + ").shortValue()";
183         } else if ("java.lang.Boolean".equals(type) || "boolean".equals(type)) {
184             return "((java.lang.Boolean)" + name + ").booleanValue()";
185         } else if ("java.lang.Byte".equals(type) || "byte".equals(type)) {
186             return "((java.lang.Byte)" + name + ").byteValue()";
187         } else if ("java.lang.Long".equals(type) || "long".equals(type)) {
188             return "((java.lang.Long)" + name + ").longValue()";
189         } else if ("java.lang.Character".equals(type) || "char".equals(type)) {
190             return "((java.lang.Character)" + name + ").charValue()";
191         } else {
192             return "(" + type + ")" + name;
193         }
194     }
195 
196     public static String getRootDirOfFile(String file) {
197         int index = file.lastIndexOf('/');
198         if (index < 0)
199             index = file.lastIndexOf('//');
200         if (index > -1) {
201             return file.substring(0, index);
202         } else {
203             return file;
204         }
205     }
206 
207     /***
208      * @param returnType
209      * @return
210      */
211     public static String jni2javaName(String returnType) {
212         if (returnType == null)
213             return null;
214         if (!returnType.startsWith("[")) {
215             return returnType;
216         } else {
217             returnType = returnType.substring(1);
218         }
219         String end = "[]";
220         while (returnType.startsWith("[")) {
221             end = end + "[]";
222             returnType = returnType.substring(1);
223         }
224         if (returnType.startsWith("B")) {
225             returnType = "byte";
226         } else if (returnType.startsWith("I")) {
227             returnType = "int";
228         } else if (returnType.startsWith("D")) {
229             returnType = "double";
230         } else if (returnType.startsWith("J")) {
231             returnType = "long";
232         } else if (returnType.startsWith("Z")) {
233             returnType = "boolean";
234         } else if (returnType.startsWith("F")) {
235             returnType = "float";
236         } else if (returnType.startsWith("S")) {
237             returnType = "short";
238         } else if (returnType.startsWith("L")) {
239             int index = returnType.indexOf(";@");
240             returnType.substring(1, index);
241         }
242         return returnType + end;
243     }
244 
245     public static String getElementValue(NodeList nodesin) {
246         if (nodesin == null || nodesin.getLength() < 1)
247             return null;
248         Node node = nodesin.item(0);
249         NodeList nodes = node.getChildNodes();
250         for (int i = 0; i < nodes.getLength(); i++) {
251             Node temp = nodes.item(i);
252             if (temp instanceof Text) {
253                 return ((Text) temp).getNodeValue();
254             }
255         }
256         return null;
257     }
258 
259     public static String javapkgToURI(String pkg) {
260         StringTokenizer tok = new StringTokenizer(pkg, ".");
261         ArrayList tokens = new ArrayList();
262         while (tok.hasMoreElements()) {
263             tokens.add(tok.nextToken());
264         }
265         int size = tokens.size();
266         if (size > 0) {
267             StringBuffer uribuf = new StringBuffer();
268             uribuf.append("http://");
269             uribuf.append((String) tokens.get(size - 1));
270             for (int i = size - 2; i >= 0; i--) {
271                 uribuf.append(".");
272                 uribuf.append((String) tokens.get(i));
273             }
274             return uribuf.toString();
275         } else {
276             return pkg;
277         }
278     }
279 
280     public static String getFileNamefromClass(J2EEWebServiceContext j2eewscontext, String qulifiedName) {
281         String outdir = j2eewscontext.getMiscInfo().getOutPutPath();
282         if (!outdir.endsWith("/"))
283             outdir = outdir + "/";
284         return outdir + qulifiedName.replace('.', '/') + ".java";
285     }
286 
287     public static void prepareTheDir(String fileName) {
288         File file = new File(fileName);
289         File parent = file.getParentFile();
290         parent.mkdirs();
291     }
292     private static DocumentBuilderFactory getDOMFactory() {
293           DocumentBuilderFactory dbf;
294           try {
295               dbf = DocumentBuilderFactory.newInstance();
296               dbf.setNamespaceAware(true);
297           } catch (Exception e) {
298 //              log.error(Messages.getMessage("exception00"), e );
299               dbf = null;
300           }
301           return (dbf);
302       }
303       
304     public static Document createDocument(InputStream in) throws GenerationFault {
305         try {
306             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
307             dbf.setNamespaceAware(true);
308             dbf.setValidating(false);
309             dbf.setExpandEntityReferences(false);
310             DocumentBuilder db = dbf.newDocumentBuilder();
311             EntityResolver er = new EntityResolver() {
312                 public InputSource resolveEntity(String publicId,
313                                                  String systemId)
314                         throws SAXException, IOException {
315                     InputStream is = null;
316                     if ("http://java.sun.com/dtd/ejb-jar_2_0.dtd".equalsIgnoreCase(systemId)) {
317                         return getInputSource(Utils.class.getClassLoader().getResourceAsStream("ejb-jar_2_0.dtd"));
318                     } else if ("http://java.sun.com/dtd/web-app_2_3.dtd".equalsIgnoreCase(systemId))
319                         return getInputSource(Utils.class.getClassLoader().getResourceAsStream("web-app_2_3.dtd"));
320                     return null;
321                 }
322 
323                 private InputSource getInputSource(InputStream is) throws IOException {
324                     if (is == null)
325                         throw new IOException("error at the project set up can not find entity");
326                     return new InputSource(is);
327                 }
328             };
329             db.setEntityResolver(er);
330             return db.parse(in);
331         } catch (Exception e) {
332             throw GenerationFault.createGenerationFault(e);
333         }
334     }
335 
336 
337 }
338