package org.apache.xmlgraphics.xmp;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;
import org.apache.xmlgraphics.util.QName;
import org.apache.xmlgraphics.util.XMLizable;
import org.apache.xmlgraphics.xmp.merge.MergeRuleSet;
import org.apache.xmlgraphics.xmp.merge.PropertyMerger;
public class Metadata implements XMLizable, PropertyAccess {
private Map properties = new java.util.HashMap();
public void setProperty(XMPProperty prop) {
properties.put(prop.getName(), prop);
}
public XMPProperty getProperty(String uri, String localName) {
return getProperty(new QName(uri, localName));
}
public XMPProperty getProperty(QName name) {
XMPProperty prop = (XMPProperty)properties.get(name);
return prop;
}
public XMPProperty removeProperty(QName name) {
return (XMPProperty)properties.remove(name);
}
public XMPProperty getValueProperty() {
return getProperty(XMPConstants.RDF_VALUE);
}
public int getPropertyCount() {
return this.properties.size();
}
public Iterator iterator() {
return this.properties.keySet().iterator();
}
public void mergeInto(Metadata target, List<Class> exclude) {
XMPSchemaRegistry registry = XMPSchemaRegistry.getInstance();
for (Object o : properties.values()) {
XMPProperty prop = (XMPProperty) o;
XMPSchema schema = registry.getSchema(prop.getNamespace());
if (!exclude.contains(schema.getClass())) {
MergeRuleSet rules = schema.getDefaultMergeRuleSet();
PropertyMerger merger = rules.getPropertyMergerFor(prop);
merger.merge(prop, target);
}
}
}
public void toSAX(ContentHandler handler) throws SAXException {
AttributesImpl atts = new AttributesImpl();
handler.startPrefixMapping("x", XMPConstants.XMP_NAMESPACE);
handler.startElement(XMPConstants.XMP_NAMESPACE, "xmpmeta", "x:xmpmeta", atts);
handler.startPrefixMapping("rdf", XMPConstants.RDF_NAMESPACE);
handler.startElement(XMPConstants.RDF_NAMESPACE, "RDF", "rdf:RDF", atts);
Set namespaces = new java.util.HashSet();
Iterator iter = properties.keySet().iterator();
while (iter.hasNext()) {
QName n = ((QName)iter.next());
namespaces.add(n.getNamespaceURI());
}
iter = namespaces.iterator();
while (iter.hasNext()) {
String ns = (String)iter.next();
XMPSchema schema = XMPSchemaRegistry.getInstance().getSchema(ns);
String prefix = (schema != null ? schema.getPreferredPrefix() : null);
boolean first = true;
boolean empty = true;
for (Object o : properties.values()) {
XMPProperty prop = (XMPProperty) o;
if (prop.getName().getNamespaceURI().equals(ns)) {
if (first) {
if (prefix == null) {
prefix = prop.getName().getPrefix();
}
atts.clear();
atts.addAttribute(XMPConstants.RDF_NAMESPACE,
"about", "rdf:about", "CDATA", "");
if (prefix != null) {
handler.startPrefixMapping(prefix, ns);
}
handler.startElement(XMPConstants.RDF_NAMESPACE,
"Description", "rdf:Description", atts);
empty = false;
first = false;
}
prop.toSAX(handler);
}
}
if (!empty) {
handler.endElement(XMPConstants.RDF_NAMESPACE, "Description", "rdf:Description");
if (prefix != null) {
handler.endPrefixMapping(prefix);
}
}
}
handler.endElement(XMPConstants.RDF_NAMESPACE, "RDF", "rdf:RDF");
handler.endPrefixMapping("rdf");
handler.endElement(XMPConstants.XMP_NAMESPACE, "xmpmeta", "x:xmpmeta");
handler.endPrefixMapping("x");
}
}