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: Metadata.java 426584 2006-07-28 16:01:47Z jeremias $ */
019:
020: package org.apache.xmlgraphics.xmp;
021:
022: import java.util.Iterator;
023: import java.util.Map;
024: import java.util.Set;
025:
026: import org.apache.xmlgraphics.util.QName;
027: import org.apache.xmlgraphics.util.XMLizable;
028: import org.apache.xmlgraphics.xmp.merge.MergeRuleSet;
029: import org.apache.xmlgraphics.xmp.merge.PropertyMerger;
030: import org.xml.sax.ContentHandler;
031: import org.xml.sax.SAXException;
032: import org.xml.sax.helpers.AttributesImpl;
033:
034: /**
035: * This class represents the root of an XMP metadata tree. It's more or less equivalent to the
036: * x:xmpmeta element together with its nested rdf:RDF element.
037: */
038: public class Metadata implements XMLizable {
039:
040: private Map properties = new java.util.HashMap();
041:
042: /**
043: * Sets a property.
044: * @param prop the property
045: */
046: public void setProperty(XMPProperty prop) {
047: properties.put(prop.getName(), prop);
048: }
049:
050: /**
051: * Returns a property
052: * @param uri the namespace URI of the property
053: * @param localName the local name of the property
054: * @return the requested property or null if it's not available
055: */
056: public XMPProperty getProperty(String uri, String localName) {
057: return getProperty(new QName(uri, localName));
058: }
059:
060: /**
061: * Returns a property.
062: * @param name the name of the property
063: * @return the requested property or null if it's not available
064: */
065: public XMPProperty getProperty(QName name) {
066: XMPProperty prop = (XMPProperty) properties.get(name);
067: return prop;
068: }
069:
070: /** @return the number of properties in this metadata object. */
071: public int getPropertyCount() {
072: return this .properties.size();
073: }
074:
075: /**
076: * Merges this metadata object into a given target metadata object. The merge rule set provided
077: * by each schema is used for the merge.
078: * @param target the target metadata to merge the local metadata into
079: */
080: public void mergeInto(Metadata target) {
081: XMPSchemaRegistry registry = XMPSchemaRegistry.getInstance();
082: Iterator iter = properties.values().iterator();
083: while (iter.hasNext()) {
084: XMPProperty prop = (XMPProperty) iter.next();
085: XMPSchema schema = registry.getSchema(prop.getNamespace());
086: MergeRuleSet rules = schema.getDefaultMergeRuleSet();
087: PropertyMerger merger = rules.getPropertyMergerFor(prop);
088: merger.merge(prop, target);
089: }
090: }
091:
092: /** @see org.apache.xmlgraphics.util.XMLizable#toSAX(org.xml.sax.ContentHandler) */
093: public void toSAX(ContentHandler handler) throws SAXException {
094: AttributesImpl atts = new AttributesImpl();
095: handler.startElement(XMPConstants.XMP_NAMESPACE, "xmpmeta",
096: "x:xmpmeta", atts);
097: handler.startElement(XMPConstants.RDF_NAMESPACE, "RDF",
098: "rdf:RDF", atts);
099: //Get all property namespaces
100: Set namespaces = new java.util.HashSet();
101: Iterator iter = properties.keySet().iterator();
102: while (iter.hasNext()) {
103: namespaces.add(((QName) iter.next()).getNamespaceURI());
104: }
105: //One Description element per namespace
106: iter = namespaces.iterator();
107: while (iter.hasNext()) {
108: String ns = (String) iter.next();
109: XMPSchema schema = XMPSchemaRegistry.getInstance()
110: .getSchema(ns);
111: String prefix = (schema != null ? schema
112: .getPreferredPrefix() : null);
113: if (prefix != null) {
114: handler.startPrefixMapping(prefix, ns);
115: }
116:
117: atts.clear();
118: atts.addAttribute(XMPConstants.RDF_NAMESPACE, "about",
119: "rdf:about", "CDATA", "");
120: handler.startElement(XMPConstants.RDF_NAMESPACE, "RDF",
121: "rdf:Description", atts);
122:
123: Iterator props = properties.values().iterator();
124: while (props.hasNext()) {
125: XMPProperty prop = (XMPProperty) props.next();
126: if (prop.getName().getNamespaceURI().equals(ns)) {
127: prop.toSAX(handler);
128: }
129: }
130: handler.endElement(XMPConstants.RDF_NAMESPACE, "RDF",
131: "rdf:Description");
132: if (prefix != null) {
133: handler.endPrefixMapping(prefix);
134: }
135: }
136:
137: handler
138: .endElement(XMPConstants.RDF_NAMESPACE, "RDF",
139: "rdf:RDF");
140: handler.endElement(XMPConstants.XMP_NAMESPACE, "xmpmeta",
141: "x:xmpmeta");
142: }
143: }
|