From fc44d4bcad00192f0df8f6086737b9b126094dcd Mon Sep 17 00:00:00 2001 From: Andreas Fitzek Date: Thu, 26 Sep 2013 15:48:43 +0200 Subject: initial code commit --- .../pdfas/common/settings/IProfileConstants.java | 65 +++++++++++ .../gv/egiz/pdfas/common/settings/ISettings.java | 13 +++ .../at/gv/egiz/pdfas/common/settings/Settings.java | 124 +++++++++++++++++++++ .../common/settings/SignatureProfileEntry.java | 39 +++++++ .../common/settings/SignatureProfileSettings.java | 121 ++++++++++++++++++++ .../egiz/pdfas/common/settings/package-info.java | 8 ++ 6 files changed, 370 insertions(+) create mode 100644 pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/settings/IProfileConstants.java create mode 100644 pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/settings/ISettings.java create mode 100644 pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/settings/Settings.java create mode 100644 pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/settings/SignatureProfileEntry.java create mode 100644 pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/settings/SignatureProfileSettings.java create mode 100644 pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/settings/package-info.java (limited to 'pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/settings') diff --git a/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/settings/IProfileConstants.java b/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/settings/IProfileConstants.java new file mode 100644 index 00000000..2e2ee024 --- /dev/null +++ b/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/settings/IProfileConstants.java @@ -0,0 +1,65 @@ +package at.gv.egiz.pdfas.common.settings; + +/** + * Created with IntelliJ IDEA. + * User: afitzek + * Date: 9/10/13 + * Time: 12:58 PM + * To change this template use File | Settings | File Templates. + */ +public interface IProfileConstants { + /** + * The settings key prefix for signature definitions. "sig_obj." + */ + public static final String SIG_OBJ = "sig_obj."; + + public static final String SIG_DATE = "SIG_DATE"; + + /** + * The settings key prefix for the signature table object definition + */ + public static final String TABLE = "table."; + + /** + * The settings value refering to a table + */ + public final static String TYPE_TABLE = "TABLE"; + + /** + * The settings value refering to an image + */ + public final static String TYPE_IMAGE = "i"; + + /** + * The settings value refering to a text caption + */ + public final static String TYPE_CAPTION = "c"; + + /** + * The settings value refering to a text value + */ + public final static String TYPE_VALUE = "v"; + + /** + * The settings key sub prefix getting the width of columns for a table + * definition + */ + public final static String COLS_WITH = "ColsWidth"; + + /** + * The settings key sub prefix getting the style definition + */ + public final static String STYLE = "Style"; + + public final static String PROFILE_VALUE = "value"; + + public final static String PROFILE_KEY = "key"; + + public final static String KEY_SEPARATOR = "."; + + public final static String INCLUDE = "include"; + + public final static String CFG_DIR = "cfg"; + public final static String CFG_FILE = "config.properties"; + +} diff --git a/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/settings/ISettings.java b/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/settings/ISettings.java new file mode 100644 index 00000000..9e3291d2 --- /dev/null +++ b/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/settings/ISettings.java @@ -0,0 +1,13 @@ +package at.gv.egiz.pdfas.common.settings; + +import java.util.Map; +import java.util.Vector; + +public interface ISettings { + public String getValue(String key); + public boolean hasValue(String key); + public boolean hasPrefix(String prefix); + public Map getValuesPrefix(String prefix); + public Vector getFirstLevelKeys(String prefix); + public String getWorkingDirectory(); +} diff --git a/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/settings/Settings.java b/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/settings/Settings.java new file mode 100644 index 00000000..da946215 --- /dev/null +++ b/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/settings/Settings.java @@ -0,0 +1,124 @@ +package at.gv.egiz.pdfas.common.settings; + +import at.gv.egiz.pdfas.common.exceptions.PdfAsSettingsException; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.*; + +public class Settings implements ISettings, IProfileConstants{ + + private static final Logger logger = LoggerFactory.getLogger(Settings.class); + + protected Properties properties = new Properties(); + + protected File workDirectory; + + public Settings(File workDirectory) { + try { + this.workDirectory = workDirectory; + loadSettings(workDirectory); + } catch (PdfAsSettingsException e) { + logger.error(e.getMessage(), e); + } + } + + public void loadSettings(File workDirectory) throws PdfAsSettingsException { + try { + + String configDir = workDirectory.getAbsolutePath() + File.separator + CFG_DIR; + String configFile = configDir + File.separator + CFG_FILE; + logger.debug("Loading cfg file: " + configFile); + properties.load(new FileInputStream(configFile)); + + Map includes = this.getValuesPrefix(INCLUDE); + + if(includes != null) { + Iterator includeIterator = includes.values().iterator(); + while(includeIterator.hasNext()) { + String includeFile = configDir + File.separator + includeIterator.next(); + logger.debug("Loading included cfg file: " + includeFile); + properties.load(new FileInputStream(includeFile)); + } + } + + logger.debug("Configured Properties:"); + if(logger.isDebugEnabled()) { + properties.list(System.out); + } + + } catch (IOException e) { + throw new PdfAsSettingsException("Failed to read settings!", e); + } + } + + public String getValue(String key) { + return properties.getProperty(key); + } + + public boolean hasValue(String key) { + return properties.containsKey(key); + } + + public Map getValuesPrefix(String prefix) { + Iterator keyIterator = properties.keySet().iterator(); + Map valueMap = new HashMap(); + while(keyIterator.hasNext()) { + String key = keyIterator.next().toString(); + + if(key.startsWith(prefix)) { + valueMap.put(key, properties.getProperty(key)); + } + } + + if(valueMap.isEmpty()) { + return null; + } + + return valueMap; + } + + public Vector getFirstLevelKeys(String prefix) { + String mPrefix = prefix.endsWith(".")?prefix:prefix+"."; + Iterator keyIterator = properties.keySet().iterator(); + Vector valueMap = new Vector(); + while(keyIterator.hasNext()) { + String key = keyIterator.next().toString(); + + if(key.startsWith(prefix)) { + int keyIdx = key.indexOf('.', mPrefix.length()) > 0 ? key.indexOf('.', mPrefix.length()) : key.length(); + String firstLevels = key.substring(0, keyIdx); + if(!valueMap.contains(firstLevels)) { + valueMap.add(firstLevels); + } + } + } + + if(valueMap.isEmpty()) { + return null; + } + + return valueMap; + } + + public boolean hasPrefix(String prefix) { + Iterator keyIterator = properties.keySet().iterator(); + while(keyIterator.hasNext()) { + String key = keyIterator.next().toString(); + + if(key.startsWith(prefix)) { + return true; + } + } + return false; + } + + public String getWorkingDirectory() { + return this.workDirectory.getAbsolutePath(); + } + +} diff --git a/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/settings/SignatureProfileEntry.java b/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/settings/SignatureProfileEntry.java new file mode 100644 index 00000000..3df929b1 --- /dev/null +++ b/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/settings/SignatureProfileEntry.java @@ -0,0 +1,39 @@ +package at.gv.egiz.pdfas.common.settings; + +public class SignatureProfileEntry { + private String key = null; + private String caption = null; + private String value = null; + + public SignatureProfileEntry() { + } + + public String getKey() { + return key; + } + + public void setKey(String key) { + this.key = key; + } + + public String getCaption() { + return caption; + } + + public void setCaption(String caption) { + this.caption = caption; + } + + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } + + @Override + public String toString() { + return getKey() + "[ " + getCaption() + " : " + getValue() + " ]"; + } +} diff --git a/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/settings/SignatureProfileSettings.java b/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/settings/SignatureProfileSettings.java new file mode 100644 index 00000000..46f2ed09 --- /dev/null +++ b/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/settings/SignatureProfileSettings.java @@ -0,0 +1,121 @@ +package at.gv.egiz.pdfas.common.settings; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.*; + +public class SignatureProfileSettings implements IProfileConstants { + + private static final Logger logger = LoggerFactory.getLogger(SignatureProfileSettings.class); + + private Map profileInformations = new HashMap(); + + private Map profileSettings = new HashMap(); + + private String profileID; + + public SignatureProfileSettings(String profileID, ISettings configuration) { + this.profileID = profileID; + String profilePrefix = SIG_OBJ + profileID + KEY_SEPARATOR; + String keysPrefix = profilePrefix + PROFILE_KEY; + String valuesPrefix = profilePrefix + PROFILE_VALUE; + String tablePrefix = profilePrefix + TABLE; + + logger.debug("Reading Profile: " + profileID); + logger.debug("Keys Prefix: " + keysPrefix); + logger.debug("Values Prefix: " + valuesPrefix); + logger.debug("Table Prefix: " + tablePrefix); + + Map keys = configuration.getValuesPrefix(keysPrefix); + Map values = configuration.getValuesPrefix(valuesPrefix); + + Iterator keyIterator = keys.keySet().iterator(); + + while(keyIterator.hasNext()) { + String key = keyIterator.next(); + key = key.substring(key.lastIndexOf('.') + 1); + String valueKey = keys.get(keysPrefix + KEY_SEPARATOR + key); + + String valueValue = values.get(valuesPrefix + KEY_SEPARATOR + key); + + + SignatureProfileEntry entry = new SignatureProfileEntry(); + entry.setKey(key); + entry.setCaption(valueKey); + entry.setValue(valueValue); + profileInformations.put(key, entry); + logger.debug(" " + entry.toString()); + } + + // Find entries where only values exists + Iterator valuesIterator = values.keySet().iterator(); + + while(valuesIterator.hasNext()) { + String key = valuesIterator.next(); + key = key.substring(key.lastIndexOf('.') + 1); + + String valueValue = values.get(valuesPrefix + KEY_SEPARATOR + key); + + SignatureProfileEntry entry = profileInformations.get(key); + if(entry == null) { + entry = new SignatureProfileEntry(); + entry.setKey(key); + entry.setCaption(null); + entry.setValue(valueValue); + profileInformations.put(key, entry); + } + + logger.debug(" " + entry.toString()); + } + + Map others = configuration.getValuesPrefix(profilePrefix); + + Iterator otherIterator = others.keySet().iterator(); + + while(otherIterator.hasNext()) { + String key = otherIterator.next(); + + logger.trace("Checking key " + key); + if( key.startsWith(keysPrefix) || + key.startsWith(valuesPrefix) || + key.startsWith(tablePrefix)) { + continue; + } + + String value = others.get(key); + key = key.substring(key.lastIndexOf('.') + 1); + + profileSettings.put(key, others.get(value)); + + logger.debug(" Settings: " + key + " : " + value); + } + } + + public String getCaption(String key) { + SignatureProfileEntry entry = profileInformations.get(key); + if(entry != null) { + return entry.getCaption(); + } + return null; + } + + public String getValue(String key) { + SignatureProfileEntry entry = profileInformations.get(key); + if(entry != null) { + String value = entry.getValue(); + + if(value == null) { + // TODO: try to find default value for key! + } + + return value; + } + // TODO: try to find default value for key! + return null; + } + + public String getProfileID() { + return profileID; + } +} diff --git a/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/settings/package-info.java b/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/settings/package-info.java new file mode 100644 index 00000000..c2b123e2 --- /dev/null +++ b/pdf-as-common/src/main/java/at/gv/egiz/pdfas/common/settings/package-info.java @@ -0,0 +1,8 @@ +/** + * + */ +/** + * @author afitzek + * + */ +package at.gv.egiz.pdfas.common.settings; \ No newline at end of file -- cgit v1.2.3