001: /*
002: Copyright © 2006,2007 Stefano Chizzolini. http://clown.stefanochizzolini.it
003:
004: Contributors:
005: * Stefano Chizzolini (original code developer, http://www.stefanochizzolini.it):
006: contributed code is Copyright © 2006,2007 by Stefano Chizzolini.
007:
008: This file should be part of the source code distribution of "PDF Clown library"
009: (the Program): see the accompanying README files for more info.
010:
011: This Program is free software; you can redistribute it and/or modify it under
012: the terms of the GNU General Public License as published by the Free Software
013: Foundation; either version 2 of the License, or (at your option) any later version.
014:
015: This Program is distributed in the hope that it will be useful, but WITHOUT ANY
016: WARRANTY, either expressed or implied; without even the implied warranty of
017: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the License for more details.
018:
019: You should have received a copy of the GNU General Public License along with this
020: Program (see README files); if not, go to the GNU website (http://www.gnu.org/).
021:
022: Redistribution and use, with or without modification, are permitted provided that such
023: redistributions retain the above copyright notice, license and disclaimer, along with
024: this list of conditions.
025: */
026:
027: package it.stefanochizzolini.clown.objects;
028:
029: import it.stefanochizzolini.clown.files.File;
030:
031: import java.util.regex.Matcher;
032: import java.util.regex.Pattern;
033:
034: /**
035: PDF name object [PDF:1.6:3.2.4].
036: */
037: /*
038: NOTE: As name objects are atomic symbols uniquely defined by sequences of characters,
039: the bytes making up the name are never treated as text, always keeping them escaped.
040: */
041: public final class PdfName extends PdfAtomicObject<String> {
042: // <class>
043: // <static>
044: // <fields>
045: /*
046: NOTE: Name lexical conventions prescribe that the following reserved characters
047: are to be escaped when placed inside names' character sequences:
048: - delimiters;
049: - whitespaces;
050: - '#' (number sign character).
051: */
052: private static final Pattern EscapedPattern = Pattern
053: .compile("#([\\da-fA-F]{2})");
054: private static final Pattern UnescapedPattern = Pattern
055: .compile("[\\s\\(\\)<>\\[\\]{}/%#]");
056:
057: public static final PdfName A85 = new PdfName("A85");
058: public static final PdfName AHx = new PdfName("AHx");
059: public static final PdfName Annots = new PdfName("Annots");
060: public static final PdfName Ascent = new PdfName("Ascent");
061: public static final PdfName ASCII85Decode = new PdfName(
062: "ASCII85Decode");
063: public static final PdfName ASCIIHexDecode = new PdfName(
064: "ASCIIHexDecode");
065: public static final PdfName Author = new PdfName("Author");
066: public static final PdfName BaseFont = new PdfName("BaseFont");
067: public static final PdfName BitsPerComponent = new PdfName(
068: "BitsPerComponent");
069: public static final PdfName BBox = new PdfName("BBox");
070: public static final PdfName BPC = new PdfName("BPC");
071: public static final PdfName CalGray = new PdfName("CalGray");
072: public static final PdfName CalRGB = new PdfName("CalRGB");
073: public static final PdfName CapHeight = new PdfName("CapHeight");
074: public static final PdfName Catalog = new PdfName("Catalog");
075: public static final PdfName CCF = new PdfName("CCF");
076: public static final PdfName CCITTFaxDecode = new PdfName(
077: "CCITTFaxDecode");
078: public static final PdfName CenterWindow = new PdfName(
079: "CenterWindow");
080: public static final PdfName ColorSpace = new PdfName("ColorSpace");
081: public static final PdfName Contents = new PdfName("Contents");
082: public static final PdfName Count = new PdfName("Count");
083: public static final PdfName CreationDate = new PdfName(
084: "CreationDate");
085: public static final PdfName Creator = new PdfName("Creator");
086: public static final PdfName Crypt = new PdfName("Crypt");
087: public static final PdfName CS = new PdfName("CS");
088: public static final PdfName D = new PdfName("D");
089: public static final PdfName Descent = new PdfName("Descent");
090: public static final PdfName Dest = new PdfName("Dest");
091: public static final PdfName Dests = new PdfName("Dests");
092: public static final PdfName DisplayDocTitle = new PdfName(
093: "DisplayDocTitle");
094: public static final PdfName DCT = new PdfName("DCT");
095: public static final PdfName DCTDecode = new PdfName("DCTDecode");
096: public static final PdfName DeviceCMYK = new PdfName("DeviceCMYK");
097: public static final PdfName DeviceGray = new PdfName("DeviceGray");
098: public static final PdfName DeviceRGB = new PdfName("DeviceRGB");
099: public static final PdfName Encoding = new PdfName("Encoding");
100: public static final PdfName Encrypt = new PdfName("Encrypt");
101: public static final PdfName F = new PdfName("F");
102: public static final PdfName Filter = new PdfName("Filter");
103: public static final PdfName First = new PdfName("First");
104: public static final PdfName FirstChar = new PdfName("FirstChar");
105: public static final PdfName Fit = new PdfName("Fit");
106: public static final PdfName FitH = new PdfName("FitH");
107: public static final PdfName FitV = new PdfName("FitV");
108: public static final PdfName FitR = new PdfName("FitR");
109: public static final PdfName FitB = new PdfName("FitB");
110: public static final PdfName FitBH = new PdfName("FitBH");
111: public static final PdfName FitBV = new PdfName("FitBV");
112: public static final PdfName FitWindow = new PdfName("FitWindow");
113: public static final PdfName Fl = new PdfName("Fl");
114: public static final PdfName Flags = new PdfName("Flags");
115: public static final PdfName FlateDecode = new PdfName("FlateDecode");
116: public static final PdfName Font = new PdfName("Font");
117: public static final PdfName FontBBox = new PdfName("FontBBox");
118: public static final PdfName FontDescriptor = new PdfName(
119: "FontDescriptor");
120: public static final PdfName FontFile = new PdfName("FontFile");
121: public static final PdfName FontFile3 = new PdfName("FontFile3");
122: public static final PdfName FontName = new PdfName("FontName");
123: public static final PdfName Form = new PdfName("Form");
124: public static final PdfName FullScreen = new PdfName("FullScreen");
125: public static final PdfName H = new PdfName("H");
126: public static final PdfName Height = new PdfName("Height");
127: public static final PdfName HideMenubar = new PdfName("HideMenubar");
128: public static final PdfName HideToolbar = new PdfName("HideToolbar");
129: public static final PdfName HideWindowUI = new PdfName(
130: "HideWindowUI");
131: public static final PdfName ICCBased = new PdfName("ICCBased");
132: public static final PdfName Image = new PdfName("Image");
133: public static final PdfName Info = new PdfName("Info");
134: public static final PdfName ItalicAngle = new PdfName("ItalicAngle");
135: public static final PdfName JBIG2Decode = new PdfName("JBIG2Decode");
136: public static final PdfName JPXDecode = new PdfName("JPXDecode");
137: public static final PdfName Keywords = new PdfName("Keywords");
138: public static final PdfName Kids = new PdfName("Kids");
139: public static final PdfName Lab = new PdfName("Lab");
140: public static final PdfName Last = new PdfName("Last");
141: public static final PdfName LastChar = new PdfName("LastChar");
142: public static final PdfName Leading = new PdfName("Leading");
143: public static final PdfName Length = new PdfName("Length");
144: public static final PdfName Limits = new PdfName("Limits");
145: public static final PdfName LZW = new PdfName("LZW");
146: public static final PdfName LZWDecode = new PdfName("LZWDecode");
147: public static final PdfName Matrix = new PdfName("Matrix");
148: public static final PdfName MediaBox = new PdfName("MediaBox");
149: public static final PdfName ModDate = new PdfName("ModDate");
150: public static final PdfName Names = new PdfName("Names");
151: public static final PdfName Next = new PdfName("Next");
152: public static final PdfName OneColumn = new PdfName("OneColumn");
153: public static final PdfName OpenType = new PdfName("OpenType");
154: public static final PdfName Outlines = new PdfName("Outlines");
155: public static final PdfName Page = new PdfName("Page");
156: public static final PdfName PageLayout = new PdfName("PageLayout");
157: public static final PdfName PageMode = new PdfName("PageMode");
158: public static final PdfName Pages = new PdfName("Pages");
159: public static final PdfName Parent = new PdfName("Parent");
160: public static final PdfName Prev = new PdfName("Prev");
161: public static final PdfName Producer = new PdfName("Producer");
162: public static final PdfName Resources = new PdfName("Resources");
163: public static final PdfName RGB = new PdfName("RGB");
164: public static final PdfName RL = new PdfName("RL");
165: public static final PdfName Root = new PdfName("Root");
166: public static final PdfName RunLengthDecode = new PdfName(
167: "RunLengthDecode");
168: public static final PdfName SinglePage = new PdfName("SinglePage");
169: public static final PdfName Size = new PdfName("Size");
170: public static final PdfName Subject = new PdfName("Subject");
171: public static final PdfName Subtype = new PdfName("Subtype");
172: public static final PdfName StemV = new PdfName("StemV");
173: public static final PdfName Title = new PdfName("Title");
174: public static final PdfName TrueType = new PdfName("TrueType");
175: public static final PdfName Type = new PdfName("Type");
176: public static final PdfName Type1 = new PdfName("Type1");
177: public static final PdfName Type1C = new PdfName("Type1C");
178: public static final PdfName TwoColumnLeft = new PdfName(
179: "TwoColumnLeft");
180: public static final PdfName UseNone = new PdfName("UseNone");
181: public static final PdfName UseOutlines = new PdfName("UseOutlines");
182: public static final PdfName UseThumbs = new PdfName("UseThumbs");
183: public static final PdfName ViewerPreferences = new PdfName(
184: "ViewerPreferences");
185: public static final PdfName W = new PdfName("W");
186: public static final PdfName Width = new PdfName("Width");
187: public static final PdfName Widths = new PdfName("Widths");
188: public static final PdfName WinAnsiEncoding = new PdfName(
189: "WinAnsiEncoding");
190: public static final PdfName XObject = new PdfName("XObject");
191: public static final PdfName XYZ = new PdfName("XYZ");
192:
193: // </fields>
194:
195: // <interface>
196: // <public>
197: public static String toPdf(String value) {
198: return "/" + value;
199: }
200:
201: // </public>
202: // </interface>
203: // </static>
204:
205: // <dynamic>
206: // <constructors>
207: public PdfName(String value) {
208: setValue(value);
209: }
210:
211: /**
212: For internal use only.
213: */
214: /*
215: NOTE: To avoid ambiguities due to the presence of '#' characters,
216: it's necessary to explicitly state when a name value has already been escaped.
217: This is tipically the case of names parsed from a previously-serialized PDF file.
218: */
219: public PdfName(String value, boolean escaped) {
220: if (escaped) {
221: super .setValue(value);
222: } else {
223: this .setValue(value);
224: }
225: }
226:
227: // </constructors>
228:
229: // <interface>
230: // <public>
231: @Override
232: public Object clone(File context) {
233: // Shallow copy.
234: PdfName clone = (PdfName) super .clone();
235:
236: // Deep copy.
237: /* NOTE: No mutable object to be cloned. */
238:
239: return clone;
240: }
241:
242: @Override
243: public boolean equals(Object object) {
244: if (!(object instanceof PdfName))
245: return false;
246:
247: return getValue().equals(((PdfName) object).getValue());
248: }
249:
250: @Override
251: public int hashCode() {
252: return getValue().hashCode();
253: }
254:
255: @Override
256: public void setValue(String value) {
257: /*
258: NOTE: Before being accepted, any character sequence identifying a name MUST be normalized
259: escaping reserved characters.
260: */
261: StringBuilder buffer = new StringBuilder();
262: int index = 0;
263: Matcher unescapedMatcher = UnescapedPattern.matcher(value);
264: while (unescapedMatcher.find()) {
265: int start = unescapedMatcher.start();
266: if (start > index) {
267: buffer.append(value.substring(index, start));
268: }
269:
270: buffer.append('#' + Integer
271: .toHexString((int) unescapedMatcher.group(0)
272: .charAt(0)));
273:
274: index = unescapedMatcher.end();
275: }
276: if (index < value.length()) {
277: buffer.append(value.substring(index));
278: }
279:
280: super .setValue(buffer.toString());
281: }
282:
283: @Override
284: public String toPdf() {
285: return PdfName.toPdf(getValue());
286: }
287:
288: @Override
289: public String toString() {
290: /*
291: NOTE: The textual representation of a name concerns unescaping reserved characters.
292: */
293: String value = getValue();
294: StringBuilder buffer = new StringBuilder();
295: int index = 0;
296: Matcher escapedMatcher = EscapedPattern.matcher(value);
297: while (escapedMatcher.find()) {
298: int start = escapedMatcher.start();
299: if (start > index) {
300: buffer.append(value.substring(index, start));
301: }
302:
303: buffer.append((char) Integer.parseInt(escapedMatcher
304: .group(1), 16));
305:
306: index = escapedMatcher.end();
307: }
308: if (index < value.length()) {
309: buffer.append(value.substring(index));
310: }
311:
312: return buffer.toString();
313: }
314: // </public>
315: // </interface>
316: // </dynamic>
317: // </class>
318: }
|