public final class

TagSoupDocumentParser

extends java.lang.Object

 java.lang.Object

↳androidx.test.espresso.web.assertion.TagSoupDocumentParser

Gradle dependencies

compile group: 'androidx.test.espresso', name: 'espresso-web', version: '3.5.0-alpha06'

  • groupId: androidx.test.espresso
  • artifactId: espresso-web
  • version: 3.5.0-alpha06

Artifact androidx.test.espresso:espresso-web:3.5.0-alpha06 it located at Google repository (https://maven.google.com/)

Androidx artifact mapping:

androidx.test.espresso:espresso-web com.android.support.test.espresso:espresso-web

Androidx class mapping:

androidx.test.espresso.web.assertion.TagSoupDocumentParser android.support.test.espresso.web.assertion.TagSoupDocumentParser

Overview

Uses TagSoup to parse html into Documents.

Summary

Methods
public static TagSoupDocumentParsernewInstance()

public org.w3c.dom.Documentparse(java.lang.String html)

Parses the given html into an org.w3c.dom.Document.

from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Methods

public static TagSoupDocumentParser newInstance()

public org.w3c.dom.Document parse(java.lang.String html)

Parses the given html into an org.w3c.dom.Document.

Source

/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package androidx.test.espresso.web.assertion;

import static com.google.common.base.Preconditions.checkNotNull;

import java.io.IOException;
import java.io.StringBufferInputStream;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.sax.SAXSource;
import org.ccil.cowan.tagsoup.Parser;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;

/** Uses TagSoup to parse html into Documents. */
// Note: preferring this minimalistic approach to a full implementation of
// javax.xml.parsers.DocumentBuilder.java because we don't need document building capabilities and
// other parsing features.
public final class TagSoupDocumentParser {
  private static final ThreadLocal<TransformerFactory> transformerFactory =
      new ThreadLocal<TransformerFactory>() {
        @Override
        protected TransformerFactory initialValue() {
          return TransformerFactory.newInstance();
        }
      };

  private final Parser parser;

  private TagSoupDocumentParser() throws SAXNotRecognizedException, SAXNotSupportedException {
    parser = new Parser();
    // We do xpath evaluations which are not namespace aware. So make the parser
    // not use any namespaces.
    parser.setFeature(Parser.namespacesFeature, false);
  }

  // VisibleForTesting
  public static TagSoupDocumentParser newInstance()
      throws SAXNotRecognizedException, SAXNotSupportedException {
    return new TagSoupDocumentParser();
  }

  /** Parses the given html into an {@link Document}. */
  // VisibleForTesting
  public Document parse(String html) throws SAXException, IOException {
    checkNotNull(html);
    SAXSource in = new SAXSource(parser, new InputSource(new StringBufferInputStream(html)));
    DOMResult out = new DOMResult();
    try {
      transformerFactory.get().newTransformer().transform(in, out);
    } catch (TransformerException e) {
      throw new SAXException(e);
    }
    return (Document) out.getNode();
  }
}