EMMA Coverage Report (generated Wed Dec 16 17:19:59 CET 2009)
[all classes][org.sourceforge.xradar.util]

COVERAGE SUMMARY FOR SOURCE FILE [FileUtils.java]

nameclass, %method, %block, %line, %
FileUtils.java100% (1/1)44%  (4/9)20%  (47/237)21%  (10.7/52)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class FileUtils100% (1/1)44%  (4/9)20%  (47/237)21%  (10.7/52)
FileUtils (): void 0%   (0/1)0%   (0/3)0%   (0/1)
canFileBeCreated (String): boolean 0%   (0/1)0%   (0/22)0%   (0/9)
createParentDirectories (String): boolean 0%   (0/1)0%   (0/18)0%   (0/4)
deleteFile (String): boolean 0%   (0/1)0%   (0/16)0%   (0/4)
saveOnFile (String, InputStream): void 0%   (0/1)0%   (0/88)0%   (0/19)
createDir (String): void 100% (1/1)31%  (12/39)62%  (3.7/6)
checkFileNotEmpty (String): void 100% (1/1)50%  (14/28)75%  (3/4)
addSeparatorIfMissing (String): String 100% (1/1)87%  (13/15)67%  (2/3)
exists (String): boolean 100% (1/1)100% (8/8)100% (2/2)

1/**
2 * BSD-style license; for more info see http://xradar.sourceforge.net/license.html
3 */
4package org.sourceforge.xradar.util;
5 
6import java.io.File;
7import java.io.FileNotFoundException;
8import java.io.FileOutputStream;
9import java.io.IOException;
10import java.io.InputStream;
11import java.io.InputStreamReader;
12import java.io.OutputStreamWriter;
13 
14import org.sourceforge.xradar.XRadarException;
15import org.sourceforge.xradar.statics.Report;
16 
17/**
18 * <p>An utility class to regroup every common functions
19 * about files handling.</p>
20 * 
21 * @author rpelisse, belaran@gmail.com
22 *
23 */
24public final class FileUtils {
25 
26        // As we mostly used URLs, the file separtor should always be '/'
27        private static final String FILE_SEPARATOR = "/"; 
28        // Should not be instantiated
29        private FileUtils() {}
30 
31        /**
32         * <p>A simple commidity function to erase a file.</p>
33         * @param product
34         * @return
35         */
36        public static boolean deleteFile(String product) {
37                File toDelete = new File(product);
38                if ( toDelete.exists() && toDelete.canWrite() )
39                        return toDelete.delete();
40                return false;
41        }
42 
43        /**
44         * <p>A commidity method to check if we can create a file.</p>
45         * @param filename
46         * @return
47         */
48        public static boolean canFileBeCreated(String filename) {
49                File toCreate = new File(filename);
50                if ( ! toCreate.exists()  ) {
51                        try {
52                                if ( toCreate.createNewFile() ) {
53                                        FileUtils.deleteFile(filename);
54                                        return true;
55                                }
56                        } catch (IOException e) {
57                                // well, we can't create it so..;
58                                return false;
59                        }
60                }
61                return false;
62        }
63 
64        /**
65         * <p>A commodity method to check if a file exists.</p>
66         * @param fileName
67         * @return
68         */
69        public static boolean exists(String filename) {
70                File file = new File(filename);
71                return file.exists();
72        }
73 
74        /**
75         * <p>Create any parent directory necessary for the provided filename.</p>
76         * @param filename,
77         */
78        public static boolean createParentDirectories(String filename) {
79//                FIXME: this class should have a logger and log this kind of debug info:                
80//                System.out.println(">>> index:" + filename.lastIndexOf(FileUtils.FILE_SEPARATOR) + "[separator:" + FileUtils.FILE_SEPARATOR + "]");
81//                System.out.println(">>> called with" + filename);
82                File dir = new File(filename.substring(0,filename.lastIndexOf(FILE_SEPARATOR)));
83                if ( ! dir.exists() )
84                        return dir.mkdirs();
85                return true;
86        }
87 
88        /**
89         * <p>Add the systemSepartor to the end of the provided directory path.</p>
90         * @param
91         * @return
92         */
93        public static String addSeparatorIfMissing(String directoryName) {
94                if (! directoryName.endsWith(FILE_SEPARATOR))
95                        return directoryName + FILE_SEPARATOR;
96                return directoryName;
97        }
98 
99        /**
100         * Save the content of product into file named accordingly to the content
101         * of target.
102         *
103         * @param target
104         * @param product
105         * @throws StaticsException
106         */
107        public static void saveOnFile(String target,InputStream product) throws XRadarException
108        {
109                try
110                {
111                        File file = new File(target);
112                        if ( ! file.createNewFile() && ! file.exists())
113                                throw new XRadarException("Failed to create file " + target);
114 
115                        // This ignores the xml encoding specified on the header.
116                        // FileWriter javadoc: "The constructors of this class assume that the default character encoding and the default byte-buffer size are acceptable"
117                        //                        FileWriter fileWriter = new FileWriter(file);
118                        //                        fileWriter.write(product);
119                        //                        fileWriter.close();
120                        //                        
121                        // workaround: Try to find the encoding. If it not found use by default UTF-8.
122                        String encoding = "UTF-8";
123                        //FIXME: Repair this hack        
124                        /*                        int firstIndex = product.indexOf("<?xml version=\"1.0\" encoding=\"");
125                        if (firstIndex != -1) {
126                                firstIndex = "<?xml version=\"1.0\" encoding=\"".length();
127                        }
128                        int lastIndex = product.indexOf("\"?>");
129                        if (firstIndex != -1 & lastIndex > firstIndex) {
130                                encoding = product.substring(firstIndex, lastIndex);
131                        }
132 
133                        logger.finest("encoding for file \"" + target + "\" is :" + encoding);
134                         */                                        
135                        OutputStreamWriter writer = new OutputStreamWriter (new FileOutputStream (file), encoding);
136                        char cbuf[] = new char[10000]; // TODO: Is this value the more relevant ?
137                        InputStreamReader reader = new InputStreamReader(product);
138                        int nbCharRead = reader.read();
139                        do 
140                        {
141                                writer.write(cbuf);
142                                nbCharRead = reader.read();
143                        }
144                        while ( nbCharRead != -1 ) ;
145                        writer.flush();
146                        writer.close();
147 
148                }
149                catch (FileNotFoundException e)
150                {
151                        throw new XRadarException("Can't write file  " + target,e);
152                }
153                catch (IOException e)
154                {
155                        throw new XRadarException("Can't write file " + target,e);
156                }
157        }
158 
159        /**
160         * <p>A dummy method, that most likely need improvement, that allow to check if an
161         * XML file is not empty. It basicly check the size, in octet, of the file and ensure that it does
162         * have at least more than 8 characters, which is rougly the size of a proper XML prologue.</p>
163         *  
164         * @param filename
165         * @throws XRadarException
166         */
167        public static void checkFileNotEmpty(String filename) throws XRadarException {
168                File resultFile = new File(filename);
169                if ( ! resultFile.exists() || (resultFile.length() < 8 ) ) {
170                        throw new XRadarException("Filename " + resultFile + " is missing or incomplete.");
171                }
172        }
173 
174        /**
175         * Create the specified directory
176         * @param dirPath
177         * @throws XRadarException
178         */
179        public static void createDir(String dirPath) throws XRadarException
180        {
181                File directory = new File(dirPath);
182                // If directory does not exist, we create it
183                if ( ! directory.exists() && ! directory.mkdirs() ) 
184                        throw new XRadarException(dirPath + " does not exist, attempted to create it but failed");
185                else if ( ! directory.isDirectory() )
186                        throw new XRadarException(dirPath + " exist but is not a directory !");
187                //TODO: Check for  write access ?
188        }
189 
190}

[all classes][org.sourceforge.xradar.util]
EMMA 2.0.5312 (C) Vladimir Roubtsov