1 21 package au.id.jericho.lib.html; 22 23 import java.io.*; 24 25 39 public final class Attribute extends Segment { 40 private final String key; 41 private final Segment nameSegment; 42 private final Segment valueSegment; 43 private final Segment valueSegmentIncludingQuotes; 44 45 static final String CHECKED="checked"; 46 static final String CLASS="class"; 47 static final String DISABLED="disabled"; 48 static final String ID="id"; 49 static final String MULTIPLE="multiple"; 50 static final String NAME="name"; 51 static final String SELECTED="selected"; 52 static final String STYLE="style"; 53 static final String TYPE="type"; 54 static final String VALUE="value"; 55 56 65 Attribute(final Source source, final String key, final Segment nameSegment) { 66 this(source,key,nameSegment,null,null); 67 } 68 69 85 Attribute(final Source source, final String key, final Segment nameSegment, final Segment valueSegment, final Segment valueSegmentIncludingQuotes) { 86 super(source,nameSegment.getBegin(),(valueSegmentIncludingQuotes==null ? nameSegment.getEnd() : valueSegmentIncludingQuotes.getEnd())); 87 this.key=key; 88 this.nameSegment=nameSegment; 89 this.valueSegment=valueSegment; 90 this.valueSegmentIncludingQuotes=valueSegmentIncludingQuotes; 91 } 92 93 103 public String getKey() { 104 return key; 105 } 106 107 115 public String getName() { 116 return nameSegment.toString(); 117 } 118 119 124 public Segment getNameSegment() { 125 return nameSegment; 126 } 127 128 138 public boolean hasValue() { 139 return valueSegment!=null; 140 } 141 142 187 public String getValue() { 188 return CharacterReference.decode(valueSegment,true); 189 } 190 191 196 public Segment getValueSegment() { 197 return valueSegment; 198 } 199 200 208 public Segment getValueSegmentIncludingQuotes() { 209 return valueSegmentIncludingQuotes; 210 } 211 212 219 public char getQuoteChar() { 220 if (valueSegment==valueSegmentIncludingQuotes) return ' '; return source.charAt(valueSegmentIncludingQuotes.getBegin()); 222 } 223 224 228 public String getDebugInfo() { 229 final StringBuffer sb=new StringBuffer ().append(key).append(super.getDebugInfo()).append(",name=").append(nameSegment.getDebugInfo()); 230 if (hasValue()) 231 sb.append(",value=").append(valueSegment.getDebugInfo()).append('"').append(valueSegment).append("\"\n"); 232 else 233 sb.append(",NO VALUE\n"); 234 return sb.toString(); 235 } 236 237 Tag appendTidy(final StringBuffer sb, Tag nextTag) { 238 sb.append(' '); 239 Util.appendTo(sb,nameSegment); 240 if (valueSegment!=null) { 241 sb.append("=\""); 242 while (nextTag!=null && nextTag.begin<valueSegment.begin) nextTag=nextTag.findNextTag(); 243 if (nextTag==null || nextTag.begin>=valueSegment.end) { 244 appendTidyValue(sb,valueSegment); 245 } else { 246 int i=valueSegment.begin; 247 while (nextTag!=null && nextTag.begin<valueSegment.end) { 248 appendTidyValue(sb,new Segment(source,i,nextTag.begin)); 249 if (nextTag.end>valueSegment.end) { 250 sb.append(new Segment(source,nextTag.begin,i=valueSegment.end)); 251 break; 252 } 253 sb.append(nextTag); 254 i=nextTag.end; 255 nextTag=nextTag.findNextTag(); 256 } 257 if (i<valueSegment.end) appendTidyValue(sb,new Segment(source,i,valueSegment.end)); 258 } 259 sb.append('"'); 260 } 261 return nextTag; 262 } 263 264 private static void appendTidyValue(final StringBuffer sb, final CharSequence unencodedValue) { 265 CharacterReference.appendEncode(sb,CharacterReference.decode(unencodedValue,true),false); 266 } 267 268 static StringBuffer appendHTML(final StringBuffer sb, final CharSequence name, final CharSequence value) { 269 sb.append(' '); 270 Util.appendTo(sb,name); 271 if (value!=null) { 272 sb.append("=\""); 273 CharacterReference.appendEncode(sb,value,false); 274 sb.append('"'); 275 } 276 return sb; 277 } 278 279 static Writer appendHTML(final Writer writer, final CharSequence name, final CharSequence value) throws IOException { 280 writer.write(' '); 281 Util.appendTo(writer,name); 282 if (value!=null) { 283 writer.write("=\""); 284 writer.write(CharacterReference.encode(value)); 285 writer.write('"'); 286 } 287 return writer; 288 } 289 } 290 | Popular Tags |