]> _ Git - cubeextranet.git/blob
afb86a2e03cadc3f12d6f2bcaf21c9d141088067
[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 org.apache.pdfbox.cos.COSBase;\r
20 import org.apache.pdfbox.cos.COSDictionary;\r
21 import org.apache.pdfbox.cos.COSName;\r
22 import org.apache.pdfbox.pdmodel.common.PDDictionaryWrapper;\r
23 import org.apache.pdfbox.pdmodel.documentinterchange.taggedpdf.PDExportFormatAttributeObject;\r
24 import org.apache.pdfbox.pdmodel.documentinterchange.taggedpdf.PDLayoutAttributeObject;\r
25 import org.apache.pdfbox.pdmodel.documentinterchange.taggedpdf.PDListAttributeObject;\r
26 import org.apache.pdfbox.pdmodel.documentinterchange.taggedpdf.PDPrintFieldAttributeObject;\r
27 import org.apache.pdfbox.pdmodel.documentinterchange.taggedpdf.PDTableAttributeObject;\r
28 \r
29 /**\r
30  * An attribute object.\r
31  *\r
32  * @author <a href="mailto:Johannes%20Koch%20%3Ckoch@apache.org%3E">Johannes Koch</a>\r
33  * @version $Revision: $\r
34  *\r
35  */\r
36 public abstract class PDAttributeObject extends PDDictionaryWrapper\r
37 {\r
38 \r
39     /**\r
40      * Creates an attribute object.\r
41      * \r
42      * @param dictionary the dictionary\r
43      * @return the attribute object\r
44      */\r
45     public static PDAttributeObject create(COSDictionary dictionary)\r
46     {\r
47         String owner = dictionary.getNameAsString(COSName.O);\r
48         if (PDUserAttributeObject.OWNER_USER_PROPERTIES.equals(owner))\r
49         {\r
50             return new PDUserAttributeObject(dictionary);\r
51         }\r
52         else if (PDListAttributeObject.OWNER_LIST.equals(owner))\r
53         {\r
54             return new PDListAttributeObject(dictionary);\r
55         }\r
56         else if (PDPrintFieldAttributeObject.OWNER_PRINT_FIELD.equals(owner))\r
57         {\r
58             return new PDPrintFieldAttributeObject(dictionary);\r
59         }\r
60         else if (PDTableAttributeObject.OWNER_TABLE.equals(owner))\r
61         {\r
62             return new PDTableAttributeObject(dictionary);\r
63         }\r
64         else if (PDLayoutAttributeObject.OWNER_LAYOUT.equals(owner))\r
65         {\r
66             return new PDLayoutAttributeObject(dictionary);\r
67         }\r
68         else if (PDExportFormatAttributeObject.OWNER_XML_1_00.equals(owner)\r
69             || PDExportFormatAttributeObject.OWNER_HTML_3_20.equals(owner)\r
70             || PDExportFormatAttributeObject.OWNER_HTML_4_01.equals(owner)\r
71             || PDExportFormatAttributeObject.OWNER_OEB_1_00.equals(owner)\r
72             || PDExportFormatAttributeObject.OWNER_RTF_1_05.equals(owner)\r
73             || PDExportFormatAttributeObject.OWNER_CSS_1_00.equals(owner)\r
74             || PDExportFormatAttributeObject.OWNER_CSS_2_00.equals(owner))\r
75         {\r
76             return new PDExportFormatAttributeObject(dictionary);\r
77         }\r
78         return new PDDefaultAttributeObject(dictionary);\r
79     }\r
80 \r
81     private PDStructureElement structureElement;\r
82 \r
83     /**\r
84      * Gets the structure element.\r
85      * \r
86      * @return the structure element\r
87      */\r
88     private PDStructureElement getStructureElement()\r
89     {\r
90         return this.structureElement;\r
91     }\r
92 \r
93     /**\r
94      * Sets the structure element.\r
95      * \r
96      * @param structureElement the structure element\r
97      */\r
98     protected void setStructureElement(PDStructureElement structureElement)\r
99     {\r
100         this.structureElement = structureElement;\r
101     }\r
102 \r
103 \r
104     /**\r
105      * Default constructor.\r
106      */\r
107     public PDAttributeObject()\r
108     {\r
109     }\r
110 \r
111     /**\r
112      * Creates a new attribute object with a given dictionary.\r
113      * \r
114      * @param dictionary the dictionary\r
115      */\r
116     public PDAttributeObject(COSDictionary dictionary)\r
117     {\r
118         super(dictionary);\r
119     }\r
120 \r
121 \r
122     /**\r
123      * Returns the owner of the attributes.\r
124      * \r
125      * @return the owner of the attributes\r
126      */\r
127     public String getOwner()\r
128     {\r
129         return this.getCOSDictionary().getNameAsString(COSName.O);\r
130     }\r
131 \r
132     /**\r
133      * Sets the owner of the attributes.\r
134      * \r
135      * @param owner the owner of the attributes\r
136      */\r
137     protected void setOwner(String owner)\r
138     {\r
139         this.getCOSDictionary().setName(COSName.O, owner);\r
140     }\r
141 \r
142     /**\r
143      * Detects whether there are no properties in the attribute object.\r
144      * \r
145      * @return <code>true</code> if the attribute object is empty,\r
146      *  <code>false</code> otherwise\r
147      */\r
148     public boolean isEmpty()\r
149     {\r
150         // only entry is the owner?\r
151         return (this.getCOSDictionary().size() == 1) && (this.getOwner() != null);\r
152     }\r
153 \r
154 \r
155     /**\r
156      * Notifies the attribute object change listeners if the attribute is changed.\r
157      * \r
158      * @param oldBase old value\r
159      * @param newBase new value\r
160      */\r
161     protected void potentiallyNotifyChanged(COSBase oldBase, COSBase newBase)\r
162     {\r
163         if (this.isValueChanged(oldBase, newBase))\r
164         {\r
165             this.notifyChanged();\r
166         }\r
167     }\r
168 \r
169     /**\r
170      * Is the value changed?\r
171      * \r
172      * @param oldValue old value\r
173      * @param newValue new value\r
174      * @return <code>true</code> if the value is changed, <code>false</code>\r
175      * otherwise\r
176      */\r
177     private boolean isValueChanged(COSBase oldValue, COSBase newValue)\r
178     {\r
179         if (oldValue == null)\r
180         {\r
181             if (newValue == null)\r
182             {\r
183                 return false;\r
184             }\r
185             return true;\r
186         }\r
187         return !oldValue.equals(newValue);\r
188     }\r
189 \r
190     /**\r
191      * Notifies the attribute object change listeners about a change in this\r
192      * attribute object.\r
193      */\r
194     protected void notifyChanged()\r
195     {\r
196         if (this.getStructureElement() != null)\r
197         {\r
198             this.getStructureElement().attributeChanged(this);\r
199         }\r
200     }\r
201 \r
202     @Override\r
203     public String toString()\r
204     {\r
205         return new StringBuilder("O=").append(this.getOwner()).toString();\r
206     }\r
207 \r
208     /**\r
209      * Creates a String representation of an Object array.\r
210      * \r
211      * @param array the Object array\r
212      * @return the String representation\r
213      */\r
214     protected static String arrayToString(Object[] array)\r
215     {\r
216         StringBuilder sb = new StringBuilder("[");\r
217         for (int i = 0; i < array.length; i++)\r
218         {\r
219             if (i > 0)\r
220             {\r
221                 sb.append(", ");\r
222             }\r
223             sb.append(array[i]);\r
224         }\r
225         return sb.append(']').toString();\r
226     }\r
227 \r
228     /**\r
229      * Creates a String representation of a float array.\r
230      * \r
231      * @param array the float array\r
232      * @return the String representation\r
233      */\r
234     protected static String arrayToString(float[] array)\r
235     {\r
236         StringBuilder sb = new StringBuilder("[");\r
237         for (int i = 0; i < array.length; i++)\r
238         {\r
239             if (i > 0)\r
240             {\r
241                 sb.append(", ");\r
242             }\r
243             sb.append(array[i]);\r
244         }\r
245         return sb.append(']').toString();\r
246     }\r
247 \r
248 }\r