00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 package org.objectweb.proactive.core.descriptor.xml;
00032
00033 import org.objectweb.proactive.core.xml.handler.BasicUnmarshaller;
00034 import org.objectweb.proactive.core.xml.io.Attributes;
00035
00036
00043 public class PathHandler extends BasicUnmarshaller
00044 implements ProActiveDescriptorConstants {
00045
00046
00047
00048 private static final String ORIGIN_ATTRIBUTE = "origin";
00049 private static final String USER_HOME_ORIGIN = "user.home";
00050 private static final String WORKING_DIRECTORY_ORIGIN = "user.dir";
00051 private static final String FROM_CLASSPATH_ORIGIN = "user.classpath";
00052
00053
00054 private static final String DEFAULT_ORIGIN = USER_HOME_ORIGIN;
00055 private static final String VALUE_ATTRIBUTE = "value";
00056 private static final String proActiveDir = System.getProperty(
00057 "proactive.home");
00058 private static final String userDir = System.getProperty("user.dir");
00059 private static final String userHome = System.getProperty("user.home");
00060 private static final String javaHome = System.getProperty("java.home");
00061 private static final String pathSeparator = System.getProperty(
00062 "path.separator");
00063 private static final String fileSeparator = System.getProperty(
00064 "file.separator");
00065
00066
00067
00068
00069 public PathHandler() {
00070 }
00071
00072
00073
00074
00075 public Object getResultObject() throws org.xml.sax.SAXException {
00076 return super.getResultObject();
00077 }
00078
00079 public void startContextElement(String name, Attributes attributes)
00080 throws org.xml.sax.SAXException {
00081
00082
00083
00084 String origin = attributes.getValue(ORIGIN_ATTRIBUTE);
00085 if (!checkNonEmpty(origin)) {
00086 origin = DEFAULT_ORIGIN;
00087 }
00088 String value = attributes.getValue(VALUE_ATTRIBUTE);
00089
00090
00091
00092
00093
00094 if (!checkNonEmpty(value)) {
00095 throw new org.xml.sax.SAXException(
00096 "Path element defined without a value");
00097 }
00098
00099
00100 if (name.equals(ABS_PATH_TAG)) {
00101 setResultObject(value);
00102 } else if (name.equals(REL_PATH_TAG)) {
00103 if (origin.equals(USER_HOME_ORIGIN)) {
00104 setResultObject(resolvePath(userHome, value));
00105 } else if (origin.equals(WORKING_DIRECTORY_ORIGIN)) {
00106 setResultObject(resolvePath(userDir, value));
00107
00108
00109 } else if (origin.equals(FROM_CLASSPATH_ORIGIN)) {
00110 setResultObject(resolvePathFromClasspath(value));
00111 } else {
00112 throw new org.xml.sax.SAXException(
00113 "Relative Path element defined with an unknown origin=" +
00114 origin);
00115 }
00116 }
00117 }
00118
00119
00120
00121
00122 private String resolvePath(String origin, String value) {
00123 java.io.File originDirectory = new java.io.File(origin);
00124
00125
00126 if (value.startsWith("/")) {
00127 value = value.substring(1);
00128 }
00129 java.io.File file = new java.io.File(originDirectory, value);
00130 return file.getAbsolutePath();
00131 }
00132
00133 private String resolvePathFromClasspath(String value) {
00134 ClassLoader cl = this.getClass().getClassLoader();
00135 java.net.URL url = cl.getResource(value);
00136 return url.getPath();
00137 }
00138
00139
00140
00141
00142 }