KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > cos > COSName


1 /**
2  * Copyright (c) 2003-2006, www.pdfbox.org
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * 3. Neither the name of pdfbox; nor the names of its
14  * contributors may be used to endorse or promote products derived from this
15  * software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * http://www.pdfbox.org
29  *
30  */

31 package org.pdfbox.cos;
32
33 import java.io.IOException JavaDoc;
34 import java.io.OutputStream JavaDoc;
35
36 import java.util.Collections JavaDoc;
37 import java.util.HashMap JavaDoc;
38 import java.util.Map JavaDoc;
39
40 import org.pdfbox.exceptions.COSVisitorException;
41 import org.pdfbox.persistence.util.COSHEXTable;
42
43
44 /**
45  * This class represents a PDF named object.
46  *
47  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
48  * @version $Revision: 1.42 $
49  */

50 public final class COSName extends COSBase implements Comparable JavaDoc
51 {
52     /**
53      * Note: This is synchronized because a HashMap must be synchronized if accessed by
54      * multiple threads.
55      */

56     private static Map JavaDoc nameMap = Collections.synchronizedMap( new HashMap JavaDoc(8192) );
57
58     
59     /**
60      * A common COSName value.
61      */

62     public static final COSName A = new COSName( "A" );
63     /**
64      * A common COSName value.
65      */

66     public static final COSName AA = new COSName( "AA" );
67     /**
68     * A common COSName value.
69     */

70     public static final COSName ACRO_FORM = new COSName( "AcroForm" );
71     /**
72     * A common COSName value.
73     */

74     public static final COSName ANNOTS = new COSName( "Annots" );
75     /**
76      * A common COSName value.
77      */

78     public static final COSName ART_BOX = new COSName("ArtBox" );
79     /**
80     * A common COSName value.
81     */

82     public static final COSName ASCII85_DECODE = new COSName( "ASCII85Decode" );
83     /**
84     * A common COSName value.
85     */

86     public static final COSName ASCII85_DECODE_ABBREVIATION = new COSName( "A85" );
87     /**
88     * A common COSName value.
89     */

90     public static final COSName ASCII_HEX_DECODE = new COSName( "ASCIIHexDecode" );
91     /**
92     * A common COSName value.
93     */

94     public static final COSName ASCII_HEX_DECODE_ABBREVIATION = new COSName( "AHx" );
95     /**
96     * A common COSName value.
97     */

98     public static final COSName AP = new COSName( "AP" );
99     /**
100      * A common COSName value.
101      */

102     public static final COSName B = new COSName( "B" );
103     /**
104     * A common COSName value.
105     */

106     public static final COSName BASE_ENCODING = new COSName( "BaseEncoding" );
107     /**
108     * A common COSName value.
109     */

110     public static final COSName BASE_FONT = new COSName( "BaseFont" );
111     /**
112     * A common COSName value.
113     */

114     public static final COSName BBOX = new COSName( "BBox" );
115     /**
116      * A common COSName value.
117      */

118     public static final COSName BLEED_BOX = new COSName("BleedBox" );
119     /**
120     * A common COSName value.
121     */

122     public static final COSName CATALOG = new COSName( "Catalog" );
123     /**
124     * A common COSName value.
125     */

126     public static final COSName CALGRAY = new COSName( "CalGray" );
127     /**
128     * A common COSName value.
129     */

130     public static final COSName CALRGB = new COSName( "CalRGB" );
131     /**
132     * A common COSName value.
133     */

134     public static final COSName CCITTFAX_DECODE = new COSName( "CCITTFaxDecode" );
135     /**
136     * A common COSName value.
137     */

138     public static final COSName CCITTFAX_DECODE_ABBREVIATION = new COSName( "CCF" );
139     /**
140     * A common COSName value.
141     */

142     public static final COSName COLORSPACE = new COSName( "ColorSpace" );
143     /**
144     * A common COSName value.
145     */

146     public static final COSName CONTENTS = new COSName( "Contents" );
147     /**
148     * A common COSName value.
149     */

150     public static final COSName COUNT = new COSName( "Count" );
151     /**
152      * A common COSName value.
153      */

154     public static final COSName CROP_BOX = new COSName( "CropBox" );
155     /**
156      * A common COSName value.
157      */

158     public static final COSName DCT_DECODE = new COSName( "DCTDecode" );
159     /**
160      * A common COSName value.
161      */

162     public static final COSName DCT_DECODE_ABBREVIATION = new COSName( "DCT" );
163     /**
164      * A common COSName value.
165      */

166     public static final COSName DESCENDANT_FONTS = new COSName( "DescendantFonts" );
167     /**
168      * A common COSName value.
169      */

170     public static final COSName DEST = new COSName( "Dest" );
171     /**
172     * A common COSName value.
173     */

174     public static final COSName DEVICECMYK = new COSName( "DeviceCMYK" );
175     /**
176     * A common COSName value.
177     */

178     public static final COSName DEVICEGRAY = new COSName( "DeviceGray" );
179     /**
180     * A common COSName value.
181     */

182     public static final COSName DEVICEN = new COSName( "DeviceN" );
183     /**
184     * A common COSName value.
185     */

186     public static final COSName DEVICERGB = new COSName( "DeviceRGB" );
187     /**
188      * A common COSName value.
189      */

190     public static final COSName DIFFERENCES = new COSName( "Differences" );
191     /**
192     * A common COSName value.
193     */

194     public static final COSName DV = new COSName( "DV" );
195     /**
196     * A common COSName value.
197     */

198     public static final COSName ENCODING = new COSName( "Encoding" );
199     /**
200      * A common COSName value.
201      */

202     public static final COSName ENCODING_90MS_RKSJ_H = new COSName( "90ms-RKSJ-H" );
203     /**
204      * A common COSName value.
205      */

206     public static final COSName ENCODING_90MS_RKSJ_V = new COSName( "90ms-RKSJ-V" );
207     /**
208      * A common COSName value.
209      */

210     public static final COSName ENCODING_ETEN_B5_H = new COSName( "ETen?B5?H" );
211     /**
212      * A common COSName value.
213      */

214     public static final COSName ENCODING_ETEN_B5_V = new COSName( "ETen?B5?V" );
215     /**
216      * A common COSName value.
217      */

218     public static final COSName FIELDS = new COSName( "Fields" );
219     /**
220     * A common COSName value.
221     */

222     public static final COSName FILTER = new COSName( "Filter" );
223     /**
224     * A common COSName value.
225     */

226     public static final COSName FIRST_CHAR = new COSName( "FirstChar" );
227     /**
228     * A common COSName value.
229     */

230     public static final COSName FLATE_DECODE = new COSName( "FlateDecode" );
231     /**
232     * A common COSName value.
233     */

234     public static final COSName FLATE_DECODE_ABBREVIATION = new COSName( "Fl" );
235     /**
236     * A common COSName value.
237     */

238     public static final COSName FONT = new COSName( "Font" );
239     /**
240      * A common COSName value.
241      */

242     public static final COSName FONT_FILE = new COSName("FontFile");
243     /**
244      * A common COSName value.
245      */

246     public static final COSName FONT_FILE2 = new COSName("FontFile2");
247     /**
248      * A common COSName value.
249      */

250     public static final COSName FONT_FILE3 = new COSName("FontFile3");
251     /**
252      * A common COSName value.
253      */

254     public static final COSName FONT_DESC = new COSName("FontDescriptor");
255     /**
256      * A common COSName value.
257      */

258     public static final COSName FONT_MATRIX = new COSName("FontMatrix" );
259     /**
260     * A common COSName value.
261     */

262     public static final COSName FORMTYPE = new COSName( "FormType" );
263     /**
264     * A common COSName value.
265     */

266     public static final COSName FRM = new COSName( "FRM" );
267     /**
268      * A common COSName value.
269      */

270      public static final COSName H = new COSName( "H" );
271     /**
272     * A common COSName value.
273     */

274     public static final COSName HEIGHT = new COSName( "Height" );
275     /**
276     * A common COSName value.
277     */

278     public static final COSName ICCBASED = new COSName( "ICCBased" );
279     /**
280     * A common COSName value.
281     */

282     public static final COSName IDENTITY_H = new COSName( "Identity-H" );
283     /**
284     * A common COSName value.
285     */

286     public static final COSName IMAGE = new COSName( "Image" );
287     /**
288     * A common COSName value.
289     */

290     public static final COSName INDEXED = new COSName( "Indexed" );
291     /**
292      * A common COSName value.
293      */

294     public static final COSName INFO = new COSName( "Info" );
295     /**
296     * A common COSName value.
297     */

298     public static final COSName JPX_DECODE = new COSName( "JPXDecode" );
299     /**
300     * A common COSName value.
301     */

302     public static final COSName KIDS = new COSName( "Kids" );
303     /**
304     * A common COSName value.
305     */

306     public static final COSName LAB = new COSName( "Lab" );
307     /**
308     * A common COSName value.
309     */

310     public static final COSName LAST_CHAR = new COSName( "LastChar" );
311     /**
312     * A common COSName value.
313     */

314     public static final COSName LENGTH = new COSName( "Length" );
315     /**
316      * A common COSName value.
317      */

318     public static final COSName LENGTH1 = new COSName( "Length1" );
319     /**
320     * A common COSName value.
321     */

322     public static final COSName LZW_DECODE = new COSName( "LZWDecode" );
323     /**
324     * A common COSName value.
325     */

326     public static final COSName LZW_DECODE_ABBREVIATION = new COSName( "LZW" );
327     /**
328     * A common COSName value.
329     */

330     public static final COSName MAC_ROMAN_ENCODING = new COSName( "MacRomanEncoding" );
331     /**
332     * A common COSName value.
333     */

334     public static final COSName MATRIX = new COSName( "Matrix" );
335     /**
336      * A common COSName value.
337      */

338     public static final COSName MEDIA_BOX = new COSName( "MediaBox" );
339     /**
340      * A common COSName value.
341      */

342     public static final COSName METADATA = new COSName( "Metadata" );
343     /**
344     * A common COSName value.
345     */

346     public static final COSName N = new COSName( "N" );
347     /**
348     * A common COSName value.
349     */

350     public static final COSName NAME = new COSName( "Name" );
351     /**
352     * A common COSName value.
353     */

354     public static final COSName P = new COSName( "P" );
355     /**
356     * A common COSName value.
357     */

358     public static final COSName PAGE = new COSName( "Page" );
359     /**
360     * A common COSName value.
361     */

362     public static final COSName PAGES = new COSName( "Pages" );
363     /**
364     * A common COSName value.
365     */

366     public static final COSName PARENT = new COSName( "Parent" );
367     /**
368     * A common COSName value.
369     */

370     public static final COSName PATTERN = new COSName( "Pattern" );
371     /**
372     * A common COSName value.
373     */

374     public static final COSName PDF_DOC_ENCODING = new COSName( "PDFDocEncoding" );
375     /**
376     * A common COSName value.
377     */

378     public static final COSName PREV = new COSName( "Prev" );
379     /**
380      * A common COSName value.
381      */

382      public static final COSName R = new COSName( "R" );
383     /**
384     * A common COSName value.
385     */

386     public static final COSName RESOURCES = new COSName( "Resources" );
387     /**
388     * A common COSName value.
389     */

390     public static final COSName ROOT = new COSName( "Root" );
391     /**
392      * A common COSName value.
393      */

394     public static final COSName ROTATE = new COSName( "Rotate" );
395     /**
396     * A common COSName value.
397     */

398     public static final COSName RUN_LENGTH_DECODE = new COSName( "RunLengthDecode" );
399     /**
400     * A common COSName value.
401     */

402     public static final COSName RUN_LENGTH_DECODE_ABBREVIATION = new COSName( "RL" );
403     /**
404     * A common COSName value.
405     */

406     public static final COSName SEPARATION = new COSName( "Separation" );
407     /**
408     * A common COSName value.
409     */

410     public static final COSName STANDARD_ENCODING = new COSName( "StandardEncoding" );
411     /**
412     * A common COSName value.
413     */

414     public static final COSName SUBTYPE = new COSName( "Subtype" );
415     /**
416      * A common COSName value.
417      */

418     public static final COSName TRIM_BOX = new COSName("TrimBox" );
419     /**
420      * A common COSName value.
421      */

422     public static final COSName TRUE_TYPE = new COSName("TrueType" );
423     /**
424     * A common COSName value.
425     */

426     public static final COSName TO_UNICODE = new COSName( "ToUnicode" );
427     /**
428     * A common COSName value.
429     */

430     public static final COSName TYPE = new COSName( "Type" );
431     /**
432      * A common COSName value.
433      */

434     public static final COSName TYPE0 = new COSName( "Type0" );
435     /**
436     * A common COSName value.
437     */

438     public static final COSName V = new COSName( "V" );
439     /**
440      * A common COSName value.
441      */

442      public static final COSName VERSION = new COSName( "Version" );
443     /**
444     * A common COSName value.
445     */

446     public static final COSName WIDTHS = new COSName( "Widths" );
447     /**
448     * A common COSName value.
449     */

450     public static final COSName WIN_ANSI_ENCODING = new COSName( "WinAnsiEncoding" );
451     /**
452     * A common COSName value.
453     */

454     public static final COSName XOBJECT = new COSName( "XObject" );
455     
456     /**
457      * The prefix to a PDF name.
458      */

459     public static final byte[] NAME_PREFIX = new byte[] { 47 }; // The / character
460
/**
461      * The escape character for a name.
462      */

463     public static final byte[] NAME_ESCAPE = new byte[] { 35 }; //The # character
464

465     private String JavaDoc name;
466     private int hashCode;
467
468     /**
469      * This will get a COSName object with that name.
470      *
471      * @param aName The name of the object.
472      *
473      * @return A COSName with the specified name.
474      */

475     public static final COSName getPDFName( String JavaDoc aName )
476     {
477         COSName name = null;
478         if( aName != null )
479         {
480             name = (COSName)nameMap.get( aName );
481             if( name == null )
482             {
483                 //name is added to map in the constructor
484
name = new COSName( aName );
485             }
486         }
487         return name;
488     }
489
490     /**
491      * Private constructor. This will limit the number of COSName objects.
492      * that are created.
493      *
494      * @param aName The name of the COSName object.
495      */

496     private COSName( String JavaDoc aName )
497     {
498         name = aName;
499         nameMap.put( aName, this );
500         hashCode = name.hashCode();
501     }
502
503     /**
504      * This will get the name of this COSName object.
505      *
506      * @return The name of the object.
507      */

508     public String JavaDoc getName()
509     {
510         return name;
511     }
512
513     /**
514      * {@inheritDoc}
515      */

516     public String JavaDoc toString()
517     {
518         return "COSName{" + name + "}";
519     }
520
521     /**
522      * {@inheritDoc}
523      */

524     public boolean equals( Object JavaDoc o )
525     {
526         boolean retval = this == o;
527         if( !retval && o instanceof COSName )
528         {
529             COSName other = (COSName)o;
530             retval = name == other.name || name.equals( other.name );
531         }
532         return retval;
533     }
534
535     /**
536      * {@inheritDoc}
537      */

538     public int hashCode()
539     {
540         return hashCode;
541     }
542     
543     /**
544      * {@inheritDoc}
545      */

546     public int compareTo(Object JavaDoc o)
547     {
548         COSName other = (COSName)o;
549         return this.name.compareTo( other.name );
550     }
551
552
553
554     /**
555      * visitor pattern double dispatch method.
556      *
557      * @param visitor The object to notify when visiting this object.
558      * @return any object, depending on the visitor implementation, or null
559      * @throws COSVisitorException If an error occurs while visiting this object.
560      */

561     public Object JavaDoc accept(ICOSVisitor visitor) throws COSVisitorException
562     {
563         return visitor.visitFromName(this);
564     }
565     
566     /**
567      * This will output this string as a PDF object.
568      *
569      * @param output The stream to write to.
570      * @throws IOException If there is an error writing to the stream.
571      */

572     public void writePDF( OutputStream JavaDoc output ) throws IOException JavaDoc
573     {
574         output.write(NAME_PREFIX);
575         byte[] bytes = getName().getBytes();
576         for (int i = 0; i < bytes.length;i++)
577         {
578             int current = ((bytes[i]+256)%256);
579
580             if(current <= 32 || current >= 127 ||
581                current == '(' ||
582                current == ')' ||
583                current == '[' ||
584                current == ']' ||
585                current == '/' ||
586                current == '%' ||
587                current == '<' ||
588                current == '>' ||
589                current == NAME_ESCAPE[0] )
590             {
591                 output.write(NAME_ESCAPE);
592                 output.write(COSHEXTable.TABLE[current]);
593             }
594             else
595             {
596                 output.write(current);
597             }
598         }
599     }
600 }
Popular Tags