Class EmbeddedDocumentUtil

java.lang.Object
org.apache.tika.extractor.EmbeddedDocumentUtil

public class EmbeddedDocumentUtil extends Object
Static utility methods to handle common issues with embedded documents.
  • Method Details

    • getEmbeddedDocumentExtractor

      public static EmbeddedDocumentExtractor getEmbeddedDocumentExtractor(ParseContext context)
      Looks up the EmbeddedDocumentExtractor configured for this parse.

      A configured parse (one that has gone through AutoDetectParser) always has one bound in the context. If none is bound -- e.g. a concrete parser was invoked directly with a bare ParseContext -- this returns the stateless ParsingEmbeddedDocumentExtractor.INSTANCE, which delegates to whatever Parser is in the context (or silently skips embedded documents if none is set).

      Parameters:
      context - the parse context
      Returns:
      the EmbeddedDocumentExtractor to use for this parse
    • getStatelessParser

      public static Parser getStatelessParser(ParseContext context)
      Utility function to get the Parser that was sent in to the ParseContext to handle embedded documents. If it is stateful, unwrap it to get its stateless delegating parser.

      If there is no Parser in the parser context, this will return null.

      Parameters:
      context -
      Returns:
    • getDetector

      public static Detector getDetector(ParseContext context)
      Looks up the Detector configured for this parse.

      A configured parse (one that has gone through AutoDetectParser) always has one bound in the context. If none is bound -- e.g. a concrete parser was invoked directly with a bare ParseContext -- this returns NoOpDetector.INSTANCE rather than constructing an SPI-discovered DefaultDetector: an honest "unknown" beats a partially-informed guess from a detector the caller never configured.

      Parameters:
      context - the parse context
      Returns:
      the Detector to use for this parse
    • getMimeTypes

      public static MimeTypes getMimeTypes(ParseContext context)
    • getExtension

      public static String getExtension(TikaInputStream is, Metadata metadata, ParseContext context)
    • normalizeMediaType

      public static String normalizeMediaType(String mediaType)
      Normalizes internal OCR routing media types (e.g., image/ocr-png) back to standard media types (e.g., image/png). Returns the input unchanged if it is not an OCR routing type.
      Parameters:
      mediaType - the media type string
      Returns:
      the normalized media type string, or the original if no normalization needed
    • getExtensionForMediaType

      public static String getExtensionForMediaType(String mediaType)
      Looks up the file extension for a given media type string.
      Parameters:
      mediaType - the media type string (e.g., "image/png"), parameters allowed
      Returns:
      the extension including the dot (e.g., ".png"), or empty string if unknown
    • generateResourceName

      public static String generateResourceName(EmbeddedDocumentUtil.EmbeddedResourcePrefix type, int count, String mediaType)
      Generates a canonical resource name from a type, counter, and media type. For example: generateResourceName(EmbeddedResourcePrefix.EMBEDDED, 0, "image/png") returns "embedded-0.png".
      Parameters:
      type - the embedded resource type
      count - the counter value
      mediaType - the media type string, or null if unknown
      Returns:
      the generated resource name with extension
    • setGeneratedResourceName

      public static void setGeneratedResourceName(Metadata metadata, EmbeddedDocumentUtil.EmbeddedResourcePrefix type, int count, String mediaType)
      Sets a generated resource name on the metadata and marks the extension as inferred.
      Parameters:
      metadata - the metadata to update
      type - the embedded resource type
      count - the counter value
      mediaType - the media type string, or null if unknown
    • recordException

      public static void recordException(Throwable t, Metadata m)
    • recordEmbeddedStreamException

      public static void recordEmbeddedStreamException(Throwable t, Metadata m)
    • tryToFindExistingLeafParser

      public static Parser tryToFindExistingLeafParser(Class clazz, ParseContext context)
      Tries to find an existing parser within the ParseContext. It looks inside of CompositeParsers and ParserDecorators. The use case is when a parser needs to parse an internal stream that is _part_ of the document, e.g. rtf body inside an msg.

      Can return null if the context contains no parser or the correct parser can't be found.

      Parameters:
      clazz - parser class to search for
      context -
      Returns: