KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > it > stefanochizzolini > clown > objects > PdfDate


1 /*
2   Copyright 2006 Stefano Chizzolini. http://clown.stefanochizzolini.it
3
4   Contributors:
5     * Stefano Chizzolini (original code developer, info@stefanochizzolini.it):
6       contributed code is Copyright 2006 by Stefano Chizzolini.
7
8   This file should be part of the source code distribution of "PDF Clown library"
9   (the Program): see the accompanying README files for more info.
10
11   This Program is free software; you can redistribute it and/or modify it under
12   the terms of the GNU General Public License as published by the Free Software
13   Foundation; either version 2 of the License, or (at your option) any later version.
14
15   This Program is distributed in the hope that it will be useful, but WITHOUT ANY
16   WARRANTY, either expressed or implied; without even the implied warranty of
17   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the License for more details.
18
19   You should have received a copy of the GNU General Public License along with this
20   Program (see README files); if not, go to the GNU website (http://www.gnu.org/).
21
22   Redistribution and use, with or without modification, are permitted provided that such
23   redistributions retain the above copyright notice, license and disclaimer, along with
24   this list of conditions.
25 */

26
27 package it.stefanochizzolini.clown.objects;
28
29 import it.stefanochizzolini.clown.files.File;
30 import java.text.SimpleDateFormat JavaDoc;
31 import java.util.Date JavaDoc;
32
33 /**
34   PDF date object.
35 */

36 public class PdfDate
37   extends PdfAtomicObject<Date JavaDoc>
38   implements IPdfString
39 {
40   /*
41     NOTE: although the spec [PDF:1.6:3.8.3] prescribes that PdfDate IS a
42     PdfLiteral, we chose to treat PdfDate as a type non-derived from
43     PdfLiteral, 'cause its internal representation is definitely unmatchable
44     (could anyone else deal consistently with generic inheritance,
45     matching String-s and Date-s?).
46     IPdfString interface has been added to support its external representation
47     as a string.
48   */

49   // <class>
50
// <static>
51
// <fields>
52
protected static final SimpleDateFormat JavaDoc formatter;
53   // </fields>
54

55   // <constructors>
56
static
57   {formatter = new SimpleDateFormat JavaDoc("yyyyMMddHHmmssZ");}
58   // </constructors>
59

60   // <interface>
61
// <public>
62
/**
63     Converts a date value to a PDF-formatted date literal.
64   */

65   public static String JavaDoc toPdf(
66     Date JavaDoc value
67     )
68   {return PdfLiteral.toPdf(format(value));}
69
70   /**
71     Converts a PDF-formatted date value to a date value.
72   */

73   public static Date JavaDoc toDate(
74     String JavaDoc value
75     )
76   {
77     //TODO:IMPL this code is quite ugly... is there a more elegant solution (regex)?
78
// Normalize datetime value.
79
// Cut leading "D:" tag!
80
value = value.substring(2);
81     int length = value.length();
82     switch(length)
83     {
84       case 8: // Date only.
85
value += "000000+0000";
86         break;
87       case 14: // Datetime without timezone.
88
value += "+0000";
89         break;
90       case 15: // Datetime at UT timezone ("Z" tag).
91
value = value.substring(0,length-1) + "+0000";
92         break;
93       case 17: // Datetime at non-UT timezone without minutes.
94
value += "00";
95         break;
96       case 21: // Datetime at non-UT full timezone ("'mm'" PDF timezone-minutes format).
97
value = value.substring(0,length-1).replace("\'","");
98         break;
99     }
100
101     try
102     {
103       // Parse datetime value!
104
return formatter.parse(value);
105     }
106     catch(Exception JavaDoc e)
107     {
108       // Propagate the exception!
109
throw new RuntimeException JavaDoc(e);
110     }
111   }
112   // </public>
113

114   // <protected>
115
/**
116     Formats a date value as a PDF-formatted date value.
117   */

118   protected static String JavaDoc format(
119     Date JavaDoc value
120     )
121   {
122     String JavaDoc formattedValue = formatter.format(value);
123
124     return ("D:" + formattedValue.substring(0,17) + '\'' + formattedValue.substring(17) + '\'');
125   }
126   // </protected>
127
// </interface>
128
// </static>
129

130   // <dynamic>
131
// <constructors>
132
public PdfDate(
133     )
134   {}
135
136   public PdfDate(
137     Date JavaDoc value
138     )
139   {setValue(value);}
140   // </constructors>
141

142   // <interface>
143
// <public>
144
@Override JavaDoc
145   public Object JavaDoc clone(
146     File context
147     )
148   {
149     // Shallow copy.
150
PdfDate clone = (PdfDate)super.clone();
151
152     // Deep copy.
153
clone.setValue((Date JavaDoc)getValue().clone()); // Date class is mutable.
154

155     return clone;
156   }
157
158   @Override JavaDoc
159   public String JavaDoc toPdf(
160     )
161   {return PdfDate.toPdf(getValue());}
162
163   // <IPdfString>
164
public String JavaDoc getStringValue(
165     )
166   {return format(getValue());}
167
168   public void setStringValue(
169     String JavaDoc value
170     )
171   {setValue(toDate(value));}
172   // </IPdfString>
173
// </public>
174
// </interface>
175
// </dynamic>
176
// </class>
177
}
Popular Tags