org.jsoup.nodes.Document.createShell()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(6.0k)|赞(0)|评价(0)|浏览(168)

本文整理了Java中org.jsoup.nodes.Document.createShell()方法的一些代码示例,展示了Document.createShell()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Document.createShell()方法的具体详情如下:
包路径:org.jsoup.nodes.Document
类名称:Document
方法名:createShell

Document.createShell介绍

[英]Create a valid, empty shell of a document, suitable for adding more elements to.
[中]创建一个有效的、空的文档外壳,适合向其中添加更多元素。

代码示例

代码示例来源:origin: org.jsoup/jsoup

/**
 * Parse a fragment of HTML into the {@code body} of a Document.
 *
 * @param bodyHtml fragment of HTML
 * @param baseUri base URI of document (i.e. original fetch location), for resolving relative URLs.
 *
 * @return Document, with empty head, and HTML parsed into body
 */
public static Document parseBodyFragment(String bodyHtml, String baseUri) {
  Document doc = Document.createShell(baseUri);
  Element body = doc.body();
  List<Node> nodeList = parseFragment(bodyHtml, body, baseUri);
  Node[] nodes = nodeList.toArray(new Node[nodeList.size()]); // the node list gets modified when re-parented
  for (int i = nodes.length - 1; i > 0; i--) {
    nodes[i].remove();
  }
  for (Node node : nodes) {
    body.appendChild(node);
  }
  return doc;
}

代码示例来源:origin: org.jsoup/jsoup

public boolean isValidBodyHtml(String bodyHtml) {
  Document clean = Document.createShell("");
  Document dirty = Document.createShell("");
  ParseErrorList errorList = ParseErrorList.tracking(1);
  List<Node> nodes = Parser.parseFragment(bodyHtml, dirty.body(), "", errorList);
  dirty.body().insertChildren(0, nodes);
  int numDiscarded = copySafeNodes(dirty.body(), clean.body());
  return numDiscarded == 0 && errorList.size() == 0;
}

代码示例来源:origin: org.jsoup/jsoup

/**
 Creates a new, clean document, from the original dirty document, containing only elements allowed by the whitelist.
 The original document is not modified. Only elements from the dirt document's <code>body</code> are used.
 @param dirtyDocument Untrusted base document to clean.
 @return cleaned document.
 */
public Document clean(Document dirtyDocument) {
  Validate.notNull(dirtyDocument);
  Document clean = Document.createShell(dirtyDocument.baseUri());
  if (dirtyDocument.body() != null) // frameset documents won't have a body. the clean doc will have empty body.
    copySafeNodes(dirtyDocument.body(), clean.body());
  return clean;
}

代码示例来源:origin: org.jsoup/jsoup

/**
 Determines if the input document <b>body</b>is valid, against the whitelist. It is considered valid if all the tags and attributes
 in the input HTML are allowed by the whitelist, and that there is no content in the <code>head</code>.
 <p>
 This method can be used as a validator for user input. An invalid document will still be cleaned successfully
 using the {@link #clean(Document)} document. If using as a validator, it is recommended to still clean the document
 to ensure enforced attributes are set correctly, and that the output is tidied.
 </p>
 @param dirtyDocument document to test
 @return true if no tags or attributes need to be removed; false if they do
 */
public boolean isValid(Document dirtyDocument) {
  Validate.notNull(dirtyDocument);
  Document clean = Document.createShell(dirtyDocument.baseUri());
  int numDiscarded = copySafeNodes(dirtyDocument.body(), clean.body());
  return numDiscarded == 0
    && dirtyDocument.head().childNodes().size() == 0; // because we only look at the body, but we start from a shell, make sure there's nothing in the head
}

代码示例来源:origin: com.vaadin/vaadin-server

Document document = Document.createShell("");
BootstrapPageResponse pageResponse = new BootstrapPageResponse(this,
    request, context.getSession(), context.getUIClass(),

代码示例来源:origin: br.com.anteros/Anteros-Bean-Validation

/**
 * Returns a document whose {@code <body>} element contains the given HTML fragment.
 */
private Document getFragmentAsDocument(CharSequence value) {
  // using the XML parser ensures that all elements in the input are retained, also if they actually are not allowed at the given
  // location; E.g. a <td> element isn't allowed directly within the <body> element, so it would be used by the default HTML parser.
  // we need to retain it though to apply the given white list properly; See HV-873
  Document fragment = Jsoup.parse( value.toString(), "", Parser.xmlParser() );
  Document document = Document.createShell( "" );
  // add the fragment's nodes to the body of resulting document
  Iterator<Element> nodes = fragment.children().iterator();
  while ( nodes.hasNext() ) {
    document.body().appendChild( nodes.next() );
  }
  return document;
}

代码示例来源:origin: org.hibernate.validator/hibernate-validator

/**
   * Returns a document whose {@code <body>} element contains the given HTML fragment.
   */
  private Document getFragmentAsDocument(CharSequence value) {
    // using the XML parser ensures that all elements in the input are retained, also if they actually are not allowed at the given
    // location; E.g. a <td> element isn't allowed directly within the <body> element, so it would be used by the default HTML parser.
    // we need to retain it though to apply the given white list properly; See HV-873
    Document fragment = Jsoup.parse( value.toString(), baseURI, Parser.xmlParser() );
    Document document = Document.createShell( baseURI );

    // add the fragment's nodes to the body of resulting document
    Iterator<Element> nodes = fragment.children().iterator();
    while ( nodes.hasNext() ) {
      document.body().appendChild( nodes.next() );
    }

    return document;
  }
}

代码示例来源:origin: org.apache.servicemix.bundles/org.apache.servicemix.bundles.hibernate-validator

/**
   * Returns a document whose {@code <body>} element contains the given HTML fragment.
   */
  private Document getFragmentAsDocument(CharSequence value) {
    // using the XML parser ensures that all elements in the input are retained, also if they actually are not allowed at the given
    // location; E.g. a <td> element isn't allowed directly within the <body> element, so it would be used by the default HTML parser.
    // we need to retain it though to apply the given white list properly; See HV-873
    Document fragment = Jsoup.parse( value.toString(), "", Parser.xmlParser() );
    Document document = Document.createShell( "" );

    // add the fragment's nodes to the body of resulting document
    Iterator<Element> nodes = fragment.children().iterator();
    while ( nodes.hasNext() ) {
      document.body().appendChild( nodes.next() );
    }

    return document;
  }
}

代码示例来源:origin: org.kantega.openaksess/openaksess-core

@Override
public Document runFilter(Document document) {
  final Document clean = Document.createShell(document.baseUri());
  if (document.body() != null) // frameset documents won't have a body. the clean doc will have empty body.
    copySafeNodes(document.body(), clean.body());
  return clean;
}

相关文章