1 28 29 package org.objectweb.util.explorer.parser.lib; 30 31 import java.io.File ; 32 import java.io.FileInputStream ; 33 import java.io.FileNotFoundException ; 34 import java.io.IOException ; 35 import java.io.InputStream ; 36 import java.io.InputStreamReader ; 37 import java.net.MalformedURLException ; 38 import java.net.URL ; 39 40 import org.objectweb.fractal.api.NoSuchInterfaceException; 41 import org.objectweb.fractal.api.control.BindingController; 42 import org.objectweb.fractal.api.control.IllegalBindingException; 43 import org.objectweb.fractal.api.control.IllegalLifeCycleException; 44 import org.objectweb.util.explorer.core.common.lib.ClassResolver; 45 import org.objectweb.util.explorer.core.common.lib.XMLFileFilter; 46 import org.objectweb.util.explorer.explorerConfig.beans.ExplorerBean; 47 import org.objectweb.util.explorer.explorerConfig.beans.ExplorerBeanImpl; 48 import org.objectweb.util.explorer.parser.api.ExplorerParser; 49 50 58 public class ExplorerParserLoader 59 extends AbstractParser 60 implements BindingController 61 { 62 63 69 70 protected ExplorerParser dispatcher_; 71 72 78 84 94 protected InputStream getInputStream(String resource){ 95 InputStream inputStream = ClassResolver.getResourceAsStream(resource); 97 if(inputStream==null) { 98 try { 99 inputStream = new URL (resource).openStream(); 101 } catch (MalformedURLException e) { 102 try { 103 inputStream = new FileInputStream (resource); 105 } catch (FileNotFoundException e1) { 106 getTrace().info(resource + ": File not found on the file system!"); 107 return null; 108 } 109 } catch (IOException e) { 110 getTrace().info(resource + ": Failed to open stream on resource!"); 111 return null; 112 } 113 } 114 return inputStream; 115 } 116 117 120 protected void parse(InputStream inputStream){ 121 if(inputStream!=null){ 122 try { 123 ExplorerBean explorer = ExplorerBeanImpl.unmarshalBean(new InputStreamReader (inputStream),false); 125 if (explorer != null){ 126 dispatcher_.parseExplorer(explorer); 127 } 128 } catch (java.io.IOException e) { 129 getTrace().info("Error during parsing!\n" + e.getMessage()); 130 } 131 } 132 } 133 134 138 protected void parse(String resource){ 139 File file = new File (resource); 141 if (file.exists() && file.isDirectory()) { 142 File [] files = file.listFiles(new XMLFileFilter()); 143 for (int i = 0; i < files.length; i++) { 144 parse(files[i].toString()); 145 } 146 } else { 147 parse(getInputStream(resource)); 148 } 149 } 150 151 157 160 public void bindFc(String itfName, Object itfValue) 161 throws NoSuchInterfaceException, IllegalBindingException, 162 IllegalLifeCycleException { 163 if(itfName.equals(ExplorerParser.EXPLORER_PARSER)) 164 dispatcher_ = (ExplorerParser)itfValue; 165 } 166 169 public Object lookupFc(String itfName) throws NoSuchInterfaceException { 170 if(itfName.equals(ExplorerParser.EXPLORER_PARSER)) 171 return dispatcher_; 172 return null; 173 } 174 177 public void unbindFc(String itfName) throws NoSuchInterfaceException, 178 IllegalBindingException, IllegalLifeCycleException { 179 if(itfName.equals(ExplorerParser.EXPLORER_PARSER)) 180 dispatcher_ = null; 181 } 182 183 186 public String [] listFc(){ 187 return new String []{ExplorerParser.EXPLORER_PARSER}; 188 } 189 190 197 200 public void parse() { 201 int i = 0; 205 while(i<urlFiles_.size()){ 206 parse((String )urlFiles_.get(i)); 207 i++; 208 } 209 urlFiles_.clear(); 210 } 211 212 } 213 | Popular Tags |