KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > cmaptypes > CMap


1 /**
2  * Copyright (c) 2003, 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.cmaptypes;
32
33 import java.io.IOException JavaDoc;
34
35 import java.util.ArrayList JavaDoc;
36 import java.util.HashMap JavaDoc;
37 import java.util.List JavaDoc;
38 import java.util.Map JavaDoc;
39
40 /**
41  * This class represents a CMap file.
42  *
43  * @author Ben Litchfield (ben@csh.rit.edu)
44  * @version $Revision: 1.6 $
45  */

46 public class CMap
47 {
48     private List JavaDoc codeSpaceRanges = new ArrayList JavaDoc();
49     private Map JavaDoc singleByteMappings = new HashMap JavaDoc();
50     private Map JavaDoc doubleByteMappings = new HashMap JavaDoc();
51
52     /**
53      * Creates a new instance of CMap.
54      */

55     public CMap()
56     {
57         //default constructor
58
}
59     
60     /**
61      * This will tell if this cmap has any one byte mappings.
62      *
63      * @return true If there are any one byte mappings, false otherwise.
64      */

65     public boolean hasOneByteMappings()
66     {
67         return singleByteMappings.size() > 0;
68     }
69     
70     /**
71      * This will tell if this cmap has any two byte mappings.
72      *
73      * @return true If there are any two byte mappings, false otherwise.
74      */

75     public boolean hasTwoByteMappings()
76     {
77         return doubleByteMappings.size() > 0;
78     }
79
80     /**
81      * This will perform a lookup into the map.
82      *
83      * @param code The code used to lookup.
84      * @param offset The offset into the byte array.
85      * @param length The length of the data we are getting.
86      *
87      * @return The string that matches the lookup.
88      */

89     public String JavaDoc lookup( byte[] code, int offset, int length )
90     {
91
92         String JavaDoc result = null;
93         Integer JavaDoc key = null;
94         if( length == 1 )
95         {
96             
97             key = new Integer JavaDoc( (code[offset]+256)%256 );
98             result = (String JavaDoc)singleByteMappings.get( key );
99         }
100         else if( length == 2 )
101         {
102             int intKey = (code[offset]+256)%256;
103             intKey <<= 8;
104             intKey += (code[offset+1]+256)%256;
105             key = new Integer JavaDoc( intKey );
106
107             result = (String JavaDoc)doubleByteMappings.get( key );
108         }
109
110         return result;
111     }
112
113     /**
114      * This will add a mapping.
115      *
116      * @param src The src to the mapping.
117      * @param dest The dest to the mapping.
118      *
119      * @throws IOException if the src is invalid.
120      */

121     public void addMapping( byte[] src, String JavaDoc dest ) throws IOException JavaDoc
122     {
123         if( src.length == 1 )
124         {
125             singleByteMappings.put( new Integer JavaDoc( src[0] ), dest );
126         }
127         else if( src.length == 2 )
128         {
129             int intSrc = src[0];
130             intSrc <<= 8;
131             intSrc |= (src[1]&0xFF);
132             doubleByteMappings.put( new Integer JavaDoc( intSrc ), dest );
133         }
134         else
135         {
136             throw new IOException JavaDoc( "Mapping code should be 1 or two bytes and not " + src.length );
137         }
138     }
139
140
141     /**
142      * This will add a codespace range.
143      *
144      * @param range A single codespace range.
145      */

146     public void addCodespaceRange( CodespaceRange range )
147     {
148         codeSpaceRanges.add( range );
149     }
150
151     /**
152      * Getter for property codeSpaceRanges.
153      *
154      * @return Value of property codeSpaceRanges.
155      */

156     public List JavaDoc getCodeSpaceRanges()
157     {
158         return codeSpaceRanges;
159     }
160
161 }
Popular Tags