PropertyDescriptionImpl.java 6.7 KB
/*
 * Decompiled with CFR 0_118.
 */
package com.adobe.xmp.schema.model.impl;

import com.adobe.xmp.schema.model.ArrayType;
import com.adobe.xmp.schema.model.PropertyDescription;
import com.adobe.xmp.schema.model.PropertyType;
import com.adobe.xmp.schema.model.SchemaVisitor;
import com.adobe.xmp.schema.model.UnspecifiedType;
import com.adobe.xmp.schema.model.XMPSchemaException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import javax.xml.namespace.QName;

/*
 * This class specifies class file version 49.0 but uses Java 6 signatures.  Assumed Java 6.
 */
public class PropertyDescriptionImpl
implements PropertyDescription {
    private String namespaceURI;
    private String name;
    private PropertyType type;
    private ArrayList<PropertyDescription> qualifiers = new ArrayList();
    private boolean mandatory;
    private boolean deprecated;
    private String label;
    private String description;
    private boolean readOnly;
    private Map<QName, Map<String, String>> decoratorStore;

    PropertyDescriptionImpl(String name, PropertyType type) {
        this.name = name;
        this.type = type;
    }

    @Override
    public String getNamespaceURI() {
        return this.namespaceURI;
    }

    @Override
    public void setNamespaceURI(String namespaceURI) {
        this.namespaceURI = namespaceURI;
    }

    @Override
    public String getName() {
        return this.name;
    }

    @Override
    public PropertyType getType() {
        return this.type;
    }

    @Override
    public void replaceType(PropertyType finalType) {
        if (this.type instanceof UnspecifiedType) {
            this.type = finalType;
        } else if (this.type instanceof ArrayType && ((ArrayType)this.type).getItemType() instanceof UnspecifiedType) {
            ((ArrayType)this.type).setItemType(finalType);
        }
    }

    @Override
    public void addQualifier(PropertyDescription qualifier) {
        this.qualifiers.add(qualifier);
    }

    @Override
    public boolean hasQualifiers() {
        return this.qualifiers != null && !this.qualifiers.isEmpty();
    }

    @Override
    public PropertyDescription getQualifier(String namespaceURI, String localName) {
        for (PropertyDescription qualifier : this.qualifiers) {
            if (!qualifier.getName().equals(localName) || !qualifier.getNamespaceURI().equals(namespaceURI)) continue;
            return qualifier;
        }
        return null;
    }

    @Override
    public List<PropertyDescription> getQualifiers() {
        return this.qualifiers;
    }

    @Override
    public void removeQualifier(String namespaceURI, String localName) {
        Iterator<PropertyDescription> it = this.qualifiers.iterator();
        while (it.hasNext()) {
            PropertyDescription qualifier = it.next();
            if (!qualifier.getName().equals(localName) || !qualifier.getNamespaceURI().equals(namespaceURI)) continue;
            it.remove();
        }
    }

    @Override
    public void setMandatory(boolean mandatory) {
        this.mandatory = mandatory;
    }

    @Override
    public boolean isMandatory() {
        return this.mandatory;
    }

    @Override
    public boolean isDeprecated() {
        return this.deprecated;
    }

    @Override
    public void setDeprecated(boolean deprecated) {
        this.deprecated = deprecated;
    }

    @Override
    public boolean isReadOnly() {
        return this.readOnly;
    }

    @Override
    public void setReadOnly(boolean readOnly) {
        this.readOnly = readOnly;
    }

    @Override
    public String getLabel() {
        return this.label;
    }

    @Override
    public void setLabel(String label) {
        this.label = label;
    }

    @Override
    public String getDescription() {
        return this.description;
    }

    @Override
    public void setDescription(String description) {
        this.description = description;
    }

    @Override
    public void accept(SchemaVisitor schemaVisitor) throws XMPSchemaException {
        schemaVisitor.visit(this);
    }

    @Override
    public Map<String, String> getDecorator(String namespaceURI, String localName) {
        QName qname = new QName(namespaceURI, localName);
        Map<QName, Map<String, String>> decorators = this.getDecoratorStore();
        Map<String, String> propertyValues = decorators.get(qname);
        if (propertyValues == null) {
            propertyValues = new TreeMap<String, String>();
            decorators.put(qname, propertyValues);
        }
        return propertyValues;
    }

    @Override
    public boolean hasDecorator(String namespaceURI, String localName) {
        if (this.decoratorStore != null) {
            QName qname = new QName(namespaceURI, localName);
            Map<QName, Map<String, String>> decorators = this.getDecoratorStore();
            if (decorators.containsKey(qname)) {
                return decorators.get(qname).size() > 0;
            }
        }
        return false;
    }

    @Override
    public boolean hasDecorators() {
        return this.decoratorStore != null && this.getDecoratorStore().size() > 0;
    }

    @Override
    public Set<QName> getDecoratorSet() {
        if (this.decoratorStore != null) {
            Iterator<Map.Entry<QName, Map<String, String>>> it = this.decoratorStore.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry<QName, Map<String, String>> entry = it.next();
                if (entry.getValue() != null && entry.getValue().size() != 0) continue;
                it.remove();
            }
            return this.decoratorStore.keySet();
        }
        return this.createSortedMap().keySet();
    }

    @Override
    public void removeDecoratorNS(String namespaceURI) {
        Iterator<QName> it = this.getDecoratorSet().iterator();
        while (it.hasNext()) {
            QName qname = it.next();
            if (!qname.getNamespaceURI().equals(namespaceURI)) continue;
            it.remove();
        }
    }

    private Map<QName, Map<String, String>> getDecoratorStore() {
        if (this.decoratorStore == null) {
            this.decoratorStore = this.createSortedMap();
        }
        return this.decoratorStore;
    }

    private Map<QName, Map<String, String>> createSortedMap() {
        return new TreeMap<QName, Map<String, String>>(new Comparator<QName>(){

            @Override
            public int compare(QName q1, QName q2) {
                int result = q1.getNamespaceURI().compareTo(q2.getNamespaceURI());
                if (result == 0) {
                    result = q1.getLocalPart().compareTo(q2.getLocalPart());
                }
                return result;
            }
        });
    }

}