1 57 58 package org.apache.commons.discovery.resource.names; 59 60 import java.io.BufferedReader ; 61 import java.io.IOException ; 62 import java.io.InputStream ; 63 import java.io.InputStreamReader ; 64 65 import org.apache.commons.discovery.Resource; 66 import org.apache.commons.discovery.ResourceDiscover; 67 import org.apache.commons.discovery.ResourceListener; 68 import org.apache.commons.discovery.ResourceNameDiscover; 69 import org.apache.commons.discovery.ResourceNameListener; 70 import org.apache.commons.discovery.log.DiscoveryLogFactory; 71 import org.apache.commons.discovery.resource.ClassLoaders; 72 import org.apache.commons.discovery.resource.DiscoverResources; 73 import org.apache.commons.logging.Log; 74 75 76 77 93 public class DiscoverNamesInFile extends ResourceNameDiscoverImpl 94 implements ResourceNameDiscover, ResourceNameListener, ResourceListener 95 { 96 private static Log log = DiscoveryLogFactory.newLog(DiscoverNamesInFile.class); 97 public static void setLog(Log _log) { 98 log = _log; 99 } 100 101 private ResourceDiscover discoverResources; 102 103 106 public DiscoverNamesInFile() { 107 this.discoverResources = new DiscoverResources(); 108 this.discoverResources.setListener(this); 109 } 110 111 114 public DiscoverNamesInFile(ClassLoaders loaders) { 115 this.discoverResources = new DiscoverResources(loaders); 116 this.discoverResources.setListener(this); 117 } 118 119 122 public void setClassLoaders(ClassLoaders loaders) { 123 discoverResources.setClassLoaders(loaders); 124 } 125 126 128 public boolean find(String fileName) { 129 if (log.isDebugEnabled()) 130 log.debug("find: fileName='" + fileName + "'"); 131 132 return discoverResources.find(fileName); 133 } 134 135 public boolean found(String resourceName) { 136 return find(resourceName); 137 } 138 139 143 public boolean found(Resource info) { 144 InputStream is = info.getResourceAsStream(); 145 146 if( is != null ) { 147 try { 148 try { 149 BufferedReader rd; 153 try { 154 rd = new BufferedReader (new InputStreamReader (is, "UTF-8")); 155 } catch (java.io.UnsupportedEncodingException e) { 156 rd = new BufferedReader (new InputStreamReader (is)); 157 } 158 159 try { 160 String resourceName; 161 while( (resourceName = rd.readLine()) != null) { 162 int idx = resourceName.indexOf('#'); 163 if (idx >= 0) { 164 resourceName = resourceName.substring(0, idx); 165 } 166 resourceName = resourceName.trim(); 167 168 if (resourceName.length() != 0) { 169 if (log.isDebugEnabled()) 170 log.debug("readResourceNames: found '" + resourceName + "'"); 171 172 if (!notifyListener(resourceName)) { 173 return false; 174 } 175 } 176 } 177 } finally { 178 rd.close(); 179 } 180 } finally { 181 is.close(); 182 } 183 } catch (IOException e) { 184 } 186 } 187 188 return true; 189 } 190 } 191 | Popular Tags |