]> source.dussan.org Git - xmlgraphics-fop.git/commitdiff
Reestablishing PDF 1.3 compatibility ("Z" is not available under JDK 1.3).
authorJeremias Maerki <jeremias@apache.org>
Wed, 5 Apr 2006 14:08:10 +0000 (14:08 +0000)
committerJeremias Maerki <jeremias@apache.org>
Wed, 5 Apr 2006 14:08:10 +0000 (14:08 +0000)
git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@391622 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/fop/pdf/PDFMetadata.java

index 1dad2b5a576dd2b29e342963159a10fe51da9362..42aaed1b0ecf128dc90b625cd37c59da18b357b5 100644 (file)
@@ -22,6 +22,7 @@ import java.io.IOException;
 import java.io.OutputStream;
 import java.text.DateFormat;
 import java.text.SimpleDateFormat;
+import java.util.Calendar;
 import java.util.Date;
 
 import javax.xml.transform.OutputKeys;
@@ -48,7 +49,7 @@ public class PDFMetadata extends PDFStream {
     private static final String XMLNS = "http://www.w3.org/2000/xmlns/";
 
     private static DateFormat pseudoISO8601DateFormat = new SimpleDateFormat(
-        "yyyy'-'MM'-'dd'T'HH':'mm':'ssZ");
+        "yyyy'-'MM'-'dd'T'HH':'mm':'ss");
 
     private Document xmpMetadata;
     private boolean readOnly = true;
@@ -148,14 +149,42 @@ public class PDFMetadata extends PDFStream {
         return sb.toString();
     }
 
-    private static String formatDate(Date dt) {
-        String s = pseudoISO8601DateFormat.format(dt);
+    /**
+     * Formats a Date using ISO 8601 format in the default time zone.
+     * @param dt the date
+     * @return the formatted date
+     */
+    public static String formatISO8601Date(Date dt) {
+        //ISO 8601 cannot be expressed directly using SimpleDateFormat
+        StringBuffer sb = new StringBuffer(pseudoISO8601DateFormat.format(dt));
+        Calendar cal = Calendar.getInstance();
+        cal.setTime(dt);
+        int offset = cal.get(Calendar.ZONE_OFFSET);
+        offset += cal.get(Calendar.DST_OFFSET);
+        offset /= (1000 * 60); //Convert to minutes
         
-        //Now insert the colon that's not possible using SimpleDateFormat
-        int tzpos = s.length() - 4;
-        String tz = s.substring(tzpos);
-        s = s.substring(0, tzpos) + tz.substring(0, 2) + ":" + tz.substring(2);
-        return s;
+        if (offset == 0) {
+            sb.append('Z');
+        } else {
+            int zoh = offset / 60;
+            int zom = Math.abs(offset % 60);
+            if (zoh > 0) {
+                sb.append('+');
+            } else {
+                sb.append('-');
+            }
+            if (zoh < 10) {
+                sb.append('0');
+            }
+            sb.append(zoh);
+            sb.append(':');
+            if (zom < 10) {
+                sb.append('0');
+            }
+            sb.append(zom);
+        }
+        
+        return sb.toString();
     }
     
     /**
@@ -209,7 +238,7 @@ public class PDFMetadata extends PDFStream {
         }
         el = doc.createElementNS(XMPConstants.DUBLIN_CORE_NAMESPACE, "dc:date");
         desc.appendChild(el);
-        el.appendChild(doc.createTextNode(formatDate(info.getCreationDate())));
+        el.appendChild(doc.createTextNode(formatISO8601Date(info.getCreationDate())));
         
         //XMP Basic Schema
         desc = doc.createElementNS(XMPConstants.RDF_NAMESPACE, "rdf:Description");
@@ -218,7 +247,7 @@ public class PDFMetadata extends PDFStream {
         rdf.appendChild(desc);
         el = doc.createElementNS(XMPConstants.XMP_BASIC_NAMESPACE, "xmp:CreateDate");
         desc.appendChild(el);
-        el.appendChild(doc.createTextNode(formatDate(info.getCreationDate())));
+        el.appendChild(doc.createTextNode(formatISO8601Date(info.getCreationDate())));
         if (info.getCreator() != null) {
             el = doc.createElementNS(XMPConstants.XMP_BASIC_NAMESPACE, "xmp:CreatorTool");
             desc.appendChild(el);