1 31 package org.objectweb.proactive.core.descriptor.xml; 32 33 import org.objectweb.proactive.core.xml.handler.BasicUnmarshaller; 34 import org.objectweb.proactive.core.xml.io.Attributes; 35 36 37 43 public class PathHandler extends BasicUnmarshaller implements ProActiveDescriptorConstants { 44 48 private static final String ORIGIN_ATTRIBUTE = "origin"; 49 private static final String USER_HOME_ORIGIN = "user.home"; 50 private static final String WORKING_DIRECTORY_ORIGIN = "user.dir"; 51 private static final String FROM_CLASSPATH_ORIGIN = "user.classpath"; 52 private static final String DEFAULT_ORIGIN = USER_HOME_ORIGIN; 54 private static final String VALUE_ATTRIBUTE = "value"; 55 private static final String proActiveDir = System.getProperty( 56 "proactive.home"); 57 private static final String userDir = System.getProperty("user.dir"); 58 private static final String userHome = System.getProperty("user.home"); 59 private static final String javaHome = System.getProperty("java.home"); 60 private static final String pathSeparator = System.getProperty( 61 "path.separator"); 62 private static final String fileSeparator = System.getProperty( 63 "file.separator"); 64 65 67 public PathHandler() { 71 } 72 73 public Object getResultObject() throws org.xml.sax.SAXException { 77 return super.getResultObject(); 78 } 79 80 public void startContextElement(String name, Attributes attributes) 81 throws org.xml.sax.SAXException { 82 String origin = attributes.getValue(ORIGIN_ATTRIBUTE); 86 if (!checkNonEmpty(origin)) { 87 origin = DEFAULT_ORIGIN; 88 } 89 String value = attributes.getValue(VALUE_ATTRIBUTE); 90 if (!checkNonEmpty(value)) { 95 throw new org.xml.sax.SAXException ( 96 "Path element defined without a value"); 97 } 98 99 if (name.equals(ABS_PATH_TAG)) { 101 setResultObject(value); 102 } else if (name.equals(REL_PATH_TAG)) { 103 if (origin.equals(USER_HOME_ORIGIN)) { 104 setResultObject(resolvePath(userHome, value)); 105 } else if (origin.equals(WORKING_DIRECTORY_ORIGIN)) { 106 setResultObject(resolvePath(userDir, value)); 107 } else if (origin.equals(FROM_CLASSPATH_ORIGIN)) { 110 setResultObject(resolvePathFromClasspath(value)); 111 } else { 112 throw new org.xml.sax.SAXException ( 113 "Relative Path element defined with an unknown origin=" + 114 origin); 115 } 116 } 117 } 118 119 private String resolvePath(String origin, String value) { 123 java.io.File originDirectory = new java.io.File (origin); 124 if(value.startsWith("/")){ 126 value = value.substring(1); 127 } 128 java.io.File file = new java.io.File (originDirectory, value); 129 return file.getAbsolutePath(); 130 } 131 132 private String resolvePathFromClasspath(String value) { 133 ClassLoader cl = this.getClass().getClassLoader(); 134 java.net.URL url = cl.getResource(value); 135 return url.getPath(); 136 } 137 138 } 142 | Popular Tags |