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