Source for org.jfree.repository.DefaultNameGenerator

   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:  * DefaultNameGenerator.java
  27:  * ------------
  28:  * (C) Copyright 2006, by Pentaho Corporation.
  29:  */
  30: 
  31: package org.jfree.repository;
  32: 
  33: public class DefaultNameGenerator implements NameGenerator
  34: {
  35:   private ContentLocation location;
  36:   private String defaultNameHint;
  37:   private String defaultSuffix;
  38: 
  39:   public DefaultNameGenerator(final ContentLocation location)
  40:   {
  41:     this(location, "file");
  42:   }
  43: 
  44:   public DefaultNameGenerator(final ContentLocation location,
  45:                               final String defaultNameHint)
  46:   {
  47:     this(location, defaultNameHint, null);
  48:   }
  49: 
  50:   public DefaultNameGenerator(final ContentLocation location,
  51:                               final String defaultNameHint,
  52:                               final String defaultSuffix)
  53:   {
  54:     if (location == null)
  55:     {
  56:       throw new NullPointerException();
  57:     }
  58:     if (defaultNameHint == null)
  59:     {
  60:       throw new NullPointerException();
  61:     }
  62: 
  63:     this.location = location;
  64:     this.defaultNameHint = defaultNameHint;
  65:     this.defaultSuffix = defaultSuffix;
  66:   }
  67: 
  68:   /**
  69:    * Generates a new, unique name for storing resources in the output
  70:    * repository. Assuming that proper synchronization has been applied, the
  71:    * generated name will be unique within that repository.
  72:    *
  73:    * @param nameHint a user defined name for that resource.
  74:    * @param mimeType the mime type of the resource to be stored in the
  75:    *                 repository.
  76:    * @return the generated, fully qualified name.
  77:    */
  78:   public String generateName(final String nameHint, final String mimeType)
  79:       throws ContentIOException
  80:   {
  81:     final String name;
  82:     if (nameHint != null)
  83:     {
  84:       name = nameHint;
  85:     }
  86:     else
  87:     {
  88:       name = defaultNameHint;
  89:     }
  90: 
  91:     final String suffix;
  92:     if (defaultSuffix != null)
  93:     {
  94:       suffix = defaultSuffix;
  95:     }
  96:     else
  97:     {
  98:       suffix = getSuffixForType(mimeType, location);
  99:     }
 100: 
 101:     final String firstFileName = name + "." + suffix;
 102:     if (location.exists(firstFileName) == false)
 103:     {
 104:       return firstFileName;
 105:     }
 106:     int counter = 0;
 107:     while (true)
 108:     {
 109:       if (counter < 0) // wraparound should not happen..
 110:       {
 111:         throw new ContentIOException();
 112:       }
 113: 
 114:       final String filename = name + counter + "." + suffix;
 115:       if (location.exists(filename) == false)
 116:       {
 117:         return filename;
 118:       }
 119:       counter += 1;
 120:     }
 121:   }
 122: 
 123:   private String getSuffixForType(final String mimeType,
 124:                                   final ContentLocation location)
 125:   {
 126:     final Repository repository = location.getRepository();
 127:     final MimeRegistry mimeRegistry = repository.getMimeRegistry();
 128:     return mimeRegistry.getSuffix(mimeType);
 129:   }
 130: }