001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /* $Id: XMPProperty.java 426584 2006-07-28 16:01:47Z jeremias $ */
019:
020: package org.apache.xmlgraphics.xmp;
021:
022: import org.apache.xmlgraphics.util.QName;
023: import org.apache.xmlgraphics.util.XMLizable;
024: import org.xml.sax.ContentHandler;
025: import org.xml.sax.SAXException;
026: import org.xml.sax.helpers.AttributesImpl;
027:
028: /**
029: * This class is the base class for all XMP properties.
030: */
031: public class XMPProperty implements XMLizable {
032:
033: private QName name;
034: private Object value;
035: private String xmllang;
036:
037: /**
038: * Creates a new XMP property.
039: * @param name the name of the property
040: * @param value the value for the property
041: */
042: public XMPProperty(QName name, Object value) {
043: this .name = name;
044: this .value = value;
045: }
046:
047: /** @return the qualified name of the property (namespace URI + local name) */
048: public QName getName() {
049: return this .name;
050: }
051:
052: /** @return the namespace URI of the property */
053: public String getNamespace() {
054: return getName().getNamespaceURI();
055: }
056:
057: /**
058: * Sets the value of the property
059: * @param value the new value
060: */
061: public void setValue(Object value) {
062: this .value = value;
063: }
064:
065: /**
066: * @return the property value (can be a normal Java object (normally a String) or a descendant
067: * of XMPComplexValue.
068: */
069: public Object getValue() {
070: return this .value;
071: }
072:
073: /**
074: * Sets the xml:lang value for this property
075: * @param lang the language ("x-default" for the default language, null to make the value
076: * language-independent)
077: */
078: public void setXMLLang(String lang) {
079: this .xmllang = lang;
080: }
081:
082: /**
083: * @return the language for language-dependent values ("x-default" for the default language)
084: */
085: public String getXMLLang() {
086: return this .xmllang;
087: }
088:
089: /** @return the XMPArray for an array or null if the value is not an array. */
090: public XMPArray getArrayValue() {
091: return (value instanceof XMPArray ? (XMPArray) value : null);
092: }
093:
094: /**
095: * Converts a simple value to an array of a given type if the value is not already an array.
096: * @param type the desired type of array
097: */
098: public void convertSimpleValueToArray(XMPArrayType type) {
099: if (getArrayValue() == null) {
100: XMPArray array = new XMPArray(type);
101: if (getXMLLang() != null) {
102: array.add(getValue().toString(), getXMLLang());
103: } else {
104: array.add(getValue());
105: }
106: setValue(array);
107: setXMLLang(null);
108: }
109: }
110:
111: private String getEffectiveQName() {
112: String prefix = getName().getPrefix();
113: if (prefix == null || "".equals(prefix)) {
114: XMPSchema schema = XMPSchemaRegistry.getInstance()
115: .getSchema(getNamespace());
116: prefix = schema.getPreferredPrefix();
117: }
118: return prefix + ":" + getName().getLocalName();
119: }
120:
121: /** @see org.apache.xmlgraphics.util.XMLizable#toSAX(org.xml.sax.ContentHandler) */
122: public void toSAX(ContentHandler handler) throws SAXException {
123: AttributesImpl atts = new AttributesImpl();
124: String qName = getEffectiveQName();
125: handler.startElement(getName().getNamespaceURI(), getName()
126: .getLocalName(), qName, atts);
127: if (value instanceof XMPComplexValue) {
128: XMPComplexValue cv = ((XMPComplexValue) value);
129: Object obj = cv.getSimpleValue();
130: if (obj != null) {
131: char[] chars = obj.toString().toCharArray();
132: handler.characters(chars, 0, chars.length);
133: } else {
134: cv.toSAX(handler);
135: }
136: } else {
137: char[] chars = value.toString().toCharArray();
138: handler.characters(chars, 0, chars.length);
139: }
140: handler.endElement(getName().getNamespaceURI(), getName()
141: .getLocalName(), qName);
142: }
143: }
|