001: /*
002: * Copyright 2006 Luca Garulli (luca.garulli@assetdata.it)
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.romaframework.core;
018:
019: import java.net.MalformedURLException;
020: import java.net.URL;
021:
022: import org.romaframework.core.config.RomaApplicationContext;
023:
024: public class Utility {
025:
026: private static final String ASPECT = "aspect";
027: public static final String ROMA_PACKAGE = "org.romaframework";
028: public static final char PACKAGE_SEPARATOR = '.';
029: public static final String PACKAGE_SEPARATOR_STRING = ".";
030: public static final char PATH_SEPARATOR = '/';
031: public static final String PATH_SEPARATOR_STRING = "/";
032: public static final char WINDOWS_SEPARATOR = '\\';
033: public static final String CLASSPATH_PREFIX = "classpath:";
034: public static final String FILE_PREFIX = "file:";
035:
036: public static URL loadURL(String iPath) {
037: URL url = null;
038: try {
039: if (iPath.startsWith(CLASSPATH_PREFIX)) {
040: // BUILD URL FROM CLASS LOADER
041: url = Utility.class.getClassLoader().getResource(
042: iPath.substring(CLASSPATH_PREFIX.length()));
043: } else if (iPath.startsWith(FILE_PREFIX)) {
044: // LOAD FILE DIRECTLY
045: url = new URL(iPath);
046: }
047: } catch (MalformedURLException e) {
048: }
049: return url;
050: }
051:
052: public static String getClearName(String iJavaName) {
053: StringBuffer buffer = new StringBuffer();
054:
055: char c;
056: buffer.append(Character.toUpperCase(iJavaName.charAt(0)));
057: for (int i = 1; i < iJavaName.length(); ++i) {
058: c = iJavaName.charAt(i);
059:
060: if (Character.isUpperCase(c))
061: buffer.append(' ');
062:
063: buffer.append(c);
064: }
065:
066: return buffer.toString();
067: }
068:
069: public static boolean isRomaClass(Class iClass) {
070: return iClass.getName().startsWith(ROMA_PACKAGE);
071: }
072:
073: public static String[] getResourceNamesFirstSeparator(
074: String iResourceName, String iSeparator,
075: String iDefaultPrefixName) {
076: if (iResourceName == null)
077: return null;
078:
079: String names[] = new String[2];
080: int pos = iResourceName.indexOf(iSeparator);
081: names[0] = pos > -1 ? iResourceName.substring(0, pos)
082: : iDefaultPrefixName;
083:
084: names[1] = iResourceName.substring(pos + 1);
085: return names;
086: }
087:
088: public static String[] getResourceNamesLastSeparator(
089: String iResourceName, String iSeparator,
090: String iDefaultPrefixName) {
091: if (iResourceName == null)
092: return null;
093:
094: String names[] = new String[2];
095: int pos = iResourceName.lastIndexOf(iSeparator);
096: names[0] = pos > -1 ? iResourceName.substring(0, pos)
097: : iDefaultPrefixName;
098:
099: names[1] = iResourceName.substring(pos + 1);
100: return names;
101: }
102:
103: /**
104: * Get the full package of aspect inside user application.
105: *
106: * @param iAspectName
107: * Aspect name
108: * @return
109: */
110: public static String getApplicationAspectPackage(String iAspectName) {
111: return RomaApplicationContext.getInstance()
112: .getApplicationPackage()
113: + PACKAGE_SEPARATOR + iAspectName;
114: }
115:
116: /**
117: * Get the full package of aspect inside Roma.
118: *
119: * @param iAspectName
120: * Aspect name
121: * @return
122: */
123: public static String getRomaAspectPackage(String iAspectName) {
124: return ROMA_PACKAGE + PACKAGE_SEPARATOR + ASPECT
125: + PACKAGE_SEPARATOR + iAspectName;
126: }
127:
128: /**
129: * Convert a package path style in resource path style. If the path starts with ".", then it means relative path.
130: *
131: * @param iClassPath
132: * @return
133: */
134: public static String getAbsoluteResourcePath(String iClassPath) {
135: int pos = iClassPath.indexOf(FILE_PREFIX);
136: String prefix = pos > -1 ? iClassPath.substring(0, pos
137: + FILE_PREFIX.length()) : "";
138: String data = pos > -1 ? iClassPath.substring(pos
139: + FILE_PREFIX.length()) : iClassPath;
140:
141: if (data.startsWith("."))
142: // RELATIVE PATH: CHANGE IN ABSOLUTE
143: data = RomaApplicationContext.getApplicationPath()
144: + data.substring(1);
145:
146: if (prefix.length() > 0 && data.charAt(0) != PATH_SEPARATOR)
147: data = PATH_SEPARATOR + data;
148:
149: data = data.replace(PACKAGE_SEPARATOR, PATH_SEPARATOR);
150:
151: return prefix + data;
152: }
153:
154: /**
155: * Convert a package path style in resource path style.
156: *
157: * @param iClassPath
158: * @return
159: */
160: public static String getPackagePath(String iResourcePath) {
161: String path = iResourcePath.charAt(0) == PATH_SEPARATOR ? iResourcePath
162: .substring(1)
163: : iResourcePath;
164: return path.replace(PATH_SEPARATOR, PACKAGE_SEPARATOR);
165: }
166:
167: public static String getResourcePath(String iClassPath) {
168: return iClassPath.replace(PACKAGE_SEPARATOR, PATH_SEPARATOR);
169: }
170:
171: /**
172: * Convert a package path style in resource path style.
173: *
174: * @param iResourcePath
175: * @return
176: */
177: public static String getUniversalResourcePath(String iResourcePath) {
178: return iResourcePath.replace(WINDOWS_SEPARATOR, PATH_SEPARATOR);
179: }
180:
181: public static String getContainerFile(String filePath) {
182: return filePath;
183: }
184:
185: public static String removeLastSeparatorIfAny(String entryName) {
186: if (entryName.endsWith(PATH_SEPARATOR_STRING))
187: entryName = entryName.substring(0, entryName.length() - 1);
188: return entryName;
189: }
190:
191: public static String filePatternToRegExp(String iPattern) {
192: iPattern = iPattern.replace(".", "\\.");
193: iPattern = iPattern.replace("*", ".*");
194: return iPattern;
195: }
196: }
|