1 26 package org.objectweb.util.explorer.context.lib; 27 28 import java.io.FileInputStream ; 29 import java.io.FileNotFoundException ; 30 import java.io.IOException ; 31 import java.io.InputStream ; 32 import java.io.InputStreamReader ; 33 import java.net.MalformedURLException ; 34 import java.net.URL ; 35 import java.util.Iterator ; 36 import java.util.List ; 37 38 import org.objectweb.fractal.api.NoSuchInterfaceException; 39 import org.objectweb.util.explorer.context.api.ContextParser; 40 import org.objectweb.util.explorer.context.api.ContextPropertiesFeeder; 41 import org.objectweb.util.explorer.contextConfig.Context; 42 import org.objectweb.util.explorer.contextConfig.ContextImpl; 43 import org.objectweb.util.explorer.contextConfig.Decoder; 44 import org.objectweb.util.explorer.contextConfig.Decoders; 45 import org.objectweb.util.explorer.core.common.lib.BindingFeature; 46 import org.objectweb.util.explorer.core.common.lib.ClassResolver; 47 48 56 public class Parser 57 extends BindingFeature 58 implements ContextParser 59 { 60 61 67 73 79 89 protected InputStream getInputStream(String resource){ 90 InputStream inputStream = ClassResolver.getResourceAsStream(resource); 92 if(inputStream==null) { 93 try { 94 inputStream = new URL (resource).openStream(); 96 } catch (MalformedURLException e) { 97 try { 98 inputStream = new FileInputStream (resource); 100 } catch (FileNotFoundException e1) { 101 getTrace().info(resource + ": File not found on the file system!"); 102 return null; 103 } 104 } catch (IOException e) { 105 getTrace().info(resource + ": Failed to open stream on resource!"); 106 return null; 107 } 108 } 109 return inputStream; 110 } 111 112 116 protected void buildDecoder(Decoders decoders) { 117 if (decoders != null) { 118 List decoderList = decoders.getDecoderList(); 119 Iterator it = decoderList.iterator(); 120 while (it.hasNext()) { 121 Decoder decoderNode = (Decoder) it.next(); 122 String className = decoderNode.getJavaClass(); 123 try { 124 Class c = ClassResolver.resolve(className); 125 if (c!=null && org.objectweb.util.explorer.context.api.Decoder.class.isAssignableFrom(c)){ 126 getPropertiesFeeder().addDecoder((org.objectweb.util.explorer.context.api.Decoder)c.newInstance()); 127 } 128 } catch (ClassNotFoundException e) { 129 getTrace().warn(className + " : Class not found !"); 130 } catch (Exception e) { 131 getTrace().warn(className + " : Class not found !"); 132 } 133 } 134 } 135 } 136 137 140 protected void parse(InputStream inputStream){ 141 if(inputStream!=null){ 142 try { 143 Context context = ContextImpl.unmarshal(new InputStreamReader (inputStream),false); 145 if (context != null){ 146 buildDecoder(context.getDecoders()); 147 } 148 } catch (java.io.IOException e) { 149 getTrace().info("Error during parsing!\n" + e.getMessage()); 150 } 151 } 152 } 153 154 protected ContextPropertiesFeeder getPropertiesFeeder(){ 155 try { 156 return (ContextPropertiesFeeder)lookupFc(ContextPropertiesFeeder.CONTEXT_PROPERTIES_FEEDER); 157 } catch (NoSuchInterfaceException e) { 158 getTrace().warn(ContextPropertiesFeeder.CONTEXT_PROPERTIES_FEEDER + ": interface not found!"); 159 return null; 160 } 161 } 162 163 169 172 public String [] clientFc() { 173 return new String []{ContextPropertiesFeeder.CONTEXT_PROPERTIES_FEEDER}; 174 } 175 176 182 185 public void parse(String fileUrl) { 186 if(fileUrl!=null && !fileUrl.equals("")) 187 parse(getInputStream(fileUrl)); 188 } 189 190 } 191 192 193 | Popular Tags |