From 8e0ccca7cfd1680e33737e0db8d6e1091661568c Mon Sep 17 00:00:00 2001 From: Thomas <> Date: Mon, 26 Jun 2023 21:11:26 +0200 Subject: refact(placeholder): clean-up, bugfix and optimize QR-Code placeholder detection Major re-factoring of QR-Code detection with huge clean-up of code. --- .../at/gv/egiz/pdfas/lib/settings/Settings.java | 684 +++++++++------------ 1 file changed, 305 insertions(+), 379 deletions(-) (limited to 'pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/settings/Settings.java') diff --git a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/settings/Settings.java b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/settings/Settings.java index c5b6dc57..0b376455 100644 --- a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/settings/Settings.java +++ b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/settings/Settings.java @@ -35,438 +35,364 @@ import java.util.Map; import java.util.Map.Entry; import java.util.Properties; import java.util.Vector; +import java.util.stream.Collectors; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOCase; import org.apache.commons.io.filefilter.WildcardFileFilter; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import at.gv.egiz.pdfas.common.exceptions.PdfAsSettingsException; import at.gv.egiz.pdfas.common.settings.IProfileConstants; import at.gv.egiz.pdfas.common.settings.ISettings; import at.gv.egiz.pdfas.common.settings.Profiles; +import lombok.extern.slf4j.Slf4j; + +@Slf4j +public class Settings implements ISettings, IProfileConstants { + protected Properties properties = new Properties(); + protected File workDirectory; + + public Settings(File workDirectory) { + try { + this.workDirectory = workDirectory; + loadSettings(workDirectory); + } catch (final PdfAsSettingsException e) { + log.error(e.getMessage(), e); + } + } -public class Settings implements ISettings, IProfileConstants { + private void loadSettingsRecursive(File workDirectory, File file) + throws RuntimeException { + try { + final String configDir = workDirectory.getAbsolutePath() + File.separator + + CFG_DIR; + final Properties tmpProps = new Properties(); + log.debug("Loading: " + file.getName()); + tmpProps.load(new FileInputStream(file)); - private static final Logger logger = LoggerFactory - .getLogger(Settings.class); + properties.putAll(tmpProps); - protected Properties properties = new Properties(); + this.getValuesPrefix(INCLUDE, tmpProps).values() + .forEach(el -> loadAdditionConfigFile(el, configDir)); - protected File workDirectory; + } catch (final IOException e) { + throw new RuntimeException(e); - public Settings(File workDirectory) { - try { - this.workDirectory = workDirectory; - loadSettings(workDirectory); - } catch (PdfAsSettingsException e) { - logger.error(e.getMessage(), e); - } } + } - private void loadSettingsRecursive(File workDirectory, File file) - throws PdfAsSettingsException { - try { - String configDir = workDirectory.getAbsolutePath() + File.separator - + CFG_DIR; - Properties tmpProps = new Properties(); - logger.debug("Loading: " + file.getName()); - tmpProps.load(new FileInputStream(file)); - - properties.putAll(tmpProps); - - Map includes = this.getValuesPrefix(INCLUDE, - tmpProps); - File contextFolder = new File(configDir); - if (includes != null) { - Iterator includeIterator = includes.values().iterator(); - while (includeIterator.hasNext()) { - contextFolder = new File(configDir); - String includeFileName = includeIterator.next(); - - File includeInstruction = new File(contextFolder, - includeFileName); - contextFolder = includeInstruction.getParentFile(); - String includeName = includeInstruction.getName(); - - WildcardFileFilter fileFilter = new WildcardFileFilter( - includeName, IOCase.SENSITIVE); - Collection includeFiles = null; - - if (contextFolder != null && contextFolder.exists() - && contextFolder.isDirectory()) { - includeFiles = FileUtils.listFiles(contextFolder, - fileFilter, null); - } - if (includeFiles != null && !includeFiles.isEmpty()) { - logger.debug("Including '" + includeFileName + "'."); - for (File includeFile : includeFiles) { - loadSettingsRecursive(workDirectory, includeFile); - } - } - } - } - - } catch (IOException e) { - throw new PdfAsSettingsException("Failed to read settings!", e); - } - } + private void loadAdditionConfigFile(String includeFileName, String configDir) { + File contextFolder = new File(configDir); - private void showAugments(Profiles profiles) { - if (!profiles.getAugments().isEmpty()) { - logger.debug("\tAugments for {}", profiles.getName()); - for (int i = 0; i < profiles.getAugments().size(); i++) { - logger.debug("\t\t{}", profiles.getAugments().get(i).getName()); - } - } + final File includeInstruction = new File(contextFolder, includeFileName); + contextFolder = includeInstruction.getParentFile(); + final String includeName = includeInstruction.getName(); + + final WildcardFileFilter fileFilter = new WildcardFileFilter( + includeName, IOCase.SENSITIVE); + Collection includeFiles = null; + + if (contextFolder != null && contextFolder.exists() + && contextFolder.isDirectory()) { + includeFiles = FileUtils.listFiles(contextFolder, + fileFilter, null); + } + if (includeFiles != null && !includeFiles.isEmpty()) { + log.debug("Including '" + includeFileName + "'."); + for (final File includeFile : includeFiles) { + loadSettingsRecursive(workDirectory, includeFile); + } } - private boolean isAugmentsReady(Profiles profiles) { - Iterator augmentingProfiles = profiles.getAugments().iterator(); - boolean allInitialized = true; - while (augmentingProfiles.hasNext()) { - if (!augmentingProfiles.next().isInitialized()) { - allInitialized = false; - } + } + + private void showAugments(Profiles profiles) { + if (!profiles.getAugments().isEmpty()) { + log.debug("\tAugments for {}", profiles.getName()); + for (final Profiles element : profiles.getAugments()) { + log.debug("\t\t{}", element.getName()); + } + } + } + + private boolean isAugmentsReady(Profiles profiles) { + boolean allInitialized = true; + for (final Profiles element : profiles.getAugments()) { + if (!element.isInitialized()) { + allInitialized = false; + } + } + return allInitialized; + } + + private boolean isParentReady(Profiles profiles) { + if (profiles.getParent() != null) { + return profiles.getParent().isInitialized(); + } else { + return false; + } + } + + private void performAugmentConfiguration(Profiles profiles) { + final String childBase = "sig_obj." + + profiles.getName(); + + for (final Profiles augmentingProfile : profiles.getAugments()) { + final String augmentingBase = "sig_obj." + augmentingProfile.getName(); + + for (final String key : this.getKeys( + augmentingBase + ".")) { + final String keyToCopy = key.substring(augmentingBase + .length()); + // log.debug("Profile: {} => {}", + // key, childBase+keyToCopy); + final String sourceKey = augmentingBase + keyToCopy; + final String targetKey = childBase + keyToCopy; + + if (!this.hasValue(targetKey)) { + properties.setProperty(targetKey, + this.getValue(sourceKey)); + // log.debug("Replaced: {} with Value from {}", + // childBase+keyToCopy, parentBase+keyToCopy); + } else { + // log.debug("NOT Replaced: {} with Value from {}", + // childBase+keyToCopy, parentBase+keyToCopy); } - return allInitialized; + } } - - private boolean isParentReady(Profiles profiles) { - if (profiles.getParent() != null) { - return profiles.getParent().isInitialized(); + } + + private void performParentConfiguration(Profiles profiles) { + if (profiles.getParent() != null) { + // If Parent is initialized Copy Properties from Parent + // to this profile + final String parentBase = "sig_obj." + profiles.getParent().getName(); + final String childBase = "sig_obj." + + profiles.getName(); + + for (final String key : this.getKeys( + parentBase + ".")) { + final String keyToCopy = key.substring(parentBase + .length()); + // log.debug("Profile: {} => {}", + // key, childBase+keyToCopy); + final String sourceKey = parentBase + keyToCopy; + final String targetKey = childBase + keyToCopy; + + if (!this.hasValue(targetKey)) { + properties.setProperty(targetKey, + this.getValue(sourceKey)); + // log.debug("Replaced: {} with Value from {}", + // childBase+keyToCopy, parentBase+keyToCopy); } else { - return false; + // log.debug("NOT Replaced: {} with Value from {}", + // childBase+keyToCopy, parentBase+keyToCopy); } + } + } + } + + private void buildProfiles() { + final Map profiles = new HashMap<>(); + + for (final String key : this.getFirstLevelKeys("sig_obj.types.")) { + final String profile = key.substring("sig_obj.types.".length()); + // System.out.println("[" + profile + "]: " + this.getValue(key)); + if (this.getValue(key).equals("on")) { + final Profiles prof = new Profiles(profile); + profiles.put(profile, prof); + } } - private void performAugmentConfiguration(Profiles profiles) { - Iterator augmentingProfiles = profiles.getAugments().iterator(); - - String childBase = "sig_obj." - + profiles.getName(); - - while (augmentingProfiles.hasNext()) { - Profiles augmentingProfile = augmentingProfiles.next(); - String augmentingBase = "sig_obj." + augmentingProfile.getName(); - - Iterator augmentingKeyIt = this.getKeys( - augmentingBase + ".").iterator(); - - while (augmentingKeyIt.hasNext()) { - String key = augmentingKeyIt.next(); - String keyToCopy = key.substring(augmentingBase - .length()); - //logger.debug("Profile: {} => {}", - // key, childBase+keyToCopy); - String sourceKey = augmentingBase + keyToCopy; - String targetKey = childBase + keyToCopy; - - if (!this.hasValue(targetKey)) { - properties.setProperty(targetKey, - this.getValue(sourceKey)); - //logger.debug("Replaced: {} with Value from {}", - // childBase+keyToCopy, parentBase+keyToCopy); - } else { - //logger.debug("NOT Replaced: {} with Value from {}", - // childBase+keyToCopy, parentBase+keyToCopy); - } - } - } + for (final Entry entry : profiles.entrySet()) { + entry.getValue().findParent(properties, profiles); } - private void performParentConfiguration(Profiles profiles) { - if (profiles.getParent() != null) { - // If Parent is initialized Copy Properties from Parent - // to this profile - String parentBase = "sig_obj." + profiles.getParent().getName(); - String childBase = "sig_obj." - + profiles.getName(); - - Iterator parentKeyIt = this.getKeys( - parentBase + ".").iterator(); - while (parentKeyIt.hasNext()) { - String key = parentKeyIt.next(); - String keyToCopy = key.substring(parentBase - .length()); - //logger.debug("Profile: {} => {}", - // key, childBase+keyToCopy); - String sourceKey = parentBase + keyToCopy; - String targetKey = childBase + keyToCopy; - - if (!this.hasValue(targetKey)) { - properties.setProperty(targetKey, - this.getValue(sourceKey)); - //logger.debug("Replaced: {} with Value from {}", - // childBase+keyToCopy, parentBase+keyToCopy); - } else { - //logger.debug("NOT Replaced: {} with Value from {}", - // childBase+keyToCopy, parentBase+keyToCopy); - } - } - } + for (final Entry entry : profiles.entrySet()) { + if (entry.getValue().getParent() == null) { + log.debug("Got Profile: [{}] : {}", entry.getKey(), entry.getValue().getName()); + showAugments(entry.getValue()); + } else { + log.debug("Got Profile: [{}] : {} (Parent {})", entry.getKey(), + entry.getValue().getName(), entry.getValue().getParent().getName()); + showAugments(entry.getValue()); + } } - private void buildProfiles() { - Map profiles = new HashMap(); - - Iterator itKeys = this.getFirstLevelKeys("sig_obj.types.") - .iterator(); - while (itKeys.hasNext()) { - String key = itKeys.next(); - String profile = key.substring("sig_obj.types.".length()); - //System.out.println("[" + profile + "]: " + this.getValue(key)); - if (this.getValue(key).equals("on")) { - Profiles prof = new Profiles(profile); - profiles.put(profile, prof); - } - } + log.debug("Configured Settings: {}", + properties.size()); + + // Resolve Parent Structures ... + while (!profiles.isEmpty()) { + final List removes = new ArrayList<>(); + for (final Entry entry : profiles.entrySet()) { + // Remove all base Profiles ... + if (entry.getValue().getParent() == null && entry.getValue().getAugments().isEmpty()) { + // Has neither parent or augmenting profiles + + entry.getValue().setInitialized(true); + removes.add(entry.getKey()); + } else if (entry.getValue().getParent() == null) { + // Has augmenting profiles but no parent + + // check if all augmenting profiles are initialized if so + // add them + + final Profiles profile = entry.getValue(); + if (this.isAugmentsReady(profile)) { + this.performAugmentConfiguration(profile); + // Copy done + entry.getValue().setInitialized(true); + removes.add(entry.getKey()); + } else { + log.debug("Not all augmenting profiles are ready yet for {}", entry.getValue().getName()); + } + } else if (entry.getValue().getAugments().isEmpty()) { + + // Has parent but no augmenting profiles + final Profiles profile = entry.getValue(); + + if (this.isParentReady(profile)) { + this.performParentConfiguration(profile); + // Copy done + entry.getValue().setInitialized(true); + removes.add(entry.getKey()); + } + } else { - // Initialize Parent Structure ... - Iterator> profileIterator = profiles.entrySet() - .iterator(); - while (profileIterator.hasNext()) { - Entry entry = profileIterator.next(); - entry.getValue().findParent(properties, profiles); - } + // Has parent and augmenting profiles - // Debug Output - Iterator> profileIteratorDbg = profiles.entrySet() - .iterator(); - while (profileIteratorDbg.hasNext()) { - Entry entry = profileIteratorDbg.next(); - if (entry.getValue().getParent() == null) { - logger.debug("Got Profile: [{}] : {}", entry.getKey(), entry.getValue().getName()); - showAugments(entry.getValue()); - } else { - logger.debug("Got Profile: [{}] : {} (Parent {})", entry.getKey(), - entry.getValue().getName(), entry.getValue().getParent().getName()); - showAugments(entry.getValue()); - } - } + final Profiles profile = entry.getValue(); + if (this.isAugmentsReady(profile) && this.isParentReady(profile)) { + // order is curcial, augments preceed over parent configuration + this.performAugmentConfiguration(profile); + this.performParentConfiguration(profile); - logger.debug("Configured Settings: {}", - properties.size()); - - // Resolve Parent Structures ... - while (!profiles.isEmpty()) { - List removes = new ArrayList(); - Iterator> profileIt = profiles.entrySet() - .iterator(); - while (profileIt.hasNext()) { - Entry entry = profileIt.next(); - - // Remove all base Profiles ... - if (entry.getValue().getParent() == null && entry.getValue().getAugments().isEmpty()) { - // Has neither parent or augmenting profiles - - entry.getValue().setInitialized(true); - removes.add(entry.getKey()); - } else if (entry.getValue().getParent() == null) { - // Has augmenting profiles but no parent - - // check if all augmenting profiles are initialized if so - // add them - - Profiles profile = entry.getValue(); - if (this.isAugmentsReady(profile)) { - this.performAugmentConfiguration(profile); - // Copy done - entry.getValue().setInitialized(true); - removes.add(entry.getKey()); - } else { - logger.debug("Not all augmenting profiles are ready yet for {}", entry.getValue().getName()); - } - } else if (entry.getValue().getAugments().isEmpty()) { - - // Has parent but no augmenting profiles - Profiles profile = entry.getValue(); - - if (this.isParentReady(profile)) { - this.performParentConfiguration(profile); - // Copy done - entry.getValue().setInitialized(true); - removes.add(entry.getKey()); - } - } else { - - // Has parent and augmenting profiles - - Profiles profile = entry.getValue(); - if (this.isAugmentsReady(profile) && this.isParentReady(profile)) { - // order is curcial, augments preceed over parent configuration - this.performAugmentConfiguration(profile); - this.performParentConfiguration(profile); - - // Copy done - entry.getValue().setInitialized(true); - removes.add(entry.getKey()); - } - } - } - - // Remove all Profiles from Remove List - - if (removes.isEmpty() && !profiles.isEmpty()) { - logger.error("Failed to build inheritant Profiles, running in infinite loop! (aborting ...)"); - logger.error("Profiles that cannot be resolved completly:"); - Iterator> failedProfiles = profiles.entrySet().iterator(); - while (failedProfiles.hasNext()) { - Entry entry = failedProfiles.next(); - logger.error("Problem Profile: [{}] : {}", entry.getKey(), entry.getValue().getName()); - } - return; - } - - Iterator removeIt = removes.iterator(); - while (removeIt.hasNext()) { - profiles.remove(removeIt.next()); - } + // Copy done + entry.getValue().setInitialized(true); + removes.add(entry.getKey()); + } } + } - logger.debug("Derived Settings: {}", - properties.size()); + // Remove all Profiles from Remove List - } - - public void debugDumpProfileSettings(String profileName) { - Iterator keysIterator = this.getKeys("sig_obj." + profileName + ".").iterator(); - - logger.debug("Configuration for {}", profileName); - while(keysIterator.hasNext()) { - String key = keysIterator.next(); - logger.debug(" {}: {}", key, this.getValue(key)); + if (removes.isEmpty() && !profiles.isEmpty()) { + log.error("Failed to build inheritant Profiles, running in infinite loop! (aborting ...)"); + log.error("Profiles that cannot be resolved completly:"); + for (final Entry entry : profiles.entrySet()) { + log.error("Problem Profile: [{}] : {}", entry.getKey(), entry.getValue().getName()); } - } - - public void loadSettings(File workDirectory) throws PdfAsSettingsException { - // try { - String configDir = workDirectory.getAbsolutePath() + File.separator - + CFG_DIR; - String configFile = configDir + File.separator + CFG_FILE; - loadSettingsRecursive(workDirectory, new File(configFile)); - buildProfiles(); - /* - * logger.debug("Loading cfg file: " + configFile); - * - * - * properties.load(new FileInputStream(configFile)); - * - * Map includes = this.getValuesPrefix(INCLUDE); File - * contextFolder = new File(configDir); if (includes != null) { - * Iterator includeIterator = includes.values().iterator(); - * while (includeIterator.hasNext()) { String includeFileName = - * includeIterator.next(); if (includeFileName.contains("*")) { - * WildcardFileFilter fileFilter = new WildcardFileFilter( - * includeFileName, IOCase.SENSITIVE); Collection includeFiles = - * null; - * - * if (contextFolder != null && contextFolder.exists() && - * contextFolder.isDirectory()) { includeFiles = - * FileUtils.listFiles(contextFolder, fileFilter, null); } if - * (includeFiles != null && !includeFiles.isEmpty()) { - * logger.info("Including '" + includeFileName + "'."); for (File - * includeFile : includeFiles) { properties .load(new - * FileInputStream(includeFile)); } } } else { String includeFile = - * configDir + File.separator + includeFileName; - * logger.debug("Loading included cfg file: " + includeFile); try { - * properties.load(new FileInputStream(includeFile)); } catch (Throwable - * e) { logger.error("Failed to load cfg file " + includeFile, e); } } } - * } - */ - // logger.debug("Configured Properties:"); - /* - * if(logger.isDebugEnabled()) { properties.list(System.out); } - */ - - // } catch (IOException e) { - // throw new PdfAsSettingsException("Failed to read settings!", e); - // } - } + return; + } - public String getValue(String key) { - return properties.getProperty(key); + for (final String element : removes) { + profiles.remove(element); + } } - public boolean hasValue(String key) { - return properties.containsKey(key); - } + log.debug("Derived Settings: {}", + properties.size()); - private Map getValuesPrefix(String prefix, Properties props) { - Iterator keyIterator = props.keySet().iterator(); - Map valueMap = new HashMap(); - while (keyIterator.hasNext()) { - String key = keyIterator.next().toString(); + } + public void debugDumpProfileSettings(String profileName) { + log.debug("Configuration for {}", profileName); + for (final String key : this.getKeys("sig_obj." + profileName + ".")) { + log.debug(" {}: {}", key, this.getValue(key)); + } + } - if (key.startsWith(prefix)) { - valueMap.put(key, props.getProperty(key)); - } - } + public void loadSettings(File workDirectory) throws PdfAsSettingsException { + try { + final String configDir = workDirectory.getAbsolutePath() + File.separator + + CFG_DIR; + final String configFile = configDir + File.separator + CFG_FILE; + loadSettingsRecursive(workDirectory, new File(configFile)); + buildProfiles(); - if (valueMap.isEmpty()) { - return null; - } + } catch (final RuntimeException e) { + throw new PdfAsSettingsException("Failed to read settings!", e); - return valueMap; } - - public Map getValuesPrefix(String prefix) { - return getValuesPrefix(prefix, properties); + } + + @Override + public String getValue(String key) { + return properties.getProperty(key); + } + + @Override + public boolean hasValue(String key) { + return properties.containsKey(key); + } + + private Map getValuesPrefix(String prefix, Properties props) { + return props.entrySet().stream() + .filter(el -> el.getKey().toString().startsWith(prefix)) + .collect(Collectors.toMap(key -> key.getKey().toString(), value -> value.getValue().toString())); + + } + + @Override + public Map getValuesPrefix(String prefix) { + return getValuesPrefix(prefix, properties); + } + + public Vector getKeys(String prefix) { + final Iterator keyIterator = properties.keySet().iterator(); + final Vector valueMap = new Vector<>(); + while (keyIterator.hasNext()) { + final String key = keyIterator.next().toString(); + + if (key.startsWith(prefix)) { + valueMap.add(key); + } } - - public Vector getKeys(String prefix) { - Iterator keyIterator = properties.keySet().iterator(); - Vector valueMap = new Vector(); - while (keyIterator.hasNext()) { - String key = keyIterator.next().toString(); - - if (key.startsWith(prefix)) { - valueMap.add(key); - } + return valueMap; + } + + @Override + public Vector getFirstLevelKeys(String prefix) { + final String mPrefix = prefix.endsWith(".") ? prefix : prefix + "."; + final Iterator keyIterator = properties.keySet().iterator(); + final Vector valueMap = new Vector<>(); + while (keyIterator.hasNext()) { + final String key = keyIterator.next().toString(); + + if (key.startsWith(prefix)) { + final int keyIdx = key.indexOf('.', mPrefix.length()) > 0 ? key + .indexOf('.', mPrefix.length()) : key.length(); + final String firstLevels = key.substring(0, keyIdx); + if (!valueMap.contains(firstLevels)) { + valueMap.add(firstLevels); } - 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; + if (valueMap.isEmpty()) { + return null; } - public boolean hasPrefix(String prefix) { - Iterator keyIterator = properties.keySet().iterator(); - while (keyIterator.hasNext()) { - String key = keyIterator.next().toString(); + return valueMap; + } - if (key.startsWith(prefix)) { - return true; - } - } - return false; - } + @Override + public boolean hasPrefix(String prefix) { + final Iterator keyIterator = properties.keySet().iterator(); + while (keyIterator.hasNext()) { + final String key = keyIterator.next().toString(); - public String getWorkingDirectory() { - return this.workDirectory.getAbsolutePath(); + if (key.startsWith(prefix)) { + return true; + } } + return false; + } + + @Override + public String getWorkingDirectory() { + return this.workDirectory.getAbsolutePath(); + } } -- cgit v1.2.3