Coverage Report - net.sourceforge.pebble.domain.FileMetaData
 
Classes in this File Line Coverage Branch Coverage Complexity
FileMetaData
96%
49/51
86%
45/52
2.688
 
 1  
 /*
 2  
  * Copyright (c) 2003-2011, Simon Brown
 3  
  * All rights reserved.
 4  
  *
 5  
  * Redistribution and use in source and binary forms, with or without
 6  
  * modification, are permitted provided that the following conditions are met:
 7  
  *
 8  
  *   - Redistributions of source code must retain the above copyright
 9  
  *     notice, this list of conditions and the following disclaimer.
 10  
  *
 11  
  *   - Redistributions in binary form must reproduce the above copyright
 12  
  *     notice, this list of conditions and the following disclaimer in
 13  
  *     the documentation and/or other materials provided with the
 14  
  *     distribution.
 15  
  *
 16  
  *   - Neither the name of Pebble nor the names of its contributors may
 17  
  *     be used to endorse or promote products derived from this software
 18  
  *     without specific prior written permission.
 19  
  *
 20  
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 21  
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 22  
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 23  
  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 24  
  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 25  
  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 26  
  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 27  
  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 28  
  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 29  
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 30  
  * POSSIBILITY OF SUCH DAMAGE.
 31  
  */
 32  
 package net.sourceforge.pebble.domain;
 33  
 
 34  
 import java.io.File;
 35  
 import java.util.Date;
 36  
 
 37  
 /**
 38  
  * Represents the meta data associated with a file.
 39  
  *
 40  
  * @author    Simon Brown
 41  
  */
 42  
 public class FileMetaData {
 43  
 
 44  
   public static final String BLOG_FILE = "blogFile";
 45  
   public static final String BLOG_IMAGE = "blogImage";
 46  
   public static final String THEME_FILE = "themeFile";
 47  
   public static final String BLOG_DATA = "blogData";
 48  
 
 49  
   private FileManager context;
 50  
 
 51  
   private String name;
 52  
   private String path;
 53  
   private Date lastModified;
 54  
   private long size;
 55  
   private boolean directory;
 56  
   private String type;
 57  
 
 58  1052
   FileMetaData(FileManager context, String absolutePath) {
 59  1052
     this.context = context;
 60  
 
 61  1052
     if (absolutePath == null || absolutePath.equals("") || absolutePath.equals("/")) {
 62  376
       this.path="/";
 63  376
       this.name = "";
 64  
     } else {
 65  676
       if (absolutePath.endsWith("/")) {
 66  20
         absolutePath = absolutePath.substring(0, absolutePath.length()-1);
 67  
       }
 68  
 
 69  676
       if (absolutePath.indexOf("/") > -1) {
 70  664
         this.path = absolutePath.substring(0, absolutePath.lastIndexOf("/"));
 71  664
         if (this.path.length() == 0) {
 72  596
           this.path = "/";
 73  
         }
 74  664
         this.name = absolutePath.substring(absolutePath.lastIndexOf("/")+1, absolutePath.length());
 75  
       } else {
 76  12
         this.path = absolutePath;
 77  12
         this.name = "";
 78  
       }
 79  
 
 80  
       // finally, all paths must start with "/"
 81  676
       if (!this.path.startsWith("/")) {
 82  12
         this.path = "/" + this.path;
 83  
       }
 84  
     }
 85  1052
   }
 86  
 
 87  
   public String getName() {
 88  244
     return name;
 89  
   }
 90  
 
 91  
   public void setName(String name) {
 92  4
     this.name = name;
 93  4
   }
 94  
 
 95  
   public Date getLastModified() {
 96  4
     return lastModified;
 97  
   }
 98  
 
 99  
   public void setLastModified(Date lastModified) {
 100  900
     this.lastModified = lastModified;
 101  900
   }
 102  
 
 103  
   public boolean isDirectory() {
 104  176
     return directory;
 105  
   }
 106  
 
 107  
   public void setDirectory(boolean directory) {
 108  608
     this.directory = directory;
 109  608
   }
 110  
 
 111  
   public String getPath() {
 112  108
     return path;
 113  
   }
 114  
 
 115  
   public String getAbsolutePath() {
 116  2592
     if (!path.endsWith("/")) {
 117  96
       return path + "/" + name;
 118  
     } else {
 119  2496
       return path + name;
 120  
     }
 121  
   }
 122  
 
 123  
   public String getUrl() {
 124  40
     String url = null;
 125  
 
 126  40
     if (type != null && type.equals(FileMetaData.BLOG_IMAGE)) {
 127  12
       url = "images" + getAbsolutePath();
 128  28
     } else if (type != null && type.equals(FileMetaData.BLOG_FILE)) {
 129  20
       url = "files" + getAbsolutePath();
 130  8
     } else if (type != null && type.equals(FileMetaData.THEME_FILE)) {
 131  8
       url = "theme" + getAbsolutePath();
 132  
     }
 133  
 
 134  40
     if (url != null && isDirectory() && !url.endsWith("/")) {
 135  0
       url += "/";
 136  
     }
 137  
 
 138  40
     return url;
 139  
   }
 140  
 
 141  
   public boolean isEditable() {
 142  52
     if (isDirectory()) {
 143  4
       return false;
 144  
     } else {
 145  48
       String filename = name.toLowerCase();
 146  48
       return
 147  
           filename.endsWith(".txt") ||
 148  
           filename.endsWith(".properties") ||
 149  
           filename.endsWith(".jsp") ||
 150  
           filename.endsWith(".jspf") ||
 151  
           filename.endsWith(".html") ||
 152  
           filename.endsWith(".htm") ||
 153  
           filename.endsWith(".css") ||
 154  
           filename.endsWith(".xml");
 155  
     }
 156  
   }
 157  
 
 158  
   public long getSize() {
 159  360
     return this.size;
 160  
   }
 161  
 
 162  
   public double getSizeInKB() {
 163  12
     return (this.size / 1024.0);
 164  
   }
 165  
 
 166  
   public void setSize(long size) {
 167  884
     this.size = size;
 168  884
   }
 169  
 
 170  
   public void setType(String type) {
 171  960
     this.type = type;
 172  960
   }
 173  
 
 174  
   public File getFile() {
 175  0
     return context.getFile(getAbsolutePath());
 176  
   }
 177  
 
 178  
 }