ru.novosoft.dc.core
Class NSDCContext

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

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.io.PrintStream systemLog, java.lang.String[] names)
          Construct an instance of the context and create a default logger of the INFO_LOG logging level.
 
Method Summary
 java.lang.String configPath()
          Return the canonical path to the converter's configuration directory.
protected  void correctOption(java.lang.String name, java.lang.String value)
          Correction of option value to the given value.
 java.lang.String dataPath()
          Return the canonical path to the converter's data directory.
 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).
 java.lang.String getFilePath(java.lang.String relPath, java.lang.String path)
          Try to get a resource path using the IOService.getFilePath method with the homePath() as the base path.
 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 or unregistered.
 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 prepare the path composed from home path and the option value.
 java.io.InputStream getResourceAsStream(java.lang.String relPath, java.lang.String path)
          Try to get a resource using the IOService.getResourceAsStream method with the homePath() as the base path and the class of this object as a base class.
 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 getUserPreferences()
          Return a name of user preferences file.
 java.lang.String getUserPreferencesPath()
          Return a path to the user preferences file.
 java.io.InputStream getUserPreferencesStream()
          Try to open an input stream to user preferences and return it or null.
 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".
 void loadConfigurationFrom(java.util.Properties properties)
          Load configuration from the given properties and freeze core options after that.
 void loadConfigurationFrom(java.lang.String filename)
          Load configuration properties from the specified resource, register them at this instance and freeze core options after that.
 void loadOptions(java.lang.String filename)
          Load options from file.
 boolean loadUserPreferences()
          Load user preference options from the file of user preferences (returned by getUserPreferences()).
 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.io.InputStream openDataResource(java.lang.String path)
          Get a resource using the getResourceAsStream method with the value of "data-path" option as the relative path.
 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 or null.
protected  void register(java.util.Properties properties)
          Register specified properties at the context.
 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.io.PrintStream systemLog,
                      java.lang.String[] names)
Construct an instance of the context and create a default logger of the INFO_LOG logging level.
Parameters:
homePath - a canonical path to converter's home directory or null if home path is unknown
systemLog - a print stream used as the system log in all loggers
names - an array of convertor-specific names of options
Method Detail

loadConfigurationFrom

public void loadConfigurationFrom(java.lang.String filename)
                           throws java.io.IOException
Load configuration properties from the specified resource, register them at this instance and freeze core options after that.
Parameters:
filename - a name of configuration file
Throws:
NSDCException - if cannot find the configuration file
java.io.IOException - if an error occur while opening or reading configuration

loadConfigurationFrom

public void loadConfigurationFrom(java.util.Properties properties)
Load configuration from the given properties and freeze core options after that.
Parameters:
properties - configuration properties

register

protected void register(java.util.Properties properties)
Register specified properties at the context. 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:
properties - configuration properties of the converter

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 or unregistered.

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 prepare the path composed from home path and the option value.
Returns:
a canonical path
Throws:
NSDCException - if the path is invalid or cannot be specified

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".

correctOption

protected void correctOption(java.lang.String name,
                             java.lang.String value)
Correction of option value to the given value.

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 specified file 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.
Throws:
NSDCException - if cannot select a log path

pluginPath

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

pluginExtension

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

getResourceAsStream

public final java.io.InputStream getResourceAsStream(java.lang.String relPath,
                                                     java.lang.String path)
Try to get a resource using the IOService.getResourceAsStream method with the homePath() as the base path and the class of this object as a base class.

getFilePath

public final java.lang.String getFilePath(java.lang.String relPath,
                                          java.lang.String path)
Try to get a resource path using the IOService.getFilePath method with the homePath() as the base path.

openDataResource

public final java.io.InputStream openDataResource(java.lang.String path)
Get a resource using the getResourceAsStream method with the value of "data-path" option as the relative path.
Throws:
java.lang.IllegalArgumentException - if cannot open the given resource

loadOptions

public void loadOptions(java.lang.String filename)
Load options from file. File is searched in file system and resources using the following method until succeed attempt:
  1. Try to find the file as is;
  2. Try to find the file in the subdirectory of home directory specified by the "config-path" option;
  3. Try to find the file in resources using the "config-path" option value as a base path for search;
  4. If previous attempts fail, add the extension specified in the "options-file-extension" option to the filename and repeat the Steps 1-3.
Throws:
NSDCException - if loading of options fails

getUserPreferencesStream

public final java.io.InputStream getUserPreferencesStream()
Try to open an input stream to user preferences and return it or null.

getUserPreferencesPath

public final java.lang.String getUserPreferencesPath()
Return a path to the user preferences file.

loadUserPreferences

public boolean loadUserPreferences()
Load user preference options from the file of user preferences (returned by getUserPreferences()). 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.

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 validation 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

getUserPreferences

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

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