Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.itextpdf.styledxmlparser.jsoup.nodes.Document Maven / Gradle / Ivy
Go to download
Styled XML parser is used by iText modules to parse HTML and XML
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2024 Apryse Group NV
Authors: Apryse Software.
This program is offered under a commercial and under the AGPL license.
For commercial licensing, contact us at https://itextpdf.com/sales. For AGPL licensing, see below.
AGPL licensing:
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see .
*/
package com.itextpdf.styledxmlparser.jsoup.nodes;
import com.itextpdf.styledxmlparser.jsoup.helper.DataUtil;
import com.itextpdf.styledxmlparser.jsoup.helper.Validate;
import com.itextpdf.styledxmlparser.jsoup.internal.StringUtil;
import com.itextpdf.styledxmlparser.jsoup.parser.ParseSettings;
import com.itextpdf.styledxmlparser.jsoup.parser.Parser;
import com.itextpdf.styledxmlparser.jsoup.parser.Tag;
import com.itextpdf.styledxmlparser.jsoup.select.Elements;
import com.itextpdf.styledxmlparser.jsoup.select.Evaluator;
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;
import java.util.ArrayList;
import java.util.List;
/**
A HTML Document.
@author Jonathan Hedley, [email protected] */
public class Document extends Element {
private OutputSettings outputSettings = new OutputSettings();
private Parser parser; // the parser used to parse this document
private QuirksMode quirksMode = QuirksMode.noQuirks;
private final String location;
private boolean updateMetaCharset = false;
/**
Create a new, empty Document.
@param baseUri base URI of document
@see com.itextpdf.styledxmlparser.jsoup.Jsoup#parse
@see #createShell
*/
public Document(String baseUri) {
super(Tag.valueOf("#root", ParseSettings.htmlDefault), baseUri);
this.location = baseUri;
this.parser = Parser.htmlParser(); // default, but overridable
}
/**
Create a valid, empty shell of a document, suitable for adding more elements to.
@param baseUri baseUri of document
@return document with html, head, and body elements.
*/
public static Document createShell(String baseUri) {
Validate.notNull(baseUri);
Document doc = new Document(baseUri);
doc.parser = doc.parser();
Element html = doc.appendElement("html");
html.appendElement("head");
html.appendElement("body");
return doc;
}
/**
* Get the URL this Document was parsed from. If the starting URL is a redirect,
* this will return the final URL from which the document was served from.
* Will return an empty string if the location is unknown (e.g. if parsed from a String).
* @return location
*/
public String location() {
return location;
}
/**
* Returns this Document's doctype.
* @return document type, or null if not set
*/
public DocumentType documentType() {
for (Node node : childNodes) {
if (node instanceof DocumentType)
return (DocumentType) node;
else if (!(node instanceof LeafNode)) // scans forward across comments, text, processing instructions etc
break;
}
return null;
}
/**
Find the root HTML element, or create it if it doesn't exist.
@return the root HTML element.
*/
private Element htmlEl() {
for (Element el: childElementsList()) {
if (el.normalName().equals("html"))
return el;
}
return appendElement("html");
}
/**
Get this document's {@code head} element.
As a side-effect, if this Document does not already have a HTML structure, it will be created. If you do not want
that, use {@code #selectFirst("head")} instead.
@return {@code head} element.
*/
public Element head() {
Element html = htmlEl();
for (Element el: html.childElementsList()) {
if (el.normalName().equals("head"))
return el;
}
return html.prependElement("head");
}
/**
Get this document's {@code
} or {@code } element.
As a side-effect , if this Document does not already have a HTML structure, it will be created with a {@code
} element. If you do not want that, use {@code #selectFirst("body")} instead.
@return {@code body} element for documents with a {@code }, a new {@code } element if the document
had no contents, or the outermost {@code element} for frameset documents.
*/
public Element body() {
Element html = htmlEl();
for (Element el: html.childElementsList()) {
if ("body".equals(el.normalName()) || "frameset".equals(el.normalName()))
return el;
}
return html.appendElement("body");
}
/**
Get the string contents of the document's {@code title} element.
@return Trimmed title, or empty string if none set.
*/
public String title() {
// title is a preserve whitespace tag (for document output), but normalised here
Element titleEl = head().selectFirst(titleEval);
return titleEl != null ? StringUtil.normaliseWhitespace(titleEl.text()).trim() : "";
}
private static final Evaluator titleEval = new Evaluator.Tag("title");
/**
Set the document's {@code title} element. Updates the existing element, or adds {@code title} to {@code head} if
not present
@param title string to set as title
*/
public void title(String title) {
Validate.notNull(title);
Element titleEl = head().selectFirst(titleEval);
if (titleEl == null) // add to head
titleEl = head().appendElement("title");
titleEl.text(title);
}
/**
Create a new Element, with this document's base uri. Does not make the new element a child of this document.
@param tagName element tag name (e.g. {@code a})
@return new element
*/
public Element createElement(String tagName) {
return new Element(Tag.valueOf(tagName, ParseSettings.preserveCase), this.baseUri());
}
/**
Normalise the document. This happens after the parse phase so generally does not need to be called.
Moves any text content that is not in the body element into the body.
@return this document after normalisation
*/
public Document normalise() {
Element htmlEl = htmlEl(); // these all create if not found
Element head = head();
body();
// pull text nodes out of root, html, and head els, and push into body. non-text nodes are already taken care
// of. do in inverse order to maintain text order.
normaliseTextNodes(head);
normaliseTextNodes(htmlEl);
normaliseTextNodes(this);
normaliseStructure("head", htmlEl);
normaliseStructure("body", htmlEl);
ensureMetaCharsetElement();
return this;
}
// does not recurse.
private void normaliseTextNodes(Element element) {
List toMove = new ArrayList<>();
for (Node node: element.childNodes) {
if (node instanceof TextNode) {
TextNode tn = (TextNode) node;
if (!tn.isBlank())
toMove.add(tn);
}
}
for (int i = toMove.size()-1; i >= 0; i--) {
Node node = toMove.get(i);
element.removeChild(node);
body().prependChild(new TextNode(" "));
body().prependChild(node);
}
}
// merge multiple or contents into one, delete the remainder, and ensure they are owned by
private void normaliseStructure(String tag, Element htmlEl) {
Elements elements = this.getElementsByTag(tag);
Element master = elements.first(); // will always be available as created above if not existent
if (elements.size() > 1) { // dupes, move contents to master
List toMove = new ArrayList<>();
for (int i = 1; i < elements.size(); i++) {
Node dupe = elements.get(i);
toMove.addAll(dupe.ensureChildNodes());
dupe.remove();
}
for (Node dupe : toMove)
master.appendChild(dupe);
}
// ensure parented by
if (master.parent() != null && !master.parent().equals(htmlEl)) {
htmlEl.appendChild(master); // includes remove()
}
}
@Override
public String outerHtml() {
return super.html(); // no outer wrapper tag
}
/**
Set the text of the {@code body} of this document. Any existing nodes within the body will be cleared.
@param text unencoded text
@return this document
*/
@Override
public Element text(String text) {
body().text(text); // overridden to not nuke doc structure
return this;
}
@Override
public String nodeName() {
return "#document";
}
/**
* Sets the charset used in this document. This method is equivalent
* to {@link OutputSettings#charset(java.nio.charset.Charset)
* OutputSettings.charset(Charset)} but in addition it updates the
* charset / encoding element within the document.
*
*
* This enables
* {@link #updateMetaCharsetElement(boolean) meta charset update}.
*
* If there's no element with charset / encoding information yet it will be created.
* Obsolete charset / encoding definitions are removed!
*
*
Elements used:
*
*
* Html: <meta charset="CHARSET">
* Xml: <?xml version="1.0" encoding="CHARSET">
*
*
* @param charset Charset
*
* @see #updateMetaCharsetElement(boolean)
* @see OutputSettings#charset(java.nio.charset.Charset)
*/
public void charset(Charset charset) {
updateMetaCharsetElement(true);
outputSettings.charset(charset);
ensureMetaCharsetElement();
}
/**
* Returns the charset used in this document. This method is equivalent
* to {@link OutputSettings#charset()}.
*
* @return Current Charset
*
* @see OutputSettings#charset()
*/
public Charset charset() {
return outputSettings.charset();
}
/**
* Sets whether the element with charset information in this document is
* updated on changes through {@link #charset(java.nio.charset.Charset)
* Document.charset(Charset)} or not.
*
*
* If set to false (default) there are no elements modified.
*
* @param update If true the element updated on charset
* changes, false if not
*
* @see #charset(java.nio.charset.Charset)
*/
public void updateMetaCharsetElement(boolean update) {
this.updateMetaCharset = update;
}
/**
* Returns whether the element with charset information in this document is
* updated on changes through {@link #charset(java.nio.charset.Charset)
* Document.charset(Charset)} or not.
*
* @return Returns true if the element is updated on charset
* changes, false if not
*/
public boolean updateMetaCharsetElement() {
return updateMetaCharset;
}
@Override
public Object clone() {
Document clone = (Document) super.clone();
clone.outputSettings = (OutputSettings) this.outputSettings.clone();
return clone;
}
/**
* Ensures a meta charset (html) or xml declaration (xml) with the current
* encoding used. This only applies with
* {@link #updateMetaCharsetElement(boolean) updateMetaCharset} set to
* true , otherwise this method does nothing.
*
*
* An existing element gets updated with the current charset
* If there's no element yet it will be inserted
* Obsolete elements are removed
*
* Elements used:
*
*
* Html: <meta charset="CHARSET">
* Xml: <?xml version="1.0" encoding="CHARSET">
*
*/
private void ensureMetaCharsetElement() {
if (updateMetaCharset) {
OutputSettings.Syntax syntax = outputSettings().syntax();
if (syntax == OutputSettings.Syntax.html) {
Element metaCharset = selectFirst("meta[charset]");
if (metaCharset != null) {
metaCharset.attr("charset", charset().displayName());
} else {
head().appendElement("meta").attr("charset", charset().displayName());
}
select("meta[name=charset]").remove(); // Remove obsolete elements
} else if (syntax == OutputSettings.Syntax.xml) {
Node node = ensureChildNodes().get(0);
if (node instanceof XmlDeclaration) {
XmlDeclaration decl = (XmlDeclaration) node;
if (decl.name().equals("xml")) {
decl.attr("encoding", charset().displayName());
if (decl.hasAttr("version"))
decl.attr("version", "1.0");
} else {
decl = new XmlDeclaration("xml", false);
decl.attr("version", "1.0");
decl.attr("encoding", charset().displayName());
prependChild(decl);
}
} else {
XmlDeclaration decl = new XmlDeclaration("xml", false);
decl.attr("version", "1.0");
decl.attr("encoding", charset().displayName());
prependChild(decl);
}
}
}
}
/**
* A Document's output settings control the form of the text() and html() methods.
*/
public static class OutputSettings implements Cloneable {
/**
* The output serialization syntax.
*/
public enum Syntax {html, xml}
private Entities.EscapeMode escapeMode = Entities.EscapeMode.base;
private Charset charset = DataUtil.UTF_8;
private final ThreadLocal encoderThreadLocal = new ThreadLocal<>(); // initialized by start of OuterHtmlVisitor
Entities.CoreCharset coreCharset; // fast encoders for ascii and utf8
private boolean prettyPrint = true;
private boolean outline = false;
private int indentAmount = 1;
private Syntax syntax = Syntax.html;
public OutputSettings() {}
/**
* Get the document's current HTML escape mode: base
, which provides a limited set of named HTML
* entities and escapes other characters as numbered entities for maximum compatibility; or extended
,
* which uses the complete set of HTML named entities.
*
* The default escape mode is base
.
* @return the document's current escape mode
*/
public Entities.EscapeMode escapeMode() {
return escapeMode;
}
/**
* Set the document's escape mode, which determines how characters are escaped when the output character set
* does not support a given character:- using either a named or a numbered escape.
* @param escapeMode the new escape mode to use
* @return the document's output settings, for chaining
*/
public OutputSettings escapeMode(Entities.EscapeMode escapeMode) {
this.escapeMode = escapeMode;
return this;
}
/**
* Get the document's current output charset, which is used to control which characters are escaped when
* generating HTML (via the html()
methods), and which are kept intact.
*
* Where possible (when parsing from a URL or File), the document's output charset is automatically set to the
* input charset. Otherwise, it defaults to UTF-8.
* @return the document's current charset.
*/
public Charset charset() {
return charset;
}
/**
* Update the document's output charset.
* @param charset the new charset to use.
* @return the document's output settings, for chaining
*/
public OutputSettings charset(Charset charset) {
this.charset = charset;
return this;
}
/**
* Update the document's output charset.
* @param charset the new charset (by name) to use.
* @return the document's output settings, for chaining
*/
public OutputSettings charset(String charset) {
charset(Charset.forName(charset));
return this;
}
CharsetEncoder prepareEncoder() {
// created at start of OuterHtmlVisitor so each pass has own encoder, so OutputSettings can be shared among threads
CharsetEncoder encoder = charset.newEncoder();
encoderThreadLocal.set(encoder);
coreCharset = Entities.getCoreCharsetByName(encoder.charset().name());
return encoder;
}
CharsetEncoder encoder() {
CharsetEncoder encoder = encoderThreadLocal.get();
return encoder != null ? encoder : prepareEncoder();
}
/**
* Get the document's current output syntax.
* @return current syntax
*/
public Syntax syntax() {
return syntax;
}
/**
* Set the document's output syntax. Either {@code html}, with empty tags and boolean attributes (etc), or
* {@code xml}, with self-closing tags.
* @param syntax serialization syntax
* @return the document's output settings, for chaining
*/
public OutputSettings syntax(Syntax syntax) {
this.syntax = syntax;
return this;
}
/**
* Get if pretty printing is enabled. Default is true. If disabled, the HTML output methods will not re-format
* the output, and the output will generally look like the input.
* @return if pretty printing is enabled.
*/
public boolean prettyPrint() {
return prettyPrint;
}
/**
* Enable or disable pretty printing.
* @param pretty new pretty print setting
* @return this, for chaining
*/
public OutputSettings prettyPrint(boolean pretty) {
prettyPrint = pretty;
return this;
}
/**
* Get if outline mode is enabled. Default is false. If enabled, the HTML output methods will consider
* all tags as block.
* @return if outline mode is enabled.
*/
public boolean outline() {
return outline;
}
/**
* Enable or disable HTML outline mode.
* @param outlineMode new outline setting
* @return this, for chaining
*/
public OutputSettings outline(boolean outlineMode) {
outline = outlineMode;
return this;
}
/**
* Get the current tag indent amount, used when pretty printing.
* @return the current indent amount
*/
public int indentAmount() {
return indentAmount;
}
/**
* Set the indent amount for pretty printing
* @param indentAmount number of spaces to use for indenting each level. Must be {@literal >=} 0.
* @return this, for chaining
*/
public OutputSettings indentAmount(int indentAmount) {
Validate.isTrue(indentAmount >= 0);
this.indentAmount = indentAmount;
return this;
}
@Override
public Object clone() {
OutputSettings clone = (OutputSettings) partialClone();
// new charset and charset encoder
clone.charset(charset.name());
clone.escapeMode = escapeMode;
return clone;
}
private Object partialClone() {
try {
return super.clone();
} catch (CloneNotSupportedException e) {
throw new RuntimeException(e);
}
}
}
/**
* Get the document's current output settings.
* @return the document's current output settings.
*/
public OutputSettings outputSettings() {
return outputSettings;
}
/**
* Set the document's output settings.
* @param outputSettings new output settings.
* @return this document, for chaining.
*/
public Document outputSettings(OutputSettings outputSettings) {
Validate.notNull(outputSettings);
this.outputSettings = outputSettings;
return this;
}
public enum QuirksMode {
noQuirks, quirks, limitedQuirks
}
public QuirksMode quirksMode() {
return quirksMode;
}
public Document quirksMode(QuirksMode quirksMode) {
this.quirksMode = quirksMode;
return this;
}
/**
* Get the parser that was used to parse this document.
* @return the parser
*/
public Parser parser() {
return parser;
}
/**
* Set the parser used to create this document. This parser is then used when further parsing within this document
* is required.
* @param parser the configured parser to use when further parsing is required for this document.
* @return this document, for chaining.
*/
public Document parser(Parser parser) {
this.parser = parser;
return this;
}
}