From 93c95e60f52cb9b37c8cc3d5ceaa37417cf71001 Mon Sep 17 00:00:00 2001 From: Tobias Kellner Date: Thu, 27 Nov 2014 21:02:02 +0100 Subject: Cache the signature placeholder --- .../main/java/at/asit/pdfover/gui/Constants.java | 6 ++ .../composites/SimpleConfigurationComposite.java | 8 +- .../gui/utils/SignaturePlaceholderCache.java | 112 +++++++++++++++++++++ .../gui/workflow/states/PositioningState.java | 3 +- 4 files changed, 123 insertions(+), 6 deletions(-) create mode 100644 pdf-over-gui/src/main/java/at/asit/pdfover/gui/utils/SignaturePlaceholderCache.java (limited to 'pdf-over-gui/src/main/java') diff --git a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/Constants.java b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/Constants.java index 67d30f01..d2d285c1 100644 --- a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/Constants.java +++ b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/Constants.java @@ -62,6 +62,12 @@ public class Constants { /** The configuration version file name */ public static final String CONFIG_VERSION_FILENAME = ".version"; //$NON-NLS-1$ + /** The signature placeholder cache file name */ + public static final String PLACEHOLDER_CACHE_FILENAME = ".placeholder"; //$NON-NLS-1$ + + /** The signature placeholder cache properties file name */ + public static final String PLACEHOLDER_CACHE_PROPS_FILENAME = ".placeholder.properties"; //$NON-NLS-1$ + /** The minimum PDF-AS configuration version (older ones will be backed up and updated */ public static final String MIN_PDF_AS_CONFIG_VERSION = "4.1.0"; //$NON-NLS-1$ diff --git a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/composites/SimpleConfigurationComposite.java b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/composites/SimpleConfigurationComposite.java index eccda66c..a5e0461e 100644 --- a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/composites/SimpleConfigurationComposite.java +++ b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/composites/SimpleConfigurationComposite.java @@ -65,6 +65,7 @@ import at.asit.pdfover.gui.controls.Dialog.BUTTONS; import at.asit.pdfover.gui.exceptions.InvalidNumberException; import at.asit.pdfover.gui.utils.ImageConverter; import at.asit.pdfover.gui.utils.Messages; +import at.asit.pdfover.gui.utils.SignaturePlaceholderCache; import at.asit.pdfover.gui.workflow.config.ConfigurationContainer; import at.asit.pdfover.gui.workflow.states.State; import at.asit.pdfover.signator.FileNameEmblem; @@ -673,9 +674,7 @@ public class SimpleConfigurationComposite extends BaseConfigurationComposite { param.setSignatureLanguage(this.configurationContainer.getSignLocale().getLanguage()); - img = ImageConverter - .convertToSWT((BufferedImage) param - .getPlaceholder()); + img = SignaturePlaceholderCache.getSWTPlaceholder(param); } } else { if (this.signer != null) { @@ -687,8 +686,7 @@ public class SimpleConfigurationComposite extends BaseConfigurationComposite { param.setSignatureLanguage(this.configurationContainer.getSignLocale().getLanguage()); param.setEmblem(new FileNameEmblem(image)); - img = ImageConverter.convertToSWT((BufferedImage) param - .getPlaceholder()); + img = SignaturePlaceholderCache.getSWTPlaceholder(param); } else { img = new ImageData(image); } diff --git a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/utils/SignaturePlaceholderCache.java b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/utils/SignaturePlaceholderCache.java new file mode 100644 index 00000000..769c477b --- /dev/null +++ b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/utils/SignaturePlaceholderCache.java @@ -0,0 +1,112 @@ +/* + * Copyright 2012 by A-SIT, Secure Information Technology Center Austria + * + * 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://joinup.ec.europa.eu/software/page/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. + */ +package at.asit.pdfover.gui.utils; + +// Imports +import java.awt.Image; +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.Properties; + +import javax.imageio.ImageIO; + +import org.eclipse.swt.graphics.ImageData; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import at.asit.pdfover.gui.Constants; +import at.asit.pdfover.signator.SignatureParameter; + +/** + * + */ +public class SignaturePlaceholderCache { + /** + * SLF4J Logger instance + **/ + private static final Logger log = LoggerFactory + .getLogger(SignaturePlaceholderCache.class); + + private static void saveImage(BufferedImage image, String fileDir, String fileName, String fileExt) throws IOException { + File file = new File(fileDir, fileName + "." + fileExt); //$NON-NLS-1$ + ImageIO.write(image, fileExt, file); // ignore returned boolean + } + + private static Image loadImage(String fileDir, String fileName, String fileExt) throws IOException { + return ImageIO.read(new File(fileDir, fileName + "." + fileExt)); //$NON-NLS-1$ + } + + /** + * Get placeholder as AWT Image + * @param param SignatureParameter + * @return the placeholder AWT Image + */ + public static Image getPlaceholder(SignatureParameter param) { + final String fileDir = Constants.CONFIG_DIRECTORY; + final String imgFileName = Constants.PLACEHOLDER_CACHE_FILENAME; + final String imgFileExt = "png"; //$NON-NLS-1$ + final String propFileName = Constants.PLACEHOLDER_CACHE_PROPS_FILENAME; + + final String sigLangProp = "LANG"; //$NON-NLS-1$ + final String sigEmblProp = "EMBL"; //$NON-NLS-1$ + final String sigNoteProp = "NOTE"; //$NON-NLS-1$ + String sigLang = param.getSignatureLanguage(); + String sigEmbl = (param.getEmblem() == null ? "" : param.getEmblem().getFileName()); //$NON-NLS-1$ + String sigNote = param.getProperty("SIG_NOTE"); //$NON-NLS-1$ + + Properties sigProps = new Properties(); + // compare cache, try to load if match + try { + InputStream in = new FileInputStream(new File(fileDir, propFileName)); + sigProps.load(in); + if (sigProps.getProperty(sigLangProp).equals(sigLang) && + sigProps.getProperty(sigEmblProp).equals(sigEmbl) && + sigProps.getProperty(sigNoteProp).equals(sigNote)) + return loadImage(fileDir, imgFileName, imgFileExt); + } catch (Exception e) { + log.debug("Can't load signature Placeholder", e); //$NON-NLS-1$ + } + + // create new cache + try { + sigProps.setProperty(sigLangProp, sigLang); + sigProps.setProperty(sigEmblProp, sigEmbl); + sigProps.setProperty(sigNoteProp, sigNote); + OutputStream out = new FileOutputStream(new File(fileDir, propFileName)); + sigProps.store(out, null); + Image img = param.getPlaceholder(); + saveImage((BufferedImage) img, fileDir, imgFileName, imgFileExt); + return img; + } catch (IOException e) { + log.error("Can't save signature Placeholder", e); //$NON-NLS-1$ + return param.getPlaceholder(); + } + } + + /** + * Get placeholder as SWT ImageData + * @param param SignatureParameter + * @return the placeholder SWT ImageData + */ + public static ImageData getSWTPlaceholder(SignatureParameter param) { + return ImageConverter.convertToSWT((BufferedImage) getPlaceholder(param)); + } +} diff --git a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/states/PositioningState.java b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/states/PositioningState.java index fed293fc..6c3d11a8 100644 --- a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/states/PositioningState.java +++ b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/states/PositioningState.java @@ -32,6 +32,7 @@ import at.asit.pdfover.gui.composites.PositioningComposite; import at.asit.pdfover.gui.controls.Dialog.BUTTONS; import at.asit.pdfover.gui.controls.ErrorDialog; import at.asit.pdfover.gui.utils.Messages; +import at.asit.pdfover.gui.utils.SignaturePlaceholderCache; import at.asit.pdfover.gui.workflow.StateMachine; import at.asit.pdfover.gui.workflow.Status; import at.asit.pdfover.signator.Emblem; @@ -109,7 +110,7 @@ public class PositioningState extends State { param.setSignatureLanguage(getStateMachine().getConfigProvider().getSignLocale().getLanguage()); this.positionComposite.setPlaceholder( - param.getPlaceholder(), + SignaturePlaceholderCache.getPlaceholder(param), param.getPlaceholderDimension().getWidth(), param.getPlaceholderDimension().getHeight(), getStateMachine().getConfigProvider().getPlaceholderTransparency()); -- cgit v1.2.3