OSDN Git Service

Change generation of files/paths in logs dir to use FileManager
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / utilities / StringUtils.java
1 /*\r
2  * This file is part of NeverNote \r
3  * Copyright 2009 Randy Baumgarte\r
4  * \r
5  * This file may be licensed under the terms of of the\r
6  * GNU General Public License Version 2 (the ``GPL'').\r
7  *\r
8  * Software distributed under the License is distributed\r
9  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either\r
10  * express or implied. See the GPL for the specific language\r
11  * governing rights and limitations.\r
12  *\r
13  * You should have received a copy of the GPL along with this\r
14  * program. If not, go to http://www.gnu.org/licenses/gpl.html\r
15  * or write to the Free Software Foundation, Inc.,\r
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.\r
17  *\r
18 */\r
19 \r
20 package cx.fbn.nevernote.utilities;\r
21 \r
22 import java.util.HashMap;\r
23 \r
24 public class StringUtils {\r
25 \r
26           private StringUtils() {}\r
27           \r
28           private static HashMap<String,String> htmlEntities;\r
29           static {\r
30             htmlEntities = new HashMap<String,String>();\r
31             htmlEntities.put("&lt;","<")    ; htmlEntities.put("&gt;",">");\r
32             htmlEntities.put("&amp;","&")   ; htmlEntities.put("&quot;","\"");\r
33             htmlEntities.put("&agrave;","à"); htmlEntities.put("&agrave;","À");\r
34             htmlEntities.put("&acirc;","â") ; htmlEntities.put("&auml;","ä");\r
35             htmlEntities.put("&auml;","Ä")  ; htmlEntities.put("&acirc;","Â");\r
36             htmlEntities.put("&aring;","å") ; htmlEntities.put("&aring;","Å");\r
37             htmlEntities.put("&aelig;","æ") ; htmlEntities.put("&aElig;","Æ" );\r
38             htmlEntities.put("&ccedil;","ç"); htmlEntities.put("&ccedil;","Ç");\r
39             htmlEntities.put("&eacute;","é"); htmlEntities.put("&eacute;","É" );\r
40             htmlEntities.put("&egrave;","è"); htmlEntities.put("&egrave;","È");\r
41             htmlEntities.put("&ecirc;","ê") ; htmlEntities.put("&ecirc;","Ê");\r
42             htmlEntities.put("&euml;","ë")  ; htmlEntities.put("&euml;","Ë");\r
43             htmlEntities.put("&iuml;","ï")  ; htmlEntities.put("&iuml;","Ï");\r
44             htmlEntities.put("&ocirc;","ô") ; htmlEntities.put("&ocirc;","Ô");\r
45             htmlEntities.put("&ouml;","ö")  ; htmlEntities.put("&ouml;","Ö");\r
46             htmlEntities.put("&oslash;","ø") ; htmlEntities.put("&oslash;","Ø");\r
47             htmlEntities.put("&szlig;","ß") ; htmlEntities.put("&ugrave;","ù");\r
48             htmlEntities.put("&ugrave;","Ù"); htmlEntities.put("&ucirc;","û");\r
49             htmlEntities.put("&ucirc;","Û") ; htmlEntities.put("&uuml;","ü");\r
50             htmlEntities.put("&uuml;","Ü")  ; htmlEntities.put("&nbsp;"," ");\r
51             htmlEntities.put("&copy;","\u00a9"); htmlEntities.put("&apos;", "'");\r
52             htmlEntities.put("&reg;","\u00ae"); htmlEntities.put("&iexcl;", "\u00a1");\r
53             htmlEntities.put("&euro;","\u20a0"); htmlEntities.put("&cent;", "\u00a2");\r
54             htmlEntities.put("&pound;", "\u00a3"); htmlEntities.put("&curen;", "\u00a4");\r
55             htmlEntities.put("&yen;", "\u00a5"); htmlEntities.put("&brvbar;", "\u00a6");\r
56             htmlEntities.put("&sect;", "\u00a7"); htmlEntities.put("&uml;", "\u00a8");\r
57             htmlEntities.put("&copy;", "\u00a9"); htmlEntities.put("&ordf;", "\u00aa");\r
58             htmlEntities.put("&laqo;", "\u00ab"); htmlEntities.put("&not;", "\u00ac");\r
59             htmlEntities.put("&reg;", "\u00ae"); htmlEntities.put("&macr;", "\u00af");\r
60           }\r
61 \r
62 \r
63           \r
64           public static final String unescapeHTML(String source, int start){\r
65                      int i,j;\r
66 \r
67                      i = source.indexOf("&", start);\r
68                      while (i>-1) {\r
69                         j = source.indexOf(";" ,i);\r
70                         if (j > i) {\r
71                            String entityToLookFor = source.substring(i , j + 1);\r
72                            String value = htmlEntities.get(entityToLookFor);\r
73                            if (value != null) {\r
74                                    value = " ";\r
75                                    source = new StringBuffer().append(source.substring(0 , i).toLowerCase())\r
76                                            .append(value)\r
77                                            .append(source.substring(j + 1))\r
78                                            .toString();\r
79                                    i = source.indexOf("&", i+1);\r
80                            }\r
81                         }\r
82                      }\r
83                      return source;\r
84                   }\r
85 \r
86           \r
87           public static final String unescapeHTML2(String source, int start){\r
88              int i,j;\r
89 \r
90              i = source.indexOf("&", start);\r
91              if (i > -1) {\r
92                 j = source.indexOf(";" ,i);\r
93                 if (j > i) {\r
94                    String entityToLookFor = source.substring(i , j + 1);\r
95                    String value = htmlEntities.get(entityToLookFor);\r
96                    if (value != null) {\r
97                            value = " ";\r
98                            source = new StringBuffer().append(source.substring(0 , i).toLowerCase())\r
99                                    .append(value)\r
100                                    .append(source.substring(j + 1))\r
101                                    .toString();\r
102                 return unescapeHTML(source, i + 1); // recursive call\r
103                    }\r
104                 }\r
105              }\r
106              return source;\r
107           }\r
108 }\r