LanguageTool: don't crash if REST protocol isn't set
[LibreOffice.git] / reportbuilder / java / org / libreoffice / report / pentaho / DefaultNameGenerator.java
blob46f2a032aeffe10a7405c7a195056f46a355edb8
1 /*
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)
40 throws IOException
42 return generateName(namePrefix, mimeType, true);
45 public String generateStorageName(final String namePrefix, final String mimeType)
46 throws IOException
48 return generateName(namePrefix, mimeType, false);
51 /**
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.
57 * @param isStream
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)
62 throws IOException
64 final String name;
65 if (namePrefix != null)
67 name = namePrefix;
69 else
71 name = "file";
74 StringBuffer firstFileName = new StringBuffer();
75 firstFileName.append(name);
76 final String suffix;
77 if (mimeType != null)
79 suffix = getSuffixForType(mimeType);
80 firstFileName.append('.');
81 firstFileName.append(suffix);
83 else
85 suffix = null;
87 String newName = firstFileName.toString();
88 boolean exists;
89 if (isStream)
91 exists = outputRepository.exists(newName);
93 else
95 exists = outputRepository.existsStorage(newName);
97 if (exists)
99 int counter = 0;
100 while (exists)
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);
109 if (suffix != null)
111 firstFileName.append('.');
112 firstFileName.append(suffix);
114 newName = firstFileName.toString();
115 if (isStream)
117 exists = outputRepository.exists(newName);
119 else
121 exists = outputRepository.existsStorage(newName);
123 counter++;
126 return newName;
129 private String getSuffixForType(final String mimeType)
131 if ("image/png".equals(mimeType))
133 return "png";
135 if ("image/jpeg".equals(mimeType))
137 return "jpg";
139 if ("image/gif".equals(mimeType))
141 return "gif";
144 // todo ... use a flexible mapping ...
145 return "dat";