Package com.igormaznitsa.jcp.context
Class PreprocessorContext
java.lang.Object
com.igormaznitsa.jcp.context.PreprocessorContext
The preprocessor context class is a main class which contains all options of the preprocessor and allows to work with variables in expressions
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate boolean
private boolean
private boolean
private final boolean
private boolean
private boolean
private final TextFileDataContainer
private PreprocessingState
static final String
static final String
static final String
static final String
static final String
private String
private File
private String[]
private boolean
private String
private boolean
private final Map
<String, SpecialVariableProcessor> private String
private PreprocessorExtension
private PreprocessorLogger
private boolean
private boolean
private String
private File[]
private boolean
private boolean
-
Constructor Summary
ConstructorsConstructorDescriptionThe constructorPreprocessorContext
(PreprocessorContext context) Make clone of a preprocessor context but without cloning state. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addConfigFile
(File file) Add a configuration file, it is a file which contains directives and global variable definitionsRemove all local variables from the inside storageboolean
containsGlobalVariable
(String name) Check that there is a named global variable in the inside storageboolean
containsLocalVariable
(String name) Check that a local variable for a name is presentedIt allows to create a File object for its path subject to the destination directory pathboolean
Get the flag to clear the destination directory before preprocessingfindFileInSourceFolder
(String path) It finds a file for its path among files in source folder, it is prohibited to return files out of preprocessing folders.findVariableForName
(String name, boolean enforceUnknownVarAsNull) Find value among local and global variables for a name.File[]
Get array of current registered configuration filesGet the string representation of the destination directoryGet the current destination directory as a File objectString[]
Get excluded file extension list as a string arrayString[]
Get patterns for excluded folders.Get the current character encoding for text readinggetLocalVariable
(String name) Get a local variable valueGet the current character encoding for text writingprivate File[]
Inside auxiliary method to parse the source directories list into file arrayGet the last generated preprocessing state, it is the current oneGet the current preprocessor extensionString[]
Get file extensions of files to be preprocessed as a string arraygetSharedResource
(String name) Get a shared source from inside mapGet the source directories as semi separated stringFile[]
Get the current source directories as a file arrayboolean
Get flag that whitespace allowed between directive and comment.boolean
boolean
isCloned()
Check that the preprocessor context is a clone of another context.boolean
Check the flag to check content of existing file before saving.boolean
Check that the preprocessor must copy file attributes.final boolean
Check that a file is allowed to be preprocessed fo its extensionfinal boolean
Check that a file is excluded from preprocessing and coping actionsboolean
Check that writing operations is disabledboolean
isGlobalVariable
(String variableName) Check that there is a global variable with such name.boolean
Check that the preprocessor must keep lines as commented onesboolean
isLocalVariable
(String variableName) Check that there is a local variable with such name.boolean
Get flag indicating whether preserve-indent is enabled or disabled.boolean
It returns the flag shows that all comments must be removed from the resultboolean
Get flag shows that unknown variable is recognized as FALSE.boolean
Check the verbose flagvoid
Print some debug info into the current logvoid
Print an information about an error into the current logvoid
logForVerbose
(String str) void
Print an information into the current logvoid
logWarning
(String text) Print an information about a warning situation into the current logmakeException
(String text, Throwable cause) Prepare exception with message and cause, or return cause if it is a preprocessor exceptionprivate static String
makeStackView
(TextFileDataContainer cloneSource, boolean cloned, List<TextFileDataContainer> list) produceNewPreprocessingState
(FileInfoContainer fileContainer, int phaseIndex) Generate new preprocessing state object, also the new preprocessing state will be saved as the current one in the contextproduceNewPreprocessingState
(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 contextvoid
It allows to register a special variable processor which can process some special global variablesremoveGlobalVariable
(String name) Remove a global variable value from the context.removeLocalVariable
(String name) Remove a local variable value from the context.removeSharedResource
(String name) Remove a shared object from the inside map for its namevoid
setAllowWhitespace
(boolean flag) Set flag to allow whitespace between directive and comment chars.void
setCareForLastNextLine
(boolean flag) Set the flag to care to be precise in processing the last file next line charsetClearDestinationDirBefore
(boolean flag) Set the flag to clear the destination directory before preprocessingsetCompareDestination
(boolean flag) Set the flag to check before saving if the content changed.setCopyFileAttributes
(boolean value) Set the flag to copy file attributes.setDestinationDirectory
(String directory) Set a destination directory for result filessetExcludedFileExtensions
(String extensions) Set comma separated list of file extensions to be excluded from preprocessingvoid
setExcludedFolderPatterns
(String... patterns) Set patterns for excluded folders.void
setFileOutputDisabled
(boolean flag) It allows to disable all writing operations of the preprocessorsetGlobalVariable
(String name, Value value) Set a global variable valuesetInCharacterEncoding
(String characterEncoding) Set the character encoding for reading texts, it must be supported by the Java platform else an exception will be thrownsetKeepLines
(boolean flag) Set the flag to keep lines as commented onessetLocalVariable
(String name, Value value) Set a local variable valuesetOutCharacterEncoding
(String characterEncoding) Set the output texts character encoding, it must be supported by the Java platform else an exception will be thrownsetPreprocessorExtension
(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 workvoid
Set the logger to print information and error messagesvoid
setPreserveIndent
(boolean flag) Set flag to control whether prefixes "//$", "//$$" should replaced with equal length whitespace strings rather than just removed.setProcessingFileExtensions
(String extensions) Set file extensions of files to be preprocessed, it is a comma separated listsetRemoveComments
(boolean removingComments) Set the remove comments flagvoid
setSharedResource
(String name, Object obj) Set a shared source, it is an object saved into the inside map for a namesetSourceDirectories
(String directories) Set source directoriesvoid
setUnknownVariableAsFalse
(boolean flag) Set flag to interpret unknown variable value as FALSE.setVerbose
(boolean flag) Set the verbose flag
-
Field Details
-
DEFAULT_SOURCE_DIRECTORY
-
DEFAULT_DEST_DIRECTORY
-
DEFAULT_PROCESSING_EXTENSIONS
- See Also:
-
DEFAULT_EXCLUDED_EXTENSIONS
- See Also:
-
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
-
destinationDirectory
-
destinationDirectoryFile
-
sourceDirectoryFiles
-
processingFileExtensions
-
excludedFileExtensions
-
preprocessorExtension
-
inCharacterEncoding
-
outCharacterEncoding
-
globalVarTable
-
localVarTable
-
mapVariableNameToSpecialVarProcessor
-
preprocessorLogger
-
configFiles
-
currentState
-
cloned
private final boolean cloned -
currentInCloneSource
-
excludedFolderPatterns
-
-
Constructor Details
-
PreprocessorContext
public PreprocessorContext()The constructor -
PreprocessorContext
Make clone of a preprocessor context but without cloning state.- Parameters:
context
- the context to be cloned, must not be null.
-
-
Method Details
-
setExcludedFolderPatterns
Set patterns for excluded folders.- Parameters:
patterns
- array contains Ant path patterns
-
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
Set the logger to print information and error messages- Parameters:
logger
- a logger to be used for output, it can be null
-
registerSpecialVariableProcessor
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
Print an information into the current log- Parameters:
text
- a String to be printed into the information log, it can be null
-
logError
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
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
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
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
Set source directories- Parameters:
directories
- semi separated list of source directories, must not be null- Returns:
- this preprocessor context instance
-
getSourceDirectories
Get the source directories as semi separated string- Returns:
- the current source directories semi separated list
-
getSourceDirectoryAsFiles
Get the current source directories as a file array- Returns:
- the current source directories as a file array
-
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
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
Get the current destination directory as a File object- Returns:
- the current destination directory as an object
-
getDestinationDirectory
Get the string representation of the destination directory- Returns:
- the current destination directory as a String
-
setProcessingFileExtensions
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
Get file extensions of files to be preprocessed as a string array- Returns:
- a string array of file extensions to be preprocessed
-
isFileAllowedToBeProcessed
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
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
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
Get excluded file extension list as a string array- Returns:
- a string array contains file extensions to be excluded from preprocessing act
-
setClearDestinationDirBefore
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
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 casevalue
- the value for the variable, it must not be null- Returns:
- this preprocessor context
- See Also:
-
removeLocalVariable
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
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
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
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
Remove all local variables from the inside storage- Returns:
- this preprocessor context
-
setGlobalVariable
Set a global variable value- Parameters:
name
- the variable name, it must not be null and will be normalized to the supported formatvalue
- the variable value, it must not be null- Returns:
- this preprocessor context
-
containsGlobalVariable
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
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 formatenforceUnknownVarAsNull
- 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
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
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
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
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
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
Set the flag to copy file attributes.- Parameters:
value
- true if file attributes must be copied, false otherwise.- Returns:
- the preprocessor context
-
setPreprocessorExtension
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
Get the current preprocessor extension- Returns:
- the current preprocessor extension, it can be null
- See Also:
-
setInCharacterEncoding
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
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
Get the current character encoding for text reading- Returns:
- the current read texts character encoding as a String
-
getOutCharacterEncoding
Get the current character encoding for text writing- Returns:
- the current text writing character encoding as a String
-
createDestinationFileForPath
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
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
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
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 nullphaseIndex
- 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 nulltextContainer
- the text container to be used to create the new preprocessing state, it must not be null- Returns:
- new generated preprocessing state
-
getPreprocessingState
Get the last generated preprocessing state, it is the current one- Returns:
- the last generated preprocessing state
-
makeException
Prepare exception with message and cause, or return cause if it is a preprocessor exception- Parameters:
text
- the message text, must not be nullcause
- the cause, it can be null- Returns:
- prepared exception with additional information
-
logForVerbose
-
makeStackView
private static String makeStackView(TextFileDataContainer cloneSource, boolean cloned, @MustNotContainNull List<TextFileDataContainer> list)
-