| 1 | /*
|
|---|
| 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
|---|
| 3 | *
|
|---|
| 4 | * Copyright 2008 jOpenDocument, by ILM Informatique. All rights reserved.
|
|---|
| 5 | *
|
|---|
| 6 | * The contents of this file are subject to the terms of the GNU
|
|---|
| 7 | * General Public License Version 3 only ("GPL").
|
|---|
| 8 | * You may not use this file except in compliance with the License.
|
|---|
| 9 | * You can obtain a copy of the License at http://www.gnu.org/licenses/gpl-3.0.html
|
|---|
| 10 | * See the License for the specific language governing permissions and limitations under the License.
|
|---|
| 11 | *
|
|---|
| 12 | * When distributing the software, include this License Header Notice in each file.
|
|---|
| 13 | *
|
|---|
| 14 | */
|
|---|
| 15 |
|
|---|
| 16 | package org.jopendocument.util;
|
|---|
| 17 |
|
|---|
| 18 | import java.util.HashMap;
|
|---|
| 19 | import java.util.Map;
|
|---|
| 20 |
|
|---|
| 21 | import org.jopendocument.util.StringUtils.Escaper;
|
|---|
| 22 |
|
|---|
| 23 | public final class FileUtils {
|
|---|
| 24 |
|
|---|
| 25 | private FileUtils() {
|
|---|
| 26 | // all static
|
|---|
| 27 | }
|
|---|
| 28 |
|
|---|
| 29 | // **io
|
|---|
| 30 |
|
|---|
| 31 | private static final Map<String, String> ext2mime;
|
|---|
| 32 | static {
|
|---|
| 33 | ext2mime = new HashMap<>();
|
|---|
| 34 | ext2mime.put(".xml", "text/xml");
|
|---|
| 35 | ext2mime.put(".jpg", "image/jpeg");
|
|---|
| 36 | ext2mime.put(".png", "image/png");
|
|---|
| 37 | ext2mime.put(".tiff", "image/tiff");
|
|---|
| 38 | }
|
|---|
| 39 |
|
|---|
| 40 | /**
|
|---|
| 41 | * Try to guess the media type of the passed file name (see <a
|
|---|
| 42 | * href="http://www.iana.org/assignments/media-types">iana</a>).
|
|---|
| 43 | *
|
|---|
| 44 | * @param fname a file name.
|
|---|
| 45 | * @return its mime type.
|
|---|
| 46 | */
|
|---|
| 47 | public static final String findMimeType(String fname) {
|
|---|
| 48 | for (final Map.Entry<String, String> e : ext2mime.entrySet()) {
|
|---|
| 49 | if (fname.toLowerCase().endsWith(e.getKey()))
|
|---|
| 50 | return e.getValue();
|
|---|
| 51 | }
|
|---|
| 52 | return null;
|
|---|
| 53 | }
|
|---|
| 54 |
|
|---|
| 55 | /**
|
|---|
| 56 | * An escaper suitable for producing valid filenames.
|
|---|
| 57 | */
|
|---|
| 58 | public static final Escaper FILENAME_ESCAPER = new StringUtils.Escaper('\'', 'Q');
|
|---|
| 59 | static {
|
|---|
| 60 | // from windows explorer
|
|---|
| 61 | FILENAME_ESCAPER.add('"', 'D').add(':', 'C').add('/', 'S').add('\\', 'A');
|
|---|
| 62 | FILENAME_ESCAPER.add('<', 'L').add('>', 'G').add('*', 'R').add('|', 'P').add('?', 'M');
|
|---|
| 63 | }
|
|---|
| 64 | }
|
|---|