001 /** 002 * =========================================================== 003 * LibRepository : a free Java content repository access layer 004 * =========================================================== 005 * 006 * Project Info: http://jfreereport.pentaho.org/librepository/ 007 * 008 * (C) Copyright 2006, by Pentaho Corporation and Contributors. 009 * 010 * This library is free software; you can redistribute it and/or modify it under the terms 011 * of the GNU Lesser General Public License as published by the Free Software Foundation; 012 * either version 2.1 of the License, or (at your option) any later version. 013 * 014 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 015 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 016 * See the GNU Lesser General Public License for more details. 017 * 018 * You should have received a copy of the GNU Lesser General Public License along with this 019 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, 020 * Boston, MA 02111-1307, USA. 021 * 022 * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 023 * in the United States and other countries.] 024 * 025 * ------------ 026 * FileContentLocation.java 027 * ------------ 028 * (C) Copyright 2006, by Pentaho Corporation. 029 */ 030 031 package org.jfree.repository.file; 032 033 import java.io.File; 034 import java.io.IOException; 035 036 import org.jfree.io.IOUtils; 037 import org.jfree.repository.ContentCreationException; 038 import org.jfree.repository.ContentEntity; 039 import org.jfree.repository.ContentIOException; 040 import org.jfree.repository.ContentItem; 041 import org.jfree.repository.ContentLocation; 042 import org.jfree.repository.Repository; 043 044 /** 045 * Creation-Date: 13.11.2006, 12:01:11 046 * 047 * @author Thomas Morgner 048 */ 049 public class FileContentLocation extends FileContentEntity implements ContentLocation 050 { 051 public FileContentLocation(final ContentLocation parent, final File backend) throws ContentIOException 052 { 053 super(parent, backend); 054 if (backend.exists() == false || backend.isDirectory() == false) 055 { 056 throw new ContentIOException("The given backend-file is not a directory."); 057 } 058 } 059 060 public FileContentLocation(final Repository repository, final File backend) throws ContentIOException 061 { 062 super(repository, backend); 063 if (backend.exists() == false || backend.isDirectory() == false) 064 { 065 throw new ContentIOException("The given backend-file is not a directory."); 066 } 067 } 068 069 public ContentEntity[] listContents() throws ContentIOException 070 { 071 final File file = getBackend(); 072 final File[] files = file.listFiles(); 073 final ContentEntity[] entities = new ContentEntity[files.length]; 074 for (int i = 0; i < files.length; i++) 075 { 076 final File child = files[i]; 077 if (child.isDirectory()) 078 { 079 entities[i] = new FileContentLocation(this, child); 080 } 081 else if (child.isFile()) 082 { 083 entities[i] = new FileContentLocation(this, child); 084 } 085 } 086 return entities; 087 } 088 089 public ContentEntity getEntry(final String name) throws ContentIOException 090 { 091 final File file = getBackend(); 092 final File child = new File (file, name); 093 if (child.exists() == false) 094 { 095 throw new ContentIOException("Not found."); 096 } 097 try 098 { 099 if (IOUtils.getInstance().isSubDirectory(file, child)) 100 { 101 throw new ContentIOException("Not sub-directory"); 102 } 103 } 104 catch (IOException e) 105 { 106 throw new ContentIOException("IO Error.", e); 107 } 108 109 if (child.isDirectory()) 110 { 111 return new FileContentLocation(this, child); 112 } 113 else if (child.isFile()) 114 { 115 return new FileContentItem(this, child); 116 } 117 else 118 { 119 throw new ContentIOException("Not File nor directory."); 120 } 121 } 122 123 public ContentItem createItem(final String name) throws ContentCreationException 124 { 125 final File file = getBackend(); 126 final File child = new File (file, name); 127 if (child.exists()) 128 { 129 throw new ContentCreationException("File already exists: " + child); 130 } 131 try 132 { 133 if (child.createNewFile() == false) 134 { 135 throw new ContentCreationException("Unable to create"); 136 } 137 return new FileContentItem(this, child); 138 } 139 catch (IOException e) 140 { 141 throw new ContentCreationException("IOError while create", e); 142 } 143 } 144 145 public ContentLocation createLocation(final String name) 146 throws ContentCreationException 147 { 148 final File file = getBackend(); 149 final File child = new File (file, name); 150 if (child.exists()) 151 { 152 throw new ContentCreationException("File already exists."); 153 } 154 if (child.mkdir() == false) 155 { 156 throw new ContentCreationException("Unable to create"); 157 } 158 try 159 { 160 return new FileContentLocation(this, child); 161 } 162 catch (ContentIOException e) 163 { 164 throw new ContentCreationException("Failed to create the content-location", e); 165 } 166 } 167 168 public boolean exists(final String name) 169 { 170 final File file = getBackend(); 171 final File child = new File (file, name); 172 return (child.exists()); 173 } 174 }