001: /**
002: * Copyright (c) 2003-2006, www.pdfbox.org
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions are met:
007: *
008: * 1. Redistributions of source code must retain the above copyright notice,
009: * this list of conditions and the following disclaimer.
010: * 2. Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: * 3. Neither the name of pdfbox; nor the names of its
014: * contributors may be used to endorse or promote products derived from this
015: * software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
018: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
019: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
020: * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
021: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
022: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
023: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
024: * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
026: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: *
028: * http://www.pdfbox.org
029: *
030: */package org.pdfbox.cos;
031:
032: import java.io.IOException;
033: import java.io.OutputStream;
034:
035: import java.util.Collections;
036: import java.util.HashMap;
037: import java.util.Map;
038:
039: import org.pdfbox.exceptions.COSVisitorException;
040: import org.pdfbox.persistence.util.COSHEXTable;
041:
042: /**
043: * This class represents a PDF named object.
044: *
045: * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
046: * @version $Revision: 1.42 $
047: */
048: public final class COSName extends COSBase implements Comparable {
049: /**
050: * Note: This is synchronized because a HashMap must be synchronized if accessed by
051: * multiple threads.
052: */
053: private static Map nameMap = Collections
054: .synchronizedMap(new HashMap(8192));
055:
056: /**
057: * A common COSName value.
058: */
059: public static final COSName A = new COSName("A");
060: /**
061: * A common COSName value.
062: */
063: public static final COSName AA = new COSName("AA");
064: /**
065: * A common COSName value.
066: */
067: public static final COSName ACRO_FORM = new COSName("AcroForm");
068: /**
069: * A common COSName value.
070: */
071: public static final COSName ANNOTS = new COSName("Annots");
072: /**
073: * A common COSName value.
074: */
075: public static final COSName ART_BOX = new COSName("ArtBox");
076: /**
077: * A common COSName value.
078: */
079: public static final COSName ASCII85_DECODE = new COSName(
080: "ASCII85Decode");
081: /**
082: * A common COSName value.
083: */
084: public static final COSName ASCII85_DECODE_ABBREVIATION = new COSName(
085: "A85");
086: /**
087: * A common COSName value.
088: */
089: public static final COSName ASCII_HEX_DECODE = new COSName(
090: "ASCIIHexDecode");
091: /**
092: * A common COSName value.
093: */
094: public static final COSName ASCII_HEX_DECODE_ABBREVIATION = new COSName(
095: "AHx");
096: /**
097: * A common COSName value.
098: */
099: public static final COSName AP = new COSName("AP");
100: /**
101: * A common COSName value.
102: */
103: public static final COSName B = new COSName("B");
104: /**
105: * A common COSName value.
106: */
107: public static final COSName BASE_ENCODING = new COSName(
108: "BaseEncoding");
109: /**
110: * A common COSName value.
111: */
112: public static final COSName BASE_FONT = new COSName("BaseFont");
113: /**
114: * A common COSName value.
115: */
116: public static final COSName BBOX = new COSName("BBox");
117: /**
118: * A common COSName value.
119: */
120: public static final COSName BLEED_BOX = new COSName("BleedBox");
121: /**
122: * A common COSName value.
123: */
124: public static final COSName CATALOG = new COSName("Catalog");
125: /**
126: * A common COSName value.
127: */
128: public static final COSName CALGRAY = new COSName("CalGray");
129: /**
130: * A common COSName value.
131: */
132: public static final COSName CALRGB = new COSName("CalRGB");
133: /**
134: * A common COSName value.
135: */
136: public static final COSName CCITTFAX_DECODE = new COSName(
137: "CCITTFaxDecode");
138: /**
139: * A common COSName value.
140: */
141: public static final COSName CCITTFAX_DECODE_ABBREVIATION = new COSName(
142: "CCF");
143: /**
144: * A common COSName value.
145: */
146: public static final COSName COLORSPACE = new COSName("ColorSpace");
147: /**
148: * A common COSName value.
149: */
150: public static final COSName CONTENTS = new COSName("Contents");
151: /**
152: * A common COSName value.
153: */
154: public static final COSName COUNT = new COSName("Count");
155: /**
156: * A common COSName value.
157: */
158: public static final COSName CROP_BOX = new COSName("CropBox");
159: /**
160: * A common COSName value.
161: */
162: public static final COSName DCT_DECODE = new COSName("DCTDecode");
163: /**
164: * A common COSName value.
165: */
166: public static final COSName DCT_DECODE_ABBREVIATION = new COSName(
167: "DCT");
168: /**
169: * A common COSName value.
170: */
171: public static final COSName DESCENDANT_FONTS = new COSName(
172: "DescendantFonts");
173: /**
174: * A common COSName value.
175: */
176: public static final COSName DEST = new COSName("Dest");
177: /**
178: * A common COSName value.
179: */
180: public static final COSName DEVICECMYK = new COSName("DeviceCMYK");
181: /**
182: * A common COSName value.
183: */
184: public static final COSName DEVICEGRAY = new COSName("DeviceGray");
185: /**
186: * A common COSName value.
187: */
188: public static final COSName DEVICEN = new COSName("DeviceN");
189: /**
190: * A common COSName value.
191: */
192: public static final COSName DEVICERGB = new COSName("DeviceRGB");
193: /**
194: * A common COSName value.
195: */
196: public static final COSName DIFFERENCES = new COSName("Differences");
197: /**
198: * A common COSName value.
199: */
200: public static final COSName DV = new COSName("DV");
201: /**
202: * A common COSName value.
203: */
204: public static final COSName ENCODING = new COSName("Encoding");
205: /**
206: * A common COSName value.
207: */
208: public static final COSName ENCODING_90MS_RKSJ_H = new COSName(
209: "90ms-RKSJ-H");
210: /**
211: * A common COSName value.
212: */
213: public static final COSName ENCODING_90MS_RKSJ_V = new COSName(
214: "90ms-RKSJ-V");
215: /**
216: * A common COSName value.
217: */
218: public static final COSName ENCODING_ETEN_B5_H = new COSName(
219: "ETen?B5?H");
220: /**
221: * A common COSName value.
222: */
223: public static final COSName ENCODING_ETEN_B5_V = new COSName(
224: "ETen?B5?V");
225: /**
226: * A common COSName value.
227: */
228: public static final COSName FIELDS = new COSName("Fields");
229: /**
230: * A common COSName value.
231: */
232: public static final COSName FILTER = new COSName("Filter");
233: /**
234: * A common COSName value.
235: */
236: public static final COSName FIRST_CHAR = new COSName("FirstChar");
237: /**
238: * A common COSName value.
239: */
240: public static final COSName FLATE_DECODE = new COSName(
241: "FlateDecode");
242: /**
243: * A common COSName value.
244: */
245: public static final COSName FLATE_DECODE_ABBREVIATION = new COSName(
246: "Fl");
247: /**
248: * A common COSName value.
249: */
250: public static final COSName FONT = new COSName("Font");
251: /**
252: * A common COSName value.
253: */
254: public static final COSName FONT_FILE = new COSName("FontFile");
255: /**
256: * A common COSName value.
257: */
258: public static final COSName FONT_FILE2 = new COSName("FontFile2");
259: /**
260: * A common COSName value.
261: */
262: public static final COSName FONT_FILE3 = new COSName("FontFile3");
263: /**
264: * A common COSName value.
265: */
266: public static final COSName FONT_DESC = new COSName(
267: "FontDescriptor");
268: /**
269: * A common COSName value.
270: */
271: public static final COSName FONT_MATRIX = new COSName("FontMatrix");
272: /**
273: * A common COSName value.
274: */
275: public static final COSName FORMTYPE = new COSName("FormType");
276: /**
277: * A common COSName value.
278: */
279: public static final COSName FRM = new COSName("FRM");
280: /**
281: * A common COSName value.
282: */
283: public static final COSName H = new COSName("H");
284: /**
285: * A common COSName value.
286: */
287: public static final COSName HEIGHT = new COSName("Height");
288: /**
289: * A common COSName value.
290: */
291: public static final COSName ICCBASED = new COSName("ICCBased");
292: /**
293: * A common COSName value.
294: */
295: public static final COSName IDENTITY_H = new COSName("Identity-H");
296: /**
297: * A common COSName value.
298: */
299: public static final COSName IMAGE = new COSName("Image");
300: /**
301: * A common COSName value.
302: */
303: public static final COSName INDEXED = new COSName("Indexed");
304: /**
305: * A common COSName value.
306: */
307: public static final COSName INFO = new COSName("Info");
308: /**
309: * A common COSName value.
310: */
311: public static final COSName JPX_DECODE = new COSName("JPXDecode");
312: /**
313: * A common COSName value.
314: */
315: public static final COSName KIDS = new COSName("Kids");
316: /**
317: * A common COSName value.
318: */
319: public static final COSName LAB = new COSName("Lab");
320: /**
321: * A common COSName value.
322: */
323: public static final COSName LAST_CHAR = new COSName("LastChar");
324: /**
325: * A common COSName value.
326: */
327: public static final COSName LENGTH = new COSName("Length");
328: /**
329: * A common COSName value.
330: */
331: public static final COSName LENGTH1 = new COSName("Length1");
332: /**
333: * A common COSName value.
334: */
335: public static final COSName LZW_DECODE = new COSName("LZWDecode");
336: /**
337: * A common COSName value.
338: */
339: public static final COSName LZW_DECODE_ABBREVIATION = new COSName(
340: "LZW");
341: /**
342: * A common COSName value.
343: */
344: public static final COSName MAC_ROMAN_ENCODING = new COSName(
345: "MacRomanEncoding");
346: /**
347: * A common COSName value.
348: */
349: public static final COSName MATRIX = new COSName("Matrix");
350: /**
351: * A common COSName value.
352: */
353: public static final COSName MEDIA_BOX = new COSName("MediaBox");
354: /**
355: * A common COSName value.
356: */
357: public static final COSName METADATA = new COSName("Metadata");
358: /**
359: * A common COSName value.
360: */
361: public static final COSName N = new COSName("N");
362: /**
363: * A common COSName value.
364: */
365: public static final COSName NAME = new COSName("Name");
366: /**
367: * A common COSName value.
368: */
369: public static final COSName P = new COSName("P");
370: /**
371: * A common COSName value.
372: */
373: public static final COSName PAGE = new COSName("Page");
374: /**
375: * A common COSName value.
376: */
377: public static final COSName PAGES = new COSName("Pages");
378: /**
379: * A common COSName value.
380: */
381: public static final COSName PARENT = new COSName("Parent");
382: /**
383: * A common COSName value.
384: */
385: public static final COSName PATTERN = new COSName("Pattern");
386: /**
387: * A common COSName value.
388: */
389: public static final COSName PDF_DOC_ENCODING = new COSName(
390: "PDFDocEncoding");
391: /**
392: * A common COSName value.
393: */
394: public static final COSName PREV = new COSName("Prev");
395: /**
396: * A common COSName value.
397: */
398: public static final COSName R = new COSName("R");
399: /**
400: * A common COSName value.
401: */
402: public static final COSName RESOURCES = new COSName("Resources");
403: /**
404: * A common COSName value.
405: */
406: public static final COSName ROOT = new COSName("Root");
407: /**
408: * A common COSName value.
409: */
410: public static final COSName ROTATE = new COSName("Rotate");
411: /**
412: * A common COSName value.
413: */
414: public static final COSName RUN_LENGTH_DECODE = new COSName(
415: "RunLengthDecode");
416: /**
417: * A common COSName value.
418: */
419: public static final COSName RUN_LENGTH_DECODE_ABBREVIATION = new COSName(
420: "RL");
421: /**
422: * A common COSName value.
423: */
424: public static final COSName SEPARATION = new COSName("Separation");
425: /**
426: * A common COSName value.
427: */
428: public static final COSName STANDARD_ENCODING = new COSName(
429: "StandardEncoding");
430: /**
431: * A common COSName value.
432: */
433: public static final COSName SUBTYPE = new COSName("Subtype");
434: /**
435: * A common COSName value.
436: */
437: public static final COSName TRIM_BOX = new COSName("TrimBox");
438: /**
439: * A common COSName value.
440: */
441: public static final COSName TRUE_TYPE = new COSName("TrueType");
442: /**
443: * A common COSName value.
444: */
445: public static final COSName TO_UNICODE = new COSName("ToUnicode");
446: /**
447: * A common COSName value.
448: */
449: public static final COSName TYPE = new COSName("Type");
450: /**
451: * A common COSName value.
452: */
453: public static final COSName TYPE0 = new COSName("Type0");
454: /**
455: * A common COSName value.
456: */
457: public static final COSName V = new COSName("V");
458: /**
459: * A common COSName value.
460: */
461: public static final COSName VERSION = new COSName("Version");
462: /**
463: * A common COSName value.
464: */
465: public static final COSName WIDTHS = new COSName("Widths");
466: /**
467: * A common COSName value.
468: */
469: public static final COSName WIN_ANSI_ENCODING = new COSName(
470: "WinAnsiEncoding");
471: /**
472: * A common COSName value.
473: */
474: public static final COSName XOBJECT = new COSName("XObject");
475:
476: /**
477: * The prefix to a PDF name.
478: */
479: public static final byte[] NAME_PREFIX = new byte[] { 47 }; // The / character
480: /**
481: * The escape character for a name.
482: */
483: public static final byte[] NAME_ESCAPE = new byte[] { 35 }; //The # character
484:
485: private String name;
486: private int hashCode;
487:
488: /**
489: * This will get a COSName object with that name.
490: *
491: * @param aName The name of the object.
492: *
493: * @return A COSName with the specified name.
494: */
495: public static final COSName getPDFName(String aName) {
496: COSName name = null;
497: if (aName != null) {
498: name = (COSName) nameMap.get(aName);
499: if (name == null) {
500: //name is added to map in the constructor
501: name = new COSName(aName);
502: }
503: }
504: return name;
505: }
506:
507: /**
508: * Private constructor. This will limit the number of COSName objects.
509: * that are created.
510: *
511: * @param aName The name of the COSName object.
512: */
513: private COSName(String aName) {
514: name = aName;
515: nameMap.put(aName, this );
516: hashCode = name.hashCode();
517: }
518:
519: /**
520: * This will get the name of this COSName object.
521: *
522: * @return The name of the object.
523: */
524: public String getName() {
525: return name;
526: }
527:
528: /**
529: * {@inheritDoc}
530: */
531: public String toString() {
532: return "COSName{" + name + "}";
533: }
534:
535: /**
536: * {@inheritDoc}
537: */
538: public boolean equals(Object o) {
539: boolean retval = this == o;
540: if (!retval && o instanceof COSName) {
541: COSName other = (COSName) o;
542: retval = name == other.name || name.equals(other.name);
543: }
544: return retval;
545: }
546:
547: /**
548: * {@inheritDoc}
549: */
550: public int hashCode() {
551: return hashCode;
552: }
553:
554: /**
555: * {@inheritDoc}
556: */
557: public int compareTo(Object o) {
558: COSName other = (COSName) o;
559: return this .name.compareTo(other.name);
560: }
561:
562: /**
563: * visitor pattern double dispatch method.
564: *
565: * @param visitor The object to notify when visiting this object.
566: * @return any object, depending on the visitor implementation, or null
567: * @throws COSVisitorException If an error occurs while visiting this object.
568: */
569: public Object accept(ICOSVisitor visitor)
570: throws COSVisitorException {
571: return visitor.visitFromName(this );
572: }
573:
574: /**
575: * This will output this string as a PDF object.
576: *
577: * @param output The stream to write to.
578: * @throws IOException If there is an error writing to the stream.
579: */
580: public void writePDF(OutputStream output) throws IOException {
581: output.write(NAME_PREFIX);
582: byte[] bytes = getName().getBytes();
583: for (int i = 0; i < bytes.length; i++) {
584: int current = ((bytes[i] + 256) % 256);
585:
586: if (current <= 32 || current >= 127 || current == '('
587: || current == ')' || current == '['
588: || current == ']' || current == '/'
589: || current == '%' || current == '<'
590: || current == '>' || current == NAME_ESCAPE[0]) {
591: output.write(NAME_ESCAPE);
592: output.write(COSHEXTable.TABLE[current]);
593: } else {
594: output.write(current);
595: }
596: }
597: }
598: }
|