~hjd/ubuntu/wily/xmlgraphics-commons/debian-merged

« back to all changes in this revision

Viewing changes to .pc/xml-rdf-resource.patch/src/java/org/apache/xmlgraphics/xmp/XMPProperty.java

  • Committer: Bazaar Package Importer
  • Author(s): Vincent Fourmond
  • Date: 2011-02-11 14:15:14 UTC
  • mfrom: (8.1.2 experimental)
  • Revision ID: james.westby@ubuntu.com-20110211141514-h67achft6x31gju1
Tags: 1.4.dfsg-3
Uploading to unstable, hoping we won't break too many things ;-)...

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
18
/* $Id: XMPProperty.java 892977 2009-12-21 21:08:42Z jeremias $ */
 
19
 
 
20
package org.apache.xmlgraphics.xmp;
 
21
 
 
22
import java.util.Iterator;
 
23
import java.util.Map;
 
24
 
 
25
import org.xml.sax.ContentHandler;
 
26
import org.xml.sax.SAXException;
 
27
import org.xml.sax.helpers.AttributesImpl;
 
28
 
 
29
import org.apache.xmlgraphics.util.QName;
 
30
import org.apache.xmlgraphics.util.XMLizable;
 
31
 
 
32
/**
 
33
 * This class is the base class for all XMP properties.
 
34
 */
 
35
public class XMPProperty implements XMLizable {
 
36
 
 
37
    private QName name;
 
38
    private Object value;
 
39
    private String xmllang;
 
40
    private Map qualifiers;
 
41
 
 
42
    /**
 
43
     * Creates a new XMP property.
 
44
     * @param name the name of the property
 
45
     * @param value the value for the property
 
46
     */
 
47
    public XMPProperty(QName name, Object value) {
 
48
        this.name = name;
 
49
        this.value = value;
 
50
    }
 
51
 
 
52
    /** @return the qualified name of the property (namespace URI + local name) */
 
53
    public QName getName() {
 
54
        return this.name;
 
55
    }
 
56
 
 
57
    /** @return the namespace URI of the property */
 
58
    public String getNamespace() {
 
59
        return getName().getNamespaceURI();
 
60
    }
 
61
 
 
62
    /**
 
63
     * Sets the value of the property
 
64
     * @param value the new value
 
65
     */
 
66
    public void setValue(Object value) {
 
67
        this.value = value;
 
68
    }
 
69
 
 
70
    /**
 
71
     * @return the property value (can be a normal Java object (normally a String) or a descendant
 
72
     *         of XMPComplexValue.
 
73
     */
 
74
    public Object getValue() {
 
75
        return this.value;
 
76
    }
 
77
 
 
78
    /**
 
79
     * Sets the xml:lang value for this property
 
80
     * @param lang the language ("x-default" for the default language, null to make the value
 
81
     *             language-independent)
 
82
     */
 
83
    public void setXMLLang(String lang) {
 
84
        this.xmllang = lang;
 
85
    }
 
86
 
 
87
    /**
 
88
     * @return the language for language-dependent values ("x-default" for the default language)
 
89
     */
 
90
    public String getXMLLang() {
 
91
        return this.xmllang;
 
92
    }
 
93
 
 
94
    /**
 
95
     * Indicates whether the property is an array.
 
96
     * @return true if the property is an array
 
97
     */
 
98
    public boolean isArray() {
 
99
        return value instanceof XMPArray;
 
100
    }
 
101
 
 
102
    /** @return the XMPArray for an array or null if the value is not an array. */
 
103
    public XMPArray getArrayValue() {
 
104
        return (isArray() ? (XMPArray)value : null);
 
105
    }
 
106
 
 
107
    /**
 
108
     * Converts a simple value to an array of a given type if the value is not already an array.
 
109
     * @param type the desired type of array
 
110
     * @return the array value
 
111
     */
 
112
    public XMPArray convertSimpleValueToArray(XMPArrayType type) {
 
113
        if (getArrayValue() == null) {
 
114
            XMPArray array = new XMPArray(type);
 
115
            if (getXMLLang() != null) {
 
116
                array.add(getValue().toString(), getXMLLang());
 
117
            } else {
 
118
                array.add(getValue());
 
119
            }
 
120
            setValue(array);
 
121
            setXMLLang(null);
 
122
            return array;
 
123
        } else {
 
124
            return getArrayValue();
 
125
        }
 
126
    }
 
127
 
 
128
    /** @return the XMPStructure for a structure or null if the value is not a structure. */
 
129
    public PropertyAccess getStructureValue() {
 
130
        return (value instanceof XMPStructure ? (XMPStructure)value : null);
 
131
    }
 
132
 
 
133
    private boolean hasPropertyQualifiers() {
 
134
        return (this.qualifiers == null) || (this.qualifiers.size() == 0);
 
135
    }
 
136
 
 
137
    /**
 
138
     * Indicates whether this property is actually not a structure, but a normal property with
 
139
     * property qualifiers. If this method returns true, this structure can be converted to
 
140
     * an simple XMPProperty using the simplify() method.
 
141
     * @return true if this property is a structure property with property qualifiers
 
142
     */
 
143
    public boolean isQualifiedProperty() {
 
144
        PropertyAccess props = getStructureValue();
 
145
        if (props != null) {
 
146
            XMPProperty rdfValue = props.getValueProperty();
 
147
            return (rdfValue != null);
 
148
        } else {
 
149
            return hasPropertyQualifiers();
 
150
        }
 
151
    }
 
152
 
 
153
    public void simplify() {
 
154
        PropertyAccess props = getStructureValue();
 
155
        if (props != null) {
 
156
            XMPProperty rdfValue = props.getValueProperty();
 
157
            if (rdfValue != null) {
 
158
                if (hasPropertyQualifiers()) {
 
159
                    throw new IllegalStateException("Illegal internal state"
 
160
                            + " (qualifiers present on non-simplified property)");
 
161
                }
 
162
                XMPProperty prop = new XMPProperty(getName(), rdfValue);
 
163
                Iterator iter = props.iterator();
 
164
                while (iter.hasNext()) {
 
165
                    QName name = (QName)iter.next();
 
166
                    if (!XMPConstants.RDF_VALUE.equals(name)) {
 
167
                        prop.setPropertyQualifier(name, props.getProperty(name));
 
168
                    }
 
169
                }
 
170
            }
 
171
        }
 
172
    }
 
173
 
 
174
 
 
175
    private void setPropertyQualifier(QName name, XMPProperty property) {
 
176
        if (this.qualifiers == null) {
 
177
            this.qualifiers = new java.util.HashMap();
 
178
        }
 
179
        this.qualifiers.put(name, property);
 
180
    }
 
181
 
 
182
    private String getEffectiveQName() {
 
183
        String prefix = getName().getPrefix();
 
184
        if (prefix == null || "".equals(prefix)) {
 
185
            XMPSchema schema = XMPSchemaRegistry.getInstance().getSchema(getNamespace());
 
186
            prefix = schema.getPreferredPrefix();
 
187
        }
 
188
        return prefix + ":" + getName().getLocalName();
 
189
    }
 
190
 
 
191
    /** @see org.apache.xmlgraphics.util.XMLizable#toSAX(org.xml.sax.ContentHandler) */
 
192
    public void toSAX(ContentHandler handler) throws SAXException {
 
193
        AttributesImpl atts = new AttributesImpl();
 
194
        String qName = getEffectiveQName();
 
195
        handler.startElement(getName().getNamespaceURI(),
 
196
                getName().getLocalName(), qName, atts);
 
197
        if (value instanceof XMPComplexValue) {
 
198
            XMPComplexValue cv = ((XMPComplexValue)value);
 
199
            cv.toSAX(handler);
 
200
        } else {
 
201
            char[] chars = value.toString().toCharArray();
 
202
            handler.characters(chars, 0, chars.length);
 
203
        }
 
204
        handler.endElement(getName().getNamespaceURI(),
 
205
                getName().getLocalName(), qName);
 
206
    }
 
207
 
 
208
    /** @see java.lang.Object#toString() */
 
209
    public String toString() {
 
210
        StringBuffer sb = new StringBuffer("XMP Property ");
 
211
        sb.append(getName()).append(": ");
 
212
        sb.append(getValue());
 
213
        return sb.toString();
 
214
    }
 
215
 
 
216
}