* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
public class XmlEscape {
private XmlEscape() {}
-
+
private static final TreeMap<String,Integer> charMap; // see initialization at end
private static final TreeMap<Integer,String> intMap; // see initialization at end
try {
int c;
StringBuilder esc = new StringBuilder();
- for(int cnt = 0;cnt<9 /*max*/; ++cnt) {
- if((c=r.read())<0)throw new ParseException("Invalid Data: Unfinished Escape Sequence");
- if(c!=';') {
+ for (int cnt = 0;cnt<9 /*max*/; ++cnt) {
+ if ((c=r.read())<0)throw new ParseException("Invalid Data: Unfinished Escape Sequence");
+ if (c!=';') {
esc.append((char)c);
} else { // evaluate
Integer i = charMap.get(esc.toString());
- if(i==null) {
+ if (i==null) {
// leave in nasty XML format for now.
sb.append('&');
sb.append(esc);
break;
}
}
-
-
+
+
} catch (IOException e) {
throw new ParseException(e);
}
}
-
+
public static void xmlEscape(StringBuilder sb, int chr) {
sb.append('&');
sb.append(intMap.get(chr));
sb.append(';');
}
-
+
public static String convert(StringBuilder insb) {
int idx, ch;
StringBuilder sb=null;
- for(idx=0;idx<insb.length();++idx) {
+ for (idx=0;idx<insb.length();++idx) {
ch = insb.charAt(idx);
- if(ch>=160 || ch==34 || ch==38 || ch==39 || ch==60 || ch==62) {
+ if (ch>=160 || ch==34 || ch==38 || ch==39 || ch==60 || ch==62) {
sb = new StringBuilder();
sb.append(insb,0,idx);
break;
}
}
-
- if(sb==null)return insb.toString();
-
- for(int i=idx;i<insb.length();++i) {
+
+ if (sb==null)return insb.toString();
+
+ for (int i=idx;i<insb.length();++i) {
ch = insb.charAt(i);
- if(ch<160) {
+ if (ch<160) {
switch(ch) {
case 34: sb.append("""); break;
case 38: sb.append("&"); break;
}
} else { // use map
String s = intMap.get(ch);
- if(s==null)sb.append((char)ch);
+ if (s==null)sb.append((char)ch);
else {
sb.append('&');
sb.append(s);
charMap.put("clubs",9827);
charMap.put("hearts",9829);
charMap.put("diams",9830);
-
- for( Entry<String, Integer> es: charMap.entrySet()) {
- if(es.getValue()>=160); // save small space... note that no longer has amp, etc.
+
+ for ( Entry<String, Integer> es: charMap.entrySet()) {
+ if (es.getValue()>=160); // save small space... note that no longer has amp, etc.
intMap.put(es.getValue(), es.getKey());
}
}