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.logicalstructure;
19 import org.apache.pdfbox.cos.COSBase;
20 import org.apache.pdfbox.cos.COSDictionary;
21 import org.apache.pdfbox.cos.COSName;
22 import org.apache.pdfbox.pdmodel.common.PDDictionaryWrapper;
23 import org.apache.pdfbox.pdmodel.documentinterchange.taggedpdf.PDExportFormatAttributeObject;
24 import org.apache.pdfbox.pdmodel.documentinterchange.taggedpdf.PDLayoutAttributeObject;
25 import org.apache.pdfbox.pdmodel.documentinterchange.taggedpdf.PDListAttributeObject;
26 import org.apache.pdfbox.pdmodel.documentinterchange.taggedpdf.PDPrintFieldAttributeObject;
27 import org.apache.pdfbox.pdmodel.documentinterchange.taggedpdf.PDTableAttributeObject;
30 * An attribute object.
32 * @author <a href="mailto:Johannes%20Koch%20%3Ckoch@apache.org%3E">Johannes Koch</a>
33 * @version $Revision: $
36 public abstract class PDAttributeObject extends PDDictionaryWrapper
40 * Creates an attribute object.
42 * @param dictionary the dictionary
43 * @return the attribute object
45 public static PDAttributeObject create(COSDictionary dictionary)
47 String owner = dictionary.getNameAsString(COSName.O);
48 if (PDUserAttributeObject.OWNER_USER_PROPERTIES.equals(owner))
50 return new PDUserAttributeObject(dictionary);
52 else if (PDListAttributeObject.OWNER_LIST.equals(owner))
54 return new PDListAttributeObject(dictionary);
56 else if (PDPrintFieldAttributeObject.OWNER_PRINT_FIELD.equals(owner))
58 return new PDPrintFieldAttributeObject(dictionary);
60 else if (PDTableAttributeObject.OWNER_TABLE.equals(owner))
62 return new PDTableAttributeObject(dictionary);
64 else if (PDLayoutAttributeObject.OWNER_LAYOUT.equals(owner))
66 return new PDLayoutAttributeObject(dictionary);
68 else if (PDExportFormatAttributeObject.OWNER_XML_1_00.equals(owner)
69 || PDExportFormatAttributeObject.OWNER_HTML_3_20.equals(owner)
70 || PDExportFormatAttributeObject.OWNER_HTML_4_01.equals(owner)
71 || PDExportFormatAttributeObject.OWNER_OEB_1_00.equals(owner)
72 || PDExportFormatAttributeObject.OWNER_RTF_1_05.equals(owner)
73 || PDExportFormatAttributeObject.OWNER_CSS_1_00.equals(owner)
74 || PDExportFormatAttributeObject.OWNER_CSS_2_00.equals(owner))
76 return new PDExportFormatAttributeObject(dictionary);
78 return new PDDefaultAttributeObject(dictionary);
81 private PDStructureElement structureElement;
84 * Gets the structure element.
86 * @return the structure element
88 private PDStructureElement getStructureElement()
90 return this.structureElement;
94 * Sets the structure element.
96 * @param structureElement the structure element
98 protected void setStructureElement(PDStructureElement structureElement)
100 this.structureElement = structureElement;
105 * Default constructor.
107 public PDAttributeObject()
112 * Creates a new attribute object with a given dictionary.
114 * @param dictionary the dictionary
116 public PDAttributeObject(COSDictionary dictionary)
123 * Returns the owner of the attributes.
125 * @return the owner of the attributes
127 public String getOwner()
129 return this.getCOSDictionary().getNameAsString(COSName.O);
133 * Sets the owner of the attributes.
135 * @param owner the owner of the attributes
137 protected void setOwner(String owner)
139 this.getCOSDictionary().setName(COSName.O, owner);
143 * Detects whether there are no properties in the attribute object.
145 * @return <code>true</code> if the attribute object is empty,
146 * <code>false</code> otherwise
148 public boolean isEmpty()
150 // only entry is the owner?
151 return (this.getCOSDictionary().size() == 1) && (this.getOwner() != null);
156 * Notifies the attribute object change listeners if the attribute is changed.
158 * @param oldBase old value
159 * @param newBase new value
161 protected void potentiallyNotifyChanged(COSBase oldBase, COSBase newBase)
163 if (this.isValueChanged(oldBase, newBase))
165 this.notifyChanged();
170 * Is the value changed?
172 * @param oldValue old value
173 * @param newValue new value
174 * @return <code>true</code> if the value is changed, <code>false</code>
177 private boolean isValueChanged(COSBase oldValue, COSBase newValue)
179 if (oldValue == null)
181 if (newValue == null)
187 return !oldValue.equals(newValue);
191 * Notifies the attribute object change listeners about a change in this
194 protected void notifyChanged()
196 if (this.getStructureElement() != null)
198 this.getStructureElement().attributeChanged(this);
203 public String toString()
205 return new StringBuilder("O=").append(this.getOwner()).toString();
209 * Creates a String representation of an Object array.
211 * @param array the Object array
212 * @return the String representation
214 protected static String arrayToString(Object[] array)
216 StringBuilder sb = new StringBuilder("[");
217 for (int i = 0; i < array.length; i++)
225 return sb.append(']').toString();
229 * Creates a String representation of a float array.
231 * @param array the float array
232 * @return the String representation
234 protected static String arrayToString(float[] array)
236 StringBuilder sb = new StringBuilder("[");
237 for (int i = 0; i < array.length; i++)
245 return sb.append(']').toString();