1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 package com.sun.star.report.pentaho;
24 
25 import com.sun.star.report.OutputRepository;
26 
27 import java.io.IOException;
28 
29 
30 public class DefaultNameGenerator
31 {
32 
33     private final OutputRepository outputRepository;
34 
DefaultNameGenerator(final OutputRepository outputRepository)35     public DefaultNameGenerator(final OutputRepository outputRepository)
36     {
37         if (outputRepository == null)
38         {
39             throw new NullPointerException();
40         }
41         this.outputRepository = outputRepository;
42     }
43 
generateName(final String namePrefix, final String mimeType)44     public String generateName(final String namePrefix, final String mimeType)
45             throws IOException
46     {
47         return generateName(namePrefix, mimeType, true);
48     }
49 
generateStorageName(final String namePrefix, final String mimeType)50     public String generateStorageName(final String namePrefix, final String mimeType)
51             throws IOException
52     {
53         return generateName(namePrefix, mimeType, false);
54     }
55 
56     /**
57      * Generates a new, unique name for storing resources in the output repository. Assuming that proper synchronization
58      * has been applied, the generated name will be unique within that repository.
59      *
60      * @param namePrefix a user defined name for that resource.
61      * @param mimeType   the mime type of the resource to be stored in the repository.
62      * @param isStream
63      * @return the generated, fully qualified name.
64      * @throws java.io.IOException
65      */
generateName(final String namePrefix, final String mimeType, final boolean isStream)66     private String generateName(final String namePrefix, final String mimeType, final boolean isStream)
67             throws IOException
68     {
69         final String name;
70         if (namePrefix != null)
71         {
72             name = namePrefix;
73         }
74         else
75         {
76             name = "file";
77         }
78 
79         StringBuffer firstFileName = new StringBuffer();
80         firstFileName.append(name);
81         final String suffix;
82         if (mimeType != null)
83         {
84             suffix = getSuffixForType(mimeType);
85             firstFileName.append('.');
86             firstFileName.append(suffix);
87         }
88         else
89         {
90             suffix = null;
91         }
92         String newName = firstFileName.toString();
93         boolean exists;
94         if (isStream)
95         {
96             exists = outputRepository.exists(newName);
97         }
98         else
99         {
100             exists = outputRepository.existsStorage(newName);
101         }
102         if (exists)
103         {
104             int counter = 0;
105             while (exists)
106             {
107                 if (counter < 0) // wraparound should not happen..
108                 {
109                     throw new IOException();
110                 }
111                 firstFileName.delete(0, firstFileName.length());
112                 firstFileName.append(name);
113                 firstFileName.append(counter);
114                 if (suffix != null)
115                 {
116                     firstFileName.append('.');
117                     firstFileName.append(suffix);
118                 }
119                 newName = firstFileName.toString();
120                 if (isStream)
121                 {
122                     exists = outputRepository.exists(newName);
123                 }
124                 else
125                 {
126                     exists = outputRepository.existsStorage(newName);
127                 }
128                 counter++;
129             }
130         }
131         return newName;
132     }
133 
getSuffixForType(final String mimeType)134     protected String getSuffixForType(final String mimeType)
135     {
136         if ("image/png".equals(mimeType))
137         {
138             return "png";
139         }
140         if ("image/jpeg".equals(mimeType))
141         {
142             return "jpg";
143         }
144         if ("image/gif".equals(mimeType))
145         {
146             return "gif";
147         }
148 
149         // todo ... use a flexible mapping ...
150         return "dat";
151     }
152 }
153