/** * Copyright 2006 by Know-Center, Graz, Austria * PDF-AS has been contracted by the E-Government Innovation Center EGIZ, a * joint initiative of the Federal Chancellery Austria and Graz University of * Technology. * * Licensed under the EUPL, Version 1.1 or - as soon they will be approved by * the European Commission - subsequent versions of the EUPL (the "Licence"); * You may not use this work except in compliance with the Licence. * You may obtain a copy of the Licence at: * http://www.osor.eu/eupl/ * * Unless required by applicable law or agreed to in writing, software * distributed under the Licence is distributed on an "AS IS" basis, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the Licence for the specific language governing permissions and * limitations under the Licence. * * This product combines work with different licenses. See the "NOTICE" text * file for details on the various modules and licenses. * The "NOTICE" text file is part of the distribution. Any derivative works * that you distribute must include a readable copy of the "NOTICE" text file. * * $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 { // 04.11.2010 changed by exthex - normalize methods use and propagate the keepMultipleNewlines parameter /** * 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); } throw new NormalizeException("Can not load normalizer library", e); } try { normalize_ = (Normalize) normalize_class.newInstance(); } catch (InstantiationException e) { if (logger_.isEnabledFor(Level.FATAL)) { logger_.fatal("Can not instantiate:" + class_name); } throw new NormalizeException("Can not load normalizer library", e); } catch (IllegalAccessException e) { if (logger_.isEnabledFor(Level.FATAL)) { logger_.fatal("Can not access:" + class_name); } throw new NormalizeException("Can not load normalizer library", e); } } /** * 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 * @param keepMultipleNewlines * @return the normalized string * @see NormalizeV01 */ public String normalize(String rawString, boolean keepMultipleNewlines) { return normalize_.normalize(rawString, keepMultipleNewlines); } /** * Wrapper method. Call the normalizer implementation method. Normalize the current raw string. * * @return the normalized string * @see NormalizeV01 */ public String normalize(boolean keepMultipleNewlines) { if (normString_ == null) { normString_ = normalize(rawString_, keepMultipleNewlines); } 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, e); throw new NormalizeException(log_message, e); } } } }