Clover coverage report - Maven Clover report
Coverage timestamp: Sun Mar 18 2007 17:43:17 CET
file stats: LOC: 143   Methods: 7
NCLOC: 121   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
FileResourceManager.java 85.7% 96.4% 100% 94.7%
coverage coverage
 1    package com.agical.rdoc.core.resourcemanager.impl;
 2   
 3    import java.io.File;
 4    import java.io.FileFilter;
 5    import java.io.FileNotFoundException;
 6    import java.io.FileOutputStream;
 7    import java.io.IOException;
 8    import java.io.InputStream;
 9    import java.io.OutputStream;
 10    import java.net.URL;
 11    import java.util.ArrayList;
 12    import java.util.Arrays;
 13    import java.util.Collection;
 14    import java.util.Collections;
 15    import java.util.HashSet;
 16    import java.util.Iterator;
 17    import java.util.Set;
 18   
 19    import com.agical.rdoc.core.converter.ConversionException;
 20    import com.agical.rdoc.core.converter.InputStreamConverter;
 21    import com.agical.rdoc.core.converter.ObjectConverter;
 22    import com.agical.rdoc.core.resourcemanager.ReadException;
 23    import com.agical.rdoc.core.resourcemanager.ResourceManager;
 24    import com.agical.rdoc.core.resourcemanager.ResourceProvider;
 25    import com.agical.rdoc.core.resourcemanager.ResourceRoot;
 26    import com.agical.rdoc.core.resourcemanager.WriteException;
 27    import com.agical.rdoc.core.target.TargetProvider;
 28    import com.agical.rmock.core.util.StringUtils;
 29   
 30    /**
 31    * @author brolund
 32    *
 33    * (c) 2005 Agical AB
 34    */
 35    public class FileResourceManager implements ResourceManager {
 36   
 37    private TargetProvider targetProvider = TargetProvider.NULL;
 38    private ResourceProvider resourceProvider;
 39   
 40  2 public void setTargetProvider(TargetProvider targetProvider) {
 41  2 this.targetProvider = targetProvider;
 42    }
 43   
 44  9 public void setResourceProvider(ResourceProvider resourceProvider) {
 45  9 this.resourceProvider = resourceProvider;
 46    }
 47   
 48  17 public Object read(InputStreamConverter inputStreamConverter, String fileName) throws ReadException, ConversionException {
 49  17 Object object = null;
 50  17 ResourceRoot[] resourceRoots = resourceProvider.getRoots();
 51  17 Collection triedUrls = new ArrayList();
 52  17 for (int i = 0; i < resourceRoots.length; i++) {
 53  55 URL base = resourceRoots[i].getUrl();
 54  55 String fullName = base + "/" + fileName;
 55  55 try {
 56  55 object = readStream(inputStreamConverter, fullName);
 57  15 return object;
 58    } catch (IOException e) {
 59    // log tried url and continue
 60    }
 61  39 triedUrls.add( fullName );
 62    }
 63  1 StringBuffer stringBuffer = new StringBuffer( "Could not find requested resource. Tried the following urls:\n" );
 64  1 for (Iterator iter = triedUrls.iterator(); iter.hasNext();) {
 65  1 stringBuffer.append( iter.next() ).append( "\n" );
 66    }
 67  1 throw new ReadException( stringBuffer.toString() );
 68    }
 69   
 70  55 private Object readStream(InputStreamConverter inputStreamConverter, String fileName) throws IOException {
 71  55 Object object = null;
 72  55 InputStream inputStream = null;
 73  55 try {
 74  55 URL file = new URL( fileName );
 75  55 inputStream = file.openStream();
 76  16 try {
 77  16 object = inputStreamConverter.convert( inputStream );
 78    }
 79    catch(ConversionException e) {
 80  1 throw new ConversionException("Could not convert "+fileName, e);
 81    }
 82    } finally {
 83  55 if( inputStream != null ) {
 84  16 try {
 85  16 inputStream.close();
 86    } catch (IOException e) {
 87    // ignore
 88    }
 89    }
 90    }
 91  15 return object;
 92    }
 93   
 94  1 public void write(ObjectConverter objectConverter, Object object, String filename) throws ConversionException, WriteException {
 95  1 File file = new File( targetProvider.getTarget(), filename );
 96   
 97  1 OutputStream outputStream = null;
 98  1 try {
 99  1 file.getParentFile().mkdirs();
 100  1 outputStream = new FileOutputStream( file );
 101  1 objectConverter.convert( object, outputStream );
 102    } catch (FileNotFoundException e) {
 103  0 throw new ConversionException( "Could not convert " + object + " to stream <"+e.getMessage()+">");
 104    } catch (IOException e) {
 105  0 throw new WriteException( "Cannot write to " + file );
 106    } finally {
 107  1 if( outputStream != null ) {
 108  1 try {
 109  1 outputStream.close();
 110    } catch (IOException e) {
 111    // ignore
 112    }
 113    }
 114    }
 115    }
 116   
 117  1 public String[] list(String directory) {
 118  1 Set resourceNames = new HashSet();
 119  1 ResourceRoot[] roots = resourceProvider.getRoots();
 120  1 for (int i = 0; i < roots.length; i++) {
 121  1 ResourceRoot root = roots[i];
 122  1 String pathName = root.getUrl().getPath();
 123  1 pathName = StringUtils.replace( pathName, "%20", " " );
 124  1 File subdir = new File( pathName, directory);
 125  1 File[] files = subdir.listFiles(new FileFilter() {
 126  1 public boolean accept(File file) {
 127  1 return file.isFile();
 128    }
 129    });
 130  1 resourceNames.addAll(files == null ? Collections.EMPTY_LIST : Arrays.asList(files));
 131    }
 132   
 133  1 String resources[] = new String[resourceNames.size()];
 134  1 int i = 0;
 135  1 for (Iterator iter = resourceNames.iterator(); iter.hasNext();) {
 136  1 File resourceFile = (File) iter.next();
 137  1 resources[i++]=resourceFile.getName();
 138    }
 139  1 return resources;
 140    }
 141   
 142   
 143    }