]> source.dussan.org Git - xmlgraphics-fop.git/commitdiff
Deprecated PDFEncryptionParams on FOUserAgent in factor of the more generic solution...
authorJeremias Maerki <jeremias@apache.org>
Wed, 15 Feb 2006 13:06:44 +0000 (13:06 +0000)
committerJeremias Maerki <jeremias@apache.org>
Wed, 15 Feb 2006 13:06:44 +0000 (13:06 +0000)
git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/branches/Temp_API_Finalization@378001 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/fop/apps/FOUserAgent.java
src/java/org/apache/fop/cli/CommandLineOptions.java
src/java/org/apache/fop/render/pdf/PDFRenderer.java

index 273d952f0890ba47d8691c46c940d2dbaa61d000..db1b316cc916d9995716b70fd2857d3c7a0bacf3 100644 (file)
@@ -41,6 +41,7 @@ import org.apache.fop.pdf.PDFEncryptionParams;
 import org.apache.fop.render.Renderer;
 import org.apache.fop.render.RendererFactory;
 import org.apache.fop.render.XMLHandlerRegistry;
+import org.apache.fop.render.pdf.PDFRenderer;
 
 /**
  * This is the user agent for FOP.
@@ -79,7 +80,6 @@ public class FOUserAgent {
     /** Our default resolver if none is set */
     private URIResolver foURIResolver = new FOURIResolver();
     
-    private PDFEncryptionParams pdfEncryptionParams;
     private float targetResolution = DEFAULT_TARGET_RESOLUTION;
     private Map rendererOptions = new java.util.HashMap();
     private File outputFile = null;
@@ -366,18 +366,23 @@ public class FOUserAgent {
     /**
      * Returns the parameters for PDF encryption.
      * @return the PDF encryption parameters, null if not applicable
+     * @deprecated Use (PDFEncryptionParams)getRendererOptions().get("encryption-params") 
+     *             instead.
      */
     public PDFEncryptionParams getPDFEncryptionParams() {
-        return pdfEncryptionParams;
+        return (PDFEncryptionParams)getRendererOptions().get(PDFRenderer.ENCRYPTION_PARAMS);
     }
 
     /**
      * Sets the parameters for PDF encryption.
      * @param pdfEncryptionParams the PDF encryption parameters, null to
      * disable PDF encryption
+     * @deprecated Use getRendererOptions().put("encryption-params", 
+     *             new PDFEncryptionParams(..)) instead or set every parameter separately: 
+     *             getRendererOptions().put("noprint", Boolean.TRUE).
      */
     public void setPDFEncryptionParams(PDFEncryptionParams pdfEncryptionParams) {
-        this.pdfEncryptionParams = pdfEncryptionParams;
+        getRendererOptions().put(PDFRenderer.ENCRYPTION_PARAMS, pdfEncryptionParams);
     }
 
 
index 86155239d9bc8085604dd4b58b26d36970042d5c..fc93046342d9761424065e93fbe13e37c4ec996d 100644 (file)
@@ -34,6 +34,7 @@ import org.apache.fop.pdf.PDFEncryptionManager;
 import org.apache.fop.pdf.PDFEncryptionParams;
 import org.apache.fop.render.awt.AWTRenderer;
 import org.apache.fop.render.Renderer;
+import org.apache.fop.render.pdf.PDFRenderer;
 import org.apache.fop.render.xml.XMLRenderer;
 import org.apache.fop.util.CommandLineLogger;
 
@@ -561,14 +562,17 @@ public class CommandLineOptions {
     }
 
     private PDFEncryptionParams getPDFEncryptionParams() throws FOPException {
-        if (foUserAgent.getPDFEncryptionParams() == null) {
+        PDFEncryptionParams params = (PDFEncryptionParams)foUserAgent.getRendererOptions().get(
+                        PDFRenderer.ENCRYPTION_PARAMS); 
+        if (params == null) {
             if (!PDFEncryptionManager.checkAvailableAlgorithms()) {
                 throw new FOPException("PDF encryption requested but it is not available."
                         + " Please make sure MD5 and RC4 algorithms are available.");
             }
-            foUserAgent.setPDFEncryptionParams(new PDFEncryptionParams());
+            params = new PDFEncryptionParams();
+            foUserAgent.getRendererOptions().put(PDFRenderer.ENCRYPTION_PARAMS, params);
         }
-        return foUserAgent.getPDFEncryptionParams();
+        return params;
     }
 
     private int parsePDFOwnerPassword(String[] args, int i) throws FOPException {
index f3a5d3e269980ffb664938e377c8a19c326f7009..70367ed7acc68240c33471263045a7f533a74882 100644 (file)
@@ -66,6 +66,7 @@ import org.apache.fop.pdf.PDFAnnotList;
 import org.apache.fop.pdf.PDFColor;
 import org.apache.fop.pdf.PDFDocument;
 import org.apache.fop.pdf.PDFEncryptionManager;
+import org.apache.fop.pdf.PDFEncryptionParams;
 import org.apache.fop.pdf.PDFFilterList;
 import org.apache.fop.pdf.PDFInfo;
 import org.apache.fop.pdf.PDFLink;
@@ -106,6 +107,21 @@ public class PDFRenderer extends AbstractPathOrientedRenderer {
      */
     public static final String MIME_TYPE = MimeConstants.MIME_PDF;
 
+    /** PDF encryption parameter: all parameters as object, datatype: PDFEncryptionParams */
+    public static final String ENCRYPTION_PARAMS = "encryption-params";
+    /** PDF encryption parameter: user password, datatype: String */
+    public static final String USER_PASSWORD = "user-password";
+    /** PDF encryption parameter: owner password, datatype: String */
+    public static final String OWNER_PASSWORD = "owner-password";
+    /** PDF encryption parameter: Forbids printing, datatype: Boolean or "true"/"false" */
+    public static final String NO_PRINT = "noprint";
+    /** PDF encryption parameter: Forbids copying content, datatype: Boolean or "true"/"false" */
+    public static final String NO_COPY_CONTENT = "nocopy";
+    /** PDF encryption parameter: Forbids editing content, datatype: Boolean or "true"/"false" */
+    public static final String NO_EDIT_CONTENT = "noedit";
+    /** PDF encryption parameter: Forbids annotations, datatype: Boolean or "true"/"false" */
+    public static final String NO_ANNOTATIONS = "noannotations";
+    
     /** Controls whether comments are written to the PDF stream. */
     protected static final boolean WRITE_COMMENTS = true;
     
@@ -156,6 +172,9 @@ public class PDFRenderer extends AbstractPathOrientedRenderer {
      */
     protected PDFPage currentPage;
     
+    /** the (optional) encryption parameters */
+    protected PDFEncryptionParams encryptionParams;
+    
     /** The current Transform */
     protected AffineTransform currentBasicTransform;
 
@@ -229,11 +248,70 @@ public class PDFRenderer extends AbstractPathOrientedRenderer {
         }
     }
 
+    private boolean booleanValueOf(Object obj) {
+        if (obj instanceof Boolean) {
+            return ((Boolean)obj).booleanValue();
+        } else if (obj instanceof String) {
+            return Boolean.valueOf((String)obj).booleanValue();
+        } else {
+            throw new IllegalArgumentException("Boolean or \"true\" or \"false\" expected.");
+        }
+    }
+    
     /**
      * @see org.apache.fop.render.Renderer#setUserAgent(FOUserAgent)
      */
     public void setUserAgent(FOUserAgent agent) {
         super.setUserAgent(agent);
+        PDFEncryptionParams params 
+                = (PDFEncryptionParams)agent.getRendererOptions().get(ENCRYPTION_PARAMS);
+        if (params != null) {
+            this.encryptionParams = params; //overwrite if available
+        }
+        String pwd;
+        pwd = (String)agent.getRendererOptions().get(USER_PASSWORD);
+        if (pwd != null) {
+            if (encryptionParams == null) {
+                this.encryptionParams = new PDFEncryptionParams();
+            }
+            this.encryptionParams.setUserPassword(pwd);
+        }
+        pwd = (String)agent.getRendererOptions().get(OWNER_PASSWORD);
+        if (pwd != null) {
+            if (encryptionParams == null) {
+                this.encryptionParams = new PDFEncryptionParams();
+            }
+            this.encryptionParams.setOwnerPassword(pwd);
+        }
+        Object setting;
+        setting = agent.getRendererOptions().get(NO_PRINT);
+        if (setting != null) {
+            if (encryptionParams == null) {
+                this.encryptionParams = new PDFEncryptionParams();
+            }
+            this.encryptionParams.setAllowPrint(!booleanValueOf(setting));
+        }
+        setting = agent.getRendererOptions().get(NO_COPY_CONTENT);
+        if (setting != null) {
+            if (encryptionParams == null) {
+                this.encryptionParams = new PDFEncryptionParams();
+            }
+            this.encryptionParams.setAllowCopyContent(!booleanValueOf(setting));
+        }
+        setting = agent.getRendererOptions().get(NO_EDIT_CONTENT);
+        if (setting != null) {
+            if (encryptionParams == null) {
+                this.encryptionParams = new PDFEncryptionParams();
+            }
+            this.encryptionParams.setAllowEditContent(!booleanValueOf(setting));
+        }
+        setting = agent.getRendererOptions().get(NO_ANNOTATIONS);
+        if (setting != null) {
+            if (encryptionParams == null) {
+                this.encryptionParams = new PDFEncryptionParams();
+            }
+            this.encryptionParams.setAllowEditAnnotations(!booleanValueOf(setting));
+        }
     }
 
     /**
@@ -255,8 +333,7 @@ public class PDFRenderer extends AbstractPathOrientedRenderer {
         this.pdfDoc.outputHeader(stream);
 
         //Setup encryption if necessary
-        PDFEncryptionManager.setupPDFEncryption(
-                userAgent.getPDFEncryptionParams(), this.pdfDoc);
+        PDFEncryptionManager.setupPDFEncryption(encryptionParams, this.pdfDoc);
     }
 
     /**