View Javadoc

1   /*
2    * Copyright 2005 Jukka Zitting <jz@yukatan.fi>
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package net.sf.exorcist.midgard;
17  
18  import java.util.HashSet;
19  import java.util.Set;
20  import java.util.Stack;
21  
22  public final class NameMapper {
23  
24      private Set names;
25  
26      private final Stack stack;
27  
28      public NameMapper() {
29          names = new HashSet();
30          stack = new Stack();
31      }
32  
33      public void push() {
34          stack.push(names);
35          names = new HashSet(); 
36      }
37  
38      public void pop() {
39          names = (Set) stack.pop();
40      }
41  
42      public String map(String name) {
43          name = name.replace('\u00c4', 'a');
44          name = name.replace('\u00c5', 'a');
45          name = name.replace('\u00d6', 'o');
46          name = name.replace('\u00e4', 'a');
47          name = name.replace('\u00e5', 'a');
48          name = name.replace('\u00f6', 'o');
49          name = name.toLowerCase();
50          name = name.replaceAll("[^0-9A-Za-z-]+", "");
51          if (names.contains(name)) {
52              int suffix = 2;
53              while (names.contains(name + suffix)) {
54                  suffix++;
55              }
56              name = name + suffix;
57          }
58          names.add(name);
59          return name;
60      }
61  
62      public String mime(String name) {
63          name = name.toLowerCase();
64          if (name.endsWith(".jpg") || name.endsWith(".jpeg")) {
65              return "image/jpeg";
66          } else if (name.endsWith(".gif")) {
67              return "image/gif";
68          } else if (name.endsWith(".png")) {
69              return "image/png";
70          } else if (name.endsWith(".txt")) {
71              return "text/plain";
72          } else if (name.endsWith(".html")) {
73              return "text/html";
74          } else {
75              return "application/octet-stream";
76          }
77      }
78  
79  }