001: /*
002: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
003: *
004: * "The contents of this file are subject to the Mozilla Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License at
007: * http://www.mozilla.org/MPL/
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
011: * License for the specific language governing rights and limitations under
012: * the License.
013: *
014: * The Original Code is ICEfaces 1.5 open source software code, released
015: * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
016: * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
017: * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
018: *
019: * Contributor(s): _____________________.
020: *
021: * Alternatively, the contents of this file may be used under the terms of
022: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
023: * License), in which case the provisions of the LGPL License are
024: * applicable instead of those above. If you wish to allow use of your
025: * version of this file only under the terms of the LGPL License and not to
026: * allow others to use your version of this file under the MPL, indicate
027: * your decision by deleting the provisions above and replace them with
028: * the notice and other provisions required by the LGPL License. If you do
029: * not delete the provisions above, a recipient may use your version of
030: * this file under either the MPL or the LGPL License."
031: *
032: */
033:
034: package com.icesoft.jsfmeta.util;
035:
036: import java.io.File;
037: import java.io.FileNotFoundException;
038: import java.net.URL;
039:
040: public class GeneratorUtil {
041:
042: private static String WORKING_FOLDER;
043:
044: public GeneratorUtil() {
045: }
046:
047: static {
048: String result = ".";
049: try {
050: ClassLoader classLoader = Thread.currentThread()
051: .getContextClassLoader();
052: URL localUrl = classLoader.getResource(".");
053: if (localUrl != null) {
054: result = convertFileUrlToPath(localUrl);
055: }
056:
057: } catch (Exception ex) {
058: ex.printStackTrace();
059: }
060: WORKING_FOLDER = result;
061: }
062:
063: public static String getWorkingFolder() {
064: return WORKING_FOLDER;
065: }
066:
067: public static File getDestFolder(String path)
068: throws FileNotFoundException {
069: File file = new File(path);
070: if (!file.exists()) {
071: if (!file.mkdirs()) {
072: throw new FileNotFoundException(file.getPath());
073: }
074: }
075:
076: return file;
077: }
078:
079: /**
080: * Kind of hack-ish attempt at solving problem that if the directory,
081: * where we're building the component-metadata in, has special
082: * characters in its path, like spaces, then the URL to it will be
083: * escaped, which will be interpretted as a different directory,
084: * unless we unescape it.
085: */
086: private static String convertFileUrlToPath(URL url) {
087: String path = url.getPath();
088: if (url.toExternalForm().startsWith("file:")) {
089: StringBuffer sb = new StringBuffer(path.length());
090: int pathLength = path.length();
091: for (int i = 0; i < pathLength;) {
092: char c = path.charAt(i);
093: if (c == '%') {
094: if ((i + 1) < pathLength
095: && isHexDigit(path.charAt(i + 1))) {
096: int increment = 2;
097: if ((i + 2) < pathLength
098: && isHexDigit(path.charAt(i + 2)))
099: increment++;
100: try {
101: char unescaped = (char) Integer.parseInt(
102: path
103: .substring(i + 1, i
104: + increment), 16);
105:
106: sb.append(unescaped);
107: i += increment;
108: continue;
109: } catch (NumberFormatException nfe) {
110: // Not a valid hex escape, so just fall through,
111: // and append it to the path
112: }
113: }
114: }
115: sb.append(c);
116: i++;
117: }
118: path = sb.toString();
119: }
120: return path;
121: }
122:
123: private static boolean isHexDigit(char c) {
124: return ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'));
125: }
126: }
|