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: XMPArray.java 426584 2006-07-28 16:01:47Z jeremias $ */
019:
020: package org.apache.xmlgraphics.xmp;
021:
022: import java.util.List;
023:
024: import org.xml.sax.ContentHandler;
025: import org.xml.sax.SAXException;
026: import org.xml.sax.helpers.AttributesImpl;
027:
028: /**
029: * Represents an XMP array as defined by the XMP specification.
030: * @todo Property qualifiers are currently not supported, yet.
031: */
032: public class XMPArray extends XMPComplexValue {
033:
034: private XMPArrayType type;
035: private List values = new java.util.ArrayList();
036: private List xmllang = new java.util.ArrayList();
037:
038: /**
039: * Main constructor
040: * @param type the intended type of array
041: */
042: public XMPArray(XMPArrayType type) {
043: this .type = type;
044: }
045:
046: /** @return the type of array */
047: public XMPArrayType getType() {
048: return this .type;
049: }
050:
051: /**
052: * Returns the value at a given position
053: * @param idx the index of the requested value
054: * @return the value at the given position
055: */
056: public Object getValue(int idx) {
057: return this .values.get(idx);
058: }
059:
060: /** @see org.apache.xmlgraphics.xmp.XMPComplexValue#getSimpleValue() */
061: public Object getSimpleValue() {
062: if (values.size() == 1) {
063: return getValue(0);
064: } else {
065: return null;
066: }
067: }
068:
069: /**
070: * Returns a language-dependant values (available for alternative arrays).
071: * @param lang the language ("x-default" for the default value)
072: * @return the requested value
073: */
074: public String getLangValue(String lang) {
075: String v = null;
076: for (int i = 0, c = values.size(); i < c; i++) {
077: String l = (String) xmllang.get(i);
078: if ((l == null && lang == null)
079: || (l != null && l.equals(lang))) {
080: v = values.get(i).toString();
081: break;
082: }
083: }
084: if (lang == null && v == null) {
085: v = getLangValue(XMPConstants.DEFAULT_LANGUAGE);
086: if (v == null && values.size() > 0) {
087: v = getValue(0).toString(); //get first
088: }
089: }
090: return v;
091: }
092:
093: /**
094: * Removes a language-dependant value
095: * @param lang the language ("x-default" for the default value)
096: */
097: public void removeLangValue(String lang) {
098: if (lang == null && "".equals(lang)) {
099: return;
100: }
101: for (int i = 0, c = values.size(); i < c; i++) {
102: String l = (String) xmllang.get(i);
103: if (lang.equals(l)) {
104: values.remove(i);
105: xmllang.remove(i);
106: return;
107: }
108: }
109: }
110:
111: /**
112: * Adds a new value to the array
113: * @param value the value
114: */
115: public void add(Object value) {
116: values.add(value);
117: xmllang.add(null);
118: }
119:
120: /**
121: * Adds a language-dependant value to the array. Make sure not to add the same language twice.
122: * @param value the value
123: * @param lang the language ("x-default" for the default value)
124: */
125: public void add(String value, String lang) {
126: values.add(value);
127: xmllang.add(lang);
128: }
129:
130: /** @return the current number of value in the array */
131: public int getSize() {
132: return this .values.size();
133: }
134:
135: /**
136: * Converts the array to an object array.
137: * @return an object array of all values in the array
138: */
139: public Object[] toObjectArray() {
140: Object[] res = new Object[getSize()];
141: for (int i = 0, c = res.length; i < c; i++) {
142: res[i] = getValue(i);
143: }
144: return res;
145: }
146:
147: /** @see org.apache.xmlgraphics.util.XMLizable#toSAX(org.xml.sax.ContentHandler) */
148: public void toSAX(ContentHandler handler) throws SAXException {
149: AttributesImpl atts = new AttributesImpl();
150: handler.startElement(XMPConstants.RDF_NAMESPACE,
151: type.getName(), "rdf:" + type.getName(), atts);
152: for (int i = 0, c = values.size(); i < c; i++) {
153: String value = (String) values.get(i);
154: String lang = (String) xmllang.get(i);
155: atts.clear();
156: if (lang != null) {
157: atts.addAttribute(XMPConstants.XML_NS, "lang",
158: "xml:lang", "CDATA", lang);
159: }
160: handler.startElement(XMPConstants.RDF_NAMESPACE, "li",
161: "rdf:li", atts);
162: char[] chars = value.toCharArray();
163: handler.characters(chars, 0, chars.length);
164: handler.endElement(XMPConstants.RDF_NAMESPACE, "li",
165: "rdf:li");
166: }
167: handler.endElement(XMPConstants.RDF_NAMESPACE, type.getName(),
168: "rdf:" + type.getName());
169: }
170:
171: /** @see java.lang.Object#toString() */
172: public String toString() {
173: return "XMP array: " + type + ", " + getSize();
174: }
175:
176: }
|