Changeset 1624


Ignore:
Timestamp:
11/22/11 11:09:17 (12 years ago)
Author:
gaba
Message:
 
Location:
SMC/trunk/SMC/src/eu/clarin/cmdi/mdservice/internal
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • SMC/trunk/SMC/src/eu/clarin/cmdi/mdservice/internal/MDTransformer.java

    r1600 r1624  
    1818import java.util.Iterator;
    1919import java.util.Map;
     20import java.util.Properties;
    2021import java.util.Set;
    2122import java.util.Map.Entry;
     
    4950        private URL srcFile ;
    5051        private Map<String, String[]> params;
     52       
     53        private Properties config;
     54        private ClassLoader loader = null;
     55       
    5156               
    5257        // don't use singleton!! Bad things happen
     
    5863        //}
    5964       
     65        public ClassLoader getLoader(){
     66                if (loader == null){
     67                        return Utils.class.getClassLoader();
     68                }
     69                return loader;
     70        }
     71        public void configure(String configPath){
     72                config = Utils.createConfig(configPath, null);
     73        }
     74        public void configure(Properties _config, ClassLoader _loader){
     75                config = _config;
     76                loader = _loader;
     77        }
    6078       
    6179        public URL getSrcFile() {
     
    82100        }
    83101
     102        public Properties getConfig(){
     103                if (config == null){
     104                         return Utils.getConfig();
     105                }
     106                return config;
     107        }
    84108        /**
    85109         * Get the path to the xsl file from properties, based on the key (aka format-parameter)
     
    90114        private String getXSLPath (String key) throws NoStylesheetException {           
    91115                String xslpath = "";
    92                 String xslfilename= Utils.getConfig().getProperty("xsl." + key);
     116                String xslfilename= getConfig().getProperty("xsl." + key);
    93117               
    94118                if (xslfilename!=null) {                       
    95                         xslpath =  Utils.getConfig().getProperty("scripts.path") + xslfilename;
     119                        xslpath =  getConfig().getProperty("scripts.path") + xslfilename;
    96120                } else {
    97121                        throw new NoStylesheetException("No Stylesheet found for format-key: " + key);
     
    110134         * @throws NoStylesheetException If the stylesheet could not be located
    111135         */
     136       
    112137        private StreamSource getXSLStreamSource (String key) throws NoStylesheetException{             
    113138               
    114139                InputStream xslstream;
    115                                        
     140                       
    116141                //URL myURL = new URL (getXSLPath(key));
    117142                //xslstream = myURL.openStream();
    118143                String xslPath = getXSLPath(key);
    119                 xslstream = this.getClass().getClassLoader().getResourceAsStream(xslPath);
     144                xslstream = getLoader().getResourceAsStream(xslPath);
    120145                StreamSource streamSource = new StreamSource(xslstream);
    121146
    122                 streamSource.setSystemId(this.getClass().getClassLoader().getResource(xslPath).toString());
     147                streamSource.setSystemId(getLoader().getResource(xslPath).toString());
    123148                return streamSource ;   
    124149               
    125150        }
    126        
     151
    127152        public void setTranskey(String key){
    128153                transkey = key;
     
    131156        public String getTranskey(){
    132157                //return params.get("x-cmd-format")[0];
    133                
     158                if (transkey == null & params.containsKey("fullformat")) {
     159                        transkey = params.get("fullformat")[0] ;
     160                }
    134161                if (transkey.equals("") & params.containsKey("fullformat")) {
    135162                        transkey = params.get("fullformat")[0] ;       
  • SMC/trunk/SMC/src/eu/clarin/cmdi/mdservice/internal/Utils.java

    r1600 r1624  
    1010import java.io.InputStreamReader;
    1111import java.io.OutputStream;
     12import java.util.Enumeration;
     13import java.util.Hashtable;
     14import java.util.Iterator;
     15import java.util.Map;
    1216import java.util.Properties;
    1317
     
    5761         */
    5862        private static String config_path = "mdservice.properties";
     63        private static Hashtable<String, Properties> config_hashtable;
     64       
     65        //obsolete
    5966        private static Properties  config;     
    6067
     
    7481        public static void loadConfig(String configPath) {
    7582               
     83                config = createConfig(configPath,Utils.class.getClassLoader());
     84        }
     85       
     86        public static Properties createConfig(String configPath, ClassLoader loader) {
     87               
     88                Properties properties = null;
     89               
    7690                System.setProperty("javax.xml.transform.TransformerFactory", "net.sf.saxon.TransformerFactoryImpl");
    7791                InputStream config_file;
    78                  
     92                if (loader == null){
     93                        loader = Utils.class.getClassLoader();
     94                }
    7995                try {                   
    80                         config_file = Utils.class.getClassLoader().getResourceAsStream(configPath);
     96                        config_file = loader.getResourceAsStream(configPath);
    8197                        if (config_file == null) {
    8298                            log.error("CONFIGURATION ERROR: Properties file not found!");
    8399                        } else {
    84                                 log.debug("Reading configuration from: " + Utils.class.getClassLoader().getResource(configPath));
    85                                 config = new Properties();
    86                                 config.load(config_file);       
     100                                log.debug("Reading configuration from: " + loader.getResource(configPath));
     101                                properties = new Properties();
     102                                properties.load(config_file);   
    87103                        }
    88104                       
     
    90106                        log.error("CONFIGURATION LOAD ERROR: " + e.getLocalizedMessage());
    91107                }
    92         }
    93        
     108               
     109                return properties;
     110        }
     111       
     112
     113        public static void loadConfig(String appname, String configPath, ClassLoader loader) {
     114               
     115                if (config_hashtable == null){
     116                        config_hashtable = new Hashtable<String, Properties>();
     117                }
     118                if (!config_hashtable.containsKey(appname)){
     119                        Properties config = createConfig(configPath,loader);
     120                        config_hashtable.put(appname, config);
     121                }
     122
     123        }
     124
    94125        /**
    95126         * Returns application configuration properties.
     
    97128         * @return
    98129         */
     130        //obsolete
    99131        public static Properties getConfig() {
    100132                if (config==null)  {
     
    103135                return config;
    104136        }
    105        
     137        public static Properties getAppConfig(String appname){
     138                return config_hashtable.get(appname);
     139        }
    106140/**
    107141 * convenience function to get a config property value
     
    110144 */
    111145        public static String getConfig(String key) {
    112                         return getConfig().getProperty(key);           
    113         }
     146       
     147                        //return getConfig().getProperty(key);
     148                       
     149                        Enumeration keys = config_hashtable.keys();
     150                while (keys.hasMoreElements()) {
     151                String _key = (String) keys.nextElement();
     152                if (((Properties)config_hashtable.get(_key)).getProperty(key) != null){
     153                        return ((Properties)config_hashtable.get(_key)).getProperty(key);
     154                }
     155                }
     156                        return null;
     157                                       
     158        }
     159       
    114160       
    115161        /**
     
    143189         * @return
    144190         */
    145         public static InputStream load2Stream (String path) {
     191        public static InputStream load2Stream (String path) {   
     192                return load2Stream(path,Utils.class.getClassLoader());
     193        }
     194       
     195        public static InputStream load2Stream (String path, ClassLoader loader) {
    146196               
    147197                InputStream file=null;
    148                  
     198                if (loader == null){
     199                        loader = Utils.class.getClassLoader();
     200                }
    149201                try {                   
    150                         file = Utils.class.getClassLoader().getResourceAsStream(path);
     202                        file = loader.getResourceAsStream(path);
    151203                        if (file == null) {
    152204                            log.error("File not found!: " + path);
    153205                        } else {
    154                                 log.debug("Reading in: " + Utils.class.getClassLoader().getResource(path));
     206                                log.debug("Reading in: " + loader.getResource(path));
    155207                        }
    156208                }   catch (Exception e) {
     
    160212                return file;
    161213        }
     214       
    162215       
    163216        /**
Note: See TracChangeset for help on using the changeset viewer.