Class FileReaderWriter

java.lang.Object
de.monticore.io.FileReaderWriter

public class FileReaderWriter extends Object
This class handles all I/O commands in Monticore.

Important conventions: File locations are always encoded as Path or URL objects. When creating Path objects please make use of the Path.resolve(java.nio.file.Path) method whenever applicable. This convention extends beyond the FileHandler. Do not exchange information between classes using String literals or File objects. Doing so within classes is permitted, though discouraged.
Failure to adhere to such a convention has caused innumerable trivial bugs in past implementations.

This class uses UTF_8 encoding per default and is realized as singleton.

  • Field Details

  • Constructor Details

    • FileReaderWriter

      protected FileReaderWriter()
      Uses the default encoding UTF_8;
    • FileReaderWriter

      protected FileReaderWriter(Charset charset)
      Parameters:
      charset - The initial encoding to be used until another encoding is assigned.
      See Also:
  • Method Details

    • setCharset

      public void setCharset(Charset charset)
      Sets the encoding for all subsequent operations until another encoding is assigned.
      Parameters:
      charset - The encoding to be used
    • init

      public static void init()
    • init

      public static void init(Charset charset)
    • init

      public static void init(FileReaderWriter instance)
    • getFileReaderWriter

      protected static FileReaderWriter getFileReaderWriter()
    • storeInFile

      public static void storeInFile(Path targetPath, String content)
      Writes a String to a file using the specified encoding.
      Parameters:
      targetPath - The location of the file to be written to.
      content - The String that's supposed to be written into the file
      See Also:
    • _storeInFile

      protected void _storeInFile(Path targetPath, String content)
    • readFromFile

      public static String readFromFile(Path sourcePath)
      Reads the String content from a file using the specified encoding.
      Parameters:
      sourcePath - The absolute location (fully specifies the filename) of the file to be read
      Returns:
      The String content of the file
      See Also:
    • _readFromFile

      protected String _readFromFile(Path sourcePath)
    • readFromFile

      public static String readFromFile(URL sourcePath)
      Reads the String content from a file using the specified encoding. URLs can also address files within jars.
      Parameters:
      sourcePath - The absolute location (fully specifies the filename) of the file to be read
      Returns:
      The String content of the file
      See Also:
    • _readFromFile

      protected String _readFromFile(URL sourcePath)
    • readFromFile

      public static String readFromFile(Reader reader)
    • _readFromFile

      protected String _readFromFile(Reader reader)
    • getResource

      public static Optional<URL> getResource(ClassLoader classLoader, String name)
      Tries to load a resource with the passed name in the file system using the passed classLoader. If multiple resources are found, throws an ambiguity exception. If no resource is found, returns Optional.empty()
      Parameters:
      classLoader - The classloader employed to load the resource
      name - The name of the resource to load
      Returns:
      The optional URL of the resource, if present, or Optional.empty() else
    • _getResource

      protected Optional<URL> _getResource(ClassLoader classLoader, String name)
    • existsFile

      public static boolean existsFile(Path sourcePath)
    • _existsFile

      protected boolean _existsFile(Path sourcePath)
    • getReader

      public static Reader getReader(URL location)
      Obtains the reader for a passed model coordinate. The resulting reader can be used as argument for a parse method of a language's parser.
      Parameters:
      location -
      Returns:
    • _getReader

      protected Reader _getReader(URL location)
    • closeOpenedJarFiles

      public static void closeOpenedJarFiles()
      Closes all jar files opened by getReader(URL). Must be called at the end of the program to ensure all resources are freed.