Class | Line # | Actions | |||||
---|---|---|---|---|---|---|---|
GenericFileUtils | 32 | 11 | 0% | 7 | 8 |
1 | /* | |
2 | * See the NOTICE file distributed with this work for additional | |
3 | * information regarding copyright ownership. | |
4 | * | |
5 | * This is free software; you can redistribute it and/or modify it | |
6 | * under the terms of the GNU Lesser General Public License as | |
7 | * published by the Free Software Foundation; either version 2.1 of | |
8 | * the License, or (at your option) any later version. | |
9 | * | |
10 | * This software is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | * Lesser General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU Lesser General Public | |
16 | * License along with this software; if not, write to the Free | |
17 | * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | |
18 | * 02110-1301 USA, or see the FSF site: http://www.fsf.org. | |
19 | */ | |
20 | package org.xwiki.store.filesystem.internal; | |
21 | ||
22 | import java.io.UnsupportedEncodingException; | |
23 | import java.net.URLDecoder; | |
24 | import java.net.URLEncoder; | |
25 | ||
26 | /** | |
27 | * Internal class for providing static utilities used by multiple classes in this package. | |
28 | * | |
29 | * @version $Id: bb4d6368f8f6f3b1d19ead00484423be56b2b519 $ | |
30 | * @since 3.0M2 | |
31 | */ | |
32 | final class GenericFileUtils | |
33 | { | |
34 | /** | |
35 | * This must precede the version of a file. It has to be URL invalid so that it cannot collide with | |
36 | * the name of another file. Also no other key can start with ~v because the name of the version | |
37 | * might be anything. If the prefix was "~b" and a version was made called "ak" then it would collide | |
38 | * with the DefaultFilesystemStoreTools.BACKUP_FILE_SUFFIX. | |
39 | */ | |
40 | private static final String FILE_VERSION_PREFIX = "~v"; | |
41 | ||
42 | /** | |
43 | * The character set to use for encoding and decoding. This should always be UTF-8. | |
44 | */ | |
45 | private static final String CHARSET = "UTF-8"; | |
46 | ||
47 | /** | |
48 | * Error message to give if CHARSET is unavailable. | |
49 | */ | |
50 | private static final String NO_CHARSET = | |
51 | "UTF-8 not available, this Java VM is not standards compliant!"; | |
52 | ||
53 | /** | |
54 | * Private constructor for utility class. | |
55 | */ | |
56 | 0 | ![]() |
57 | { | |
58 | } | |
59 | ||
60 | /** | |
61 | * Get a URL encoded version of the string. | |
62 | * same as URLEncoder.encode(toEncode, "UTF-8") but the checked exception is | |
63 | * caught since UTF-8 is mandatory for all Java virtual machines. | |
64 | * | |
65 | * @param toEncode the string to URL encode. | |
66 | * @return a URL encoded version of toEncode. | |
67 | * @see #getURLDecoded(String) | |
68 | */ | |
69 | 99 | ![]() |
70 | { | |
71 | 99 | try { |
72 | 99 | return URLEncoder.encode(toEncode, CHARSET); |
73 | } catch (UnsupportedEncodingException ex) { | |
74 | 0 | throw new RuntimeException(NO_CHARSET); |
75 | } | |
76 | } | |
77 | ||
78 | /** | |
79 | * Get a URL decoded version of the string. | |
80 | * same as URLEncoder.decode(toDecode, "UTF-8") but the checked exception is | |
81 | * caught since UTF-8 is mandatory for all Java virtual machines. | |
82 | * | |
83 | * @param toDecode the string to URL decode. | |
84 | * @return a URL decoded version of toDecode. | |
85 | * @see #getURLEncoded(String) | |
86 | */ | |
87 | 0 | ![]() |
88 | { | |
89 | 0 | try { |
90 | 0 | return URLDecoder.decode(toDecode, CHARSET); |
91 | } catch (UnsupportedEncodingException ex) { | |
92 | 0 | throw new RuntimeException(NO_CHARSET); |
93 | } | |
94 | } | |
95 | ||
96 | /** | |
97 | * Get a version of a filename. | |
98 | * The filename is URL encoded and the version has "~v" prepended so that it cannot be | |
99 | * mistaken for part of the filename. | |
100 | * If the filename contains one or more '.' characters then the version is inserted before | |
101 | * the last '.' character. Otherwise it is appended to the end. | |
102 | * This means a file such as: | |
103 | * file.txt version 1.1 will become file~v1.1.txt and will still be recognized by a text editor | |
104 | * A file with no extension such as myUnknownFile version 1.1 will become myUnknownFile~v1.1 | |
105 | * Because of URL encoding, a file named file~v1.3.txt of version 1.1 will become | |
106 | * file%7Ev1.3~1.1.txt and thus will not collide with file.txt version 1.1. | |
107 | * | |
108 | * @param filename the name of the file to save. This will be URL encoded. | |
109 | * @param versionName the name of the version of the file. This will also be URL encoded. | |
110 | * @return a string representing the filename and version which is guaranteed not to collide | |
111 | * with any other file gotten through DefaultFilesystemStoreTools. | |
112 | */ | |
113 | 30 | ![]() |
114 | { | |
115 | 30 | final String attachFilename = getURLEncoded(filename); |
116 | 30 | final String version = getURLEncoded(versionName); |
117 | 30 | if (attachFilename.contains(".")) { |
118 | // file.txt version 1.1 --> file~v1.1.txt | |
119 | 30 | return attachFilename.substring(0, attachFilename.lastIndexOf('.')) |
120 | + FILE_VERSION_PREFIX + version | |
121 | + attachFilename.substring(attachFilename.lastIndexOf('.')); | |
122 | } | |
123 | // someFile version 2.2 --> someFile~v2.2 | |
124 | 0 | return attachFilename + FILE_VERSION_PREFIX + version; |
125 | } | |
126 | } |