Class ClassPathSource

java.lang.Object
dev.langchain4j.data.document.source.ClassPathSource
All Implemented Interfaces:
DocumentSource

public class ClassPathSource extends Object implements DocumentSource
Specialization of a DocumentSource that knows how to read from the classpath.

Use ClassPathDocumentLoader to load Documents using this.

  • Constructor Details

    • ClassPathSource

      protected ClassPathSource(URL url, ClassLoader classLoader)
  • Method Details

    • from

      public static ClassPathSource from(String classPathResource)
      Creates a new instance of ClassPathSource from the given classpath resource string, using Thread.currentThread().getContextClassLoader() as the class loader.
      Parameters:
      classPathResource - The path of the classpath resource to be loaded.
      Returns:
      A ClassPathSource instance representing the classpath resource.
      Throws:
      IllegalArgumentException - if the classpath resource is blank or cannot be found.
      See Also:
    • from

      public static ClassPathSource from(String classPathResource, ClassLoader classLoader)
      Creates a new instance of ClassPathSource from the given classpath resource and classloader.
      Parameters:
      classPathResource - The path of the classpath resource to be loaded. Must not be blank.
      classLoader - The class loader to use for loading the resource. If null then uses Thread.currentThread().getContextClassLoader() as the class loader.
      Returns:
      A ClassPathSource instance representing the classpath resource.
      Throws:
      IllegalArgumentException - If the classpath resource is blank or cannot be found, or if the class loader is null.
    • url

      public URL url()
      Retrieves the URL associated with this ClassPathSource.
      Returns:
      The URL instance representing the classpath resource.
    • classLoader

      public ClassLoader classLoader()
      The ClassLoader that was used to load this ClassPathSource
      Returns:
      The ClassLoader that was used to load this ClassPathSource
    • isInsideArchive

      public boolean isInsideArchive()
      Determines if the resource represented by this ClassPathSource is inside an archive.
      Returns:
      true if the resource is packaged inside an archive, otherwise false.
    • inputStream

      public InputStream inputStream() throws IOException
      Description copied from interface: DocumentSource
      Provides an InputStream to read the content of the document. This method can be implemented to read from various sources like a local file or a network connection.
      Specified by:
      inputStream in interface DocumentSource
      Returns:
      An InputStream from which the document content can be read.
      Throws:
      IOException - If an I/O error occurs while creating the InputStream.
    • metadata

      public Metadata metadata()
      Description copied from interface: DocumentSource
      Returns the metadata associated with the source of the document. This could include details such as the source location, date of creation, owner, etc.
      Specified by:
      metadata in interface DocumentSource
      Returns:
      A Metadata object containing information associated with the source of the document.