Small code clean up
[phpeclipse.git] / archive / org.plog4u.wiki / src / org / plog4u / wiki / filter / FilterUtil.java
1 package org.plog4u.wiki.filter;
2
3 import java.util.Arrays;
4 import java.util.List;
5
6 public class FilterUtil {
7   public static String getImageRoot() {
8     return "theme/images";
9   }
10
11   public static String getSpaceRoot() {
12     return "space";
13   }
14
15   public static String getExecRoot() {
16     return "exec";
17   }
18
19   public static String getCommentsRoot() {
20     return "comments";
21   }
22
23   private static List extensions = Arrays.asList(new String[] { "png", "jpg", "jpeg", "gif" });
24
25   public static String normalizeServerImageName(String wikiLink) {
26     wikiLink = wikiLink.replaceAll(":", "/");
27     return wikiLink.replaceAll(" ", "_");
28   }
29
30   public static String createServerImage(String location, String name, String extension, boolean createHTMLLevel, int level) {
31     StringBuffer filename = new StringBuffer();
32     if (location != null) {
33       filename.append(location);
34       filename.append("/");
35     }
36     String normalized = normalizeServerImageName(name);
37
38     if (location == null && createHTMLLevel) {
39       for (int i = 0; i < level; i++) {
40         filename.append("../");
41       }
42     }
43
44     filename.append(normalized);
45     if (!normalized.startsWith("Image/") && extension != null) {
46       filename.append(".");
47       filename.append(extension);
48     }
49     return filename.toString();
50   }
51
52   public static String createServerImage(String location, String name, String extension) {
53     return createServerImage(location, name, extension, false, 0);
54   }
55
56   public static String createHTMLLink(String location, String name, String extension, boolean createHTMLLevel, int level) {
57     StringBuffer filename = new StringBuffer();
58     if (location != null) {
59         filename.append(location);
60         filename.append("/");
61     }
62 //    System.out.println(filename);
63     String normalized = FilterUtil.normalizeWikiLink(name);
64
65     if (location == null && createHTMLLevel) {
66       for (int i = 0; i < level; i++) {
67         filename.append("../");
68       }
69     }
70
71     filename.append(normalized);
72     if (!normalized.startsWith("Image/")) {
73       filename.append(".");
74       filename.append(extension); 
75     }
76     return filename.toString();
77   }
78
79   public static String createHTMLLink(String location, String name, String extension) {
80     return createHTMLLink(location, name, extension, false, 0);
81   }
82
83   public static String normalizeWikiLink(String wikiLink) {
84     if (wikiLink.length() > 0) {
85       int index = wikiLink.lastIndexOf('/');
86       if (index >= 0) {
87         if (wikiLink.length() == index + 1) {
88           wikiLink = wikiLink.substring(0, index + 1) + wikiLink.substring(index + 1, 1).toUpperCase();
89         } else if (wikiLink.length() > index + 1) {
90           wikiLink = wikiLink.substring(0, index + 1) + wikiLink.substring(index + 1, index + 2).toUpperCase()
91               + wikiLink.substring(index + 2);
92         }
93       } else {
94         if (wikiLink.length() > 1) {
95           wikiLink = wikiLink.substring(0, 1).toUpperCase() + wikiLink.substring(1);
96         } else {
97           wikiLink = wikiLink.substring(0, 1).toUpperCase();
98         }
99       }
100     }
101     wikiLink = wikiLink.replaceAll(":", "/");
102     return wikiLink.replaceAll(" ", "_");
103   }
104 }