]> _ Git - cubeextranet.git/blob
0043f13402214cbe9b2df4f8e1f024adad72209e
[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.logicalstructure;
18
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;
28
29 /**
30  * An attribute object.
31  *
32  * @author <a href="mailto:Johannes%20Koch%20%3Ckoch@apache.org%3E">Johannes Koch</a>
33  * @version $Revision: $
34  *
35  */
36 public abstract class PDAttributeObject extends PDDictionaryWrapper
37 {
38
39     /**
40      * Creates an attribute object.
41      * 
42      * @param dictionary the dictionary
43      * @return the attribute object
44      */
45     public static PDAttributeObject create(COSDictionary dictionary)
46     {
47         String owner = dictionary.getNameAsString(COSName.O);
48         if (PDUserAttributeObject.OWNER_USER_PROPERTIES.equals(owner))
49         {
50             return new PDUserAttributeObject(dictionary);
51         }
52         else if (PDListAttributeObject.OWNER_LIST.equals(owner))
53         {
54             return new PDListAttributeObject(dictionary);
55         }
56         else if (PDPrintFieldAttributeObject.OWNER_PRINT_FIELD.equals(owner))
57         {
58             return new PDPrintFieldAttributeObject(dictionary);
59         }
60         else if (PDTableAttributeObject.OWNER_TABLE.equals(owner))
61         {
62             return new PDTableAttributeObject(dictionary);
63         }
64         else if (PDLayoutAttributeObject.OWNER_LAYOUT.equals(owner))
65         {
66             return new PDLayoutAttributeObject(dictionary);
67         }
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))
75         {
76             return new PDExportFormatAttributeObject(dictionary);
77         }
78         return new PDDefaultAttributeObject(dictionary);
79     }
80
81     private PDStructureElement structureElement;
82
83     /**
84      * Gets the structure element.
85      * 
86      * @return the structure element
87      */
88     private PDStructureElement getStructureElement()
89     {
90         return this.structureElement;
91     }
92
93     /**
94      * Sets the structure element.
95      * 
96      * @param structureElement the structure element
97      */
98     protected void setStructureElement(PDStructureElement structureElement)
99     {
100         this.structureElement = structureElement;
101     }
102
103
104     /**
105      * Default constructor.
106      */
107     public PDAttributeObject()
108     {
109     }
110
111     /**
112      * Creates a new attribute object with a given dictionary.
113      * 
114      * @param dictionary the dictionary
115      */
116     public PDAttributeObject(COSDictionary dictionary)
117     {
118         super(dictionary);
119     }
120
121
122     /**
123      * Returns the owner of the attributes.
124      * 
125      * @return the owner of the attributes
126      */
127     public String getOwner()
128     {
129         return this.getCOSDictionary().getNameAsString(COSName.O);
130     }
131
132     /**
133      * Sets the owner of the attributes.
134      * 
135      * @param owner the owner of the attributes
136      */
137     protected void setOwner(String owner)
138     {
139         this.getCOSDictionary().setName(COSName.O, owner);
140     }
141
142     /**
143      * Detects whether there are no properties in the attribute object.
144      * 
145      * @return <code>true</code> if the attribute object is empty,
146      *  <code>false</code> otherwise
147      */
148     public boolean isEmpty()
149     {
150         // only entry is the owner?
151         return (this.getCOSDictionary().size() == 1) && (this.getOwner() != null);
152     }
153
154
155     /**
156      * Notifies the attribute object change listeners if the attribute is changed.
157      * 
158      * @param oldBase old value
159      * @param newBase new value
160      */
161     protected void potentiallyNotifyChanged(COSBase oldBase, COSBase newBase)
162     {
163         if (this.isValueChanged(oldBase, newBase))
164         {
165             this.notifyChanged();
166         }
167     }
168
169     /**
170      * Is the value changed?
171      * 
172      * @param oldValue old value
173      * @param newValue new value
174      * @return <code>true</code> if the value is changed, <code>false</code>
175      * otherwise
176      */
177     private boolean isValueChanged(COSBase oldValue, COSBase newValue)
178     {
179         if (oldValue == null)
180         {
181             if (newValue == null)
182             {
183                 return false;
184             }
185             return true;
186         }
187         return !oldValue.equals(newValue);
188     }
189
190     /**
191      * Notifies the attribute object change listeners about a change in this
192      * attribute object.
193      */
194     protected void notifyChanged()
195     {
196         if (this.getStructureElement() != null)
197         {
198             this.getStructureElement().attributeChanged(this);
199         }
200     }
201
202     @Override
203     public String toString()
204     {
205         return new StringBuilder("O=").append(this.getOwner()).toString();
206     }
207
208     /**
209      * Creates a String representation of an Object array.
210      * 
211      * @param array the Object array
212      * @return the String representation
213      */
214     protected static String arrayToString(Object[] array)
215     {
216         StringBuilder sb = new StringBuilder("[");
217         for (int i = 0; i < array.length; i++)
218         {
219             if (i > 0)
220             {
221                 sb.append(", ");
222             }
223             sb.append(array[i]);
224         }
225         return sb.append(']').toString();
226     }
227
228     /**
229      * Creates a String representation of a float array.
230      * 
231      * @param array the float array
232      * @return the String representation
233      */
234     protected static String arrayToString(float[] array)
235     {
236         StringBuilder sb = new StringBuilder("[");
237         for (int i = 0; i < array.length; i++)
238         {
239             if (i > 0)
240             {
241                 sb.append(", ");
242             }
243             sb.append(array[i]);
244         }
245         return sb.append(']').toString();
246     }
247
248 }