📄 outputdocument.java
字号:
// Jericho HTML Parser - Java based library for analysing and manipulating HTML
// Version 3.0
// Copyright (C) 2007 Martin Jericho
// http://jerichohtml.sourceforge.net/
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of either one of the following licences:
//
// 1. The Eclipse Public License (EPL) version 1.0,
// included in this distribution in the file licence-epl-1.0.html
// or available at http://www.eclipse.org/legal/epl-v10.html
//
// 2. The GNU Lesser General Public License (LGPL) version 2.1 or later,
// included in this distribution in the file licence-lgpl-2.1.txt
// or available at http://www.gnu.org/licenses/lgpl.txt
//
// This library is distributed on an "AS IS" basis,
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
// See the individual licence texts for more details.
package net.htmlparser.jericho;
import java.io.*;
import java.util.*;
/**
* Represents a modified version of an original {@link Source} document.
* <p>
* An <code>OutputDocument</code> represents an original source document 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 <code>replace</code>, <code>remove</code> or <code>insert</code> methods in this class.
* These methods internally {@linkplain #register(OutputSegment) register} one or more {@link OutputSegment} objects to define each substitution.
* <p>
* After all of the substitutions have been registered, the modified text can be retrieved using the
* {@link #writeTo(Writer)} or {@link #toString()} methods.
* <p>
* The registered {@linkplain OutputSegment 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.
* <p>
* If unexpected results are being generated from an <code>OutputDocument</code>, the {@link #getDebugInfo()} method provides information on each
* {@linkplain #getRegisteredOutputSegments() 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.
* <p>
* The following example converts all externally referenced style sheets to internal style sheets:
* <p>
* <pre>
* 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();
* </pre>
*
* @see OutputSegment
*/
public final class OutputDocument implements CharStreamSource {
private CharSequence sourceText;
private ArrayList<OutputSegment> outputSegments=new ArrayList<OutputSegment>();
/**
* Constructs a new output document based on the specified source document.
* @param source the source document.
*/
public OutputDocument(final Source source) {
if (source==null) throw new IllegalArgumentException("source argument must not be null");
this.sourceText=source;
}
OutputDocument(final ParseText parseText) {
this.sourceText=parseText;
}
/**
* Returns the original source text upon which this output document is based.
* @return the original source text upon which this output document is based.
*/
public CharSequence getSourceText() {
return sourceText;
}
/**
* Removes the specified {@linkplain Segment segment} from this output document.
* <p>
* This is equivalent to {@link #replace(Segment,CharSequence) replace}<code>(segment,null)</code>.
*
* @param segment the segment to remove.
*/
public void remove(final Segment segment) {
register(new RemoveOutputSegment(segment));
}
/**
* Removes all the segments from this output document represented by the specified source {@linkplain Segment} objects.
* <p>
* This is equivalent to the following code:<pre>
* for (Iterator i=segments.iterator(); i.hasNext();)
* {@link #remove(Segment) remove}((Segment)i.next());</pre>
*
* @param segments a collection of segments to remove, represented by source {@link Segment} objects.
*/
public void remove(final Collection<? extends Segment> segments) {
for (Segment segment : segments) remove(segment);
}
/**
* Inserts the specified text at the specified character position in this output document.
* @param pos the character position at which to insert the text.
* @param text the replacement text.
*/
public void insert(final int pos, final CharSequence text) {
register(new StringOutputSegment(pos,pos,text));
}
/**
* Replaces the specified {@linkplain Segment segment} in this output document with the specified text.
* <p>
* Specifying a <code>null</code> argument to the <code>text</code> parameter is exactly equivalent to specifying an empty string,
* and results in the segment being completely removed from the output document.
*
* @param segment the segment to replace.
* @param text the replacement text, or <code>null</code> to remove the segment.
*/
public void replace(final Segment segment, final CharSequence text) {
replace(segment.getBegin(),segment.getEnd(),text);
}
/**
* Replaces the specified segment of this output document with the specified text.
* <p>
* Specifying a <code>null</code> argument to the <code>text</code> parameter is exactly equivalent to specifying an empty string,
* and results in the segment being completely removed from the output document.
*
* @param begin the character position at which to begin the replacement.
* @param end the character position at which to end the replacement.
* @param text the replacement text, or <code>null</code> to remove the segment.
*/
public void replace(final int begin, final int end, final CharSequence text) {
register(new StringOutputSegment(begin,end,text));
}
/**
* Replaces the specified segment of this output document with the specified character.
*
* @param begin the character position at which to begin the replacement.
* @param end the character position at which to end the replacement.
* @param ch the replacement character.
*/
public void replace(final int begin, final int end, final char ch) {
register(new CharOutputSegment(begin,end,ch));
}
/**
* Replaces the specified {@link FormControl} in this output document.
* <p>
* The effect of this method is to {@linkplain #register(OutputSegment) register} zero or more
* {@linkplain OutputSegment output segments} in the output document as required to reflect
* previous modifications to the control's state.
* The state of a control includes its <a href="FormControl.html#SubmissionValue">submission value</a>,
* {@linkplain FormControl#setOutputStyle(FormControlOutputStyle) output style}, and whether it has been
* {@linkplain FormControl#setDisabled(boolean) disabled}.
* <p>
* 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.
* <p>
* 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.
*
* @param formControl the form control to replace.
* @see #replace(FormFields)
*/
public void replace(final FormControl formControl) {
formControl.replaceInOutputDocument(this);
}
/**
* {@linkplain #replace(FormControl) Replaces} all the constituent {@linkplain FormControl form controls}
* from the specified {@link FormFields} in this output document.
* <p>
* This is equivalent to the following code:
* <pre>for (Iterator i=formFields.{@link FormFields#getFormControls() getFormControls()}.iterator(); i.hasNext();)
* {@link #replace(FormControl) replace}((FormControl)i.next());</pre>
* <p>
* 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.
*
* @param formFields the form fields to replace.
* @see #replace(FormControl)
*/
public void replace(final FormFields formFields) {
formFields.replaceInOutputDocument(this);
}
/**
* Replaces the specified {@link Attributes} segment in this output document with the name/value entries
* in the returned <code>Map</code>.
* The returned map initially contains entries representing the attributes from the source document,
* which can be modified before output.
* <p>
* The documentation of the {@link #replace(Attributes,Map)} method contains more information about the requirements
* of the map entries.
* <p>
* Specifying a value of <code>true</code> as an argument to the <code>convertNamesToLowerCase</code> parameter
* 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.
* <p>
* Attribute values are automatically {@linkplain CharacterReference#decode(CharSequence) decoded} before
* being loaded into the map.
* <p>
* This method is logically equivalent to:<br />
* {@link #replace(Attributes,Map) replace}<code>(attributes, attributes.</code>{@link Attributes#populateMap(Map,boolean) populateMap(new LinkedHashMap<String,String>(),convertNamesToLowerCase)}<code>)</code>
* <p>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -