]> _ Git - cubeextranet.git/blob
9b49d370fb8085d84a0c7acfbb253ee78557a305
[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 java.util.ArrayList;
20 import java.util.Iterator;
21 import java.util.List;
22 import java.util.Map.Entry;
23
24 import org.apache.pdfbox.cos.COSBase;
25 import org.apache.pdfbox.cos.COSDictionary;
26 import org.apache.pdfbox.cos.COSName;
27
28 /**
29  * A default attribute object.
30  * 
31  * @author <a href="mailto:Johannes%20Koch%20%3Ckoch@apache.org%3E">Johannes Koch</a>
32  * @version $Revision: $
33  */
34 public class PDDefaultAttributeObject extends PDAttributeObject
35 {
36
37     /**
38      * Default constructor.
39      */
40     public PDDefaultAttributeObject()
41     {
42     }
43
44     /**
45      * Creates a default attribute object with a given dictionary.
46      * 
47      * @param dictionary the dictionary
48      */
49     public PDDefaultAttributeObject(COSDictionary dictionary)
50     {
51         super(dictionary);
52     }
53
54
55     /**
56      * Gets the attribute names.
57      * 
58      * @return the attribute names
59      */
60     public List<String> getAttributeNames()
61     {
62         List<String> attrNames = new ArrayList<String>();
63         for (Entry<COSName, COSBase> entry : this.getCOSDictionary().entrySet())
64         {
65             COSName key = entry.getKey();
66             if (!COSName.O.equals(key))
67             {
68                 attrNames.add(key.getName());
69             }
70         }
71         return attrNames;
72     }
73
74     /**
75      * Gets the attribute value for a given name.
76      * 
77      * @param attrName the given attribute name
78      * @return the attribute value for a given name
79      */
80     public COSBase getAttributeValue(String attrName)
81     {
82         return this.getCOSDictionary().getDictionaryObject(attrName);
83     }
84
85     /**
86      * Gets the attribute value for a given name.
87      * 
88      * @param attrName the given attribute name
89      * @param defaultValue the default value
90      * @return the attribute value for a given name
91      */
92     protected COSBase getAttributeValue(String attrName, COSBase defaultValue)
93     {
94         COSBase value = this.getCOSDictionary().getDictionaryObject(attrName);
95         if (value == null)
96         {
97             return defaultValue;
98         }
99         return value;
100     }
101
102     /**
103      * Sets an attribute.
104      * 
105      * @param attrName the attribute name
106      * @param attrValue the attribute value
107      */
108     public void setAttribute(String attrName, COSBase attrValue)
109     {
110         COSBase old = this.getAttributeValue(attrName);
111         this.getCOSDictionary().setItem(COSName.getPDFName(attrName), attrValue);
112         this.potentiallyNotifyChanged(old, attrValue);
113     }
114
115     @Override
116     public String toString()
117     {
118         StringBuilder sb = new StringBuilder().append(super.toString())
119             .append(", attributes={");
120         Iterator<String> it = this.getAttributeNames().iterator();
121         while (it.hasNext())
122         {
123             String name = it.next();
124             sb.append(name).append('=').append(this.getAttributeValue(name));
125             if (it.hasNext())
126             {
127                 sb.append(", ");
128             }
129         }
130         return sb.append('}').toString();
131     }
132
133 }