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
9
* http://www.apache.org/licenses/LICENSE-2.0
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.
18
/* $Id: XMPProperty.java 892977 2009-12-21 21:08:42Z jeremias $ */
20
package org.apache.xmlgraphics.xmp;
22
import java.util.Iterator;
25
import org.xml.sax.ContentHandler;
26
import org.xml.sax.SAXException;
27
import org.xml.sax.helpers.AttributesImpl;
29
import org.apache.xmlgraphics.util.QName;
30
import org.apache.xmlgraphics.util.XMLizable;
33
* This class is the base class for all XMP properties.
35
public class XMPProperty implements XMLizable {
39
private String xmllang;
40
private Map qualifiers;
43
* Creates a new XMP property.
44
* @param name the name of the property
45
* @param value the value for the property
47
public XMPProperty(QName name, Object value) {
52
/** @return the qualified name of the property (namespace URI + local name) */
53
public QName getName() {
57
/** @return the namespace URI of the property */
58
public String getNamespace() {
59
return getName().getNamespaceURI();
63
* Sets the value of the property
64
* @param value the new value
66
public void setValue(Object value) {
71
* @return the property value (can be a normal Java object (normally a String) or a descendant
74
public Object getValue() {
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)
83
public void setXMLLang(String lang) {
88
* @return the language for language-dependent values ("x-default" for the default language)
90
public String getXMLLang() {
95
* Indicates whether the property is an array.
96
* @return true if the property is an array
98
public boolean isArray() {
99
return value instanceof XMPArray;
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);
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
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());
118
array.add(getValue());
124
return getArrayValue();
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);
133
private boolean hasPropertyQualifiers() {
134
return (this.qualifiers == null) || (this.qualifiers.size() == 0);
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
143
public boolean isQualifiedProperty() {
144
PropertyAccess props = getStructureValue();
146
XMPProperty rdfValue = props.getValueProperty();
147
return (rdfValue != null);
149
return hasPropertyQualifiers();
153
public void simplify() {
154
PropertyAccess props = getStructureValue();
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)");
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));
175
private void setPropertyQualifier(QName name, XMPProperty property) {
176
if (this.qualifiers == null) {
177
this.qualifiers = new java.util.HashMap();
179
this.qualifiers.put(name, property);
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();
188
return prefix + ":" + getName().getLocalName();
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);
201
char[] chars = value.toString().toCharArray();
202
handler.characters(chars, 0, chars.length);
204
handler.endElement(getName().getNamespaceURI(),
205
getName().getLocalName(), qName);
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();