KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2   Copyright © 2006,2007 Stefano Chizzolini. http://clown.stefanochizzolini.it
3
4   Contributors:
5     * Stefano Chizzolini (original code developer, info@stefanochizzolini.it):
6       contributed code is Copyright © 2006,2007 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
31 import java.util.regex.Matcher JavaDoc;
32 import java.util.regex.Pattern JavaDoc;
33
34 /**
35   PDF name object [PDF:1.6:3.2.4].
36 */

37 /*
38   NOTE: As name objects are atomic symbols uniquely defined by sequences of characters,
39   the bytes making up the name are never treated as text, always keeping them escaped.
40 */

41 public final class PdfName
42   extends PdfAtomicObject<String JavaDoc>
43 {
44   // <class>
45
// <static>
46
// <fields>
47
/*
48     NOTE: Name lexical conventions prescribe that the following reserved characters
49     are to be escaped when placed inside names' character sequences:
50       - delimiters;
51       - whitespaces;
52       - '#' (number sign character).
53   */

54   private static final Pattern JavaDoc EscapedPattern = Pattern.compile("#([\\da-fA-F]{2})");
55   private static final Pattern JavaDoc UnescapedPattern = Pattern.compile("[\\s\\(\\)<>\\[\\]{}/%#]");
56
57   public static final PdfName A85 = new PdfName("A85");
58   public static final PdfName AHx = new PdfName("AHx");
59   public static final PdfName Annots = new PdfName("Annots");
60   public static final PdfName Ascent = new PdfName("Ascent");
61   public static final PdfName ASCII85Decode = new PdfName("ASCII85Decode");
62   public static final PdfName ASCIIHexDecode = new PdfName("ASCIIHexDecode");
63   public static final PdfName Author = new PdfName("Author");
64   public static final PdfName BaseFont = new PdfName("BaseFont");
65   public static final PdfName BitsPerComponent = new PdfName("BitsPerComponent");
66   public static final PdfName BBox = new PdfName("BBox");
67   public static final PdfName CalGray = new PdfName("CalGray");
68   public static final PdfName CalRGB = new PdfName("CalRGB");
69   public static final PdfName CapHeight = new PdfName("CapHeight");
70   public static final PdfName Catalog = new PdfName("Catalog");
71   public static final PdfName CCF = new PdfName("CCF");
72   public static final PdfName CCITTFaxDecode = new PdfName("CCITTFaxDecode");
73   public static final PdfName CenterWindow = new PdfName("CenterWindow");
74   public static final PdfName ColorSpace = new PdfName("ColorSpace");
75   public static final PdfName Contents = new PdfName("Contents");
76   public static final PdfName Count = new PdfName("Count");
77   public static final PdfName CreationDate = new PdfName("CreationDate");
78   public static final PdfName Creator = new PdfName("Creator");
79   public static final PdfName Crypt = new PdfName("Crypt");
80   public static final PdfName Descent = new PdfName("Descent");
81   public static final PdfName Dest = new PdfName("Dest");
82   public static final PdfName DisplayDocTitle = new PdfName("DisplayDocTitle");
83   public static final PdfName DCT = new PdfName("DCT");
84   public static final PdfName DCTDecode = new PdfName("DCTDecode");
85   public static final PdfName DeviceCMYK = new PdfName("DeviceCMYK");
86   public static final PdfName DeviceGray = new PdfName("DeviceGray");
87   public static final PdfName DeviceRGB = new PdfName("DeviceRGB");
88   public static final PdfName Encoding = new PdfName("Encoding");
89   public static final PdfName Encrypt = new PdfName("Encrypt");
90   public static final PdfName Filter = new PdfName("Filter");
91   public static final PdfName First = new PdfName("First");
92   public static final PdfName FirstChar = new PdfName("FirstChar");
93   public static final PdfName Fit = new PdfName("Fit");
94   public static final PdfName FitH = new PdfName("FitH");
95   public static final PdfName FitV = new PdfName("FitV");
96   public static final PdfName FitR = new PdfName("FitR");
97   public static final PdfName FitB = new PdfName("FitB");
98   public static final PdfName FitBH = new PdfName("FitBH");
99   public static final PdfName FitBV = new PdfName("FitBV");
100   public static final PdfName FitWindow = new PdfName("FitWindow");
101   public static final PdfName Fl = new PdfName("Fl");
102   public static final PdfName Flags = new PdfName("Flags");
103   public static final PdfName FlateDecode = new PdfName("FlateDecode");
104   public static final PdfName Font = new PdfName("Font");
105   public static final PdfName FontBBox = new PdfName("FontBBox");
106   public static final PdfName FontDescriptor = new PdfName("FontDescriptor");
107   public static final PdfName FontFile = new PdfName("FontFile");
108   public static final PdfName FontFile3 = new PdfName("FontFile3");
109   public static final PdfName FontName = new PdfName("FontName");
110   public static final PdfName Form = new PdfName("Form");
111   public static final PdfName FullScreen = new PdfName("FullScreen");
112   public static final PdfName Height = new PdfName("Height");
113   public static final PdfName HideMenubar = new PdfName("HideMenubar");
114   public static final PdfName HideToolbar = new PdfName("HideToolbar");
115   public static final PdfName HideWindowUI = new PdfName("HideWindowUI");
116   public static final PdfName ICCBased = new PdfName("ICCBased");
117   public static final PdfName Image = new PdfName("Image");
118   public static final PdfName Info = new PdfName("Info");
119   public static final PdfName ItalicAngle = new PdfName("ItalicAngle");
120   public static final PdfName JBIG2Decode = new PdfName("JBIG2Decode");
121   public static final PdfName JPXDecode = new PdfName("JPXDecode");
122   public static final PdfName Keywords = new PdfName("Keywords");
123   public static final PdfName Kids = new PdfName("Kids");
124   public static final PdfName Lab = new PdfName("Lab");
125   public static final PdfName Last = new PdfName("Last");
126   public static final PdfName LastChar = new PdfName("LastChar");
127   public static final PdfName Leading = new PdfName("Leading");
128   public static final PdfName Length = new PdfName("Length");
129   public static final PdfName LZW = new PdfName("LZW");
130   public static final PdfName LZWDecode = new PdfName("LZWDecode");
131   public static final PdfName MediaBox = new PdfName("MediaBox");
132   public static final PdfName ModDate = new PdfName("ModDate");
133   public static final PdfName Next = new PdfName("Next");
134   public static final PdfName OneColumn = new PdfName("OneColumn");
135   public static final PdfName OpenType = new PdfName("OpenType");
136   public static final PdfName Outlines = new PdfName("Outlines");
137   public static final PdfName Page = new PdfName("Page");
138   public static final PdfName PageLayout = new PdfName("PageLayout");
139   public static final PdfName PageMode = new PdfName("PageMode");
140   public static final PdfName Pages = new PdfName("Pages");
141   public static final PdfName Parent = new PdfName("Parent");
142   public static final PdfName Prev = new PdfName("Prev");
143   public static final PdfName Producer = new PdfName("Producer");
144   public static final PdfName Resources = new PdfName("Resources");
145   public static final PdfName RL = new PdfName("RL");
146   public static final PdfName Root = new PdfName("Root");
147   public static final PdfName RunLengthDecode = new PdfName("RunLengthDecode");
148   public static final PdfName SinglePage = new PdfName("SinglePage");
149   public static final PdfName Size = new PdfName("Size");
150   public static final PdfName Subject = new PdfName("Subject");
151   public static final PdfName Subtype = new PdfName("Subtype");
152   public static final PdfName StemV = new PdfName("StemV");
153   public static final PdfName Title = new PdfName("Title");
154   public static final PdfName TrueType = new PdfName("TrueType");
155   public static final PdfName Type = new PdfName("Type");
156   public static final PdfName Type1 = new PdfName("Type1");
157   public static final PdfName Type1C = new PdfName("Type1C");
158   public static final PdfName TwoColumnLeft = new PdfName("TwoColumnLeft");
159   public static final PdfName UseNone = new PdfName("UseNone");
160   public static final PdfName UseOutlines = new PdfName("UseOutlines");
161   public static final PdfName UseThumbs = new PdfName("UseThumbs");
162   public static final PdfName ViewerPreferences = new PdfName("ViewerPreferences");
163   public static final PdfName Width = new PdfName("Width");
164   public static final PdfName Widths = new PdfName("Widths");
165   public static final PdfName WinAnsiEncoding = new PdfName("WinAnsiEncoding");
166   public static final PdfName XObject = new PdfName("XObject");
167   public static final PdfName XYZ = new PdfName("XYZ");
168   // </fields>
169

170   // <interface>
171
// <public>
172
public static String JavaDoc toPdf(
173     String JavaDoc value
174     )
175   {return "/" + value;}
176   // </public>
177
// </interface>
178
// </static>
179

180   // <dynamic>
181
// <constructors>
182
public PdfName(
183     String JavaDoc value
184     )
185   {setValue(value);}
186
187   /**
188     For internal use only.
189   */

190   /*
191     NOTE: To avoid ambiguities due to the presence of '#' characters,
192     it's necessary to explicitly state when a name value has already been escaped.
193     This is tipically the case of names parsed from a previously-serialized PDF file.
194   */

195   public PdfName(
196     String JavaDoc value,
197     boolean escaped
198     )
199   {
200     if(escaped)
201     {super.setValue(value);}
202     else
203     {this.setValue(value);}
204   }
205   // </constructors>
206

207   // <interface>
208
// <public>
209
@Override JavaDoc
210   public Object JavaDoc clone(
211     File context
212     )
213   {
214     // Shallow copy.
215
PdfName clone = (PdfName)super.clone();
216
217     // Deep copy.
218
/* NOTE: No mutable object to be cloned. */
219
220     return clone;
221   }
222
223   @Override JavaDoc
224   public boolean equals(
225     Object JavaDoc object
226     )
227   {
228     if(!(object instanceof PdfName))
229       return false;
230
231     return getValue().equals(((PdfName)object).getValue());
232   }
233
234   @Override JavaDoc
235   public int hashCode(
236     )
237   {return getValue().hashCode();}
238
239   @Override JavaDoc
240   public void setValue(
241     String JavaDoc value
242     )
243   {
244     /*
245       NOTE: Before being accepted, any character sequence identifying a name MUST be normalized
246       escaping reserved characters.
247     */

248     StringBuilder JavaDoc buffer = new StringBuilder JavaDoc();
249     int index = 0;
250     Matcher JavaDoc unescapedMatcher = UnescapedPattern.matcher(value);
251     while(unescapedMatcher.find())
252     {
253       int start = unescapedMatcher.start();
254       if(start > index)
255       {buffer.append(value.substring(index,start));}
256
257       buffer.append(
258         '#' + Integer.toHexString(
259           (int)unescapedMatcher.group(0).charAt(0)
260           )
261         );
262
263       index = unescapedMatcher.end();
264     }
265     if(index < value.length())
266     {buffer.append(value.substring(index));}
267
268     super.setValue(buffer.toString());
269   }
270
271   @Override JavaDoc
272   public String JavaDoc toPdf(
273     )
274   {return PdfName.toPdf(getValue());}
275
276   @Override JavaDoc
277   public String JavaDoc toString(
278     )
279   {
280     /*
281       NOTE: The textual representation of a name concerns unescaping reserved characters.
282     */

283     String JavaDoc value = getValue();
284     StringBuilder JavaDoc buffer = new StringBuilder JavaDoc();
285     int index = 0;
286     Matcher JavaDoc escapedMatcher = EscapedPattern.matcher(value);
287     while(escapedMatcher.find())
288     {
289       int start = escapedMatcher.start();
290       if(start > index)
291       {buffer.append(value.substring(index,start));}
292
293       buffer.append(
294         (char)Integer.parseInt(
295           escapedMatcher.group(1),
296           16
297           )
298         );
299
300       index = escapedMatcher.end();
301     }
302     if(index < value.length())
303     {buffer.append(value.substring(index));}
304
305     return buffer.toString();
306   }
307   // </public>
308
// </interface>
309
// </dynamic>
310
// </class>
311
}
Popular Tags