]> _ Git - cubeextranet.git/blob
912cf75266164c713fc3dc1296e4b6e34eafed14
[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.taggedpdf;\r
18 \r
19 import org.apache.pdfbox.cos.COSArray;\r
20 import org.apache.pdfbox.cos.COSBase;\r
21 import org.apache.pdfbox.cos.COSDictionary;\r
22 import org.apache.pdfbox.cos.COSFloat;\r
23 import org.apache.pdfbox.cos.COSName;\r
24 import org.apache.pdfbox.cos.COSNumber;\r
25 import org.apache.pdfbox.cos.COSString;\r
26 import org.apache.pdfbox.pdmodel.documentinterchange.logicalstructure.PDAttributeObject;\r
27 import org.apache.pdfbox.pdmodel.graphics.color.PDGamma;\r
28 \r
29 /**\r
30  * A standard 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 public abstract class PDStandardAttributeObject extends PDAttributeObject\r
36 {\r
37 \r
38     /**\r
39      * Default constructor.\r
40      */\r
41     public PDStandardAttributeObject()\r
42     {\r
43     }\r
44 \r
45     /**\r
46      * Creates a new standard attribute object with a given dictionary.\r
47      * \r
48      * @param dictionary the dictionary\r
49      */\r
50     public PDStandardAttributeObject(COSDictionary dictionary)\r
51     {\r
52         super(dictionary);\r
53     }\r
54 \r
55 \r
56     /**\r
57      * Is the attribute with the given name specified in this attribute object?\r
58      * \r
59      * @param name the attribute name\r
60      * @return <code>true</code> if the attribute is specified,\r
61      * <code>false</code> otherwise\r
62      */\r
63     public boolean isSpecified(String name)\r
64     {\r
65         return this.getCOSDictionary().getDictionaryObject(name) != null;\r
66     }\r
67 \r
68 \r
69     /**\r
70      * Gets a string attribute value.\r
71      * \r
72      * @param name the attribute name\r
73      * @return the string attribute value\r
74      */\r
75     protected String getString(String name)\r
76     {\r
77         return this.getCOSDictionary().getString(name);\r
78     }\r
79 \r
80     /**\r
81      * Sets a string attribute value.\r
82      * \r
83      * @param name the attribute name\r
84      * @param value the string attribute value\r
85      */\r
86     protected void setString(String name, String value)\r
87     {\r
88         COSBase oldBase = this.getCOSDictionary().getDictionaryObject(name);\r
89         this.getCOSDictionary().setString(name, value);\r
90         COSBase newBase = this.getCOSDictionary().getDictionaryObject(name);\r
91         this.potentiallyNotifyChanged(oldBase, newBase);\r
92     }\r
93 \r
94     /**\r
95      * Gets an array of strings.\r
96      * \r
97      * @param name the attribute name\r
98      * @return the array of strings\r
99      */\r
100     protected String[] getArrayOfString(String name)\r
101     {\r
102         COSBase v = this.getCOSDictionary().getDictionaryObject(name);\r
103         if (v instanceof COSArray)\r
104         {\r
105             COSArray array = (COSArray) v;\r
106             String[] strings = new String[array.size()];\r
107             for (int i = 0; i < array.size(); i++)\r
108             {\r
109                 strings[i] = ((COSName) array.getObject(i)).getName();\r
110             }\r
111             return strings;\r
112         }\r
113         return null;\r
114     }\r
115 \r
116     /**\r
117      * Sets an array of strings.\r
118      * \r
119      * @param name the attribute name\r
120      * @param values the array of strings\r
121      */\r
122     protected void setArrayOfString(String name, String[] values)\r
123     {\r
124         COSBase oldBase = this.getCOSDictionary().getDictionaryObject(name);\r
125         COSArray array = new COSArray();\r
126         for (int i = 0; i < values.length; i++)\r
127         {\r
128             array.add(new COSString(values[i]));\r
129         }\r
130         this.getCOSDictionary().setItem(name, array);\r
131         COSBase newBase = this.getCOSDictionary().getDictionaryObject(name);\r
132         this.potentiallyNotifyChanged(oldBase, newBase);\r
133     }\r
134 \r
135     /**\r
136      * Gets a name value.\r
137      * \r
138      * @param name the attribute name\r
139      * @return the name value\r
140      */\r
141     protected String getName(String name)\r
142     {\r
143         return this.getCOSDictionary().getNameAsString(name);\r
144     }\r
145 \r
146     /**\r
147      * Gets a name value.\r
148      * \r
149      * @param name the attribute name\r
150      * @param defaultValue the default value\r
151      * @return the name value\r
152      */\r
153     protected String getName(String name, String defaultValue)\r
154     {\r
155         return this.getCOSDictionary().getNameAsString(name, defaultValue);\r
156     }\r
157 \r
158     /**\r
159      * Gets a name value or array of name values.\r
160      * \r
161      * @param name the attribute name\r
162      * @param defaultValue the default value\r
163      * @return a String or array of Strings\r
164      */\r
165     protected Object getNameOrArrayOfName(String name, String defaultValue)\r
166     {\r
167         COSBase v = this.getCOSDictionary().getDictionaryObject(name);\r
168         if (v instanceof COSArray)\r
169         {\r
170             COSArray array = (COSArray) v;\r
171             String[] names = new String[array.size()];\r
172             for (int i = 0; i < array.size(); i++)\r
173             {\r
174                 COSBase item = array.getObject(i);\r
175                 if (item instanceof COSName)\r
176                 {\r
177                     names[i] = ((COSName) item).getName();\r
178                 }\r
179             }\r
180             return names;\r
181         }\r
182         if (v instanceof COSName)\r
183         {\r
184             return ((COSName) v).getName();\r
185         }\r
186         return defaultValue;\r
187     }\r
188 \r
189     /**\r
190      * Sets a name value.\r
191      * \r
192      * @param name the attribute name\r
193      * @param value the name value\r
194      */\r
195     protected void setName(String name, String value)\r
196     {\r
197         COSBase oldBase = this.getCOSDictionary().getDictionaryObject(name);\r
198         this.getCOSDictionary().setName(name, value);\r
199         COSBase newBase = this.getCOSDictionary().getDictionaryObject(name);\r
200         this.potentiallyNotifyChanged(oldBase, newBase);\r
201     }\r
202 \r
203     /**\r
204      * Sets an array of name values.\r
205      * \r
206      * @param name the attribute name\r
207      * @param values the array of name values\r
208      */\r
209     protected void setArrayOfName(String name, String[] values)\r
210     {\r
211         COSBase oldBase = this.getCOSDictionary().getDictionaryObject(name);\r
212         COSArray array = new COSArray();\r
213         for (int i = 0; i < values.length; i++)\r
214         {\r
215             array.add(COSName.getPDFName(values[i]));\r
216         }\r
217         this.getCOSDictionary().setItem(name, array);\r
218         COSBase newBase = this.getCOSDictionary().getDictionaryObject(name);\r
219         this.potentiallyNotifyChanged(oldBase, newBase);\r
220     }\r
221 \r
222     /**\r
223      * Gets a number or a name value.\r
224      * \r
225      * @param name the attribute name\r
226      * @param defaultValue the default name\r
227      * @return a Float or a String\r
228      */\r
229     protected Object getNumberOrName(String name, String defaultValue)\r
230     {\r
231         COSBase value = this.getCOSDictionary().getDictionaryObject(name);\r
232         if (value instanceof COSNumber)\r
233         {\r
234             return ((COSNumber) value).floatValue();\r
235         }\r
236         if (value instanceof COSName)\r
237         {\r
238             return ((COSName) value).getName();\r
239         }\r
240         return defaultValue;\r
241     }\r
242 \r
243     /**\r
244      * Gets an integer.\r
245      * \r
246      * @param name the attribute name\r
247      * @param defaultValue the default value\r
248      * @return the integer\r
249      */\r
250     protected int getInteger(String name, int defaultValue)\r
251     {\r
252         return this.getCOSDictionary().getInt(name, defaultValue);\r
253     }\r
254 \r
255     /**\r
256      * Sets an integer.\r
257      * \r
258      * @param name the attribute name\r
259      * @param value the integer\r
260      */\r
261     protected void setInteger(String name, int value)\r
262     {\r
263         COSBase oldBase = this.getCOSDictionary().getDictionaryObject(name);\r
264         this.getCOSDictionary().setInt(name, value);\r
265         COSBase newBase = this.getCOSDictionary().getDictionaryObject(name);\r
266         this.potentiallyNotifyChanged(oldBase, newBase);\r
267     }\r
268 \r
269     /**\r
270      * Gets a number value.\r
271      * \r
272      * @param name the attribute name\r
273      * @param defaultValue the default value\r
274      * @return the number value\r
275      */\r
276     protected float getNumber(String name, float defaultValue)\r
277     {\r
278         return this.getCOSDictionary().getFloat(name, defaultValue);\r
279     }\r
280 \r
281     /**\r
282      * Gets a number value.\r
283      * \r
284      * @param name the attribute name\r
285      * @return the number value\r
286      */\r
287     protected float getNumber(String name)\r
288     {\r
289         return this.getCOSDictionary().getFloat(name);\r
290     }\r
291 \r
292     /**\r
293      * An "unspecified" default float value.\r
294      */\r
295     protected static final float UNSPECIFIED = -1.f;\r
296 \r
297     /**\r
298      * Gets a number or an array of numbers.\r
299      * \r
300      * @param name the attribute name\r
301      * @param defaultValue the default value\r
302      * @return a Float or an array of floats\r
303      */\r
304     protected Object getNumberOrArrayOfNumber(String name, float defaultValue)\r
305     {\r
306         COSBase v = this.getCOSDictionary().getDictionaryObject(name);\r
307         if (v instanceof COSArray)\r
308         {\r
309             COSArray array = (COSArray) v;\r
310             float[] values = new float[array.size()];\r
311             for (int i = 0; i < array.size(); i++)\r
312             {\r
313                 COSBase item = array.getObject(i);\r
314                 if (item instanceof COSNumber)\r
315                 {\r
316                     values[i] = ((COSNumber) item).floatValue();\r
317                 }\r
318             }\r
319             return values;\r
320         }\r
321         if (v instanceof COSNumber)\r
322         {\r
323             return ((COSNumber) v).floatValue();\r
324         }\r
325         if (defaultValue == UNSPECIFIED)\r
326         {\r
327             return null;\r
328         }\r
329         return defaultValue;\r
330     }\r
331 \r
332     /**\r
333      * Sets a float number.\r
334      * \r
335      * @param name the attribute name\r
336      * @param value the float number\r
337      */\r
338     protected void setNumber(String name, float value)\r
339     {\r
340         COSBase oldBase = this.getCOSDictionary().getDictionaryObject(name);\r
341         this.getCOSDictionary().setFloat(name, value);\r
342         COSBase newBase = this.getCOSDictionary().getDictionaryObject(name);\r
343         this.potentiallyNotifyChanged(oldBase, newBase);\r
344     }\r
345 \r
346     /**\r
347      * Sets an integer number.\r
348      * \r
349      * @param name the attribute name\r
350      * @param value the integer number\r
351      */\r
352     protected void setNumber(String name, int value)\r
353     {\r
354         COSBase oldBase = this.getCOSDictionary().getDictionaryObject(name);\r
355         this.getCOSDictionary().setInt(name, value);\r
356         COSBase newBase = this.getCOSDictionary().getDictionaryObject(name);\r
357         this.potentiallyNotifyChanged(oldBase, newBase);\r
358     }\r
359 \r
360     /**\r
361      * Sets an array of float numbers.\r
362      * \r
363      * @param name the attribute name\r
364      * @param values the float numbers\r
365      */\r
366     protected void setArrayOfNumber(String name, float[] values)\r
367     {\r
368         COSArray array = new COSArray();\r
369         for (int i = 0; i < values.length; i++)\r
370         {\r
371             array.add(new COSFloat(values[i]));\r
372         }\r
373         COSBase oldBase = this.getCOSDictionary().getDictionaryObject(name);\r
374         this.getCOSDictionary().setItem(name, array);\r
375         COSBase newBase = this.getCOSDictionary().getDictionaryObject(name);\r
376         this.potentiallyNotifyChanged(oldBase, newBase);\r
377     }\r
378 \r
379     /**\r
380      * Gets a colour.\r
381      * \r
382      * @param name the attribute name\r
383      * @return the colour\r
384      */\r
385     protected PDGamma getColor(String name)\r
386     {\r
387         COSArray c = (COSArray) this.getCOSDictionary().getDictionaryObject(name);\r
388         if (c != null)\r
389         {\r
390             return new PDGamma(c);\r
391         }\r
392         return null;\r
393     }\r
394 \r
395     /**\r
396      * Gets a single colour or four colours.\r
397      * \r
398      * @param name the attribute name\r
399      * @return the single ({@link PDGamma}) or a ({@link PDFourColours})\r
400      */\r
401     protected Object getColorOrFourColors(String name)\r
402     {\r
403         COSArray array =\r
404             (COSArray) this.getCOSDictionary().getDictionaryObject(name);\r
405         if (array == null)\r
406         {\r
407             return null;\r
408         }\r
409         if (array.size() == 3)\r
410         {\r
411             // only one colour\r
412             return new PDGamma(array);\r
413         }\r
414         else if (array.size() == 4)\r
415         {\r
416             return new PDFourColours(array);\r
417         }\r
418         return null;\r
419     }\r
420 \r
421     /**\r
422      * Sets a colour.\r
423      * \r
424      * @param name the attribute name\r
425      * @param value the colour\r
426      */\r
427     protected void setColor(String name, PDGamma value)\r
428     {\r
429         COSBase oldValue = this.getCOSDictionary().getDictionaryObject(name);\r
430         this.getCOSDictionary().setItem(name, value);\r
431         COSBase newValue = value == null ? null : value.getCOSObject();\r
432         this.potentiallyNotifyChanged(oldValue, newValue);\r
433     }\r
434 \r
435     /**\r
436      * Sets four colours.\r
437      * \r
438      * @param name the attribute name\r
439      * @param value the four colours\r
440      */\r
441     protected void setFourColors(String name, PDFourColours value)\r
442     {\r
443         COSBase oldValue = this.getCOSDictionary().getDictionaryObject(name);\r
444         this.getCOSDictionary().setItem(name, value);\r
445         COSBase newValue = value == null ? null : value.getCOSObject();\r
446         this.potentiallyNotifyChanged(oldValue, newValue);\r
447     }\r
448 \r
449 }\r