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 * DefaultNameGenerator.java
027 * ------------
028 * (C) Copyright 2006, by Pentaho Corporation.
029 */
030
031 package org.jfree.repository;
032
033 public class DefaultNameGenerator implements NameGenerator
034 {
035 private ContentLocation location;
036 private String defaultNameHint;
037 private String defaultSuffix;
038
039 public DefaultNameGenerator(final ContentLocation location)
040 {
041 this(location, "file", null);
042 }
043
044 public DefaultNameGenerator(final ContentLocation location,
045 final String defaultNameHint)
046 {
047 if (location == null)
048 {
049 throw new NullPointerException();
050 }
051 if (defaultNameHint == null)
052 {
053 throw new NullPointerException();
054 }
055
056 this.location = location;
057
058 // a leading point is not a sufix!
059 final int pos = defaultNameHint.lastIndexOf('.');
060 if (defaultSuffix == null && pos > 0)
061 {
062 if (pos < (defaultNameHint.length() - 1))
063 {
064 this.defaultNameHint = defaultNameHint.substring(0, pos - 1);
065 this.defaultSuffix = defaultNameHint.substring(pos + 1);
066 }
067 else
068 {
069 this.defaultNameHint = defaultNameHint.substring(0, pos - 1);
070 this.defaultSuffix = null;
071 }
072 }
073 else
074 {
075 this.defaultNameHint = defaultNameHint;
076 this.defaultSuffix = null;
077 }
078 }
079
080 public DefaultNameGenerator(final ContentLocation location,
081 final String defaultNameHint,
082 final String defaultSuffix)
083 {
084 if (location == null)
085 {
086 throw new NullPointerException();
087 }
088 if (defaultNameHint == null)
089 {
090 throw new NullPointerException();
091 }
092
093 this.location = location;
094 this.defaultNameHint = defaultNameHint;
095 this.defaultSuffix = defaultSuffix;
096 }
097
098 /**
099 * Generates a new, unique name for storing resources in the output
100 * repository. Assuming that proper synchronization has been applied, the
101 * generated name will be unique within that repository.
102 *
103 * @param nameHint a user defined name for that resource.
104 * @param mimeType the mime type of the resource to be stored in the
105 * repository.
106 * @return the generated, fully qualified name.
107 */
108 public String generateName(final String nameHint, final String mimeType)
109 throws ContentIOException
110 {
111 final String name;
112 if (nameHint != null)
113 {
114 name = nameHint;
115 }
116 else
117 {
118 name = defaultNameHint;
119 }
120
121 final String suffix;
122 if (defaultSuffix != null)
123 {
124 suffix = defaultSuffix;
125 }
126 else
127 {
128 suffix = getSuffixForType(mimeType, location);
129 }
130
131 final String firstFileName = name + "." + suffix;
132 if (location.exists(firstFileName) == false)
133 {
134 return firstFileName;
135 }
136 int counter = 0;
137 while (true)
138 {
139 if (counter < 0) // wraparound should not happen..
140 {
141 throw new ContentIOException();
142 }
143
144 final String filename = name + counter + "." + suffix;
145 if (location.exists(filename) == false)
146 {
147 return filename;
148 }
149 counter += 1;
150 }
151 }
152
153 private String getSuffixForType(final String mimeType,
154 final ContentLocation location)
155 {
156 final Repository repository = location.getRepository();
157 final MimeRegistry mimeRegistry = repository.getMimeRegistry();
158 return mimeRegistry.getSuffix(mimeType);
159 }
160 }