/*
*
* Copyright (c) 2006 by Know-Center, Graz, Austria
*
*
* This software is the confidential and proprietary information of Know-Center,
* Graz, Austria. You shall not disclose such Confidential Information and shall
* use it only in accordance with the terms of the license agreement you entered
* into with Know-Center.
*
* KNOW-CENTER MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
* SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,
* OR NON-INFRINGEMENT. KNOW-CENTER SHALL NOT BE LIABLE FOR ANY DAMAGES
* SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
* THIS SOFTWARE OR ITS DERIVATIVES.
*
* $Id: Normalizer.java,v 1.5 2006/10/31 08:20:56 wprinz Exp $
*/
package at.knowcenter.wag.egov.egiz.tools;
import java.io.Serializable;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import at.knowcenter.wag.egov.egiz.cfg.ConfigLogger;
import at.knowcenter.wag.egov.egiz.cfg.SettingsReader;
import at.knowcenter.wag.egov.egiz.exceptions.NormalizeException;
import at.knowcenter.wag.egov.egiz.exceptions.SettingsException;
/**
* This class provides wrapper methods to get an access to different normalizer implementations.
*
* This class is to load the corresponding implementation of a normalizer class. Therefor it seams
* to be a factory. The factory settings are read from the configuration file calling the
* SettingsReader.
*
* @author wlackner
* @see at.knowcenter.wag.egov.egiz.tools.Normalizer
* @see at.knowcenter.wag.egov.egiz.tools.NormalizeV01
* @see at.knowcenter.wag.egov.egiz.cfg.SettingsReader
*/
public class Normalizer implements Serializable {
/**
* SVUID.
*/
private static final long serialVersionUID = 4201772508393848555L;
/**
* The current raw string to normalize
*/
private String rawString_ = null;
/**
* The current normalisation version string
*/
private String normVersion_ = null;
/**
* The normalized string cache
*/
private String normString_ = null;
/**
* The reference to the normalizer implementation
*/
private Normalize normalize_ = null;
// /**
// * A given Encoding, not used now
// */
// private String encoding_ = null;
/**
* The SettingsReader instance
*/
private SettingsReader settings_ = null;
/**
* The factory class prefix
*/
private final static String CLASS_PREFIX = ".Normalize";
/**
* The default version string
*/
protected final static String DEFAULT_VERSION = "V01";
/**
* The settings key defined in the settings file
*
* @see SettingsReader
*/
protected final static String SETTINGS_VERSION_KEY = "normalizer.version";
/**
* The logger definition.
*/
private static final Logger logger_ = ConfigLogger.getLogger(Normalizer.class);
/**
* New Normalizer init by the raw string and a normalizer version.
*
* @param rawString the raw string to normalize
* @param normVersion the nomalizer version that should be used
* @throws NormalizeException ErrorCode:400
*/
public Normalizer(String rawString, String normVersion) throws NormalizeException {
rawString_ = rawString;
normVersion_ = normVersion;
init();
}
/**
* New Normalizer init by the raw string.
*
* @param rawString the raw string to normalize
* @throws NormalizeException ErrorCode:400
*/
public Normalizer(String rawString) throws NormalizeException {
rawString_ = rawString;
init();
}
/**
* The empty constructor.
*
* @throws NormalizeException ErrorCode:400
*/
public Normalizer() throws NormalizeException {
init();
}
/**
* Load the factory implementation. This method trys to load the configured normalizer library.
*
* @throws NormalizeException
*/
public void init() throws NormalizeException {
loadSettings();
String class_name = this.getClass().getPackage().getName() + getClassName();
Class normalize_class = null;
try {
normalize_class = Class.forName(class_name);
} catch (ClassNotFoundException e) {
if (logger_.isEnabledFor(Level.FATAL)) {
logger_.fatal("Class not found:" + class_name);
}
NormalizeException ne = new NormalizeException(400, "Can not load normalizer library");
ne.setErrorCode(400);
throw ne;
}
try {
normalize_ = (Normalize) normalize_class.newInstance();
} catch (InstantiationException e) {
if (logger_.isEnabledFor(Level.FATAL)) {
logger_.fatal("Can not instantiate:" + class_name);
}
NormalizeException ne = new NormalizeException(400, "Can not load normalizer library");
ne.setErrorCode(400);
throw ne;
} catch (IllegalAccessException e) {
if (logger_.isEnabledFor(Level.FATAL)) {
logger_.fatal("Can not access:" + class_name);
}
NormalizeException ne = new NormalizeException(400, "Can not load normalizer library");
ne.setErrorCode(400);
throw ne;
}
}
/**
* Returns the underlying normalizer instance.
* @author tknall
*/
public Normalize getInstance() {
return this.normalize_;
}
/**
* Read the class postfix from the configuration file
*
* @return the full qualified class name
*/
private String getClassName() {
if (normVersion_ == null) {
normVersion_ = settings_.getSetting(SETTINGS_VERSION_KEY, DEFAULT_VERSION);
}
return CLASS_PREFIX + normVersion_;
}
/*
* public void setEncoding(String encoding) { encoding_ = encoding; }
*/
/**
* Set the raw string to normalize
*/
public void setRawString(String rawString) {
rawString_ = rawString;
}
/**
* Return the normalized string. If the chached value does not exist the normalize method from the
* current normalizer implementation is called.
*
* @return the normalized string
*/
public String getNormalizedString() {
if (normString_ == null) {
normalize();
}
return normString_;
}
/**
* Set a normalizer version. This activity load the new requested normalizer implementation.
*
* @param normVersion the normalizer version to be use
* @throws NormalizeException ErrorCode:400
*/
public void setVersion(String normVersion) throws NormalizeException {
normVersion_ = normVersion;
init();
}
/**
* Return the current version string.
*
* @return the normaliser version string
*/
public String getVersion() {
return normVersion_;
}
/**
* Wrapper method. Call the normalizer implementation method.
*
* @param rawString the raw string to normalize
* @return the normalized string
* @see NormalizeV01
*/
public String normalize(String rawString) {
return normalize_.normalize(rawString);
}
/**
* Wrapper method. Call the normalizer implementation method. Normalize the current raw string.
*
* @return the normalized string
* @see NormalizeV01
*/
public String normalize() {
if (normString_ == null) {
normString_ = normalize(rawString_);
}
return normString_;
}
/**
* Returns the normalizer line separator string.
* @return the line separator string
*/
public String getNormCR() {
return normalize_.getNormCR();
}
/**
* load the class settings
*
* @throws NormalizeException
* @see SettingsReader
*/
private void loadSettings() throws NormalizeException {
if (settings_ == null) {
try {
settings_ = SettingsReader.getInstance();
} catch (SettingsException e) {
String log_message = "Can not load normalizer settings. Cause:\n" + e.getMessage();
logger_.error(log_message);
throw new NormalizeException(400, log_message, e);
}
}
}
}