KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icesoft > jsfmeta > util > GeneratorUtil


1
2 /*
3  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4  *
5  * "The contents of this file are subject to the Mozilla Public License
6  * Version 1.1 (the "License"); you may not use this file except in
7  * compliance with the License. You may obtain a copy of the License at
8  * http://www.mozilla.org/MPL/
9  *
10  * Software distributed under the License is distributed on an "AS IS"
11  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
12  * License for the specific language governing rights and limitations under
13  * the License.
14  *
15  * The Original Code is ICEfaces 1.5 open source software code, released
16  * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
17  * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
18  * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
19  *
20  * Contributor(s): _____________________.
21  *
22  * Alternatively, the contents of this file may be used under the terms of
23  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
24  * License), in which case the provisions of the LGPL License are
25  * applicable instead of those above. If you wish to allow use of your
26  * version of this file only under the terms of the LGPL License and not to
27  * allow others to use your version of this file under the MPL, indicate
28  * your decision by deleting the provisions above and replace them with
29  * the notice and other provisions required by the LGPL License. If you do
30  * not delete the provisions above, a recipient may use your version of
31  * this file under either the MPL or the LGPL License."
32  *
33  */

34
35 package com.icesoft.jsfmeta.util;
36
37 import java.io.File JavaDoc;
38 import java.io.FileNotFoundException JavaDoc;
39 import java.net.URL JavaDoc;
40
41 public class GeneratorUtil {
42     
43     private static String JavaDoc WORKING_FOLDER;
44     
45     public GeneratorUtil() {
46     }
47     
48     static{
49         String JavaDoc result = ".";
50         try {
51             ClassLoader JavaDoc classLoader = Thread.currentThread()
52             .getContextClassLoader();
53             URL JavaDoc localUrl = classLoader.getResource(".");
54             if(localUrl != null){
55                 result = convertFileUrlToPath(localUrl);
56             }
57             
58         } catch (Exception JavaDoc ex) {
59             ex.printStackTrace();
60         }
61         WORKING_FOLDER = result;
62     }
63     
64     public static String JavaDoc getWorkingFolder(){
65         return WORKING_FOLDER;
66     }
67     
68     public static File JavaDoc getDestFolder(String JavaDoc path) throws FileNotFoundException JavaDoc{
69         File JavaDoc file = new File JavaDoc(path);
70         if(!file.exists()){
71             if(!file.mkdirs()){
72                 throw new FileNotFoundException JavaDoc(file.getPath());
73             }
74         }
75         
76         return file;
77     }
78     
79     /**
80      * Kind of hack-ish attempt at solving problem that if the directory,
81      * where we're building the component-metadata in, has special
82      * characters in its path, like spaces, then the URL to it will be
83      * escaped, which will be interpretted as a different directory,
84      * unless we unescape it.
85      */

86     private static String JavaDoc convertFileUrlToPath(URL JavaDoc url) {
87         String JavaDoc path = url.getPath();
88         if( url.toExternalForm().startsWith("file:") ) {
89             StringBuffer JavaDoc sb = new StringBuffer JavaDoc( path.length() );
90             int pathLength = path.length();
91             for(int i = 0; i < pathLength;) {
92                 char c = path.charAt(i);
93                 if( c == '%' ) {
94                     if( (i+1) < pathLength && isHexDigit(path.charAt(i+1)) ) {
95                         int increment = 2;
96                         if( (i+2) < pathLength && isHexDigit(path.charAt(i+2)) )
97                             increment++;
98                         try {
99                             char unescaped = (char) Integer.parseInt(
100                                 path.substring(i+1, i+increment), 16);
101                             
102                             sb.append( unescaped );
103                             i += increment;
104                             continue;
105                         } catch(NumberFormatException JavaDoc nfe) {
106                             // Not a valid hex escape, so just fall through,
107
// and append it to the path
108
}
109                     }
110                 }
111                 sb.append( c );
112                 i++;
113             }
114             path = sb.toString();
115         }
116         return path;
117     }
118     
119     private static boolean isHexDigit(char c) {
120         return ( (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') );
121     }
122 }
Popular Tags