View Javadoc

1   /*
2    * ModeShape (http://www.modeshape.org)
3    * See the COPYRIGHT.txt file distributed with this work for information
4    * regarding copyright ownership.  Some portions may be licensed
5    * to Red Hat, Inc. under one or more contributor license agreements.
6    * See the AUTHORS.txt file in the distribution for a full listing of 
7    * individual contributors. 
8    *
9    * ModeShape is free software. Unless otherwise indicated, all code in ModeShape
10   * is licensed to you under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation; either version 2.1 of
12   * the License, or (at your option) any later version.
13   *
14   * ModeShape is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17   * Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public
20   * License along with this software; if not, write to the Free
21   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23   */
24  package org.modeshape.common.text;
25  
26  import java.util.BitSet;
27  import net.jcip.annotations.Immutable;
28  
29  /**
30   * An encoder useful for converting text to be used within a filename on common file systems and operating systems, including
31   * Linux, OS X, and Windows XP. This encoder is based upon the {@link UrlEncoder}, except that it removes the '*' character from
32   * the list of safe characters.
33   * 
34   * @see UrlEncoder
35   */
36  @Immutable
37  public class FilenameEncoder extends UrlEncoder {
38  
39      /**
40       * Data characters that are allowed in a URI but do not have a reserved purpose are called unreserved. These include upper and
41       * lower case letters, decimal digits, and a limited set of punctuation marks and symbols.
42       * 
43       * <pre>
44       * unreserved  = alphanum | mark
45       * mark        = &quot;-&quot; | &quot;_&quot; | &quot;.&quot; | &quot;!&quot; | &quot;&tilde;&quot; | &quot;'&quot; | &quot;(&quot; | &quot;)&quot;
46       * </pre>
47       * 
48       * Unreserved characters can be escaped without changing the semantics of the URI, but this should not be done unless the URI
49       * is being used in a context that does not allow the unescaped character to appear.
50       */
51      private static final BitSet SAFE_CHARACTERS = new BitSet(256);
52      private static final BitSet SAFE_WITH_SLASH_CHARACTERS;
53  
54      public static final char ESCAPE_CHARACTER = '%';
55  
56      static {
57          SAFE_CHARACTERS.set('a', 'z' + 1);
58          SAFE_CHARACTERS.set('A', 'Z' + 1);
59          SAFE_CHARACTERS.set('0', '9' + 1);
60          SAFE_CHARACTERS.set('-');
61          SAFE_CHARACTERS.set('_');
62          SAFE_CHARACTERS.set('.');
63          SAFE_CHARACTERS.set('!');
64          SAFE_CHARACTERS.set('~');
65          SAFE_CHARACTERS.set('\'');
66          SAFE_CHARACTERS.set('(');
67          SAFE_CHARACTERS.set(')');
68  
69          SAFE_WITH_SLASH_CHARACTERS = (BitSet)SAFE_CHARACTERS.clone();
70          SAFE_WITH_SLASH_CHARACTERS.set('/');
71      }
72  
73      /**
74       * {@inheritDoc}
75       */
76      @Override
77      public String encode( String text ) {
78          if (text == null) return null;
79          if (text.length() == 0) return text;
80          return encode(text, isSlashEncoded() ? SAFE_CHARACTERS : SAFE_WITH_SLASH_CHARACTERS);
81      }
82  
83      /**
84       * @param slashEncoded Sets slashEncoded to the specified value.
85       * @return this object, for method chaining
86       */
87      @Override
88      public FilenameEncoder setSlashEncoded( boolean slashEncoded ) {
89          super.setSlashEncoded(slashEncoded);
90          return this;
91      }
92  
93  }