org/objectweb/proactive/core/descriptor/xml/PathHandler.java

00001 /* 
00002  * ################################################################
00003  * 
00004  * ProActive: The Java(TM) library for Parallel, Distributed, 
00005  *            Concurrent computing with Security and Mobility
00006  * 
00007  * Copyright (C) 1997-2007 INRIA/University of Nice-Sophia Antipolis
00008  * Contact: proactive@objectweb.org
00009  * 
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or any later version.
00014  *  
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  * 
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with this library; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00023  * USA
00024  *  
00025  *  Initial developer(s):               The ProActive Team
00026  *                        http://www.inria.fr/oasis/ProActive/contacts.html
00027  *  Contributor(s): 
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     //  ----- PRIVATE MEMBERS -----------------------------------------------------------------------------------
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     // private static final String PROACTIVE_ORIGIN = "proactive.home";
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     //  ----- CONSTRUCTORS -----------------------------------------------------------------------------------
00068     //
00069     public PathHandler() {
00070     }
00071 
00072     //
00073     //  ----- PUBLIC METHODS -----------------------------------------------------------------------------------
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         // read from XML
00082         //    String type = attributes.getValue(TYPE_ATTRIBUTE);
00083         //    if (! checkNonEmpty(type)) type = DEFAULT_TYPE;
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         //        if (logger.isDebugEnabled()) {
00091         //            logger.debug("Found Path Element origin=" + origin + " value=" +
00092         //                value);
00093         //        }
00094         if (!checkNonEmpty(value)) {
00095             throw new org.xml.sax.SAXException(
00096                 "Path element defined without a value");
00097         }
00098 
00099         // build the associated string
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                 //            } else if (origin.equals(PROACTIVE_ORIGIN)) {
00108                 //                setResultObject(resolvePath(proActiveDir, value));
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     //  ----- PRIVATE METHODS -----------------------------------------------------------------------------------
00121     //
00122     private String resolvePath(String origin, String value) {
00123         java.io.File originDirectory = new java.io.File(origin);
00124 
00125         // in case of relative path, if the user put a / then remove it transparently
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     //  ----- INNER CLASSES -----------------------------------------------------------------------------------
00141     //
00142 }

Generated on Mon Jan 22 15:16:07 2007 for ProActive by  doxygen 1.5.1