Class CharsetDetector

java.lang.Object
org.archive.format.text.charset.CharsetDetector
Direct Known Subclasses:
RotatingCharsetDetector, StandardCharsetDetector

public abstract class CharsetDetector extends Object
Abstract class containing common methods for determining the character encoding of a text Resource, most of which should be refactored into a Util package.
Author:
brad
  • Field Details

    • MAX_CHARSET_READAHEAD

      protected static final int MAX_CHARSET_READAHEAD
      See Also:
    • CHARSET_TOKEN

      protected static final String CHARSET_TOKEN
      See Also:
    • HTTP_CONTENT_TYPE_HEADER

      protected static final String HTTP_CONTENT_TYPE_HEADER
      See Also:
    • DEFAULT_CHARSET

      public static final String DEFAULT_CHARSET
      the default charset name to use when giving up
  • Constructor Details

    • CharsetDetector

      public CharsetDetector()
  • Method Details

    • isCharsetSupported

      protected boolean isCharsetSupported(String charsetName)
    • mapCharset

      protected String mapCharset(String orig)
    • contentTypeToCharset

      protected String contentTypeToCharset(String contentType)
    • getCharsetFromHeaders

      protected String getCharsetFromHeaders(HttpHeaders headers) throws IOException
      Attempt to divine the character encoding of the document from the Content-Type HTTP header (with a "charset=")
      Returns:
      String character set found or null if the header was not present
      Throws:
      IOException
    • getCharsetFromMeta

      protected String getCharsetFromMeta(byte[] buffer, int len) throws IOException
      Attempt to find a META tag in the HTML that hints at the character set used to write the document.
      Returns:
      String character set found from META tags in the HTML
      Throws:
      IOException
    • findMetaContentType

      public static String findMetaContentType(String pageSample)
    • getCharsetFromBytes

      protected String getCharsetFromBytes(byte[] buffer, int len) throws IOException
      Attempts to figure out the character set of the document using the excellent juniversalchardet library.
      Returns:
      String character encoding found, or null if nothing looked good.
      Throws:
      IOException
    • getCharset

      public abstract String getCharset(byte[] buffer, int len, HttpHeaders headers) throws IOException
      Returns:
      String charset name for the Resource
      Throws:
      IOException - if there are problems reading the Resource