1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 }