ru.novosoft.dc.core
Class NSDCContext

java.lang.Object
  |
  +--ru.novosoft.dc.core.NSDCContext
Direct Known Subclasses:
RTF_TO_XMLContext

public abstract class NSDCContext
extends java.lang.Object

The core context for a converter of rtf-file. It provides logging and management of options. The following options must be specified while instaniation of this class:

config-path
A path to the directory containing configuration files (e.g. "/conf");

options-file-extension
An extension for files with options (e.g. ".opt");

plugin-path
A path to the plug-ins configuration directory (e.g. "/plugins");

plugin-file-extension
An extension for plug-in configuration files (e.g. ".config");

data-path
A path to the directory with XML data files used in options (e.g. "/data"); and

log-path
A path to the directory for logging (e.g. "/log").

All above mensioned options are frozen while instaniation and will be never changed later.


Field Summary
static byte FULL_LOG
          This log level means logging of all messages to files and to the system log (debug messages and messages on unknown rtf-commands are logged to files only).
static byte FULL_SILENT_LOG
          This log level means logging of all messages to files.
static byte INFO_LOG
          This log level means logging of information messages to the system log.
static byte NO_LOG
          This log level means no logging at all.
static byte NORMAL_LOG
          This log level means logging of all except debug messages to a file and to the system log.
static java.lang.String NSDC_PROPERTIES
          The default path of the file of converter's properties.
static byte SILENT_LOG
          This log level means logging of all except debug messages to a file.
 
Constructor Summary
protected NSDCContext(java.lang.String homePath, java.util.Properties properties, java.io.PrintStream systemLog, java.lang.String[] names)
          Construct an instance of the context, create a default logger of the INFO_LOG logging level, and register the specified properties.
 
Method Summary
 java.lang.String configPath()
          Return the canonical path to the converter's configuration directory.
 java.lang.String dataPath()
          Return the canonical path to the converter's data directory.
 java.lang.String findConfigFile(java.lang.String filename, java.lang.String extension)
          Find a file with the specified filename and return its canonical path.
protected  void freezeCoreOptions()
          Freeze core options against changes.
 CommonLogger getDefaultLogger()
          Return the default logger.
 double getDouble(java.lang.String name, double defaultValue)
          Return a double value of the option with the specified name or the default value if this option undefined or contains non-double value.
 java.lang.String getFileExtension(java.lang.String name, java.lang.String defaultExtension)
          Get an option value and test it to be a valid file extension, which starts from the decimal point and contains alphanumeric symbols after it (ASCII only).
 float getFloat(java.lang.String name, float defaultValue)
          Return a float value of the option with the specified name or the default value if this option undefined or contains non-float value.
 int getInt(java.lang.String name, int defaultValue)
          Return an integer value of the option with the specified name or the default value if this option undefined or contains non-integer value.
 java.lang.String getNonemptyOption(java.lang.String name)
          Return an option value if it is specified and is a nonempty string.
 java.lang.String getOption(java.lang.String name)
          Return a value of option with the specified name or null if this option undefined.
 java.lang.String getOption(java.lang.String name, java.lang.String defaultValue)
          Return a value of option with the specified name or the default value if this option undefined.
 java.lang.String getOptionAsPath(java.lang.String name)
          Get a value of option with the specified name and find the path using the IOService.findFile method with the home path and the value of this option passed in parameters.
 java.lang.String getSpecifiedOption(java.lang.String name)
          Get an option value and ensure that it is not null, i.e. the option is specified.
abstract  java.lang.String getUserPreferencesPath()
          Return a name of file of user preferences.
 java.lang.String homePath()
          Return the canonical path to the converter's home directory.
 void info(java.lang.String message)
          Send the message to the default logger.
 boolean isTrue(java.lang.String name)
          Return true if and only if the option with specified name is defined and its value is equal to "true".
static java.util.Properties loadConfiguration(java.lang.String homePath, java.lang.String filename)
          Load configuration properties from the specified file and return them.
 void loadOptions(java.lang.String filename)
          Load options from file.
 boolean loadUserPreferences()
          Load user preference options from the file of user preferences (returned by getUserPreferencesPath()).
 void loadUserPreferences(java.util.Properties properties)
          Load user preference options from a specified set of properties.
protected  boolean logOption(CommonLogger logger, java.lang.String name)
          Log an option to the logger if it is specified.
protected static boolean logOption(CommonLogger logger, java.lang.String name, java.lang.String value)
          Log an option to the logger if it has a non-null value.
protected  boolean logOptionIfNonempty(CommonLogger logger, java.lang.String name)
          Log an option to the logger if its value is nonempty string.
protected  boolean logOptionIfTrue(CommonLogger logger, java.lang.String name)
          Log an option to the logger if its value is equal to "true".
 void logOptions(CommonLogger logger)
          The method is designed for logging options to the specified logger just before a conversion.
 java.lang.String logPath(java.lang.String logDirectoryPath)
          Return the log path specified by the logDirectoryPath and the "log-name" option.
abstract  CommonLogger newLogger(java.lang.String path, int level)
          Construct a new logger of the specified log-level and return it.
abstract  PicturePool newPicturePool(java.lang.String path, boolean relative)
          Construct a new picture pool using the specified path as a parent name for pictures.
 java.util.Properties options()
          Return all options specified.
 boolean parseOption(java.lang.String option)
          Parse a string having the form "key:value" and call the setOption(key, value) method.
 java.lang.String pluginExtension()
          Return the extension for plugin configuration files.
 java.lang.String pluginPath()
          Return the canonical path to the plugin's directory.
 void setDefaultLogger(CommonLogger logger)
          Set the default logger.
 void setOption(java.lang.String name, java.lang.String value)
          Set an individual converter's option.
 void setOptionProcessor(java.lang.String name, OptionProcessor processor)
          Set a processor for the specified option name.
 void setOptions(java.util.Properties properties, java.lang.String namePrefix)
          Set options using only those properties of the passed properties' table which start from the specified prefix.
 java.io.PrintStream systemLog()
          Return a system log specified for this context.
 boolean validateOptions()
          Validate setting of options and return true if the validation is successful.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NO_LOG

public static final byte NO_LOG
This log level means no logging at all.

INFO_LOG

public static final byte INFO_LOG
This log level means logging of information messages to the system log.

SILENT_LOG

public static final byte SILENT_LOG
This log level means logging of all except debug messages to a file.

NORMAL_LOG

public static final byte NORMAL_LOG
This log level means logging of all except debug messages to a file and to the system log.

FULL_SILENT_LOG

public static final byte FULL_SILENT_LOG
This log level means logging of all messages to files.

FULL_LOG

public static final byte FULL_LOG
This log level means logging of all messages to files and to the system log (debug messages and messages on unknown rtf-commands are logged to files only).

NSDC_PROPERTIES

public static final java.lang.String NSDC_PROPERTIES
The default path of the file of converter's properties.
Constructor Detail

NSDCContext

protected NSDCContext(java.lang.String homePath,
                      java.util.Properties properties,
                      java.io.PrintStream systemLog,
                      java.lang.String[] names)
Construct an instance of the context, create a default logger of the INFO_LOG logging level, and register the specified properties. Properties starting from the "system." prefix are registered as system properties and properties starting from the "nsdc." prefix are registered as options. The prefixes are trimmed while registration. Other properties are ignored.
Parameters:
homePath - a canonical path to converter's home directory
properties - configuration properties of the converter
systemLog - a print stream used as the system log in all loggers
names - an array of convertor-specific names of options
Method Detail

freezeCoreOptions

protected final void freezeCoreOptions()
Freeze core options against changes. This method should be used after all setOptions methods in instaniation.

setOptions

public void setOptions(java.util.Properties properties,
                       java.lang.String namePrefix)
Set options using only those properties of the passed properties' table which start from the specified prefix. While adding an option, this prefix is trimmed.

setOption

public final void setOption(java.lang.String name,
                            java.lang.String value)
Set an individual converter's option.

setOptionProcessor

public final void setOptionProcessor(java.lang.String name,
                                     OptionProcessor processor)
Set a processor for the specified option name. The processor is called everytime a new value for the specified option should be set (right before setting of a new value).

parseOption

public boolean parseOption(java.lang.String option)
Parse a string having the form "key:value" and call the setOption(key, value) method. If the delimiter ":" and value are absent, the "true" value is set for this option. Return true if the option was successfully set. If the key is empty, return false.

options

public final java.util.Properties options()
Return all options specified.

getOption

public final java.lang.String getOption(java.lang.String name)
Return a value of option with the specified name or null if this option undefined.

getOption

public final java.lang.String getOption(java.lang.String name,
                                        java.lang.String defaultValue)
Return a value of option with the specified name or the default value if this option undefined.

getInt

public final int getInt(java.lang.String name,
                        int defaultValue)
Return an integer value of the option with the specified name or the default value if this option undefined or contains non-integer value.

getFloat

public final float getFloat(java.lang.String name,
                            float defaultValue)
Return a float value of the option with the specified name or the default value if this option undefined or contains non-float value.

getDouble

public final double getDouble(java.lang.String name,
                              double defaultValue)
Return a double value of the option with the specified name or the default value if this option undefined or contains non-double value.

getFileExtension

public final java.lang.String getFileExtension(java.lang.String name,
                                               java.lang.String defaultExtension)
Get an option value and test it to be a valid file extension, which starts from the decimal point and contains alphanumeric symbols after it (ASCII only). If the extension is valid, it is returned. Otherwise, the defaultExtension is returned. The returned result is converted to lower case.

getSpecifiedOption

public final java.lang.String getSpecifiedOption(java.lang.String name)
Get an option value and ensure that it is not null, i.e. the option is specified. If the option is unspecified, the NSDCException is thrown.

getNonemptyOption

public final java.lang.String getNonemptyOption(java.lang.String name)
Return an option value if it is specified and is a nonempty string. Otherwise, return null.

getOptionAsPath

public final java.lang.String getOptionAsPath(java.lang.String name)
Get a value of option with the specified name and find the path using the IOService.findFile method with the home path and the value of this option passed in parameters.
Returns:
a canonical path
Throws:
NSDCException - if find unsuccessful.

isTrue

public final boolean isTrue(java.lang.String name)
Return true if and only if the option with specified name is defined and its value is equal to "true".

setDefaultLogger

public final void setDefaultLogger(CommonLogger logger)
Set the default logger.

getDefaultLogger

public final CommonLogger getDefaultLogger()
Return the default logger.

info

public final void info(java.lang.String message)
Send the message to the default logger.

systemLog

public final java.io.PrintStream systemLog()
Return a system log specified for this context.

homePath

public final java.lang.String homePath()
Return the canonical path to the converter's home directory.

configPath

public final java.lang.String configPath()
Return the canonical path to the converter's configuration directory.

dataPath

public final java.lang.String dataPath()
Return the canonical path to the converter's data directory.

logPath

public java.lang.String logPath(java.lang.String logDirectoryPath)
Return the log path specified by the logDirectoryPath and the "log-name" option. If "log-name" value starts with "file:", the file specified after is used as a template name for log files and the logDirectoryPath value is ignored. If the logDirectoryPath is null, the value of "log-path" option is used instead.

pluginPath

public final java.lang.String pluginPath()
Return the canonical path to the plugin's directory.

pluginExtension

public final java.lang.String pluginExtension()
Return the extension for plugin configuration files.

findConfigFile

public java.lang.String findConfigFile(java.lang.String filename,
                                       java.lang.String extension)
Find a file with the specified filename and return its canonical path. The following search algorithm is applied: 1 Try to find the file as is; 2 If the attempt fails, try to find the file in the path specified by the "config-path" option; 3 If the attempt fails again, add the specified extension to the file and repeat the Steps 1 and 2. If file not found after all these attempts, the NSDCException is thrown. The null value of the extension parameter reduces the search algorithm to the executing of first two steps only.

loadOptions

public void loadOptions(java.lang.String filename)
Load options from file. File is searched using the findConfigFile method with extension specified in the "options-file-extension" option.
Throws:
NSDCException - if loading of options fails

loadUserPreferences

public boolean loadUserPreferences()
Load user preference options from the file of user preferences (returned by getUserPreferencesPath()). While loading, only known options are selected. Other options are ignored.
Returns:
true if preferences were successfully loaded

loadUserPreferences

public void loadUserPreferences(java.util.Properties properties)
Load user preference options from a specified set of properties. While loading, only known options are selected. Other options are ignored.

loadConfiguration

public static java.util.Properties loadConfiguration(java.lang.String homePath,
                                                     java.lang.String filename)
                                              throws java.io.IOException
Load configuration properties from the specified file and return them.
Parameters:
homePath - a converter's home directory path
filename - a name of configuration file
Returns:
the configuration properties
Throws:
NSDCException - if cannot find the configuration file
java.io.IOException - if an error occur while opening or reading configuration

validateOptions

public boolean validateOptions()
Validate setting of options and return true if the validation is successful. If some options are invalid, the corresponding message is logged and false is returned. The implementation in the current class simply returns true. All validations is applied in subclasses.

logOptions

public void logOptions(CommonLogger logger)
The method is designed for logging options to the specified logger just before a conversion. The default implementation does nothing. It must be overriden in subclasses.

logOption

protected final boolean logOption(CommonLogger logger,
                                  java.lang.String name)
Log an option to the logger if it is specified.
Returns:
true if an option was logged

logOptionIfNonempty

protected final boolean logOptionIfNonempty(CommonLogger logger,
                                            java.lang.String name)
Log an option to the logger if its value is nonempty string.
Returns:
true if an option was logged

logOptionIfTrue

protected final boolean logOptionIfTrue(CommonLogger logger,
                                        java.lang.String name)
Log an option to the logger if its value is equal to "true".
Returns:
true if an option was logged

logOption

protected static final boolean logOption(CommonLogger logger,
                                         java.lang.String name,
                                         java.lang.String value)
Log an option to the logger if it has a non-null value.
Returns:
true if an option was logged

getUserPreferencesPath

public abstract java.lang.String getUserPreferencesPath()
Return a name of file of user preferences.

newLogger

public abstract CommonLogger newLogger(java.lang.String path,
                                       int level)
Construct a new logger of the specified log-level and return it. The path is used as a template name for log files. Files with the same name and ".log" and ".log0" extensions will be used for logging. The ".log" file is used if the log mode is not equal to the INFO_LOG and NO_LOG. The ".log0" file is used if the log mode is equal to the FULL_SILENT_LOG or FULL_LOG.

newPicturePool

public abstract PicturePool newPicturePool(java.lang.String path,
                                           boolean relative)
Construct a new picture pool using the specified path as a parent name for pictures.
Parameters:
path - a path used for preparing of picture directory name
relative - if true, the URL of a picture in the FO file will contain the path relative to the directory of this path. Otherwise, the canonical path will be stored in the URL.
Returns:
a new picture pool


Other Novosoft LLC products:
Handy Backup Handy Password manager