1 21 package au.id.jericho.lib.html; 22 23 import java.util.*; 24 25 52 public class NumericCharacterReference extends CharacterReference { 53 private boolean hex; 54 55 private NumericCharacterReference(final Source source, final int begin, final int end, final int codePoint, final boolean hex) { 56 super(source,begin,end,codePoint); 57 this.hex=hex; 58 } 59 60 69 public boolean isDecimal() { 70 return !hex; 71 } 72 73 82 public boolean isHexadecimal() { 83 return hex; 84 } 85 86 104 public static String encode(final CharSequence unencodedText) { 105 if (unencodedText==null) return null; 106 final StringBuffer sb=new StringBuffer (unencodedText.length()*2); 107 for (int i=0; i<unencodedText.length(); i++) { 108 final char ch=unencodedText.charAt(i); 109 if (requiresEncoding(ch)) { 110 appendDecimalCharacterReferenceString(sb,ch); 111 } else { 112 sb.append(ch); 113 } 114 } 115 return sb.toString(); 116 } 117 118 133 public static String encodeDecimal(final CharSequence unencodedText) { 134 return encode(unencodedText); 135 } 136 137 152 public static String encodeHexadecimal(final CharSequence unencodedText) { 153 if (unencodedText==null) return null; 154 final StringBuffer sb=new StringBuffer (unencodedText.length()*2); 155 for (int i=0; i<unencodedText.length(); i++) { 156 final char ch=unencodedText.charAt(i); 157 if (requiresEncoding(ch)) { 158 appendHexadecimalCharacterReferenceString(sb,ch); 159 } else { 160 sb.append(ch); 161 } 162 } 163 return sb.toString(); 164 } 165 166 186 public String getCharacterReferenceString() { 187 return hex ? getHexadecimalCharacterReferenceString(codePoint) : getDecimalCharacterReferenceString(codePoint); 188 } 189 190 210 public static String getCharacterReferenceString(final int codePoint) { 211 return getDecimalCharacterReferenceString(codePoint); 212 } 213 214 static CharacterReference construct(final Source source, final int begin, final Config.UnterminatedCharacterReferenceSettings unterminatedCharacterReferenceSettings) { 215 final ParseText parseText=source.getParseText(); 217 int codePointStringBegin=begin+2; 218 boolean hex; 219 if (hex=(parseText.charAt(codePointStringBegin)=='x')) codePointStringBegin++; 220 final int unterminatedMaxCodePoint=hex ? unterminatedCharacterReferenceSettings.hexadecimalCharacterReferenceMaxCodePoint : unterminatedCharacterReferenceSettings.decimalCharacterReferenceMaxCodePoint; 221 final int maxSourcePos=parseText.length()-1; 222 String codePointString; 223 int end; 224 int x=codePointStringBegin; 225 boolean unterminated=false; 226 while (true) { 227 final char ch=parseText.charAt(x); 228 if (ch==';') { 229 end=x+1; 230 codePointString=parseText.substring(codePointStringBegin,x); 231 break; 232 } 233 if ((ch<'0' || ch>'9') && (!hex || ch<'a' || ch>'f')) { 234 unterminated=true; 237 } else if (x==maxSourcePos) { 238 unterminated=true; 240 x++; } 242 if (unterminated) { 243 if (unterminatedMaxCodePoint==INVALID_CODE_POINT) { 246 return null; 248 } else { 249 end=x; 251 codePointString=parseText.substring(codePointStringBegin,x); 252 break; 253 } 254 } 255 x++; 256 } 257 if (codePointString.length()==0) return null; 258 int codePoint=INVALID_CODE_POINT; 259 try { 260 codePoint=Integer.parseInt(codePointString,hex?16:10); 261 if (unterminated && codePoint>unterminatedMaxCodePoint) return null; 262 if (codePoint>MAX_CODE_POINT) codePoint=INVALID_CODE_POINT; 263 } catch (NumberFormatException ex) { 264 if (unterminated) return null; 266 } 268 return new NumericCharacterReference(source,begin,end,codePoint,hex); 269 } 270 271 public String getDebugInfo() { 272 final StringBuffer sb=new StringBuffer (); 273 sb.append('"'); 274 if (hex) 275 appendHexadecimalCharacterReferenceString(sb,codePoint); 276 else 277 appendDecimalCharacterReferenceString(sb,codePoint); 278 sb.append("\" "); 279 appendUnicodeText(sb,codePoint); 280 sb.append(' ').append(super.getDebugInfo()); 281 return sb.toString(); 282 } 283 } 284 285 | Popular Tags |