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