Coverage Report - org.argouml.uml.generator.TempFileUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
TempFileUtils
0%
0/39
0%
0/8
2.778
TempFileUtils$1
0%
0/16
0%
0/8
2.778
TempFileUtils$2
0%
0/3
N/A
2.778
TempFileUtils$3
0%
0/4
0%
0/2
2.778
TempFileUtils$FileAction
N/A
N/A
2.778
 
 1  
 /* $Id: TempFileUtils.java 17868 2010-01-12 20:47:51Z linus $
 2  
  *****************************************************************************
 3  
  * Copyright (c) 2009 Contributors - see below
 4  
  * All rights reserved. This program and the accompanying materials
 5  
  * are made available under the terms of the Eclipse Public License v1.0
 6  
  * which accompanies this distribution, and is available at
 7  
  * http://www.eclipse.org/legal/epl-v10.html
 8  
  *
 9  
  * Contributors:
 10  
  *    tfmorris
 11  
  *****************************************************************************
 12  
  *
 13  
  * Some portions of this file was previously release using the BSD License:
 14  
  */
 15  
 
 16  
 // Copyright (c) 2006-2008 The Regents of the University of California. All
 17  
 // Rights Reserved. Permission to use, copy, modify, and distribute this
 18  
 // software and its documentation without fee, and without a written
 19  
 // agreement is hereby granted, provided that the above copyright notice
 20  
 // and this paragraph appear in all copies. This software program and
 21  
 // documentation are copyrighted by The Regents of the University of
 22  
 // California. The software program and documentation are supplied "AS
 23  
 // IS", without any accompanying services from The Regents. The Regents
 24  
 // does not warrant that the operation of the program will be
 25  
 // uninterrupted or error-free. The end-user understands that the program
 26  
 // was developed for research purposes and is advised not to rely
 27  
 // exclusively on the program for any reason. IN NO EVENT SHALL THE
 28  
 // UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
 29  
 // SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,
 30  
 // ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
 31  
 // THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
 32  
 // SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY
 33  
 // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 34  
 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
 35  
 // PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
 36  
 // CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT,
 37  
 // UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 38  
 
 39  
 package org.argouml.uml.generator;
 40  
 
 41  
 import java.io.BufferedReader;
 42  
 import java.io.File;
 43  
 import java.io.FileReader;
 44  
 import java.io.IOException;
 45  
 import java.util.ArrayList;
 46  
 import java.util.Collection;
 47  
 import java.util.List;
 48  
 
 49  
 import org.apache.log4j.Logger;
 50  
 
 51  
 /**
 52  
  * Utility class with methods to manage files in the temporary directory.
 53  
  */
 54  0
 public class TempFileUtils {
 55  
 
 56  0
     private static final Logger LOG = Logger.getLogger(TempFileUtils.class);
 57  
 
 58  
     /**
 59  
      * Create a temporary directory.
 60  
      * 
 61  
      * @return a newly created, empty, temporary directory
 62  
      */
 63  
     public static File createTempDir() {
 64  0
         File tmpdir = null;
 65  
         try  {
 66  0
             tmpdir = File.createTempFile("argouml", null);
 67  0
             tmpdir.delete();
 68  0
             if (!tmpdir.mkdir()) {
 69  0
                 return null;
 70  
             }
 71  0
             return tmpdir;
 72  0
         } catch (IOException ioe) {
 73  0
             LOG.error("Error while creating a temporary directory", ioe);
 74  0
             return null;
 75  
         }
 76  
     }
 77  
 
 78  0
     private interface FileAction {
 79  
         /**
 80  
          * Execute some action on the specified file.
 81  
          * 
 82  
          * @param file the file on which to perform the action
 83  
          * @throws IOException
 84  
          */
 85  
         void act(File file) throws IOException;
 86  
     }
 87  
 
 88  
     /**
 89  
      * Visit directory in post-order fashion.
 90  
      */
 91  
     private static void traverseDir(File dir, FileAction action)
 92  
         throws IOException {
 93  0
         if (dir.exists()) {
 94  0
             File[] files = dir.listFiles();
 95  0
             for (int i = 0; i < files.length; i++) {
 96  0
                 if (files[i].isDirectory()) {
 97  0
                     traverseDir(files[i], action);
 98  
                 } else {
 99  0
                     action.act(files[i]);
 100  
                 }
 101  
             }
 102  0
             action.act(dir);
 103  
         }
 104  0
     }
 105  
 
 106  
     /**
 107  
      * Reads all files in a directory in memory.
 108  
      * @param dir directory to read files from
 109  
      * @return A collection of SourceUnit objects.
 110  
      */
 111  
     public static Collection<SourceUnit> readAllFiles(File dir) {
 112  
         try {
 113  0
             final List<SourceUnit> ret = new ArrayList<SourceUnit>();
 114  0
             final int prefix = dir.getPath().length() + 1;
 115  0
             traverseDir(dir, new FileAction() {
 116  
 
 117  
                 public void act(File f) throws IOException {
 118  
                     // skip backup files. This is actually a workaround for the
 119  
                     // cpp generator, which always creates backup files (it's a
 120  
                     // bug).
 121  0
                     if (!f.isDirectory() && !f.getName().endsWith(".bak")) {
 122  
                         // TODO: This is using the default platform character
 123  
                         // encoding.  Specifying an encoding will produce more 
 124  
                         // predictable results
 125  0
                         FileReader fr = new FileReader(f);
 126  0
                         BufferedReader bfr = new BufferedReader(fr);
 127  
                         try { 
 128  0
                             StringBuffer result =
 129  
                                 new StringBuffer((int) f.length());
 130  0
                             String line = bfr.readLine();
 131  
                             do {
 132  0
                                 result.append(line);
 133  0
                                 line = bfr.readLine();
 134  0
                                 if (line != null) {
 135  0
                                     result.append('\n');
 136  
                                 }
 137  0
                             } while (line != null);
 138  0
                             ret.add(new SourceUnit(f.toString().substring(
 139  
                                     prefix), result.toString()));
 140  
                         } finally {
 141  0
                             bfr.close();
 142  0
                             fr.close();
 143  0
                         }
 144  
                     }
 145  0
                 }
 146  
 
 147  
             });
 148  0
             return ret;
 149  0
         } catch (IOException ioe) {
 150  0
             LOG.error("Exception reading files", ioe);
 151  
         }
 152  0
         return null;
 153  
     }
 154  
 
 155  
     /**
 156  
      * Deletes a directory and all of its contents.
 157  
      * @param dir The directory to delete.
 158  
      */
 159  
     public static void deleteDir(File dir) {
 160  
         try {
 161  0
             traverseDir(dir, new FileAction() {
 162  
                 public void act(File f) {
 163  0
                     f.delete();
 164  0
                 }
 165  
             });
 166  0
         } catch (IOException ioe) {
 167  0
             LOG.error("Exception deleting directory", ioe);
 168  0
         }
 169  0
     }
 170  
 
 171  
     /**
 172  
      * Reads all the files within a directory tree.
 173  
      * @param dir The base directory.
 174  
      * @return The collection of files.
 175  
      */
 176  
     public static Collection<String> readFileNames(File dir) {
 177  0
         final List<String> ret = new ArrayList<String>();
 178  0
         final int prefix = dir.getPath().length() + 1;
 179  
         try {
 180  0
             traverseDir(dir, new FileAction() {
 181  
                 public void act(File f) {
 182  0
                     if (!f.isDirectory()) {
 183  0
                         ret.add(f.toString().substring(prefix));
 184  
                     }
 185  0
                 }
 186  
             });
 187  0
         } catch (IOException ioe) {
 188  0
             LOG.error("Exception reading file names", ioe);
 189  0
         }
 190  0
         return ret;
 191  
     }
 192  
 
 193  
 }