1 26 27 package org.objectweb.util.browser.core.xmlparser; 28 29 30 import org.objectweb.util.browser.core.api.ContextContainer; 31 import org.objectweb.util.browser.core.api.ContextProperty; 32 import org.objectweb.util.browser.core.api.DecoderFactory; 33 import org.objectweb.util.browser.core.common.ClassResolver; 34 import org.objectweb.util.browser.contextConfig.Context; 35 import org.objectweb.util.browser.contextConfig.ContextUnmarshaller; 36 import org.objectweb.util.browser.contextConfig.Decoders; 37 import org.objectweb.util.browser.contextConfig.Decoder; 38 import org.objectweb.util.browser.core.naming.DefaultContextContainer; 39 import org.objectweb.util.browser.core.naming.DefaultDecoderFactory; 40 41 42 import java.io.File ; 43 import java.io.FileInputStream ; 44 import java.io.FileNotFoundException ; 45 import java.io.IOException ; 46 import java.io.InputStream ; 47 import java.util.List ; 48 import java.util.Iterator ; 49 50 56 public class ContextXMLParser implements XMLParser, ContextProperty { 57 58 64 65 protected org.objectweb.util.browser.core.api.Decoder decoder_; 66 67 73 77 protected void buildDecoder(Decoders decoders) { 78 if (decoders != null) { 79 ContextContainer context = new DefaultContextContainer(); 80 List decoderList = decoders.getDecoderList(); 81 Iterator it = decoderList.iterator(); 82 while (it.hasNext()) { 83 Decoder decoderNode = (Decoder) it.next(); 84 String className = decoderNode.getJavaClass(); 85 try { 86 Class c = ClassResolver.resolve(className); 87 context.addEntry(className, c); 88 } catch (ClassNotFoundException e) { 89 System.out.println("Error : " + className + " : Class not found !"); 90 } 91 } 92 DecoderFactory decoderFactory = new DefaultDecoderFactory(); 93 decoder_ = decoderFactory.create(context); 94 } 95 } 96 97 103 106 public org.objectweb.util.browser.core.api.Decoder getDecoder() { 107 return decoder_; 108 } 109 110 113 public void setPropertyFile(String path) { 114 InputStream inputStream = null; 115 inputStream = ClassResolver.getResourceAsStream(path); 117 if(inputStream==null){ 118 try { 120 File file = new File (path); 121 if (file != null && !file.isDirectory()) { 122 try { 123 inputStream = new FileInputStream (file); 124 } catch (java.io.FileNotFoundException e2) { 125 System.out.println("Error : " + file + " : File not found !"); 126 } 127 } 128 } catch(Exception e1) { 129 System.out.println("[ContextXMLParser.setPropertyFile] " + path + ": Resource not found ! "); 130 } 131 } 132 if(inputStream!=null){ 133 try { 134 Context context = ContextUnmarshaller.unmarshal(inputStream); 135 if (context != null) { 136 buildDecoder(context.getDecoders()); 137 } 138 } catch (FileNotFoundException e) { 139 System.out.println("Error : " + path + " : File not found !"); 140 } catch (IOException e) { 141 System.out.println("Error : " + path + " : I/O Exception !"); 142 } 143 } 144 } 145 146 151 public void setPropertyFile(String [] files) { 152 for (int i = 0; i < files.length; i++) 153 setPropertyFile(files[i]); 154 } 155 156 } 157 | Popular Tags |