Source Code Cross Referenced for COSDictionary.java in  » PDF » PDFBox-0.7.3 » org » pdfbox » cos » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » PDF » PDFBox 0.7.3 » org.pdfbox.cos 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /**
0002:         * Copyright (c) 2003-2005, www.pdfbox.org
0003:         * All rights reserved.
0004:         *
0005:         * Redistribution and use in source and binary forms, with or without
0006:         * modification, are permitted provided that the following conditions are met:
0007:         *
0008:         * 1. Redistributions of source code must retain the above copyright notice,
0009:         *    this list of conditions and the following disclaimer.
0010:         * 2. Redistributions in binary form must reproduce the above copyright notice,
0011:         *    this list of conditions and the following disclaimer in the documentation
0012:         *    and/or other materials provided with the distribution.
0013:         * 3. Neither the name of pdfbox; nor the names of its
0014:         *    contributors may be used to endorse or promote products derived from this
0015:         *    software without specific prior written permission.
0016:         *
0017:         * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0018:         * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0019:         * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
0020:         * DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
0021:         * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
0022:         * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
0023:         * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
0024:         * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0025:         * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
0026:         * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0027:         *
0028:         * http://www.pdfbox.org
0029:         *
0030:         */package org.pdfbox.cos;
0031:
0032:        import java.io.IOException;
0033:        import java.util.ArrayList;
0034:        import java.util.Calendar;
0035:        import java.util.Collection;
0036:        import java.util.HashMap;
0037:        import java.util.List;
0038:        import java.util.Map;
0039:
0040:        import java.util.Iterator;
0041:
0042:        import org.pdfbox.exceptions.COSVisitorException;
0043:
0044:        import org.pdfbox.pdmodel.common.COSObjectable;
0045:        import org.pdfbox.util.DateConverter;
0046:
0047:        /**
0048:         * This class represents a dictionary where name/value pairs reside.
0049:         *
0050:         * @author <a href="ben@benlitchfield.com">Ben Litchfield</a>
0051:         * @version $Revision: 1.32 $
0052:         */
0053:        public class COSDictionary extends COSBase {
0054:            private static final String PATH_SEPARATOR = "/";
0055:
0056:            /**
0057:             * These are all of the items in the dictionary.
0058:             */
0059:            private Map items = new HashMap();
0060:
0061:            /**
0062:             * Used to store original sequence of keys, for testing.
0063:             */
0064:            private List keys = new ArrayList();
0065:
0066:            /**
0067:             * Constructor.
0068:             */
0069:            public COSDictionary() {
0070:                //default constructor
0071:            }
0072:
0073:            /**
0074:             * Copy Constructor.  This will make a shallow copy of this dictionary.
0075:             *
0076:             * @param dict The dictionary to copy.
0077:             */
0078:            public COSDictionary(COSDictionary dict) {
0079:                items = new HashMap(dict.items);
0080:                keys = new ArrayList(dict.keys);
0081:            }
0082:
0083:            /**
0084:             * @see java.util.Map#containsValue(java.lang.Object)
0085:             * 
0086:             * @param value The value to find in the map.
0087:             * 
0088:             * @return true if the map contains this value.
0089:             */
0090:            public boolean containsValue(Object value) {
0091:                boolean contains = items.containsValue(value);
0092:                if (!contains && value instanceof  COSObject) {
0093:                    contains = items.containsValue(((COSObject) value)
0094:                            .getObject());
0095:                }
0096:                return contains;
0097:            }
0098:
0099:            /**
0100:             * Search in the map for the value that matches the parameter
0101:             * and return the first key that maps to that value.
0102:             * 
0103:             * @param value The value to search for in the map.
0104:             * @return The key for the value in the map or null if it does not exist.
0105:             */
0106:            public COSName getKeyForValue(Object value) {
0107:                COSName key = null;
0108:                Iterator iter = items.entrySet().iterator();
0109:                while (key == null && iter.hasNext()) {
0110:                    Map.Entry next = (Map.Entry) iter.next();
0111:                    Object nextValue = next.getValue();
0112:                    if (nextValue.equals(value)
0113:                            || (nextValue instanceof  COSObject && ((COSObject) nextValue)
0114:                                    .getObject().equals(value))) {
0115:                        key = (COSName) next.getKey();
0116:                    }
0117:                }
0118:
0119:                return key;
0120:            }
0121:
0122:            /**
0123:             * This will return the number of elements in this dictionary.
0124:             *
0125:             * @return The number of elements in the dictionary.
0126:             */
0127:            public int size() {
0128:                return keys.size();
0129:            }
0130:
0131:            /**
0132:             * This will clear all items in the map.
0133:             */
0134:            public void clear() {
0135:                items.clear();
0136:                keys.clear();
0137:            }
0138:
0139:            /**
0140:             * This will get an object from this dictionary.  If the object is a reference then it will
0141:             * dereference it and get it from the document.  If the object is COSNull then
0142:             * null will be returned.
0143:             *
0144:             * @param key The key to the object that we are getting.
0145:             *
0146:             * @return The object that matches the key.
0147:             */
0148:            public COSBase getDictionaryObject(String key) {
0149:                return getDictionaryObject(COSName.getPDFName(key));
0150:            }
0151:
0152:            /**
0153:             * This is a special case of getDictionaryObject that takes multiple keys, it will handle
0154:             * the situation where multiple keys could get the same value, ie if either CS or ColorSpace
0155:             * is used to get the colorspace.
0156:             * This will get an object from this dictionary.  If the object is a reference then it will
0157:             * dereference it and get it from the document.  If the object is COSNull then
0158:             * null will be returned.  
0159:             *
0160:             * @param firstKey The first key to try.
0161:             * @param secondKey The second key to try.
0162:             *
0163:             * @return The object that matches the key.
0164:             */
0165:            public COSBase getDictionaryObject(String firstKey, String secondKey) {
0166:                COSBase retval = getDictionaryObject(COSName
0167:                        .getPDFName(firstKey));
0168:                if (retval == null) {
0169:                    retval = getDictionaryObject(COSName.getPDFName(secondKey));
0170:                }
0171:                return retval;
0172:            }
0173:
0174:            /**
0175:             * This is a special case of getDictionaryObject that takes multiple keys, it will handle
0176:             * the situation where multiple keys could get the same value, ie if either CS or ColorSpace
0177:             * is used to get the colorspace.
0178:             * This will get an object from this dictionary.  If the object is a reference then it will
0179:             * dereference it and get it from the document.  If the object is COSNull then
0180:             * null will be returned.  
0181:             *
0182:             * @param keyList The list of keys to find a value.
0183:             *
0184:             * @return The object that matches the key.
0185:             */
0186:            public COSBase getDictionaryObject(String[] keyList) {
0187:                COSBase retval = null;
0188:                for (int i = 0; i < keyList.length && retval == null; i++) {
0189:                    retval = getDictionaryObject(COSName.getPDFName(keyList[i]));
0190:                }
0191:                return retval;
0192:            }
0193:
0194:            /**
0195:             * This will get an object from this dictionary.  If the object is a reference then it will
0196:             * dereference it and get it from the document.  If the object is COSNull then
0197:             * null will be returned.
0198:             *
0199:             * @param key The key to the object that we are getting.
0200:             *
0201:             * @return The object that matches the key.
0202:             */
0203:            public COSBase getDictionaryObject(COSName key) {
0204:                COSBase retval = (COSBase) items.get(key);
0205:                if (retval instanceof  COSObject) {
0206:                    retval = ((COSObject) retval).getObject();
0207:                }
0208:                if (retval instanceof  COSNull) {
0209:                    retval = null;
0210:                }
0211:                return retval;
0212:            }
0213:
0214:            /**
0215:             * This will set an item in the dictionary.  If value is null then the result
0216:             * will be the same as removeItem( key ).
0217:             *
0218:             * @param key The key to the dictionary object.
0219:             * @param value The value to the dictionary object.
0220:             */
0221:            public void setItem(COSName key, COSBase value) {
0222:                if (value == null) {
0223:                    removeItem(key);
0224:                } else {
0225:                    if (!items.containsKey(key)) {
0226:                        // insert only if not already there
0227:                        keys.add(key);
0228:                    }
0229:                    items.put(key, value);
0230:                }
0231:            }
0232:
0233:            /**
0234:             * This will set an item in the dictionary.  If value is null then the result
0235:             * will be the same as removeItem( key ).
0236:             *
0237:             * @param key The key to the dictionary object.
0238:             * @param value The value to the dictionary object.
0239:             */
0240:            public void setItem(COSName key, COSObjectable value) {
0241:                COSBase base = null;
0242:                if (value != null) {
0243:                    base = value.getCOSObject();
0244:                }
0245:                setItem(key, base);
0246:            }
0247:
0248:            /**
0249:             * This will set an item in the dictionary.  If value is null then the result
0250:             * will be the same as removeItem( key ).
0251:             *
0252:             * @param key The key to the dictionary object.
0253:             * @param value The value to the dictionary object.
0254:             */
0255:            public void setItem(String key, COSObjectable value) {
0256:                setItem(COSName.getPDFName(key), value);
0257:            }
0258:
0259:            /**
0260:             * This will set an item in the dictionary.
0261:             *
0262:             * @param key The key to the dictionary object.
0263:             * @param value The value to the dictionary object.
0264:             */
0265:            public void setBoolean(String key, boolean value) {
0266:                setItem(COSName.getPDFName(key), COSBoolean.getBoolean(value));
0267:            }
0268:
0269:            /**
0270:             * This will set an item in the dictionary.
0271:             *
0272:             * @param key The key to the dictionary object.
0273:             * @param value The value to the dictionary object.
0274:             */
0275:            public void setBoolean(COSName key, boolean value) {
0276:                setItem(key, COSBoolean.getBoolean(value));
0277:            }
0278:
0279:            /**
0280:             * This will set an item in the dictionary.  If value is null then the result
0281:             * will be the same as removeItem( key ).
0282:             *
0283:             * @param key The key to the dictionary object.
0284:             * @param value The value to the dictionary object.
0285:             */
0286:            public void setItem(String key, COSBase value) {
0287:                setItem(COSName.getPDFName(key), value);
0288:            }
0289:
0290:            /**
0291:             * This is a convenience method that will convert the value to a COSName
0292:             * object.  If it is null then the object will be removed.
0293:             *
0294:             * @param key The key to the object,
0295:             * @param value The string value for the name.
0296:             */
0297:            public void setName(String key, String value) {
0298:                setName(COSName.getPDFName(key), value);
0299:            }
0300:
0301:            /**
0302:             * This is a convenience method that will convert the value to a COSName
0303:             * object.  If it is null then the object will be removed.
0304:             *
0305:             * @param key The key to the object,
0306:             * @param value The string value for the name.
0307:             */
0308:            public void setName(COSName key, String value) {
0309:                COSName name = null;
0310:                if (value != null) {
0311:                    name = COSName.getPDFName(value);
0312:                }
0313:                setItem(key, name);
0314:            }
0315:
0316:            /**
0317:             * Set the value of a date entry in the dictionary.
0318:             * 
0319:             * @param key The key to the date value.
0320:             * @param date The date value.
0321:             */
0322:            public void setDate(String key, Calendar date) {
0323:                setDate(COSName.getPDFName(key), date);
0324:            }
0325:
0326:            /**
0327:             * Set the date object.
0328:             * 
0329:             * @param key The key to the date.
0330:             * @param date The date to set.
0331:             */
0332:            public void setDate(COSName key, Calendar date) {
0333:                setString(key, DateConverter.toString(date));
0334:            }
0335:
0336:            /**
0337:             * Set the value of a date entry in the dictionary.
0338:             * 
0339:             * @param embedded The embedded dictionary.
0340:             * @param key The key to the date value.
0341:             * @param date The date value.
0342:             */
0343:            public void setEmbeddedDate(String embedded, String key,
0344:                    Calendar date) {
0345:                setEmbeddedDate(embedded, COSName.getPDFName(key), date);
0346:            }
0347:
0348:            /**
0349:             * Set the date object.
0350:             * 
0351:             * @param embedded The embedded dictionary.
0352:             * @param key The key to the date.
0353:             * @param date The date to set.
0354:             */
0355:            public void setEmbeddedDate(String embedded, COSName key,
0356:                    Calendar date) {
0357:                COSDictionary dic = (COSDictionary) getDictionaryObject(embedded);
0358:                if (dic == null && date != null) {
0359:                    dic = new COSDictionary();
0360:                    setItem(embedded, dic);
0361:                }
0362:                if (dic != null) {
0363:                    dic.setDate(key, date);
0364:                }
0365:            }
0366:
0367:            /**
0368:             * This is a convenience method that will convert the value to a COSString
0369:             * object.  If it is null then the object will be removed.
0370:             *
0371:             * @param key The key to the object,
0372:             * @param value The string value for the name.
0373:             */
0374:            public void setString(String key, String value) {
0375:                setString(COSName.getPDFName(key), value);
0376:            }
0377:
0378:            /**
0379:             * This is a convenience method that will convert the value to a COSString
0380:             * object.  If it is null then the object will be removed.
0381:             *
0382:             * @param key The key to the object,
0383:             * @param value The string value for the name.
0384:             */
0385:            public void setString(COSName key, String value) {
0386:                COSString name = null;
0387:                if (value != null) {
0388:                    name = new COSString(value);
0389:                }
0390:                setItem(key, name);
0391:            }
0392:
0393:            /**
0394:             * This is a convenience method that will convert the value to a COSString
0395:             * object.  If it is null then the object will be removed.
0396:             *
0397:             * @param embedded The embedded dictionary to set the item in.
0398:             * @param key The key to the object,
0399:             * @param value The string value for the name.
0400:             */
0401:            public void setEmbeddedString(String embedded, String key,
0402:                    String value) {
0403:                setEmbeddedString(embedded, COSName.getPDFName(key), value);
0404:            }
0405:
0406:            /**
0407:             * This is a convenience method that will convert the value to a COSString
0408:             * object.  If it is null then the object will be removed.
0409:             *
0410:             * @param embedded The embedded dictionary to set the item in.
0411:             * @param key The key to the object,
0412:             * @param value The string value for the name.
0413:             */
0414:            public void setEmbeddedString(String embedded, COSName key,
0415:                    String value) {
0416:                COSDictionary dic = (COSDictionary) getDictionaryObject(embedded);
0417:                if (dic == null && value != null) {
0418:                    dic = new COSDictionary();
0419:                    setItem(embedded, dic);
0420:                }
0421:                if (dic != null) {
0422:                    dic.setString(key, value);
0423:                }
0424:            }
0425:
0426:            /**
0427:             * This is a convenience method that will convert the value to a COSInteger
0428:             * object.  
0429:             *
0430:             * @param key The key to the object,
0431:             * @param value The int value for the name.
0432:             */
0433:            public void setInt(String key, int value) {
0434:                setInt(COSName.getPDFName(key), value);
0435:            }
0436:
0437:            /**
0438:             * This is a convenience method that will convert the value to a COSInteger
0439:             * object.
0440:             *
0441:             * @param key The key to the object,
0442:             * @param value The int value for the name.
0443:             */
0444:            public void setInt(COSName key, int value) {
0445:                COSInteger intVal = null;
0446:                intVal = new COSInteger(value);
0447:                setItem(key, intVal);
0448:            }
0449:
0450:            /**
0451:             * This is a convenience method that will convert the value to a COSInteger
0452:             * object.  
0453:             *
0454:             * @param key The key to the object,
0455:             * @param value The int value for the name.
0456:             */
0457:            public void setLong(String key, long value) {
0458:                setLong(COSName.getPDFName(key), value);
0459:            }
0460:
0461:            /**
0462:             * This is a convenience method that will convert the value to a COSInteger
0463:             * object.
0464:             *
0465:             * @param key The key to the object,
0466:             * @param value The int value for the name.
0467:             */
0468:            public void setLong(COSName key, long value) {
0469:                COSInteger intVal = null;
0470:                intVal = new COSInteger(value);
0471:                setItem(key, intVal);
0472:            }
0473:
0474:            /**
0475:             * This is a convenience method that will convert the value to a COSInteger
0476:             * object.  
0477:             *
0478:             * @param embeddedDictionary The embedded dictionary.
0479:             * @param key The key to the object,
0480:             * @param value The int value for the name.
0481:             */
0482:            public void setEmbeddedInt(String embeddedDictionary, String key,
0483:                    int value) {
0484:                setEmbeddedInt(embeddedDictionary, COSName.getPDFName(key),
0485:                        value);
0486:            }
0487:
0488:            /**
0489:             * This is a convenience method that will convert the value to a COSInteger
0490:             * object.
0491:             *
0492:             * @param embeddedDictionary The embedded dictionary.
0493:             * @param key The key to the object,
0494:             * @param value The int value for the name.
0495:             */
0496:            public void setEmbeddedInt(String embeddedDictionary, COSName key,
0497:                    int value) {
0498:                COSDictionary embedded = (COSDictionary) getDictionaryObject(embeddedDictionary);
0499:                if (embedded == null) {
0500:                    embedded = new COSDictionary();
0501:                    setItem(embeddedDictionary, embedded);
0502:                }
0503:                embedded.setInt(key, value);
0504:            }
0505:
0506:            /**
0507:             * This is a convenience method that will convert the value to a COSFloat
0508:             * object.  
0509:             *
0510:             * @param key The key to the object,
0511:             * @param value The int value for the name.
0512:             */
0513:            public void setFloat(String key, float value) {
0514:                setFloat(COSName.getPDFName(key), value);
0515:            }
0516:
0517:            /**
0518:             * This is a convenience method that will convert the value to a COSFloat
0519:             * object.
0520:             *
0521:             * @param key The key to the object,
0522:             * @param value The int value for the name.
0523:             */
0524:            public void setFloat(COSName key, float value) {
0525:                COSFloat fltVal = new COSFloat(value);
0526:                setItem(key, fltVal);
0527:            }
0528:
0529:            /**
0530:             * This is a convenience method that will get the dictionary object that
0531:             * is expected to be a name and convert it to a string.  Null is returned
0532:             * if the entry does not exist in the dictionary.
0533:             *
0534:             * @param key The key to the item in the dictionary.
0535:             * @return The name converted to a string.
0536:             */
0537:            public String getNameAsString(String key) {
0538:                return getNameAsString(COSName.getPDFName(key));
0539:            }
0540:
0541:            /**
0542:             * This is a convenience method that will get the dictionary object that
0543:             * is expected to be a name and convert it to a string.  Null is returned
0544:             * if the entry does not exist in the dictionary.
0545:             *
0546:             * @param key The key to the item in the dictionary.
0547:             * @return The name converted to a string.
0548:             */
0549:            public String getNameAsString(COSName key) {
0550:                String retval = null;
0551:                COSName name = (COSName) getDictionaryObject(key);
0552:                if (name != null) {
0553:                    retval = name.getName();
0554:                }
0555:                return retval;
0556:            }
0557:
0558:            /**
0559:             * This is a convenience method that will get the dictionary object that
0560:             * is expected to be a name and convert it to a string.  Null is returned
0561:             * if the entry does not exist in the dictionary.
0562:             *
0563:             * @param key The key to the item in the dictionary.
0564:             * @param defaultValue The value to return if the dictionary item is null.
0565:             * @return The name converted to a string.
0566:             */
0567:            public String getNameAsString(String key, String defaultValue) {
0568:                return getNameAsString(COSName.getPDFName(key), defaultValue);
0569:            }
0570:
0571:            /**
0572:             * This is a convenience method that will get the dictionary object that
0573:             * is expected to be a name and convert it to a string.  Null is returned
0574:             * if the entry does not exist in the dictionary.
0575:             *
0576:             * @param key The key to the item in the dictionary.
0577:             * @param defaultValue The value to return if the dictionary item is null.
0578:             * @return The name converted to a string.
0579:             */
0580:            public String getNameAsString(COSName key, String defaultValue) {
0581:                String retval = getNameAsString(key);
0582:                if (retval == null) {
0583:                    retval = defaultValue;
0584:                }
0585:                return retval;
0586:            }
0587:
0588:            /**
0589:             * This is a convenience method that will get the dictionary object that
0590:             * is expected to be a name and convert it to a string.  Null is returned
0591:             * if the entry does not exist in the dictionary.
0592:             *
0593:             * @param key The key to the item in the dictionary.
0594:             * @return The name converted to a string.
0595:             */
0596:            public String getString(String key) {
0597:                return getString(COSName.getPDFName(key));
0598:            }
0599:
0600:            /**
0601:             * This is a convenience method that will get the dictionary object that
0602:             * is expected to be a name and convert it to a string.  Null is returned
0603:             * if the entry does not exist in the dictionary.
0604:             *
0605:             * @param key The key to the item in the dictionary.
0606:             * @return The name converted to a string.
0607:             */
0608:            public String getString(COSName key) {
0609:                String retval = null;
0610:                COSString name = (COSString) getDictionaryObject(key);
0611:                if (name != null) {
0612:                    retval = name.getString();
0613:                }
0614:                return retval;
0615:            }
0616:
0617:            /**
0618:             * This is a convenience method that will get the dictionary object that
0619:             * is expected to be a name and convert it to a string.  Null is returned
0620:             * if the entry does not exist in the dictionary.
0621:             *
0622:             * @param key The key to the item in the dictionary.
0623:             * @param defaultValue The default value to return.
0624:             * @return The name converted to a string.
0625:             */
0626:            public String getString(String key, String defaultValue) {
0627:                return getString(COSName.getPDFName(key), defaultValue);
0628:            }
0629:
0630:            /**
0631:             * This is a convenience method that will get the dictionary object that
0632:             * is expected to be a name and convert it to a string.  Null is returned
0633:             * if the entry does not exist in the dictionary.
0634:             *
0635:             * @param key The key to the item in the dictionary.
0636:             * @param defaultValue The default value to return.
0637:             * @return The name converted to a string.
0638:             */
0639:            public String getString(COSName key, String defaultValue) {
0640:                String retval = getString(key);
0641:                if (retval == null) {
0642:                    retval = defaultValue;
0643:                }
0644:                return retval;
0645:            }
0646:
0647:            /**
0648:             * This is a convenience method that will get the dictionary object that
0649:             * is expected to be a name and convert it to a string.  Null is returned
0650:             * if the entry does not exist in the dictionary.
0651:             *
0652:             * @param embedded The embedded dictionary.
0653:             * @param key The key to the item in the dictionary.
0654:             * @return The name converted to a string.
0655:             */
0656:            public String getEmbeddedString(String embedded, String key) {
0657:                return getEmbeddedString(embedded, COSName.getPDFName(key),
0658:                        null);
0659:            }
0660:
0661:            /**
0662:             * This is a convenience method that will get the dictionary object that
0663:             * is expected to be a name and convert it to a string.  Null is returned
0664:             * if the entry does not exist in the dictionary.
0665:             *
0666:             * @param embedded The embedded dictionary.
0667:             * @param key The key to the item in the dictionary.
0668:             * @return The name converted to a string.
0669:             */
0670:            public String getEmbeddedString(String embedded, COSName key) {
0671:                return getEmbeddedString(embedded, key, null);
0672:            }
0673:
0674:            /**
0675:             * This is a convenience method that will get the dictionary object that
0676:             * is expected to be a name and convert it to a string.  Null is returned
0677:             * if the entry does not exist in the dictionary.
0678:             *
0679:             * @param embedded The embedded dictionary.
0680:             * @param key The key to the item in the dictionary.
0681:             * @param defaultValue The default value to return.
0682:             * @return The name converted to a string.
0683:             */
0684:            public String getEmbeddedString(String embedded, String key,
0685:                    String defaultValue) {
0686:                return getEmbeddedString(embedded, COSName.getPDFName(key),
0687:                        defaultValue);
0688:            }
0689:
0690:            /**
0691:             * This is a convenience method that will get the dictionary object that
0692:             * is expected to be a name and convert it to a string.  Null is returned
0693:             * if the entry does not exist in the dictionary.
0694:             *
0695:             * @param embedded The embedded dictionary.
0696:             * @param key The key to the item in the dictionary.
0697:             * @param defaultValue The default value to return.
0698:             * @return The name converted to a string.
0699:             */
0700:            public String getEmbeddedString(String embedded, COSName key,
0701:                    String defaultValue) {
0702:                String retval = defaultValue;
0703:                COSDictionary dic = (COSDictionary) getDictionaryObject(embedded);
0704:                if (dic != null) {
0705:                    retval = dic.getString(key, defaultValue);
0706:                }
0707:                return retval;
0708:            }
0709:
0710:            /**
0711:             * This is a convenience method that will get the dictionary object that
0712:             * is expected to be a name and convert it to a string.  Null is returned
0713:             * if the entry does not exist in the dictionary.
0714:             *
0715:             * @param key The key to the item in the dictionary.
0716:             * @return The name converted to a string.
0717:             * @throws IOException If there is an error converting to a date.
0718:             */
0719:            public Calendar getDate(String key) throws IOException {
0720:                return getDate(COSName.getPDFName(key));
0721:            }
0722:
0723:            /**
0724:             * This is a convenience method that will get the dictionary object that
0725:             * is expected to be a name and convert it to a string.  Null is returned
0726:             * if the entry does not exist in the dictionary.
0727:             *
0728:             * @param key The key to the item in the dictionary.
0729:             * @return The name converted to a string.
0730:             * 
0731:             * @throws IOException If there is an error converting to a date.
0732:             */
0733:            public Calendar getDate(COSName key) throws IOException {
0734:                COSString date = (COSString) getDictionaryObject(key);
0735:                return DateConverter.toCalendar(date);
0736:            }
0737:
0738:            /**
0739:             * This is a convenience method that will get the dictionary object that
0740:             * is expected to be a date.  Null is returned
0741:             * if the entry does not exist in the dictionary.
0742:             *
0743:             * @param key The key to the item in the dictionary.
0744:             * @param defaultValue The default value to return.
0745:             * @return The name converted to a string.
0746:             * @throws IOException If there is an error converting to a date.
0747:             */
0748:            public Calendar getDate(String key, Calendar defaultValue)
0749:                    throws IOException {
0750:                return getDate(COSName.getPDFName(key), defaultValue);
0751:            }
0752:
0753:            /**
0754:             * This is a convenience method that will get the dictionary object that
0755:             * is expected to be a date.  Null is returned
0756:             * if the entry does not exist in the dictionary.
0757:             *
0758:             * @param key The key to the item in the dictionary.
0759:             * @param defaultValue The default value to return.
0760:             * @return The name converted to a string.
0761:             * @throws IOException If there is an error converting to a date.
0762:             */
0763:            public Calendar getDate(COSName key, Calendar defaultValue)
0764:                    throws IOException {
0765:                Calendar retval = getDate(key);
0766:                if (retval == null) {
0767:                    retval = defaultValue;
0768:                }
0769:                return retval;
0770:            }
0771:
0772:            /**
0773:             * This is a convenience method that will get the dictionary object that
0774:             * is expected to be a name and convert it to a string.  Null is returned
0775:             * if the entry does not exist in the dictionary.
0776:             *
0777:             * @param embedded The embedded dictionary to get.
0778:             * @param key The key to the item in the dictionary.
0779:             * @return The name converted to a string.
0780:             * @throws IOException If there is an error converting to a date.
0781:             */
0782:            public Calendar getEmbeddedDate(String embedded, String key)
0783:                    throws IOException {
0784:                return getEmbeddedDate(embedded, COSName.getPDFName(key), null);
0785:            }
0786:
0787:            /**
0788:             * This is a convenience method that will get the dictionary object that
0789:             * is expected to be a name and convert it to a string.  Null is returned
0790:             * if the entry does not exist in the dictionary.
0791:             *
0792:             * @param embedded The embedded dictionary to get.
0793:             * @param key The key to the item in the dictionary.
0794:             * @return The name converted to a string.
0795:             * 
0796:             * @throws IOException If there is an error converting to a date.
0797:             */
0798:            public Calendar getEmbeddedDate(String embedded, COSName key)
0799:                    throws IOException {
0800:                return getEmbeddedDate(embedded, key, null);
0801:            }
0802:
0803:            /**
0804:             * This is a convenience method that will get the dictionary object that
0805:             * is expected to be a date.  Null is returned
0806:             * if the entry does not exist in the dictionary.
0807:             *
0808:             * @param embedded The embedded dictionary to get.
0809:             * @param key The key to the item in the dictionary.
0810:             * @param defaultValue The default value to return.
0811:             * @return The name converted to a string.
0812:             * @throws IOException If there is an error converting to a date.
0813:             */
0814:            public Calendar getEmbeddedDate(String embedded, String key,
0815:                    Calendar defaultValue) throws IOException {
0816:                return getEmbeddedDate(embedded, COSName.getPDFName(key),
0817:                        defaultValue);
0818:            }
0819:
0820:            /**
0821:             * This is a convenience method that will get the dictionary object that
0822:             * is expected to be a date.  Null is returned
0823:             * if the entry does not exist in the dictionary.
0824:             *
0825:             * @param embedded The embedded dictionary to get.
0826:             * @param key The key to the item in the dictionary.
0827:             * @param defaultValue The default value to return.
0828:             * @return The name converted to a string.
0829:             * @throws IOException If there is an error converting to a date.
0830:             */
0831:            public Calendar getEmbeddedDate(String embedded, COSName key,
0832:                    Calendar defaultValue) throws IOException {
0833:                Calendar retval = defaultValue;
0834:                COSDictionary eDic = (COSDictionary) getDictionaryObject(embedded);
0835:                if (eDic != null) {
0836:                    retval = eDic.getDate(key, defaultValue);
0837:                }
0838:                return retval;
0839:            }
0840:
0841:            /**
0842:             * This is a convenience method that will get the dictionary object that
0843:             * is expected to be a cos boolean and convert it to a primitive boolean.
0844:             *
0845:             * @param key The key to the item in the dictionary.
0846:             * @param defaultValue The value returned if the entry is null.
0847:             *
0848:             * @return The value converted to a boolean.
0849:             */
0850:            public boolean getBoolean(String key, boolean defaultValue) {
0851:                return getBoolean(COSName.getPDFName(key), defaultValue);
0852:            }
0853:
0854:            /**
0855:             * This is a convenience method that will get the dictionary object that
0856:             * is expected to be a COSBoolean and convert it to a primitive boolean.
0857:             *
0858:             * @param key The key to the item in the dictionary.
0859:             * @param defaultValue The value returned if the entry is null.
0860:             *
0861:             * @return The entry converted to a boolean.
0862:             */
0863:            public boolean getBoolean(COSName key, boolean defaultValue) {
0864:                boolean retval = defaultValue;
0865:                COSBoolean bool = (COSBoolean) getDictionaryObject(key);
0866:                if (bool != null) {
0867:                    retval = bool.getValue();
0868:                }
0869:                return retval;
0870:            }
0871:
0872:            /**
0873:             * Get an integer from an embedded dictionary.  Useful for 1-1 mappings.  default:-1
0874:             * 
0875:             * @param embeddedDictionary The name of the embedded dictionary.
0876:             * @param key The key in the embedded dictionary.
0877:             * 
0878:             * @return The value of the embedded integer.
0879:             */
0880:            public int getEmbeddedInt(String embeddedDictionary, String key) {
0881:                return getEmbeddedInt(embeddedDictionary, COSName
0882:                        .getPDFName(key));
0883:            }
0884:
0885:            /**
0886:             * Get an integer from an embedded dictionary.  Useful for 1-1 mappings.  default:-1
0887:             * 
0888:             * @param embeddedDictionary The name of the embedded dictionary.
0889:             * @param key The key in the embedded dictionary.
0890:             * 
0891:             * @return The value of the embedded integer.
0892:             */
0893:            public int getEmbeddedInt(String embeddedDictionary, COSName key) {
0894:                return getEmbeddedInt(embeddedDictionary, key, -1);
0895:            }
0896:
0897:            /**
0898:             * Get an integer from an embedded dictionary.  Useful for 1-1 mappings.
0899:             * 
0900:             * @param embeddedDictionary The name of the embedded dictionary.
0901:             * @param key The key in the embedded dictionary.
0902:             * @param defaultValue The value if there is no embedded dictionary or it does not contain the key.
0903:             * 
0904:             * @return The value of the embedded integer.
0905:             */
0906:            public int getEmbeddedInt(String embeddedDictionary, String key,
0907:                    int defaultValue) {
0908:                return getEmbeddedInt(embeddedDictionary, COSName
0909:                        .getPDFName(key), defaultValue);
0910:            }
0911:
0912:            /**
0913:             * Get an integer from an embedded dictionary.  Useful for 1-1 mappings.
0914:             * 
0915:             * @param embeddedDictionary The name of the embedded dictionary.
0916:             * @param key The key in the embedded dictionary.
0917:             * @param defaultValue The value if there is no embedded dictionary or it does not contain the key.
0918:             * 
0919:             * @return The value of the embedded integer.
0920:             */
0921:            public int getEmbeddedInt(String embeddedDictionary, COSName key,
0922:                    int defaultValue) {
0923:                int retval = defaultValue;
0924:                COSDictionary embedded = (COSDictionary) getDictionaryObject(embeddedDictionary);
0925:                if (embedded != null) {
0926:                    retval = embedded.getInt(key, defaultValue);
0927:                }
0928:                return retval;
0929:            }
0930:
0931:            /**
0932:             * This is a convenience method that will get the dictionary object that
0933:             * is expected to be an int.  -1 is returned if there is no value.
0934:             *
0935:             * @param key The key to the item in the dictionary.
0936:             * @return The integer value.
0937:             */
0938:            public int getInt(String key) {
0939:                return getInt(COSName.getPDFName(key));
0940:            }
0941:
0942:            /**
0943:             * This is a convenience method that will get the dictionary object that
0944:             * is expected to be an int.  -1 is returned if there is no value.
0945:             *
0946:             * @param key The key to the item in the dictionary.
0947:             * @return The integer value..
0948:             */
0949:            public int getInt(COSName key) {
0950:                return getInt(key, -1);
0951:            }
0952:
0953:            /**
0954:             * This is a convenience method that will get the dictionary object that
0955:             * is expected to be an integer.  If the dictionary value is null then the
0956:             * default Value will be returned.
0957:             *
0958:             * @param keyList The key to the item in the dictionary.
0959:             * @param defaultValue The value to return if the dictionary item is null.
0960:             * @return The integer value.
0961:             */
0962:            public int getInt(String[] keyList, int defaultValue) {
0963:                int retval = defaultValue;
0964:                COSNumber obj = (COSNumber) getDictionaryObject(keyList);
0965:                if (obj != null) {
0966:                    retval = obj.intValue();
0967:                }
0968:                return retval;
0969:            }
0970:
0971:            /**
0972:             * This is a convenience method that will get the dictionary object that
0973:             * is expected to be an integer.  If the dictionary value is null then the
0974:             * default Value will be returned.
0975:             *
0976:             * @param key The key to the item in the dictionary.
0977:             * @param defaultValue The value to return if the dictionary item is null.
0978:             * @return The integer value.
0979:             */
0980:            public int getInt(String key, int defaultValue) {
0981:                return getInt(new String[] { key }, defaultValue);
0982:            }
0983:
0984:            /**
0985:             * This is a convenience method that will get the dictionary object that
0986:             * is expected to be an integer.  If the dictionary value is null then the
0987:             * default Value will be returned.
0988:             *
0989:             * @param key The key to the item in the dictionary.
0990:             * @param defaultValue The value to return if the dictionary item is null.
0991:             * @return The integer value.
0992:             */
0993:            public int getInt(COSName key, int defaultValue) {
0994:                return getInt(key.getName(), defaultValue);
0995:            }
0996:
0997:            /**
0998:             * This is a convenience method that will get the dictionary object that
0999:             * is expected to be an long.  -1 is returned if there is no value.
1000:             *
1001:             * @param key The key to the item in the dictionary.
1002:             * 
1003:             * @return The long value.
1004:             */
1005:            public long getLong(String key) {
1006:                return getLong(COSName.getPDFName(key));
1007:            }
1008:
1009:            /**
1010:             * This is a convenience method that will get the dictionary object that
1011:             * is expected to be an long.  -1 is returned if there is no value.
1012:             *
1013:             * @param key The key to the item in the dictionary.
1014:             * @return The long value.
1015:             */
1016:            public long getLong(COSName key) {
1017:                return getLong(key, -1L);
1018:            }
1019:
1020:            /**
1021:             * This is a convenience method that will get the dictionary object that
1022:             * is expected to be an long.  If the dictionary value is null then the
1023:             * default Value will be returned.
1024:             *
1025:             * @param keyList The key to the item in the dictionary.
1026:             * @param defaultValue The value to return if the dictionary item is null.
1027:             * @return The long value.
1028:             */
1029:            public long getLong(String[] keyList, long defaultValue) {
1030:                long retval = defaultValue;
1031:                COSNumber obj = (COSNumber) getDictionaryObject(keyList);
1032:                if (obj != null) {
1033:                    retval = obj.longValue();
1034:                }
1035:                return retval;
1036:            }
1037:
1038:            /**
1039:             * This is a convenience method that will get the dictionary object that
1040:             * is expected to be an integer.  If the dictionary value is null then the
1041:             * default Value will be returned.
1042:             *
1043:             * @param key The key to the item in the dictionary.
1044:             * @param defaultValue The value to return if the dictionary item is null.
1045:             * @return The integer value.
1046:             */
1047:            public long getLong(String key, long defaultValue) {
1048:                return getLong(new String[] { key }, defaultValue);
1049:            }
1050:
1051:            /**
1052:             * This is a convenience method that will get the dictionary object that
1053:             * is expected to be an integer.  If the dictionary value is null then the
1054:             * default Value will be returned.
1055:             *
1056:             * @param key The key to the item in the dictionary.
1057:             * @param defaultValue The value to return if the dictionary item is null.
1058:             * @return The integer value.
1059:             */
1060:            public long getLong(COSName key, long defaultValue) {
1061:                return getLong(key.getName(), defaultValue);
1062:            }
1063:
1064:            /**
1065:             * This is a convenience method that will get the dictionary object that
1066:             * is expected to be an int.  -1 is returned if there is no value.
1067:             *
1068:             * @param key The key to the item in the dictionary.
1069:             * @return The float value.
1070:             */
1071:            public float getFloat(String key) {
1072:                return getFloat(COSName.getPDFName(key));
1073:            }
1074:
1075:            /**
1076:             * This is a convenience method that will get the dictionary object that
1077:             * is expected to be an float.  -1 is returned if there is no value.
1078:             *
1079:             * @param key The key to the item in the dictionary.
1080:             * @return The float value.
1081:             */
1082:            public float getFloat(COSName key) {
1083:                return getFloat(key, -1);
1084:            }
1085:
1086:            /**
1087:             * This is a convenience method that will get the dictionary object that
1088:             * is expected to be a float.  If the dictionary value is null then the
1089:             * default Value will be returned.
1090:             *
1091:             * @param key The key to the item in the dictionary.
1092:             * @param defaultValue The value to return if the dictionary item is null.
1093:             * @return The float value.
1094:             */
1095:            public float getFloat(String key, float defaultValue) {
1096:                return getFloat(COSName.getPDFName(key), defaultValue);
1097:            }
1098:
1099:            /**
1100:             * This is a convenience method that will get the dictionary object that
1101:             * is expected to be an float.  If the dictionary value is null then the
1102:             * default Value will be returned.
1103:             *
1104:             * @param key The key to the item in the dictionary.
1105:             * @param defaultValue The value to return if the dictionary item is null.
1106:             * @return The float value.
1107:             */
1108:            public float getFloat(COSName key, float defaultValue) {
1109:                float retval = defaultValue;
1110:                COSNumber obj = (COSNumber) getDictionaryObject(key);
1111:                if (obj != null) {
1112:                    retval = obj.floatValue();
1113:                }
1114:                return retval;
1115:            }
1116:
1117:            /**
1118:             * This will remove an item for the dictionary.  This
1119:             * will do nothing of the object does not exist.
1120:             *
1121:             * @param key The key to the item to remove from the dictionary.
1122:             */
1123:            public void removeItem(COSName key) {
1124:                keys.remove(key);
1125:                items.remove(key);
1126:            }
1127:
1128:            /**
1129:             * This will do a lookup into the dictionary.
1130:             *
1131:             * @param key The key to the object.
1132:             *
1133:             * @return The item that matches the key.
1134:             */
1135:            public COSBase getItem(COSName key) {
1136:                return (COSBase) items.get(key);
1137:            }
1138:
1139:            /**
1140:             * This will get the keys for all objects in the dictionary in the sequence that
1141:             * they were added.
1142:             *
1143:             * @return a list of the keys in the sequence of insertion
1144:             *
1145:             */
1146:            public List keyList() {
1147:                return keys;
1148:            }
1149:
1150:            /**
1151:             * This will get all of the values for the dictionary.
1152:             *
1153:             * @return All the values for the dictionary.
1154:             */
1155:            public Collection getValues() {
1156:                return items.values();
1157:            }
1158:
1159:            /**
1160:             * visitor pattern double dispatch method.
1161:             *
1162:             * @param visitor The object to notify when visiting this object.
1163:             * @return The object that the visitor returns.
1164:             *
1165:             * @throws COSVisitorException If there is an error visiting this object.
1166:             */
1167:            public Object accept(ICOSVisitor visitor)
1168:                    throws COSVisitorException {
1169:                return visitor.visitFromDictionary(this );
1170:            }
1171:
1172:            /**
1173:             * This will add all of the dictionarys keys/values to this dictionary.
1174:             *
1175:             * @param dic The dic to get the keys from.
1176:             */
1177:            public void addAll(COSDictionary dic) {
1178:                Iterator dicKeys = dic.keyList().iterator();
1179:                while (dicKeys.hasNext()) {
1180:                    COSName key = (COSName) dicKeys.next();
1181:                    COSBase value = dic.getItem(key);
1182:                    setItem(key, value);
1183:                }
1184:            }
1185:
1186:            /**
1187:             * This will add all of the dictionarys keys/values to this dictionary, but only
1188:             * if they don't already exist.  If a key already exists in this dictionary then 
1189:             * nothing is changed.
1190:             *
1191:             * @param dic The dic to get the keys from.
1192:             */
1193:            public void mergeInto(COSDictionary dic) {
1194:                Iterator dicKeys = dic.keyList().iterator();
1195:                while (dicKeys.hasNext()) {
1196:                    COSName key = (COSName) dicKeys.next();
1197:                    COSBase value = dic.getItem(key);
1198:                    if (getItem(key) == null) {
1199:                        setItem(key, value);
1200:                    }
1201:                }
1202:            }
1203:
1204:            /**
1205:             * Nice method, gives you every object you want
1206:             * Arrays works properly too. Try "P/Annots/[k]/Rect"
1207:             * where k means the index of the Annotsarray.
1208:             *
1209:             * @param objPath the relative path to the object.
1210:             * @return the object
1211:             */
1212:            public COSBase getObjectFromPath(String objPath) {
1213:                COSBase retval = null;
1214:                String[] path = objPath.split(PATH_SEPARATOR);
1215:                retval = this ;
1216:
1217:                for (int i = 0; i < path.length; i++) {
1218:                    if (retval instanceof  COSArray) {
1219:                        int idx = new Integer(path[i].replaceAll("\\[", "")
1220:                                .replaceAll("\\]", "")).intValue();
1221:                        retval = ((COSArray) retval).getObject(idx);
1222:                    } else if (retval instanceof  COSDictionary) {
1223:                        retval = ((COSDictionary) retval)
1224:                                .getDictionaryObject(path[i]);
1225:                    }
1226:                }
1227:                return retval;
1228:            }
1229:
1230:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.