2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 package org
.libreoffice
.report
.pentaho
;
20 import org
.libreoffice
.report
.OutputRepository
;
22 import java
.io
.IOException
;
25 public class DefaultNameGenerator
28 private final OutputRepository outputRepository
;
30 public DefaultNameGenerator(final OutputRepository outputRepository
)
32 if (outputRepository
== null)
34 throw new NullPointerException();
36 this.outputRepository
= outputRepository
;
39 public String
generateName(final String namePrefix
, final String mimeType
)
42 return generateName(namePrefix
, mimeType
, true);
45 public String
generateStorageName(final String namePrefix
, final String mimeType
)
48 return generateName(namePrefix
, mimeType
, false);
52 * Generates a new, unique name for storing resources in the output repository. Assuming that proper synchronization
53 * has been applied, the generated name will be unique within that repository.
55 * @param namePrefix a user defined name for that resource.
56 * @param mimeType the mime type of the resource to be stored in the repository.
58 * @return the generated, fully qualified name.
59 * @throws java.io.IOException
61 private String
generateName(final String namePrefix
, final String mimeType
, final boolean isStream
)
65 if (namePrefix
!= null)
74 StringBuffer firstFileName
= new StringBuffer();
75 firstFileName
.append(name
);
79 suffix
= getSuffixForType(mimeType
);
80 firstFileName
.append('.');
81 firstFileName
.append(suffix
);
87 String newName
= firstFileName
.toString();
91 exists
= outputRepository
.exists(newName
);
95 exists
= outputRepository
.existsStorage(newName
);
102 if (counter
< 0) // wraparound should not happen...
104 throw new IOException();
106 firstFileName
.delete(0, firstFileName
.length());
107 firstFileName
.append(name
);
108 firstFileName
.append(counter
);
111 firstFileName
.append('.');
112 firstFileName
.append(suffix
);
114 newName
= firstFileName
.toString();
117 exists
= outputRepository
.exists(newName
);
121 exists
= outputRepository
.existsStorage(newName
);
129 private String
getSuffixForType(final String mimeType
)
131 if ("image/png".equals(mimeType
))
135 if ("image/jpeg".equals(mimeType
))
139 if ("image/gif".equals(mimeType
))
144 // todo ... use a flexible mapping ...