Source for org.jfree.repository.file.FileContentLocation

   1: /**
   2:  * ===========================================================
   3:  * LibRepository : a free Java content repository access layer
   4:  * ===========================================================
   5:  *
   6:  * Project Info:  http://jfreereport.pentaho.org/librepository/
   7:  *
   8:  * (C) Copyright 2006, by Pentaho Corporation and Contributors.
   9:  *
  10:  * This library is free software; you can redistribute it and/or modify it under the terms
  11:  * of the GNU Lesser General Public License as published by the Free Software Foundation;
  12:  * either version 2.1 of the License, or (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  15:  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16:  * See the GNU Lesser General Public License for more details.
  17:  *
  18:  * You should have received a copy of the GNU Lesser General Public License along with this
  19:  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  20:  * Boston, MA 02111-1307, USA.
  21:  *
  22:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  23:  * in the United States and other countries.]
  24:  *
  25:  * ------------
  26:  * FileContentLocation.java
  27:  * ------------
  28:  * (C) Copyright 2006, by Pentaho Corporation.
  29:  */
  30: 
  31: package org.jfree.repository.file;
  32: 
  33: import java.io.File;
  34: import java.io.IOException;
  35: 
  36: import org.jfree.io.IOUtils;
  37: import org.jfree.repository.ContentCreationException;
  38: import org.jfree.repository.ContentEntity;
  39: import org.jfree.repository.ContentIOException;
  40: import org.jfree.repository.ContentItem;
  41: import org.jfree.repository.ContentLocation;
  42: import org.jfree.repository.Repository;
  43: 
  44: /**
  45:  * Creation-Date: 13.11.2006, 12:01:11
  46:  *
  47:  * @author Thomas Morgner
  48:  */
  49: public class FileContentLocation extends FileContentEntity
  50:   implements ContentLocation
  51: {
  52:   public FileContentLocation(final ContentLocation parent, final File backend)
  53:   {
  54:     super(parent, backend);
  55:   }
  56: 
  57:   public FileContentLocation(final Repository repository, final File backend)
  58:   {
  59:     super(repository, backend);
  60:   }
  61: 
  62:   public ContentEntity[] listContents() throws ContentIOException
  63:   {
  64:     final File file = getBackend();
  65:     final File[] files = file.listFiles();
  66:     final ContentEntity[] entities = new ContentEntity[files.length];
  67:     for (int i = 0; i < files.length; i++)
  68:     {
  69:       final File child = files[i];
  70:       if (child.isDirectory())
  71:       {
  72:         entities[i] = new FileContentLocation(this, child);
  73:       }
  74:       else if (child.isFile())
  75:       {
  76:         entities[i] = new FileContentLocation(this, child);
  77:       }
  78:     }
  79:     return entities;
  80:   }
  81: 
  82:   public ContentEntity getEntry(String name) throws ContentIOException
  83:   {
  84:     final File file = getBackend();
  85:     final File child = new File (file, name);
  86:     if (child.exists() == false)
  87:     {
  88:       throw new ContentIOException("Not found.");
  89:     }
  90:     try
  91:     {
  92:       if (IOUtils.getInstance().isSubDirectory(file, child))
  93:       {
  94:         throw new ContentIOException("Not sub-directory");
  95:       }
  96:     }
  97:     catch (IOException e)
  98:     {
  99:       throw new ContentIOException("IO Error.");
 100:     }
 101: 
 102:     if (child.isDirectory())
 103:     {
 104:       return new FileContentLocation(this, child);
 105:     }
 106:     else if (child.isFile())
 107:     {
 108:       return new FileContentItem(this, child);
 109:     }
 110:     else
 111:     {
 112:       throw new ContentIOException("Not File nor directory.");
 113:     }
 114:   }
 115: 
 116:   public ContentItem createItem(String name) throws ContentCreationException
 117:   {
 118:     final File file = getBackend();
 119:     final File child = new File (file, name);
 120:     if (child.exists())
 121:     {
 122:       throw new ContentCreationException("File already exists.");
 123:     }
 124:     try
 125:     {
 126:       if (child.createNewFile() == false)
 127:       {
 128:         throw new ContentCreationException("Unable to create");
 129:       }
 130:       return new FileContentItem(this, child);
 131:     }
 132:     catch (IOException e)
 133:     {
 134:       throw new ContentCreationException("IOError while create");
 135:     }
 136:   }
 137: 
 138:   public ContentLocation createLocation(String name)
 139:       throws ContentCreationException
 140:   {
 141:     final File file = getBackend();
 142:     final File child = new File (file, name);
 143:     if (child.exists())
 144:     {
 145:       throw new ContentCreationException("File already exists.");
 146:     }
 147:     if (child.mkdir() == false)
 148:     {
 149:       throw new ContentCreationException("Unable to create");
 150:     }
 151:     return new FileContentLocation(this, child);
 152:   }
 153: 
 154:   public boolean exists(final String name)
 155:   {
 156:     final File file = getBackend();
 157:     final File child = new File (file, name);
 158:     return (child.exists());
 159:   }
 160: }