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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.apache.pdfbox.pdmodel.documentinterchange.taggedpdf;
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;
30 * A standard attribute object.
32 * @author <a href="mailto:Johannes%20Koch%20%3Ckoch@apache.org%3E">Johannes Koch</a>
33 * @version $Revision: $
35 public abstract class PDStandardAttributeObject extends PDAttributeObject
39 * Default constructor.
41 public PDStandardAttributeObject()
46 * Creates a new standard attribute object with a given dictionary.
48 * @param dictionary the dictionary
50 public PDStandardAttributeObject(COSDictionary dictionary)
57 * Is the attribute with the given name specified in this attribute object?
59 * @param name the attribute name
60 * @return <code>true</code> if the attribute is specified,
61 * <code>false</code> otherwise
63 public boolean isSpecified(String name)
65 return this.getCOSDictionary().getDictionaryObject(name) != null;
70 * Gets a string attribute value.
72 * @param name the attribute name
73 * @return the string attribute value
75 protected String getString(String name)
77 return this.getCOSDictionary().getString(name);
81 * Sets a string attribute value.
83 * @param name the attribute name
84 * @param value the string attribute value
86 protected void setString(String name, String value)
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);
95 * Gets an array of strings.
97 * @param name the attribute name
98 * @return the array of strings
100 protected String[] getArrayOfString(String name)
102 COSBase v = this.getCOSDictionary().getDictionaryObject(name);
103 if (v instanceof COSArray)
105 COSArray array = (COSArray) v;
106 String[] strings = new String[array.size()];
107 for (int i = 0; i < array.size(); i++)
109 strings[i] = ((COSName) array.getObject(i)).getName();
117 * Sets an array of strings.
119 * @param name the attribute name
120 * @param values the array of strings
122 protected void setArrayOfString(String name, String[] values)
124 COSBase oldBase = this.getCOSDictionary().getDictionaryObject(name);
125 COSArray array = new COSArray();
126 for (int i = 0; i < values.length; i++)
128 array.add(new COSString(values[i]));
130 this.getCOSDictionary().setItem(name, array);
131 COSBase newBase = this.getCOSDictionary().getDictionaryObject(name);
132 this.potentiallyNotifyChanged(oldBase, newBase);
138 * @param name the attribute name
139 * @return the name value
141 protected String getName(String name)
143 return this.getCOSDictionary().getNameAsString(name);
149 * @param name the attribute name
150 * @param defaultValue the default value
151 * @return the name value
153 protected String getName(String name, String defaultValue)
155 return this.getCOSDictionary().getNameAsString(name, defaultValue);
159 * Gets a name value or array of name values.
161 * @param name the attribute name
162 * @param defaultValue the default value
163 * @return a String or array of Strings
165 protected Object getNameOrArrayOfName(String name, String defaultValue)
167 COSBase v = this.getCOSDictionary().getDictionaryObject(name);
168 if (v instanceof COSArray)
170 COSArray array = (COSArray) v;
171 String[] names = new String[array.size()];
172 for (int i = 0; i < array.size(); i++)
174 COSBase item = array.getObject(i);
175 if (item instanceof COSName)
177 names[i] = ((COSName) item).getName();
182 if (v instanceof COSName)
184 return ((COSName) v).getName();
192 * @param name the attribute name
193 * @param value the name value
195 protected void setName(String name, String value)
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);
204 * Sets an array of name values.
206 * @param name the attribute name
207 * @param values the array of name values
209 protected void setArrayOfName(String name, String[] values)
211 COSBase oldBase = this.getCOSDictionary().getDictionaryObject(name);
212 COSArray array = new COSArray();
213 for (int i = 0; i < values.length; i++)
215 array.add(COSName.getPDFName(values[i]));
217 this.getCOSDictionary().setItem(name, array);
218 COSBase newBase = this.getCOSDictionary().getDictionaryObject(name);
219 this.potentiallyNotifyChanged(oldBase, newBase);
223 * Gets a number or a name value.
225 * @param name the attribute name
226 * @param defaultValue the default name
227 * @return a Float or a String
229 protected Object getNumberOrName(String name, String defaultValue)
231 COSBase value = this.getCOSDictionary().getDictionaryObject(name);
232 if (value instanceof COSNumber)
234 return ((COSNumber) value).floatValue();
236 if (value instanceof COSName)
238 return ((COSName) value).getName();
246 * @param name the attribute name
247 * @param defaultValue the default value
248 * @return the integer
250 protected int getInteger(String name, int defaultValue)
252 return this.getCOSDictionary().getInt(name, defaultValue);
258 * @param name the attribute name
259 * @param value the integer
261 protected void setInteger(String name, int value)
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);
270 * Gets a number value.
272 * @param name the attribute name
273 * @param defaultValue the default value
274 * @return the number value
276 protected float getNumber(String name, float defaultValue)
278 return this.getCOSDictionary().getFloat(name, defaultValue);
282 * Gets a number value.
284 * @param name the attribute name
285 * @return the number value
287 protected float getNumber(String name)
289 return this.getCOSDictionary().getFloat(name);
293 * An "unspecified" default float value.
295 protected static final float UNSPECIFIED = -1.f;
298 * Gets a number or an array of numbers.
300 * @param name the attribute name
301 * @param defaultValue the default value
302 * @return a Float or an array of floats
304 protected Object getNumberOrArrayOfNumber(String name, float defaultValue)
306 COSBase v = this.getCOSDictionary().getDictionaryObject(name);
307 if (v instanceof COSArray)
309 COSArray array = (COSArray) v;
310 float[] values = new float[array.size()];
311 for (int i = 0; i < array.size(); i++)
313 COSBase item = array.getObject(i);
314 if (item instanceof COSNumber)
316 values[i] = ((COSNumber) item).floatValue();
321 if (v instanceof COSNumber)
323 return ((COSNumber) v).floatValue();
325 if (defaultValue == UNSPECIFIED)
333 * Sets a float number.
335 * @param name the attribute name
336 * @param value the float number
338 protected void setNumber(String name, float value)
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);
347 * Sets an integer number.
349 * @param name the attribute name
350 * @param value the integer number
352 protected void setNumber(String name, int value)
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);
361 * Sets an array of float numbers.
363 * @param name the attribute name
364 * @param values the float numbers
366 protected void setArrayOfNumber(String name, float[] values)
368 COSArray array = new COSArray();
369 for (int i = 0; i < values.length; i++)
371 array.add(new COSFloat(values[i]));
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);
382 * @param name the attribute name
385 protected PDGamma getColor(String name)
387 COSArray c = (COSArray) this.getCOSDictionary().getDictionaryObject(name);
390 return new PDGamma(c);
396 * Gets a single colour or four colours.
398 * @param name the attribute name
399 * @return the single ({@link PDGamma}) or a ({@link PDFourColours})
401 protected Object getColorOrFourColors(String name)
404 (COSArray) this.getCOSDictionary().getDictionaryObject(name);
409 if (array.size() == 3)
412 return new PDGamma(array);
414 else if (array.size() == 4)
416 return new PDFourColours(array);
424 * @param name the attribute name
425 * @param value the colour
427 protected void setColor(String name, PDGamma value)
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);
438 * @param name the attribute name
439 * @param value the four colours
441 protected void setFourColors(String name, PDFourColours value)
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);