Class OutputDocument
- All Implemented Interfaces:
CharStreamSource
Source document or Segment.
An OutputDocument represents an original Source document or Segment that
has been modified by substituting segments of it with other text.
Each of these substitutions must be registered in the output document,
which is most commonly done using the various replace, remove or insert methods in this class.
These methods internally register one or more OutputSegment objects to define each substitution.
If a Segment is used to construct the output document, all character positions are relative to the source document of the specified segment.
After all of the substitutions have been registered, the modified text can be retrieved using the
writeTo(Writer) or toString() methods.
The registered output segments may be adjacent and may also overlap. An output segment that is completely enclosed by another output segment is not included in the output.
If unexpected results are being generated from an OutputDocument, the getDebugInfo() method provides information on each
registered output segment, which should provide enough information to determine the cause of the problem.
In most cases the problem will be caused by overlapping output segments.
The following example converts all externally referenced style sheets to internal style sheets:
URL sourceUrl=new URL(sourceUrlString);
String htmlText=Util.getString(new InputStreamReader(sourceUrl.openStream()));
Source source=new Source(htmlText);
OutputDocument outputDocument=new OutputDocument(source);
StringBuilder sb=new StringBuilder();
List linkStartTags=source.getAllStartTags(HTMLElementName.LINK);
for (Iterator i=linkStartTags.iterator(); i.hasNext();) {
StartTag startTag=(StartTag)i.next();
Attributes attributes=startTag.getAttributes();
String rel=attributes.getValue("rel");
if (!"stylesheet".equalsIgnoreCase(rel)) continue;
String href=attributes.getValue("href");
if (href==null) continue;
String styleSheetContent;
try {
styleSheetContent=Util.getString(new InputStreamReader(new URL(sourceUrl,href).openStream()));
} catch (Exception ex) {
continue; // don't convert if URL is invalid
}
sb.setLength(0);
sb.append("<style");
Attribute typeAttribute=attributes.get("type");
if (typeAttribute!=null) sb.append(' ').append(typeAttribute);
sb.append(">\n").append(styleSheetContent).append("\n</style>");
outputDocument.replace(startTag,sb);
}
String convertedHtmlText=outputDocument.toString();
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionOutputDocument(Segment segment) Constructs a new output document based on the specifiedSegment.OutputDocument(Source source) Constructs a new output document based on the specified source document. -
Method Summary
Modifier and TypeMethodDescriptionvoidappendTo(Appendable appendable) Appends the final content of this output document to the specifiedAppendableobject.voidappendTo(Appendable appendable, int begin, int end) Appends the specified portion of the final content of this output document to the specifiedAppendableobject.Returns a string representation of this object useful for debugging purposes.longReturns the estimated maximum number of characters in the output, or-1if no estimate is available.Returns a list all of the registeredOutputSegmentobjects in this output document.Returns the original segment upon which this output document is based.Returns the original source text upon which this output document is based.voidinsert(int pos, CharSequence text) Inserts the specified text at the specified character position in this output document.voidregister(OutputSegment outputSegment) Registers the specified output segment in this output document.voidremove(int begin, int end) Removes the specified segment of this output document.voidremove(Collection<? extends Segment> segments) Removes all the segments from this output document represented by the specified source Segment objects.voidRemoves the specified segment from this output document.voidreplace(int begin, int end, char ch) Replaces the specified segment of this output document with the specified character.voidreplace(int begin, int end, CharSequence text) Replaces the specified segment of this output document with the specified text.replace(Attributes attributes, boolean convertNamesToLowerCase) Replaces the specifiedAttributessegment in this output document with the name/value entries in the returnedMap.voidreplace(Attributes attributes, Map<String, String> map) Replaces the specified attributes segment in this source document with the name/value entries in the specifiedMap.voidreplace(FormControl formControl) Replaces the specifiedFormControlin this output document.voidreplace(FormFields formFields) voidreplace(Segment segment, CharSequence text) Replaces the specified segment in this output document with the specified text.voidreplaceWithSpaces(int begin, int end) Replaces the specified segment of this output document with a string of spaces of the same length.toString()Returns the final content of this output document as aString.voidWrites the final content of this output document to the specifiedWriter.voidWrites the specified portion of the final content of this output document to the specifiedWriter.
-
Constructor Details
-
OutputDocument
Constructs a new output document based on the specified source document.- Parameters:
source- the source document.
-
OutputDocument
-
-
Method Details
-
getSegment
-
getSourceText
Returns the original source text upon which this output document is based.If a
Segmentwas used to construct the output document, this returns the text of the entire source document rather than just the segment.- Returns:
- the original source text upon which this output document is based.
-
remove
public void remove(int begin, int end) Removes the specified segment of this output document.This is equivalent to
replace(begin,end,null).- Parameters:
begin- the character position at which to begin the removal.end- the character position at which to end the removal.
-
remove
-
remove
Removes all the segments from this output document represented by the specified source Segment objects.This is equivalent to the following code:
for (Iterator i=segments.iterator(); i.hasNext();)
remove((Segment)i.next());- Parameters:
segments- a collection of segments to remove, represented by sourceSegmentobjects.
-
insert
Inserts the specified text at the specified character position in this output document.- Parameters:
pos- the character position at which to insert the text.text- the replacement text.
-
replace
Replaces the specified segment in this output document with the specified text.Specifying a
nullargument to thetextparameter is exactly equivalent to specifying an empty string, and results in the segment being completely removed from the output document.- Parameters:
segment- the segment to replace.text- the replacement text, ornullto remove the segment.
-
replace
Replaces the specified segment of this output document with the specified text.Specifying a
nullargument to thetextparameter is exactly equivalent to specifying an empty string, and results in the segment being completely removed from the output document.- Parameters:
begin- the character position at which to begin the replacement.end- the character position at which to end the replacement.text- the replacement text, ornullto remove the segment.
-
replace
public void replace(int begin, int end, char ch) Replaces the specified segment of this output document with the specified character.- Parameters:
begin- the character position at which to begin the replacement.end- the character position at which to end the replacement.ch- the replacement character.
-
replace
Replaces the specifiedFormControlin this output document.The effect of this method is to register zero or more output segments in the output document as required to reflect previous modifications to the control's state. The state of a control includes its submission value, output style, and whether it has been disabled.
The state of the form control should not be modified after this method is called, as there is no guarantee that subsequent changes either will or will not be reflected in the final output. A second call to this method with the same parameter is not allowed. It is therefore recommended to call this method as the last action before the output is generated.
Although the specifics of the number and nature of the output segments added in any particular circumstance is not defined in the specification, it can generally be assumed that only the minimum changes necessary are made to the original document. If the state of the control has not been modified, calling this method has no effect at all.
- Parameters:
formControl- the form control to replace.- See Also:
-
replace
Replaces all the constituent form controls from the specifiedFormFieldsin this output document.This is equivalent to the following code:
for (Iterator i=formFields.
getFormControls().iterator(); i.hasNext();)replace((FormControl)i.next());The state of any of the form controls in the specified form fields should not be modified after this method is called, as there is no guarantee that subsequent changes either will or will not be reflected in the final output. A second call to this method with the same parameter is not allowed. It is therefore recommended to call this method as the last action before the output is generated.
- Parameters:
formFields- the form fields to replace.- See Also:
-
replace
Replaces the specifiedAttributessegment in this output document with the name/value entries in the returnedMap. The returned map initially contains entries representing the attributes from the source document, which can be modified before output.The documentation of the
replace(Attributes,Map)method contains more information about the requirements of the map entries.Specifying a value of
trueas an argument to theconvertNamesToLowerCaseparameter causes all original attribute names to be converted to lower case in the map. This simplifies the process of finding/updating specific attributes since map keys are case sensitive.Attribute values are automatically decoded before being loaded into the map.
This method is logically equivalent to:
replace(attributes, attributes.populateMap(new LinkedHashMap<String,String>(),convertNamesToLowerCase))The use of
LinkedHashMapto implement the map ensures (probably unnecessarily) that existing attributes are output in the same order as they appear in the source document, and new attributes are output in the same order as they are added.- Example:
Source source=new Source(htmlDocument); Attributes bodyAttributes =source.getNextStartTag(0,HTMLElementName.BODY).getAttributes(); OutputDocument outputDocument=new OutputDocument(source); Map<String,String> attributesMap=outputDocument.replace(bodyAttributes,true); attributesMap.put("bgcolor","green"); String htmlDocumentWithGreenBackground=outputDocument.toString();
- Parameters:
attributes- theAttributessegment defining the span of the segment and initial name/value entries of the returned map.convertNamesToLowerCase- specifies whether all attribute names are converted to lower case in the map.- Returns:
- a
Mapcontaining the name/value entries to be output. - See Also:
-
replace
Replaces the specified attributes segment in this source document with the name/value entries in the specifiedMap.This method might be used if the
Mapcontaining the new attribute values should not be preloaded with the same entries as the source attributes, or a map implementation other thanLinkedHashMapis required. Otherwise, thereplace(Attributes, boolean convertNamesToLowerCase)method is generally more useful.An attribute with no value is represented by a map entry with a
nullvalue.Attribute values are stored unencoded in the map, and are automatically encoded if necessary during output.
The use of invalid characters in attribute names results in unspecified behaviour.
Note that methods in the
Attributesclass treat attribute names as case insensitive, whereas theMaptreats them as case sensitive.- Parameters:
attributes- theAttributesobject defining the span of the segment to replace.map- theMapcontaining the name/value entries.- See Also:
-
replaceWithSpaces
public void replaceWithSpaces(int begin, int end) Replaces the specified segment of this output document with a string of spaces of the same length.This method is most commonly used to remove segments of the document without affecting the character positions of the remaining elements.
It is used internally to implement the functionality available through the
Segment.ignoreWhenParsing()method.To remove a segment from the output document completely, use the
remove(Segment)method instead.- Parameters:
begin- the character position at which to begin the replacement.end- the character position at which to end the replacement.
-
register
Registers the specified output segment in this output document.Use this method if you want to use a customised
OutputSegmentclass.- Parameters:
outputSegment- the output segment to register.
-
writeTo
Writes the final content of this output document to the specifiedWriter.The
writeTo(Writer, int begin, int end)method allows the output of a portion of the output document.If the output is required in the form of a
Reader, useCharStreamSourceUtil.getReader(this)instead.- Specified by:
writeToin interfaceCharStreamSource- Parameters:
writer- the destinationjava.io.Writerfor the output.- Throws:
IOException- if an I/O exception occurs.- See Also:
-
writeTo
Writes the specified portion of the final content of this output document to the specifiedWriter.Any zero-length output segments located at
beginorendare included in the output.- Parameters:
writer- the destinationjava.io.Writerfor the output.begin- the character position at which to start the output, inclusive.end- the character position at which to end the output, exclusive.- Throws:
IOException- if an I/O exception occurs.- See Also:
-
appendTo
Appends the final content of this output document to the specifiedAppendableobject.The
appendTo(Appendable, int begin, int end)method allows the output of a portion of the output document.- Specified by:
appendToin interfaceCharStreamSource- Parameters:
appendable- the destinationjava.lang.Appendableobject for the output.- Throws:
IOException- if an I/O exception occurs.- See Also:
-
appendTo
Appends the specified portion of the final content of this output document to the specifiedAppendableobject.Any zero-length output segments located at
beginorendare included in the output.- Parameters:
appendable- the destinationjava.lang.Appendableobject for the output.begin- the character position at which to start the output, inclusive.end- the character position at which to end the output, exclusive.- Throws:
IOException- if an I/O exception occurs.- See Also:
-
getEstimatedMaximumOutputLength
public long getEstimatedMaximumOutputLength()Description copied from interface:CharStreamSourceReturns the estimated maximum number of characters in the output, or-1if no estimate is available.The returned value should be used as a guide for efficiency purposes only, for example to set an initial
StringBuildercapacity. There is no guarantee that the length of the output is indeed less than this value, as classes implementing this method often use assumptions based on typical usage to calculate the estimate.Although implementations of this method should never return a value less than -1, users of this method must not assume that this will always be the case. Standard practice is to interpret any negative value as meaning that no estimate is available.
- Specified by:
getEstimatedMaximumOutputLengthin interfaceCharStreamSource- Returns:
- the estimated maximum number of characters in the output, or
-1if no estimate is available.
-
toString
Returns the final content of this output document as aString.- Specified by:
toStringin interfaceCharStreamSource- Overrides:
toStringin classObject- Returns:
- the final content of this output document as a
String. - See Also:
-
getDebugInfo
Returns a string representation of this object useful for debugging purposes.The output includes details of all the
registered output segments.- Returns:
- a string representation of this object useful for debugging purposes.
-
getRegisteredOutputSegments
Returns a list all of the registeredOutputSegmentobjects in this output document.The output segments are sorted in order of their starting position in the document.
The returned list is modifiable and any changes will affect the output generated by this
OutputDocument.- Returns:
- a list all of the registered
OutputSegmentobjects in this output document.
-