]> _ Git - cubeextranet.git/blob
f11953a36666572f1fc6a2d70656c20a00e328d8
[cubeextranet.git] /
1 /*\r
2  * Licensed to the Apache Software Foundation (ASF) under one or more\r
3  * contributor license agreements.  See the NOTICE file distributed with\r
4  * this work for additional information regarding copyright ownership.\r
5  * The ASF licenses this file to You under the Apache License, Version 2.0\r
6  * (the "License"); you may not use this file except in compliance with\r
7  * the License.  You may obtain a copy of the License at\r
8  *\r
9  *      http://www.apache.org/licenses/LICENSE-2.0\r
10  *\r
11  * Unless required by applicable law or agreed to in writing, software\r
12  * distributed under the License is distributed on an "AS IS" BASIS,\r
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
14  * See the License for the specific language governing permissions and\r
15  * limitations under the License.\r
16  */\r
17 package org.apache.pdfbox.pdmodel.documentinterchange.logicalstructure;\r
18 \r
19 import java.util.ArrayList;\r
20 import java.util.Iterator;\r
21 import java.util.List;\r
22 import java.util.Map.Entry;\r
23 \r
24 import org.apache.pdfbox.cos.COSBase;\r
25 import org.apache.pdfbox.cos.COSDictionary;\r
26 import org.apache.pdfbox.cos.COSName;\r
27 \r
28 /**\r
29  * A default attribute object.\r
30  * \r
31  * @author <a href="mailto:Johannes%20Koch%20%3Ckoch@apache.org%3E">Johannes Koch</a>\r
32  * @version $Revision: $\r
33  */\r
34 public class PDDefaultAttributeObject extends PDAttributeObject\r
35 {\r
36 \r
37     /**\r
38      * Default constructor.\r
39      */\r
40     public PDDefaultAttributeObject()\r
41     {\r
42     }\r
43 \r
44     /**\r
45      * Creates a default attribute object with a given dictionary.\r
46      * \r
47      * @param dictionary the dictionary\r
48      */\r
49     public PDDefaultAttributeObject(COSDictionary dictionary)\r
50     {\r
51         super(dictionary);\r
52     }\r
53 \r
54 \r
55     /**\r
56      * Gets the attribute names.\r
57      * \r
58      * @return the attribute names\r
59      */\r
60     public List<String> getAttributeNames()\r
61     {\r
62         List<String> attrNames = new ArrayList<String>();\r
63         for (Entry<COSName, COSBase> entry : this.getCOSDictionary().entrySet())\r
64         {\r
65             COSName key = entry.getKey();\r
66             if (!COSName.O.equals(key))\r
67             {\r
68                 attrNames.add(key.getName());\r
69             }\r
70         }\r
71         return attrNames;\r
72     }\r
73 \r
74     /**\r
75      * Gets the attribute value for a given name.\r
76      * \r
77      * @param attrName the given attribute name\r
78      * @return the attribute value for a given name\r
79      */\r
80     public COSBase getAttributeValue(String attrName)\r
81     {\r
82         return this.getCOSDictionary().getDictionaryObject(attrName);\r
83     }\r
84 \r
85     /**\r
86      * Gets the attribute value for a given name.\r
87      * \r
88      * @param attrName the given attribute name\r
89      * @param defaultValue the default value\r
90      * @return the attribute value for a given name\r
91      */\r
92     protected COSBase getAttributeValue(String attrName, COSBase defaultValue)\r
93     {\r
94         COSBase value = this.getCOSDictionary().getDictionaryObject(attrName);\r
95         if (value == null)\r
96         {\r
97             return defaultValue;\r
98         }\r
99         return value;\r
100     }\r
101 \r
102     /**\r
103      * Sets an attribute.\r
104      * \r
105      * @param attrName the attribute name\r
106      * @param attrValue the attribute value\r
107      */\r
108     public void setAttribute(String attrName, COSBase attrValue)\r
109     {\r
110         COSBase old = this.getAttributeValue(attrName);\r
111         this.getCOSDictionary().setItem(COSName.getPDFName(attrName), attrValue);\r
112         this.potentiallyNotifyChanged(old, attrValue);\r
113     }\r
114 \r
115     @Override\r
116     public String toString()\r
117     {\r
118         StringBuilder sb = new StringBuilder().append(super.toString())\r
119             .append(", attributes={");\r
120         Iterator<String> it = this.getAttributeNames().iterator();\r
121         while (it.hasNext())\r
122         {\r
123             String name = it.next();\r
124             sb.append(name).append('=').append(this.getAttributeValue(name));\r
125             if (it.hasNext())\r
126             {\r
127                 sb.append(", ");\r
128             }\r
129         }\r
130         return sb.append('}').toString();\r
131     }\r
132 \r
133 }\r