1 21 package au.id.jericho.lib.html; 22 23 import java.io.*; 24 import java.util.*; 25 26 80 public final class OutputDocument implements CharStreamSource { 81 private CharSequence sourceText; 82 private ArrayList outputSegments=new ArrayList(); 83 84 88 public OutputDocument(final Source source) { 89 if (source==null) throw new IllegalArgumentException ("source argument must not be null"); 90 this.sourceText=source; 91 } 92 93 OutputDocument(final ParseText parseText) { 94 this.sourceText=parseText; 95 } 96 97 101 public CharSequence getSourceText() { 102 return sourceText; 103 } 104 105 112 public void remove(final Segment segment) { 113 replace(segment,(CharSequence )null); 114 } 115 116 125 public void remove(final Collection segments) { 126 for (Iterator i=segments.iterator(); i.hasNext();) remove((Segment)i.next()); 127 } 128 129 134 public void insert(final int pos, final CharSequence text) { 135 register(new StringOutputSegment(pos,pos,text)); 136 } 137 138 147 public void replace(final Segment segment, final CharSequence text) { 148 replace(segment.getBegin(),segment.getEnd(),text); 149 } 150 151 161 public void replace(final int begin, final int end, final CharSequence text) { 162 register(new StringOutputSegment(begin,end,text)); 163 } 164 165 172 public void replace(final int begin, final int end, final char ch) { 173 register(new CharOutputSegment(begin,end,ch)); 174 } 175 176 199 public void replace(final FormControl formControl) { 200 formControl.replaceInOutputDocument(this); 201 } 202 203 219 public void replace(final FormFields formFields) { 220 formFields.replaceInOutputDocument(this); 221 } 222 223 262 public Map replace(final Attributes attributes, boolean convertNamesToLowerCase) { 263 AttributesOutputSegment attributesOutputSegment=new AttributesOutputSegment(attributes,convertNamesToLowerCase); 264 register(attributesOutputSegment); 265 return attributesOutputSegment.getMap(); 266 } 267 268 292 public void replace(final Attributes attributes, final Map map) { 293 register(new AttributesOutputSegment(attributes,map)); 294 } 295 296 308 public void replaceWithSpaces(final int begin, final int end) { 309 register(new BlankOutputSegment(begin,end)); 310 } 311 312 319 public void register(final OutputSegment outputSegment) { 320 outputSegments.add(outputSegment); 321 } 322 323 336 public void writeTo(final Writer writer) throws IOException { 337 if (outputSegments.isEmpty()) { 338 Util.appendTo(writer,sourceText); 339 return; 340 } 341 int pos=0; 342 Collections.sort(outputSegments,OutputSegment.COMPARATOR); 343 OutputSegment lastOutputSegment=null; 344 for (final Iterator i=outputSegments.iterator(); i.hasNext();) { 345 final OutputSegment outputSegment=(OutputSegment)i.next(); 346 if (outputSegment==lastOutputSegment) continue; if (outputSegment.getBegin()<pos) throw new OverlappingOutputSegmentsException(lastOutputSegment,outputSegment); 348 if (outputSegment.getBegin()>pos) Util.appendTo(writer,sourceText,pos,outputSegment.getBegin()); 349 outputSegment.writeTo(writer); 350 lastOutputSegment=outputSegment; 351 pos=outputSegment.getEnd(); 352 } 353 if (pos<sourceText.length()) Util.appendTo(writer,sourceText,pos,sourceText.length()); 354 writer.close(); 355 } 356 357 public long getEstimatedMaximumOutputLength() { 358 long estimatedMaximumOutputLength=sourceText.length(); 359 for (final Iterator i=outputSegments.iterator(); i.hasNext();) { 360 final OutputSegment outputSegment=(OutputSegment)i.next(); 361 final int outputSegmentOriginalLength=outputSegment.getEnd()-outputSegment.getBegin(); 362 estimatedMaximumOutputLength+=(outputSegment.getEstimatedMaximumOutputLength()-outputSegmentOriginalLength); 363 } 364 return estimatedMaximumOutputLength; 365 } 366 367 373 public String toString() { 374 return CharStreamSourceUtil.toString(this); 375 } 376 377 386 public OutputDocument(final CharSequence sourceText) { 387 if (sourceText==null) throw new IllegalArgumentException ("sourceText argument must not be null"); 388 this.sourceText=sourceText; 389 } 390 391 400 public void add(final OutputSegment outputSegment) { 401 register(outputSegment); 402 } 403 404 413 public void add(final FormControl formControl) { 414 replace(formControl); 415 } 416 417 427 public void add(final FormFields formFields) { 428 formFields.replaceInOutputDocument(this); 429 } 430 431 441 public void output(final Writer writer) throws IOException { 442 writeTo(writer); 443 } 444 445 455 public Reader getReader() { 456 return CharStreamSourceUtil.getReader(this); 457 } 458 } 459 | Popular Tags |