KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > pdmodel > common > COSDictionaryMap


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.pdmodel.common;
32
33 import org.pdfbox.cos.COSBase;
34 import org.pdfbox.cos.COSBoolean;
35 import org.pdfbox.cos.COSDictionary;
36 import org.pdfbox.cos.COSFloat;
37 import org.pdfbox.cos.COSInteger;
38 import org.pdfbox.cos.COSName;
39 import org.pdfbox.cos.COSString;
40
41 import java.io.IOException JavaDoc;
42
43 import java.util.Collection JavaDoc;
44 import java.util.HashMap JavaDoc;
45 import java.util.Iterator JavaDoc;
46 import java.util.Map JavaDoc;
47 import java.util.Set JavaDoc;
48
49 /**
50  * This is a Map that will automatically sync the contents to a COSDictionary.
51  *
52  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
53  * @version $Revision: 1.10 $
54  */

55 public class COSDictionaryMap implements Map JavaDoc
56 {
57     private COSDictionary map;
58     private Map JavaDoc actuals;
59
60     /**
61      * Constructor for this map.
62      *
63      * @param actualsMap The map with standard java objects as values.
64      * @param dicMap The map with COSBase objects as values.
65      */

66     public COSDictionaryMap( Map JavaDoc actualsMap, COSDictionary dicMap )
67     {
68         actuals = actualsMap;
69         map = dicMap;
70     }
71
72
73     /**
74      * {@inheritDoc}
75      */

76     public int size()
77     {
78         return map.size();
79     }
80
81     /**
82      * {@inheritDoc}
83      */

84     public boolean isEmpty()
85     {
86         return size() == 0;
87     }
88
89     /**
90      * {@inheritDoc}
91      */

92     public boolean containsKey(Object JavaDoc key)
93     {
94         return map.keyList().contains( key );
95     }
96
97     /**
98      * {@inheritDoc}
99      */

100     public boolean containsValue(Object JavaDoc value)
101     {
102         return actuals.containsValue( value );
103     }
104
105     /**
106      * {@inheritDoc}
107      */

108     public Object JavaDoc get(Object JavaDoc key)
109     {
110         return actuals.get( key );
111     }
112
113     /**
114      * {@inheritDoc}
115      */

116     public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value)
117     {
118         COSObjectable object = (COSObjectable)value;
119
120         map.setItem( COSName.getPDFName( (String JavaDoc)key ), object.getCOSObject() );
121         return actuals.put( key, value );
122     }
123
124     /**
125      * {@inheritDoc}
126      */

127     public Object JavaDoc remove(Object JavaDoc key)
128     {
129         map.removeItem( COSName.getPDFName( (String JavaDoc)key ) );
130         return actuals.remove( key );
131     }
132
133     /**
134      * {@inheritDoc}
135      */

136     public void putAll(Map JavaDoc t)
137     {
138         throw new RuntimeException JavaDoc( "Not yet implemented" );
139     }
140
141     /**
142      * {@inheritDoc}
143      */

144     public void clear()
145     {
146         map.clear();
147         actuals.clear();
148     }
149
150     /**
151      * {@inheritDoc}
152      */

153     public Set JavaDoc keySet()
154     {
155         return actuals.keySet();
156     }
157
158     /**
159      * {@inheritDoc}
160      */

161     public Collection JavaDoc values()
162     {
163         return actuals.values();
164     }
165
166     /**
167      * {@inheritDoc}
168      */

169     public Set JavaDoc entrySet()
170     {
171         throw new RuntimeException JavaDoc( "Not yet implemented" );
172     }
173
174     /**
175      * {@inheritDoc}
176      */

177     public boolean equals(Object JavaDoc o)
178     {
179         boolean retval = false;
180         if( o instanceof COSDictionaryMap )
181         {
182             COSDictionaryMap other = (COSDictionaryMap)o;
183             retval = other.map.equals( this.map );
184         }
185         return retval;
186     }
187
188     /**
189      * {@inheritDoc}
190      */

191     public String JavaDoc toString()
192     {
193         return actuals.toString();
194     }
195
196     /**
197      * {@inheritDoc}
198      */

199     public int hashCode()
200     {
201         return map.hashCode();
202     }
203
204     /**
205      * This will take a map&lt;java.lang.String,org.pdfbox.pdmodel.COSObjectable&gt;
206      * and convert it into a COSDictionary&lt;COSName,COSBase&gt;.
207      *
208      * @param someMap A map containing COSObjectables
209      *
210      * @return A proper COSDictionary
211      */

212     public static COSDictionary convert( Map JavaDoc someMap )
213     {
214         Iterator JavaDoc iter = someMap.keySet().iterator();
215         COSDictionary dic = new COSDictionary();
216         while( iter.hasNext() )
217         {
218             String JavaDoc name = (String JavaDoc)iter.next();
219             COSObjectable object = (COSObjectable)someMap.get( name );
220             dic.setItem( COSName.getPDFName( name ), object.getCOSObject() );
221         }
222         return dic;
223     }
224     
225     /**
226      * This will take a COS dictionary and convert it into COSDictionaryMap. All cos
227      * objects will be converted to their primitive form.
228      *
229      * @param map The COS mappings.
230      * @return A standard java map.
231      * @throws IOException If there is an error during the conversion.
232      */

233     public static COSDictionaryMap convertBasicTypesToMap( COSDictionary map ) throws IOException JavaDoc
234     {
235         COSDictionaryMap retval = null;
236         if( map != null )
237         {
238             Map JavaDoc actualMap = new HashMap JavaDoc();
239             Iterator JavaDoc keyIter = map.keyList().iterator();
240             while( keyIter.hasNext() )
241             {
242                 COSName key = (COSName)keyIter.next();
243                 COSBase cosObj = map.getDictionaryObject( key );
244                 Object JavaDoc actualObject = null;
245                 if( cosObj instanceof COSString )
246                 {
247                     actualObject = ((COSString)cosObj).getString();
248                 }
249                 else if( cosObj instanceof COSInteger )
250                 {
251                     actualObject = new Integer JavaDoc( ((COSInteger)cosObj).intValue() );
252                 }
253                 else if( cosObj instanceof COSName )
254                 {
255                     actualObject = ((COSName)cosObj).getName();
256                 }
257                 else if( cosObj instanceof COSFloat )
258                 {
259                     actualObject = new Float JavaDoc( ((COSInteger)cosObj).floatValue() );
260                 }
261                 else if( cosObj instanceof COSBoolean )
262                 {
263                     actualObject = ((COSBoolean)cosObj).getValue() ? Boolean.TRUE : Boolean.FALSE;
264                 }
265                 else
266                 {
267                     throw new IOException JavaDoc( "Error:unknown type of object to convert:" + cosObj );
268                 }
269                 actualMap.put( key.getName(), actualObject );
270             }
271             retval = new COSDictionaryMap( actualMap, map );
272         }
273         
274         return retval;
275     }
276 }
Popular Tags