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