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.COSDictionary; 20 import org.apache.pdfbox.cos.COSName; 21 import org.apache.pdfbox.pdmodel.common.PDDictionaryWrapper; 22 23 /** 24 * An attribute object. 25 * 26 * @author <a href="mailto:Johannes%20Koch%20%3Ckoch@apache.org%3E">Johannes Koch</a> 27 * @version $Revision: $ 28 * 29 */ 30 public abstract class PDAttributeObject extends PDDictionaryWrapper 31 { 32 33 /** 34 * Creates an attribute object. 35 * 36 * @param dictionary the dictionary 37 * @return the attribute object 38 */ 39 public static PDAttributeObject create(COSDictionary dictionary) 40 { 41 String owner = dictionary.getNameAsString(COSName.O); 42 if (PDUserAttributeObject.USER_PROPERTIES.equals(owner)) 43 { 44 return new PDUserAttributeObject(dictionary); 45 } 46 return new PDDefaultAttributeObject(dictionary); 47 } 48 49 private PDStructureElement structureElement; 50 51 /** 52 * Gets the structure element. 53 * 54 * @return the structure element 55 */ 56 private PDStructureElement getStructureElement() 57 { 58 return this.structureElement; 59 } 60 61 /** 62 * Sets the structure element. 63 * 64 * @param structureElement the structure element 65 */ 66 protected void setStructureElement(PDStructureElement structureElement) 67 { 68 this.structureElement = structureElement; 69 } 70 71 72 /** 73 * Default constructor. 74 */ 75 public PDAttributeObject() 76 { 77 } 78 79 /** 80 * Creates a new attribute object with a given dictionary. 81 * 82 * @param dictionary the dictionary 83 */ 84 public PDAttributeObject(COSDictionary dictionary) 85 { 86 super(dictionary); 87 } 88 89 90 /** 91 * Returns the owner of the attributes. 92 * 93 * @return the owner of the attributes 94 */ 95 public String getOwner() 96 { 97 return this.getCOSDictionary().getNameAsString(COSName.O); 98 } 99 100 /** 101 * Sets the owner of the attributes. 102 * 103 * @param owner the owner of the attributes 104 */ 105 protected void setOwner(String owner) 106 { 107 this.getCOSDictionary().setName(COSName.O, owner); 108 } 109 110 /** 111 * Detects whether there are no properties in the attribute object. 112 * 113 * @return <code>true</code> if the attribute object is empty, 114 * <code>false</code> otherwise 115 */ 116 public boolean isEmpty() 117 { 118 // only entry is the owner? 119 return (this.getCOSDictionary().size() == 1) && (this.getOwner() != null); 120 } 121 122 123 /** 124 * Notifies the attribute object change listeners if the attribute is changed. 125 * 126 * @param oldValue old value 127 * @param newValue new value 128 */ 129 protected void potentiallyNotifyChanged(Object oldValue, Object newValue) 130 { 131 if (this.isValueChanged(oldValue, newValue)) 132 { 133 this.notifyChanged(); 134 } 135 } 136 137 /** 138 * Is the value changed? 139 * 140 * @param oldValue old value 141 * @param newValue new value 142 * @return <code>true</code> if the value is changed, <code>false</code> 143 * otherwise 144 */ 145 private boolean isValueChanged(Object oldValue, Object newValue) 146 { 147 if (oldValue == null) 148 { 149 if (newValue == null) 150 { 151 return false; 152 } 153 return true; 154 } 155 return !oldValue.equals(newValue); 156 } 157 158 /** 159 * Notifies the attribute object change listeners about a change in this 160 * attribute object. 161 */ 162 protected void notifyChanged() 163 { 164 if (this.getStructureElement() != null) 165 { 166 this.getStructureElement().attributeChanged(this); 167 } 168 } 169 170 @Override 171 public String toString() 172 { 173 return new StringBuilder("O=").append(this.getOwner()).toString(); 174 } 175 176 protected static String arrayToString(Object[] array) 177 { 178 StringBuilder sb = new StringBuilder("["); 179 for (int i = 0; i < array.length; i++) 180 { 181 if (i > 0) 182 { 183 sb.append(", "); 184 } 185 sb.append(array[i]); 186 } 187 return sb.append(']').toString(); 188 } 189 190 protected static String arrayToString(float[] array) 191 { 192 StringBuilder sb = new StringBuilder("["); 193 for (int i = 0; i < array.length; i++) 194 { 195 if (i > 0) 196 { 197 sb.append(", "); 198 } 199 sb.append(array[i]); 200 } 201 return sb.append(']').toString(); 202 } 203 204 }