Class PreprocessorContext

java.lang.Object
com.igormaznitsa.jcp.context.PreprocessorContext

public final class PreprocessorContext extends Object
The preprocessor context class is a main class which contains all options of the preprocessor and allows to work with variables in expressions
  • Field Details

    • DEFAULT_SOURCE_DIRECTORY

      public static final String DEFAULT_SOURCE_DIRECTORY
    • DEFAULT_DEST_DIRECTORY

      public static final String DEFAULT_DEST_DIRECTORY
    • DEFAULT_PROCESSING_EXTENSIONS

      public static final String DEFAULT_PROCESSING_EXTENSIONS
      See Also:
    • DEFAULT_EXCLUDED_EXTENSIONS

      public static final String DEFAULT_EXCLUDED_EXTENSIONS
      See Also:
    • DEFAULT_CHARSET

      public static final String DEFAULT_CHARSET
      See Also:
    • verbose

      private boolean verbose
    • removeComments

      private boolean removeComments
    • clearDestinationDirectoryBefore

      private boolean clearDestinationDirectoryBefore
    • fileOutputDisabled

      private boolean fileOutputDisabled
    • keepNonExecutingLines

      private boolean keepNonExecutingLines
    • careForLastNextLine

      private boolean careForLastNextLine
    • compareDestination

      private boolean compareDestination
    • allowWhitespace

      private boolean allowWhitespace
    • preserveIndent

      private boolean preserveIndent
    • copyFileAttributes

      private boolean copyFileAttributes
    • unknownVariableAsFalse

      private boolean unknownVariableAsFalse
    • sourceDirectories

      private String sourceDirectories
    • destinationDirectory

      private String destinationDirectory
    • destinationDirectoryFile

      private File destinationDirectoryFile
    • sourceDirectoryFiles

      private File[] sourceDirectoryFiles
    • processingFileExtensions

      private Set<String> processingFileExtensions
    • excludedFileExtensions

      private Set<String> excludedFileExtensions
    • preprocessorExtension

      private PreprocessorExtension preprocessorExtension
    • inCharacterEncoding

      private String inCharacterEncoding
    • outCharacterEncoding

      private String outCharacterEncoding
    • globalVarTable

      private final Map<String,Value> globalVarTable
    • localVarTable

      private final Map<String,Value> localVarTable
    • mapVariableNameToSpecialVarProcessor

      private final Map<String,SpecialVariableProcessor> mapVariableNameToSpecialVarProcessor
    • sharedResources

      private final Map<String,Object> sharedResources
    • preprocessorLogger

      private PreprocessorLogger preprocessorLogger
    • configFiles

      private final List<File> configFiles
    • currentState

      private transient PreprocessingState currentState
    • cloned

      private final boolean cloned
    • currentInCloneSource

      private final TextFileDataContainer currentInCloneSource
    • excludedFolderPatterns

      private String[] excludedFolderPatterns
  • Constructor Details

    • PreprocessorContext

      public PreprocessorContext()
      The constructor
    • PreprocessorContext

      public PreprocessorContext(PreprocessorContext context)
      Make clone of a preprocessor context but without cloning state.
      Parameters:
      context - the context to be cloned, must not be null.
  • Method Details

    • setExcludedFolderPatterns

      public void setExcludedFolderPatterns(@MustNotContainNull String... patterns)
      Set patterns for excluded folders.
      Parameters:
      patterns - array contains Ant path patterns
    • getExcludedFolderPatterns

      @MustNotContainNull public String[] getExcludedFolderPatterns()
      Get patterns for excluded folders.
      Returns:
      array of patterns in Ant pattern format
    • setCareForLastNextLine

      public void setCareForLastNextLine(boolean flag)
      Set the flag to care to be precise in processing the last file next line char
      Parameters:
      flag - true to turn on the mode, false to turn off
    • isCareForLastNextLine

      public boolean isCareForLastNextLine()
    • isCloned

      public boolean isCloned()
      Check that the preprocessor context is a clone of another context.
      Returns:
      true if the context is a clone, false otherwise
    • setPreprocessorLogger

      public void setPreprocessorLogger(PreprocessorLogger logger)
      Set the logger to print information and error messages
      Parameters:
      logger - a logger to be used for output, it can be null
    • registerSpecialVariableProcessor

      public void registerSpecialVariableProcessor(SpecialVariableProcessor processor)
      It allows to register a special variable processor which can process some special global variables
      Parameters:
      processor - a variable processor to be registered, it must not be null
      See Also:
    • logInfo

      public void logInfo(String text)
      Print an information into the current log
      Parameters:
      text - a String to be printed into the information log, it can be null
    • logError

      public void logError(String text)
      Print an information about an error into the current log
      Parameters:
      text - a String to be printed into the error log, it can be null
    • logDebug

      public void logDebug(String text)
      Print some debug info into the current log
      Parameters:
      text - a String to be printed into the error log, it can be null
      Since:
      6.0.1
    • logWarning

      public void logWarning(String text)
      Print an information about a warning situation into the current log
      Parameters:
      text - a String to be printed into the warning log, it can be null
    • setRemoveComments

      public PreprocessorContext setRemoveComments(boolean removingComments)
      Set the remove comments flag
      Parameters:
      removingComments - the flag to set, true if comments must be removed from the result files, otherwise else
      Returns:
      the preprocessor context instance
    • isRemoveComments

      public boolean isRemoveComments()
      It returns the flag shows that all comments must be removed from the result
      Returns:
      true if comments must be returned, otherwise false
    • setFileOutputDisabled

      public void setFileOutputDisabled(boolean flag)
      It allows to disable all writing operations of the preprocessor
      Parameters:
      flag - true if preprocessor must not make any writing operations, otherwise false
    • isFileOutputDisabled

      public boolean isFileOutputDisabled()
      Check that writing operations is disabled
      Returns:
      true if writing operations disabled, otherwise false
    • setAllowWhitespace

      public void setAllowWhitespace(boolean flag)
      Set flag to allow whitespace between directive and comment chars.
      Parameters:
      flag - true if whitespace allowed, false otherwise
    • isAllowWhitespace

      public boolean isAllowWhitespace()
      Get flag that whitespace allowed between directive and comment.
      Returns:
      true if whitespace allowed, false otherwise
    • setUnknownVariableAsFalse

      public void setUnknownVariableAsFalse(boolean flag)
      Set flag to interpret unknown variable value as FALSE.
      Parameters:
      flag - true to turn on mode when unknown variable will be recognized as FALSE
    • isUnknownVariableAsFalse

      public boolean isUnknownVariableAsFalse()
      Get flag shows that unknown variable is recognized as FALSE.
      Returns:
      true if unknown variable must be recognized as FALSE.
    • setPreserveIndent

      public void setPreserveIndent(boolean flag)
      Set flag to control whether prefixes "//$", "//$$" should replaced with equal length whitespace strings rather than just removed.
      Parameters:
      flag - true enables preserve-indent, false disables it
    • isPreserveIndent

      public boolean isPreserveIndent()
      Get flag indicating whether preserve-indent is enabled or disabled.
      Returns:
      true if preserve-indent is enabled, false otherwise
    • setSourceDirectories

      public PreprocessorContext setSourceDirectories(String directories)
      Set source directories
      Parameters:
      directories - semi separated list of source directories, must not be null
      Returns:
      this preprocessor context instance
    • setSharedResource

      public void setSharedResource(String name, Object obj)
      Set a shared source, it is an object saved into the inside map for a name
      Parameters:
      name - the name for the saved project, must not be null
      obj - the object to be saved in, must not be null
    • getSharedResource

      public Object getSharedResource(String name)
      Get a shared source from inside map
      Parameters:
      name - the name of the needed object, it must not be null
      Returns:
      a cached object or null if it is not found
    • removeSharedResource

      public Object removeSharedResource(String name)
      Remove a shared object from the inside map for its name
      Parameters:
      name - the object name, it must not be null
      Returns:
      removing object or null if it is not found
    • getSourceDirectories

      public String getSourceDirectories()
      Get the source directories as semi separated string
      Returns:
      the current source directories semi separated list
    • getSourceDirectoryAsFiles

      @MustNotContainNull public File[] getSourceDirectoryAsFiles()
      Get the current source directories as a file array
      Returns:
      the current source directories as a file array
    • getParsedSourceDirectoryAsFiles

      @MustNotContainNull private File[] getParsedSourceDirectoryAsFiles()
      Inside auxiliary method to parse the source directories list into file array
      Returns:
      parsed file list, each file must exist and be a directory
    • setDestinationDirectory

      public PreprocessorContext setDestinationDirectory(String directory)
      Set a destination directory for result files
      Parameters:
      directory - a path to the directory as String, it must not be null
      Returns:
      this preprocessor context instance
    • getDestinationDirectoryAsFile

      public File getDestinationDirectoryAsFile()
      Get the current destination directory as a File object
      Returns:
      the current destination directory as an object
    • getDestinationDirectory

      public String getDestinationDirectory()
      Get the string representation of the destination directory
      Returns:
      the current destination directory as a String
    • setProcessingFileExtensions

      public PreprocessorContext setProcessingFileExtensions(String extensions)
      Set file extensions of files to be preprocessed, it is a comma separated list
      Parameters:
      extensions - comma separated extensions list of file extensions to be preprocessed, must not be null
      Returns:
      this preprocessor context
    • getProcessingFileExtensions

      @MustNotContainNull public String[] getProcessingFileExtensions()
      Get file extensions of files to be preprocessed as a string array
      Returns:
      a string array of file extensions to be preprocessed
    • isFileAllowedToBeProcessed

      public final boolean isFileAllowedToBeProcessed(File file)
      Check that a file is allowed to be preprocessed fo its extension
      Parameters:
      file - a file to be checked
      Returns:
      true if the file is allowed, false otherwise
    • isFileExcludedFromProcess

      public final boolean isFileExcludedFromProcess(File file)
      Check that a file is excluded from preprocessing and coping actions
      Parameters:
      file - a file to be checked
      Returns:
      true if th file must be excluded, otherwise false
    • setExcludedFileExtensions

      public PreprocessorContext setExcludedFileExtensions(String extensions)
      Set comma separated list of file extensions to be excluded from preprocessing
      Parameters:
      extensions - a comma separated file extension list, it must not be null
      Returns:
      this preprocessor context
    • getExcludedFileExtensions

      @MustNotContainNull public String[] getExcludedFileExtensions()
      Get excluded file extension list as a string array
      Returns:
      a string array contains file extensions to be excluded from preprocessing act
    • setClearDestinationDirBefore

      public PreprocessorContext setClearDestinationDirBefore(boolean flag)
      Set the flag to clear the destination directory before preprocessing
      Parameters:
      flag - true if the directory must be cleaned, otherwise false
      Returns:
      this preprocessor context
    • doesClearDestinationDirBefore

      public boolean doesClearDestinationDirBefore()
      Get the flag to clear the destination directory before preprocessing
      Returns:
      true if the directory must be cleaned, otherwise false
    • setLocalVariable

      public PreprocessorContext setLocalVariable(String name, Value value)
      Set a local variable value
      Parameters:
      name - the variable name, must not be null, remember that the name will be normalized and will be entirely in lower case
      value - the value for the variable, it must not be null
      Returns:
      this preprocessor context
      See Also:
    • removeLocalVariable

      public PreprocessorContext removeLocalVariable(String name)
      Remove a local variable value from the context.
      Parameters:
      name - the variable name, must not be null, remember that the name will be normalized and will be entirely in lower case
      Returns:
      this preprocessor context
      See Also:
    • removeGlobalVariable

      public PreprocessorContext removeGlobalVariable(String name)
      Remove a global variable value from the context.
      Parameters:
      name - the variable name, must not be null, remember that the name will be normalized and will be entirely in lower case
      Returns:
      this preprocessor context
      See Also:
    • getLocalVariable

      public Value getLocalVariable(String name)
      Get a local variable value
      Parameters:
      name - the name for the variable, it can be null. The name will be normalized to allowed one.
      Returns:
      null either if the name is null or the variable is not found, otherwise its value
    • containsLocalVariable

      public boolean containsLocalVariable(String name)
      Check that a local variable for a name is presented
      Parameters:
      name - the checking name, it will be normalized to the support format and can be null
      Returns:
      false either if the name is null or there is not any local variable for the name, otherwise true
    • clearLocalVariables

      public PreprocessorContext clearLocalVariables()
      Remove all local variables from the inside storage
      Returns:
      this preprocessor context
    • setGlobalVariable

      public PreprocessorContext setGlobalVariable(String name, Value value)
      Set a global variable value
      Parameters:
      name - the variable name, it must not be null and will be normalized to the supported format
      value - the variable value, it must not be null
      Returns:
      this preprocessor context
    • containsGlobalVariable

      public boolean containsGlobalVariable(String name)
      Check that there is a named global variable in the inside storage
      Parameters:
      name - the checking name, it will be normalized to the supported format, it can be null
      Returns:
      true if such variable is presented for its name in the inside storage, otherwise false (also it is false if the name is null)
    • findVariableForName

      public Value findVariableForName(String name, boolean enforceUnknownVarAsNull)
      Find value among local and global variables for a name. It finds in the order: special processors, local variables, global variables
      Parameters:
      name - the name for the needed variable, it will be normalized to the supported format
      enforceUnknownVarAsNull - if true then state of the unknownVariableAsFalse flag in context will be ignored
      Returns:
      false if either the variable is not found or the name is null, otherwise the variable value
    • isGlobalVariable

      public boolean isGlobalVariable(String variableName)
      Check that there is a global variable with such name.
      Parameters:
      variableName - a name to be checked, can be null
      Returns:
      false if there is not such variable or it is null, true if such global or special variable exists
    • isLocalVariable

      public boolean isLocalVariable(String variableName)
      Check that there is a local variable with such name.
      Parameters:
      variableName - a name to be checked, can be null
      Returns:
      false if there is not such variable or it is null, true if such local variable exists
    • setVerbose

      public PreprocessorContext setVerbose(boolean flag)
      Set the verbose flag
      Parameters:
      flag - true if the preprocessor must be verbose, otherwise false
      Returns:
      this preprocessor context
    • isVerbose

      public boolean isVerbose()
      Check the verbose flag
      Returns:
      true if the preprocessor must be verbose, otherwise false
    • setCompareDestination

      public PreprocessorContext setCompareDestination(boolean flag)
      Set the flag to check before saving if the content changed.
      Parameters:
      flag - true if to check, false otherwise
      Returns:
      the preprocessor context
    • isCompareDestination

      public boolean isCompareDestination()
      Check the flag to check content of existing file before saving.
      Returns:
      true if the content should be checked and new content must not be replaced if it is the same
    • setKeepLines

      public PreprocessorContext setKeepLines(boolean flag)
      Set the flag to keep lines as commented ones
      Parameters:
      flag - true if the preprocessor must keep non-executing lines, otherwise false
      Returns:
      this preprocessor context
    • isKeepLines

      public boolean isKeepLines()
      Check that the preprocessor must keep lines as commented ones
      Returns:
      true if the preprocessor must keep lines, false otherwise
    • isCopyFileAttributes

      public boolean isCopyFileAttributes()
      Check that the preprocessor must copy file attributes.
      Returns:
      true if the preprocessor must copy file attributes, false otherwise.
    • setCopyFileAttributes

      public PreprocessorContext setCopyFileAttributes(boolean value)
      Set the flag to copy file attributes.
      Parameters:
      value - true if file attributes must be copied, false otherwise.
      Returns:
      the preprocessor context
    • setPreprocessorExtension

      public PreprocessorContext setPreprocessorExtension(PreprocessorExtension extension)
      Set a preprocessor extension, it is a module implements the PreprocessorExtension interface which can process and get some calls from a preprocessor during its work
      Parameters:
      extension - an object implements the PreprocessorExtension interface, it can be null
      Returns:
      this preprocessor context
      See Also:
    • getPreprocessorExtension

      public PreprocessorExtension getPreprocessorExtension()
      Get the current preprocessor extension
      Returns:
      the current preprocessor extension, it can be null
      See Also:
    • setInCharacterEncoding

      public PreprocessorContext setInCharacterEncoding(String characterEncoding)
      Set the character encoding for reading texts, it must be supported by the Java platform else an exception will be thrown
      Parameters:
      characterEncoding - a character encoding as a String, it must not be null and must be supported by the Java platform
      Returns:
      this preprocessor context
    • setOutCharacterEncoding

      public PreprocessorContext setOutCharacterEncoding(String characterEncoding)
      Set the output texts character encoding, it must be supported by the Java platform else an exception will be thrown
      Parameters:
      characterEncoding - a character encoding as a String, it must not be null and must be supported by the Java platform
      Returns:
      this preprocessor context
    • getInCharacterEncoding

      public String getInCharacterEncoding()
      Get the current character encoding for text reading
      Returns:
      the current read texts character encoding as a String
    • getOutCharacterEncoding

      public String getOutCharacterEncoding()
      Get the current character encoding for text writing
      Returns:
      the current text writing character encoding as a String
    • createDestinationFileForPath

      public File createDestinationFileForPath(String path)
      It allows to create a File object for its path subject to the destination directory path
      Parameters:
      path - the path to the file, it must not be null
      Returns:
      a generated File object for the path
    • findFileInSourceFolder

      public File findFileInSourceFolder(String path) throws IOException
      It finds a file for its path among files in source folder, it is prohibited to return files out of preprocessing folders.
      Parameters:
      path - the path to the needed file, it must not be null and the file must exist and be a file and be among files in preprocessing source folders
      Returns:
      detected file object for the path
      Throws:
      IOException - if it is impossible to find a file for the path
    • addConfigFile

      public void addConfigFile(File file)
      Add a configuration file, it is a file which contains directives and global variable definitions
      Parameters:
      file - a file, it must not be null
    • getConfigFiles

      @MustNotContainNull public File[] getConfigFiles()
      Get array of current registered configuration files
      Returns:
      a file array contains the current registered configuration files
    • produceNewPreprocessingState

      public PreprocessingState produceNewPreprocessingState(FileInfoContainer fileContainer, int phaseIndex) throws IOException
      Generate new preprocessing state object, also the new preprocessing state will be saved as the current one in the context
      Parameters:
      fileContainer - a file container which will be using the preprocessor state, it must not be null
      phaseIndex - index of phase (0 - global, 1 - preprocessing)
      Returns:
      new generated preprocessor state
      Throws:
      IOException - it will be throws if there is any error in opening and reading operations
    • produceNewPreprocessingState

      public PreprocessingState produceNewPreprocessingState(FileInfoContainer fileContainer, TextFileDataContainer textContainer)
      Generate new preprocessing state for a file container and a text container, also the new preprocessing state will be saved as the current one in the context
      Parameters:
      fileContainer - the file container to be used to create the new preprocessing state, it must not be null
      textContainer - the text container to be used to create the new preprocessing state, it must not be null
      Returns:
      new generated preprocessing state
    • getPreprocessingState

      public PreprocessingState getPreprocessingState()
      Get the last generated preprocessing state, it is the current one
      Returns:
      the last generated preprocessing state
    • makeException

      public PreprocessorException makeException(String text, Throwable cause)
      Prepare exception with message and cause, or return cause if it is a preprocessor exception
      Parameters:
      text - the message text, must not be null
      cause - the cause, it can be null
      Returns:
      prepared exception with additional information
    • logForVerbose

      public void logForVerbose(String str)
    • makeStackView

      private static String makeStackView(TextFileDataContainer cloneSource, boolean cloned, @MustNotContainNull List<TextFileDataContainer> list)