Author: koch
Date: Tue Mar 23 14:34:10 2010
New Revision: 926594

URL: http://svn.apache.org/viewvc?rev=926594&view=rev
Log:
some more about logical structure (basic support for structure attributes)

Added:
    
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/documentinterchange/logicalstructure/PDAttributeObject.java
    
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/documentinterchange/logicalstructure/PDDefaultAttributeObject.java
    
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/documentinterchange/logicalstructure/PDUserAttributeObject.java
    
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/documentinterchange/logicalstructure/PDUserProperty.java
Modified:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSName.java
    
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/documentinterchange/logicalstructure/PDStructureElement.java

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSName.java
URL: 
http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSName.java?rev=926594&r1=926593&r2=926594&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSName.java 
(original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSName.java Tue 
Mar 23 14:34:10 2010
@@ -75,6 +75,10 @@ public final class COSName extends COSBa
     */
     public static final COSName ANNOTS = new COSName( "Annots" );
     /**
+     * "Artifact"
+     */
+    public static final COSName ARTIFACT = new COSName("Artifact");
+    /**
      * A common COSName value.
      */
     public static final COSName ART_BOX = new COSName("ArtBox" );
@@ -87,6 +91,10 @@ public final class COSName extends COSBa
     */
     public static final COSName ASCII85_DECODE_ABBREVIATION = new COSName( 
"A85" );
     /**
+     * "Attached"
+     */
+    public static final COSName ATTACHED = new COSName("Attached");
+    /**
     * A common COSName value.
     */
     public static final COSName ASCII_HEX_DECODE = new COSName( 
"ASCIIHexDecode" );
@@ -487,6 +495,11 @@ public final class COSName extends COSBa
     public static final COSName NUMS = new COSName( "Nums" );
 
     /**
+     * "O"
+     */
+    public static final COSName O = new COSName("O");
+    
+    /**
      * "Obj"
      */
     public static final COSName OBJ = new COSName("Obj");

Added: 
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/documentinterchange/logicalstructure/PDAttributeObject.java
URL: 
http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/documentinterchange/logicalstructure/PDAttributeObject.java?rev=926594&view=auto
==============================================================================
--- 
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/documentinterchange/logicalstructure/PDAttributeObject.java
 (added)
+++ 
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/documentinterchange/logicalstructure/PDAttributeObject.java
 Tue Mar 23 14:34:10 2010
@@ -0,0 +1,204 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.pdfbox.pdmodel.documentinterchange.logicalstructure;
+
+import org.apache.pdfbox.cos.COSDictionary;
+import org.apache.pdfbox.cos.COSName;
+import org.apache.pdfbox.pdmodel.common.PDDictionaryWrapper;
+
+/**
+ * An attribute object.
+ *
+ * @author <a href="mailto:johannes%20koch%20%[email protected]%3e";>Johannes 
Koch</a>
+ * @version $Revision: $
+ *
+ */
+public abstract class PDAttributeObject extends PDDictionaryWrapper
+{
+
+    /**
+     * Creates an attribute object.
+     * 
+     * @param dictionary the dictionary
+     * @return the attribute object
+     */
+    public static PDAttributeObject create(COSDictionary dictionary)
+    {
+        String owner = dictionary.getNameAsString(COSName.O);
+        if (PDUserAttributeObject.USER_PROPERTIES.equals(owner))
+        {
+            return new PDUserAttributeObject(dictionary);
+        }
+        return new PDDefaultAttributeObject(dictionary);
+    }
+
+    private PDStructureElement structureElement;
+
+    /**
+     * Gets the structure element.
+     * 
+     * @return the structure element
+     */
+    private PDStructureElement getStructureElement()
+    {
+        return this.structureElement;
+    }
+
+    /**
+     * Sets the structure element.
+     * 
+     * @param structureElement the structure element
+     */
+    protected void setStructureElement(PDStructureElement structureElement)
+    {
+        this.structureElement = structureElement;
+    }
+
+
+    /**
+     * Default constructor.
+     */
+    public PDAttributeObject()
+    {
+    }
+
+    /**
+     * Creates a new attribute object with a given dictionary.
+     * 
+     * @param dictionary the dictionary
+     */
+    public PDAttributeObject(COSDictionary dictionary)
+    {
+        super(dictionary);
+    }
+
+
+    /**
+     * Returns the owner of the attributes.
+     * 
+     * @return the owner of the attributes
+     */
+    public String getOwner()
+    {
+        return this.getCOSDictionary().getNameAsString(COSName.O);
+    }
+
+    /**
+     * Sets the owner of the attributes.
+     * 
+     * @param owner the owner of the attributes
+     */
+    protected void setOwner(String owner)
+    {
+        this.getCOSDictionary().setName(COSName.O, owner);
+    }
+
+    /**
+     * Detects whether there are no properties in the attribute object.
+     * 
+     * @return <code>true</code> if the attribute object is empty,
+     *  <code>false</code> otherwise
+     */
+    public boolean isEmpty()
+    {
+        // only entry is the owner?
+        return (this.getCOSDictionary().size() == 1) && (this.getOwner() != 
null);
+    }
+
+
+    /**
+     * Notifies the attribute object change listeners if the attribute is 
changed.
+     * 
+     * @param oldValue old value
+     * @param newValue new value
+     */
+    protected void potentiallyNotifyChanged(Object oldValue, Object newValue)
+    {
+        if (this.isValueChanged(oldValue, newValue))
+        {
+            this.notifyChanged();
+        }
+    }
+
+    /**
+     * Is the value changed?
+     * 
+     * @param oldValue old value
+     * @param newValue new value
+     * @return <code>true</code> if the value is changed, <code>false</code>
+     * otherwise
+     */
+    private boolean isValueChanged(Object oldValue, Object newValue)
+    {
+        if (oldValue == null)
+        {
+            if (newValue == null)
+            {
+                return false;
+            }
+            return true;
+        }
+        return !oldValue.equals(newValue);
+    }
+
+    /**
+     * Notifies the attribute object change listeners about a change in this
+     * attribute object.
+     */
+    protected void notifyChanged()
+    {
+        if (this.getStructureElement() != null)
+        {
+            this.getStructureElement().attributeChanged(this);
+        }
+    }
+
+    @Override
+    public String toString()
+    {
+        return new StringBuilder("O=").append(this.getOwner()).toString();
+    }
+
+    protected static String arrayToString(Object[] array)
+    {
+        StringBuilder sb = new StringBuilder("[");
+        for (int i = 0; i < array.length; i++)
+        {
+            if (i > 0)
+            {
+                sb.append(", ");
+            }
+            sb.append(array[i]);
+        }
+        return sb.append(']').toString();
+    }
+
+    protected static String arrayToString(float[] array)
+    {
+        StringBuilder sb = new StringBuilder("[");
+        for (int i = 0; i < array.length; i++)
+        {
+            if (i > 0)
+            {
+                sb.append(", ");
+            }
+            sb.append(array[i]);
+        }
+        return sb.append(']').toString();
+    }
+
+}

Added: 
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/documentinterchange/logicalstructure/PDDefaultAttributeObject.java
URL: 
http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/documentinterchange/logicalstructure/PDDefaultAttributeObject.java?rev=926594&view=auto
==============================================================================
--- 
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/documentinterchange/logicalstructure/PDDefaultAttributeObject.java
 (added)
+++ 
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/documentinterchange/logicalstructure/PDDefaultAttributeObject.java
 Tue Mar 23 14:34:10 2010
@@ -0,0 +1,133 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.pdfbox.pdmodel.documentinterchange.logicalstructure;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map.Entry;
+
+import org.apache.pdfbox.cos.COSBase;
+import org.apache.pdfbox.cos.COSDictionary;
+import org.apache.pdfbox.cos.COSName;
+
+/**
+ * A default attribute object.
+ * 
+ * @author <a href="mailto:johannes%20koch%20%[email protected]%3e";>Johannes 
Koch</a>
+ * @version $Revision: $
+ */
+public class PDDefaultAttributeObject extends PDAttributeObject
+{
+
+    /**
+     * Default constructor.
+     */
+    public PDDefaultAttributeObject()
+    {
+    }
+
+    /**
+     * Creates a default attribute object with a given dictionary.
+     * 
+     * @param dictionary the dictionary
+     */
+    public PDDefaultAttributeObject(COSDictionary dictionary)
+    {
+        super(dictionary);
+    }
+
+
+    /**
+     * Gets the attribute names.
+     * 
+     * @return the attribute names
+     */
+    public List<String> getAttributeNames()
+    {
+        List<String> attrNames = new ArrayList<String>();
+        for (Entry<COSName, COSBase> entry : 
this.getCOSDictionary().entrySet())
+        {
+            COSName key = entry.getKey();
+            if (!COSName.O.equals(key))
+            {
+                attrNames.add(key.getName());
+            }
+        }
+        return attrNames;
+    }
+
+    /**
+     * Gets the attribute value for a given name.
+     * 
+     * @param attrName the given attribute name
+     * @return the attribute value for a given name
+     */
+    public COSBase getAttributeValue(String attrName)
+    {
+        return this.getCOSDictionary().getDictionaryObject(attrName);
+    }
+
+    /**
+     * Gets the attribute value for a given name.
+     * 
+     * @param attrName the given attribute name
+     * @param defaultValue the default value
+     * @return the attribute value for a given name
+     */
+    protected COSBase getAttributeValue(String attrName, COSBase defaultValue)
+    {
+        COSBase value = this.getCOSDictionary().getDictionaryObject(attrName);
+        if (value == null)
+        {
+            return defaultValue;
+        }
+        return value;
+    }
+
+    /**
+     * Sets an attribute.
+     * 
+     * @param attrName the attribute name
+     * @param attrValue the attribute value
+     */
+    public void setAttribute(String attrName, COSBase attrValue)
+    {
+        COSBase old = this.getAttributeValue(attrName);
+        this.getCOSDictionary().setItem(COSName.getPDFName(attrName), 
attrValue);
+        this.potentiallyNotifyChanged(old, attrValue);
+    }
+
+    @Override
+    public String toString()
+    {
+        StringBuilder sb = new StringBuilder().append(super.toString())
+            .append(", attributes={");
+        Iterator<String> it = this.getAttributeNames().iterator();
+        while (it.hasNext())
+        {
+            String name = it.next();
+            sb.append(name).append('=').append(this.getAttributeValue(name));
+            if (it.hasNext())
+            {
+                sb.append(", ");
+            }
+        }
+        return sb.append('}').toString();
+    }
+
+}

Modified: 
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/documentinterchange/logicalstructure/PDStructureElement.java
URL: 
http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/documentinterchange/logicalstructure/PDStructureElement.java?rev=926594&r1=926593&r2=926594&view=diff
==============================================================================
--- 
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/documentinterchange/logicalstructure/PDStructureElement.java
 (original)
+++ 
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/documentinterchange/logicalstructure/PDStructureElement.java
 Tue Mar 23 14:34:10 2010
@@ -26,7 +26,6 @@ import org.apache.pdfbox.cos.COSInteger;
 import org.apache.pdfbox.cos.COSName;
 import org.apache.pdfbox.cos.COSObject;
 import org.apache.pdfbox.pdmodel.PDPage;
-import org.apache.pdfbox.pdmodel.common.COSObjectable;
 import 
org.apache.pdfbox.pdmodel.documentinterchange.markedcontent.PDMarkedContent;
 
 /**
@@ -161,6 +160,173 @@ public class PDStructureElement extends 
     }
 
     /**
+     * Returns the attributes together with their revision numbers (A).
+     * 
+     * @return the attributes
+     */
+    public Revisions<PDAttributeObject> getAttributes()
+    {
+        Revisions<PDAttributeObject> attributes =
+            new Revisions<PDAttributeObject>();
+        COSBase a = this.getCOSDictionary().getDictionaryObject(COSName.A);
+        if (a instanceof COSArray)
+        {
+            COSArray aa = (COSArray) a;
+            Iterator<COSBase> it = aa.iterator();
+            PDAttributeObject ao = null;
+            while (it.hasNext())
+            {
+                COSBase item = it.next();
+                if (item instanceof COSDictionary)
+                {
+                    ao = PDAttributeObject.create((COSDictionary) item);
+                    ao.setStructureElement(this);
+                    attributes.addObject(ao, 0);
+                }
+                else if (item instanceof COSInteger)
+                {
+                    attributes.setRevisionNumber(ao,
+                        ((COSInteger) item).intValue());
+                }
+            }
+        }
+        if (a instanceof COSDictionary)
+        {
+            PDAttributeObject ao = PDAttributeObject.create((COSDictionary) a);
+            ao.setStructureElement(this);
+            attributes.addObject(ao, 0);
+        }
+        return attributes;
+    }
+
+    /**
+     * Sets the attributes together with their revision numbers (A).
+     * 
+     * @param attributes the attributes
+     */
+    public void setAttributes(Revisions<PDAttributeObject> attributes)
+    {
+        COSName key = COSName.A;
+        if ((attributes.size() == 1) && (attributes.getRevisionNumber(0) == 0))
+        {
+            PDAttributeObject attributeObject = attributes.getObject(0);
+            attributeObject.setStructureElement(this);
+            this.getCOSDictionary().setItem(key, attributeObject);
+            return;
+        }
+        COSArray array = new COSArray();
+        for (int i = 0; i < attributes.size(); i++)
+        {
+            PDAttributeObject attributeObject = attributes.getObject(i);
+            attributeObject.setStructureElement(this);
+            int revisionNumber = attributes.getRevisionNumber(i);
+            if (revisionNumber < 0)
+            {
+                // TODO throw Exception because revision number must be > -1?
+            }
+            array.add(attributeObject);
+            array.add(COSInteger.get(revisionNumber));
+        }
+        this.getCOSDictionary().setItem(key, array);
+    }
+
+    /**
+     * Adds an attribute object.
+     * 
+     * @param attributeObject the attribute object
+     */
+    public void addAttribute(PDAttributeObject attributeObject)
+    {
+        COSName key = COSName.A;
+        attributeObject.setStructureElement(this);
+        COSBase a = this.getCOSDictionary().getDictionaryObject(key);
+        COSArray array = null;
+        if (a instanceof COSArray)
+        {
+            array = (COSArray) a;
+        }
+        else
+        {
+            array = new COSArray();
+            if (a != null)
+            {
+                array.add(a);
+                array.add(COSInteger.get(0));
+            }
+        }
+        this.getCOSDictionary().setItem(key, array);
+        array.add(attributeObject);
+        array.add(COSInteger.get(this.getRevisionNumber()));
+    }
+
+    /**
+     * Removes an attribute object.
+     * 
+     * @param attributeObject the attribute object
+     */
+    public void removeAttribute(PDAttributeObject attributeObject)
+    {
+        COSName key = COSName.A;
+        COSBase a = this.getCOSDictionary().getDictionaryObject(key);
+        if (a instanceof COSArray)
+        {
+            COSArray array = (COSArray) a;
+            array.remove(attributeObject.getCOSObject());
+            if ((array.size() == 2) && (array.getInt(1) == 0))
+            {
+                this.getCOSDictionary().setItem(key, array.getObject(0));
+            }
+        }
+        else
+        {
+            COSBase directA = a;
+            if (a instanceof COSObject)
+            {
+                directA = ((COSObject) a).getObject();
+            }
+            if (attributeObject.getCOSObject().equals(directA))
+            {
+                this.getCOSDictionary().setItem(key, null);
+            }
+        }
+        attributeObject.setStructureElement(null);
+    }
+
+    /**
+     * Updates the revision number for the given attribute object.
+     * 
+     * @param attributeObject the attribute object
+     */
+    public void attributeChanged(PDAttributeObject attributeObject)
+    {
+        COSName key = COSName.A;
+        COSBase a = this.getCOSDictionary().getDictionaryObject(key);
+        if (a instanceof COSArray)
+        {
+            COSArray array = (COSArray) a;
+            for (int i = 0; i < array.size(); i++)
+            {
+                COSBase entry = array.getObject(i);
+                if (entry.equals(attributeObject.getCOSObject()))
+                {
+                    COSBase next = array.get(i + 1);
+                    if (next instanceof COSInteger)
+                    {
+                        array.set(i + 1, 
COSInteger.get(this.getRevisionNumber()));
+                    }
+                }
+            }
+        }
+        else
+        {
+            COSArray array = new COSArray();
+            array.add(a);
+            array.add(COSInteger.get(this.getRevisionNumber()));
+            this.getCOSDictionary().setItem(key, array);
+        }
+    }
+
+    /**
      * Returns the class names together with their revision numbers (C).
      * 
      * @return the class names

Added: 
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/documentinterchange/logicalstructure/PDUserAttributeObject.java
URL: 
http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/documentinterchange/logicalstructure/PDUserAttributeObject.java?rev=926594&view=auto
==============================================================================
--- 
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/documentinterchange/logicalstructure/PDUserAttributeObject.java
 (added)
+++ 
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/documentinterchange/logicalstructure/PDUserAttributeObject.java
 Tue Mar 23 14:34:10 2010
@@ -0,0 +1,135 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.pdfbox.pdmodel.documentinterchange.logicalstructure;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.pdfbox.cos.COSArray;
+import org.apache.pdfbox.cos.COSDictionary;
+import org.apache.pdfbox.cos.COSName;
+
+/**
+ * A User attribute object.
+ * 
+ * @author <a href="mailto:johannes%20koch%20%[email protected]%3e";>Johannes 
Koch</a>
+ * @version $Revision: $
+ */
+public class PDUserAttributeObject extends PDAttributeObject
+{
+
+    /**
+     * Attribute owner for user properties
+     */
+    public static final String USER_PROPERTIES = "UserProperties";
+
+
+    /**
+     * Default constructor
+     */
+    public PDUserAttributeObject()
+    {
+        this.setOwner(USER_PROPERTIES);
+    }
+
+    /**
+     * 
+     * @param dictionary the dictionary
+     */
+    public PDUserAttributeObject(COSDictionary dictionary)
+    {
+        super(dictionary);
+    }
+
+
+    /**
+     * Returns the user properties.
+     * 
+     * @return the user properties
+     */
+    public List<PDUserProperty> getUserProperties()
+    {
+        COSArray p = (COSArray) this.getCOSDictionary()
+            .getDictionaryObject(COSName.P);
+        List<PDUserProperty> properties = new 
ArrayList<PDUserProperty>(p.size());
+        for (int i = 0; i < p.size(); i++)
+        {
+            properties.add(
+                new PDUserProperty((COSDictionary) p.getObject(i), this));
+        }
+        return properties;
+    }
+
+    /**
+     * Sets the user properties.
+     * 
+     * @param userProperties the user properties
+     */
+    public void setUserProperties(List<PDUserProperty> userProperties)
+    {
+        COSArray p = new COSArray();
+        for (PDUserProperty userProperty : userProperties)
+        {
+            p.add(userProperty);
+        }
+        this.getCOSDictionary().setItem(COSName.P, p);
+    }
+
+    /**
+     * Adds a user property.
+     * 
+     * @param userProperty the user property
+     */
+    public void addUserProperty(PDUserProperty userProperty)
+    {
+        COSArray p = (COSArray) this.getCOSDictionary()
+            .getDictionaryObject(COSName.P);
+        p.add(userProperty);
+        this.notifyChanged();
+    }
+
+    /**
+     * Removes a user property.
+     * 
+     * @param userProperty the user property
+     */
+    public void removeUserProperty(PDUserProperty userProperty)
+    {
+        if (userProperty == null)
+        {
+            return;
+        }
+        COSArray p = (COSArray) this.getCOSDictionary()
+            .getDictionaryObject(COSName.P);
+        p.remove(userProperty.getCOSObject());
+        this.notifyChanged();
+    }
+
+    public void userPropertyChanged(PDUserProperty userProperty)
+    {
+        
+    }
+
+    @Override
+    public String toString()
+    {
+        return new StringBuilder().append(super.toString())
+            .append(", userProperties=")
+            .append(this.getUserProperties()).toString();
+    }
+
+}

Added: 
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/documentinterchange/logicalstructure/PDUserProperty.java
URL: 
http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/documentinterchange/logicalstructure/PDUserProperty.java?rev=926594&view=auto
==============================================================================
--- 
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/documentinterchange/logicalstructure/PDUserProperty.java
 (added)
+++ 
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/documentinterchange/logicalstructure/PDUserProperty.java
 Tue Mar 23 14:34:10 2010
@@ -0,0 +1,191 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.pdfbox.pdmodel.documentinterchange.logicalstructure;
+
+import org.apache.pdfbox.cos.COSBase;
+import org.apache.pdfbox.cos.COSDictionary;
+import org.apache.pdfbox.cos.COSName;
+import org.apache.pdfbox.pdmodel.common.PDDictionaryWrapper;
+
+/**
+ * A user property.
+ * 
+ * @author <a href="mailto:johannes%20koch%20%[email protected]%3e";>Johannes 
Koch</a>
+ * @version $Revision: $
+ */
+public class PDUserProperty extends PDDictionaryWrapper
+{
+
+    private final PDUserAttributeObject userAttributeObject;
+
+    /**
+     * Creates a new user property.
+     * 
+     * @param the user attribute object
+     */
+    public PDUserProperty(PDUserAttributeObject userAttributeObject)
+    {
+        this.userAttributeObject = userAttributeObject;
+    }
+
+    /**
+     * Creates a user property with a given dictionary.
+     * 
+     * @param dictionary the dictionary
+     * @param the user attribute object
+     */
+    public PDUserProperty(COSDictionary dictionary,
+        PDUserAttributeObject userAttributeObject)
+    {
+        super(dictionary);
+        this.userAttributeObject = userAttributeObject;
+    }
+
+
+    /**
+     * Returns the property name.
+     * 
+     * @return the property name
+     */
+    public String getName()
+    {
+        return this.getCOSDictionary().getNameAsString(COSName.N);
+    }
+
+    /**
+     * Sets the property name.
+     * 
+     * @param name the property name
+     */
+    public void setName(String name)
+    {
+        this.potentiallyNotifyChanged(this.getName(), name);
+        this.getCOSDictionary().setName(COSName.N, name);
+    }
+
+    /**
+     * Returns the property value.
+     * 
+     * @return the property value
+     */
+    public COSBase getValue()
+    {
+        return this.getCOSDictionary().getDictionaryObject(COSName.V);
+    }
+
+    /**
+     * Sets the property value.
+     * 
+     * @param value the property value
+     */
+    public void setValue(COSBase value)
+    {
+        this.potentiallyNotifyChanged(this.getValue(), value);
+        this.getCOSDictionary().setItem(COSName.V, value);
+    }
+
+    /**
+     * Returns the string for the property value.
+     * 
+     * @return the string for the property value
+     */
+    public String getFormattedValue()
+    {
+        return this.getCOSDictionary().getString(COSName.F);
+    }
+
+    /**
+     * Sets the string for the property value.
+     * 
+     * @param formattedValue the string for the property value
+     */
+    public void setFormattedValue(String formattedValue)
+    {
+        this.potentiallyNotifyChanged(this.getFormattedValue(), 
formattedValue);
+        this.getCOSDictionary().setString(COSName.F, formattedValue);
+    }
+
+    /**
+     * Shall the property be hidden?
+     * 
+     * @return <code>true</code> if the property shall be hidden,
+     * <code>false</code> otherwise
+     */
+    public boolean isHidden()
+    {
+        return this.getCOSDictionary().getBoolean(COSName.H, false);
+    }
+
+    /**
+     * Specifies whether the property shall be hidden.
+     * 
+     * @param hidden <code>true</code> if the property shall be hidden,
+     * <code>false</code> otherwise
+     */
+    public void setHidden(boolean hidden)
+    {
+        this.potentiallyNotifyChanged(this.isHidden(), hidden);
+        this.getCOSDictionary().setBoolean(COSName.H, hidden);
+    }
+
+
+    @Override
+    public String toString()
+    {
+        return new StringBuilder("Name=").append(this.getName())
+            .append(", Value=").append(this.getValue())
+            .append(", FormattedValue=").append(this.getFormattedValue())
+            .append(", Hidden=").append(this.isHidden()).toString();
+    }
+
+
+    /**
+     * Notifies the user attribute object if the user property is changed.
+     * 
+     * @param oldEntry old entry
+     * @param newEntry new entry
+     */
+    private void potentiallyNotifyChanged(Object oldEntry, Object newEntry)
+    {
+        if (this.isEntryChanged(oldEntry, newEntry))
+        {
+            this.userAttributeObject.userPropertyChanged(this);
+        }
+    }
+
+    /**
+     * Is the value changed?
+     * 
+     * @param oldEntry old entry
+     * @param newEntry new entry
+     * @return <code>true</code> if the entry is changed, <code>false</code>
+     * otherwise
+     */
+    private boolean isEntryChanged(Object oldEntry, Object newEntry)
+    {
+        if (oldEntry == null)
+        {
+            if (newEntry == null)
+            {
+                return false;
+            }
+            return true;
+        }
+        return !oldEntry.equals(newEntry);
+    }
+
+}


Reply via email to