Search in sources :

Example 6 with PropertyDefinition

use of eu.esdihumboldt.hale.common.schema.model.PropertyDefinition in project hale by halestudio.

the class XmlSchemaReaderTest method testRead_definitive_choice.

/**
 * Test reading a simple XML schema with choices and complex types.
 *
 * @throws Exception if reading the schema fails
 */
@Test
public void testRead_definitive_choice() throws Exception {
    URI location = getClass().getResource("/testdata/definitive/choice_complex.xsd").toURI();
    LocatableInputSupplier<? extends InputStream> input = new DefaultInputSupplier(location);
    XmlIndex schema = (XmlIndex) readSchema(input);
    // ItemsType
    TypeDefinition itemsType = schema.getType(new QName("ItemsType"));
    assertNotNull(itemsType);
    Collection<? extends ChildDefinition<?>> children = itemsType.getChildren();
    assertEquals(1, children.size());
    // choice
    GroupPropertyDefinition choice = children.iterator().next().asGroup();
    assertNotNull(choice);
    // cardinality
    Cardinality cc = choice.getConstraint(Cardinality.class);
    assertEquals(0, cc.getMinOccurs());
    assertEquals(Cardinality.UNBOUNDED, cc.getMaxOccurs());
    // choice flag
    assertTrue(choice.getConstraint(ChoiceFlag.class).isEnabled());
    // children
    assertEquals(3, choice.getDeclaredChildren().size());
    // shirt
    PropertyDefinition shirt = choice.getChild(new QName("shirt")).asProperty();
    assertNotNull(shirt);
    // hat
    PropertyDefinition hat = choice.getChild(new QName("hat")).asProperty();
    assertNotNull(hat);
    // umbrella
    PropertyDefinition umbrella = choice.getChild(new QName("umbrella")).asProperty();
    assertNotNull(umbrella);
// TODO extend with advanced complex type tests?
}
Also used : GroupPropertyDefinition(eu.esdihumboldt.hale.common.schema.model.GroupPropertyDefinition) DefaultInputSupplier(eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier) Cardinality(eu.esdihumboldt.hale.common.schema.model.constraint.property.Cardinality) QName(javax.xml.namespace.QName) XmlIndex(eu.esdihumboldt.hale.io.xsd.model.XmlIndex) URI(java.net.URI) GroupPropertyDefinition(eu.esdihumboldt.hale.common.schema.model.GroupPropertyDefinition) PropertyDefinition(eu.esdihumboldt.hale.common.schema.model.PropertyDefinition) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) Test(org.junit.Test)

Example 7 with PropertyDefinition

use of eu.esdihumboldt.hale.common.schema.model.PropertyDefinition in project hale by halestudio.

the class XmlSchemaReaderTest method testRead_shiporder_types_cycle.

/**
 * Test reading a simple XML schema that uses several custom named types and
 * has a cycle.
 *
 * @throws Exception if reading the schema fails
 */
@Test
public void testRead_shiporder_types_cycle() throws Exception {
    URI location = getClass().getResource("/testdata/shiporder/shiporder-types-cycle.xsd").toURI();
    LocatableInputSupplier<? extends InputStream> input = new DefaultInputSupplier(location);
    XmlIndex schema = (XmlIndex) readSchema(input);
    String ns = "http://www.example.com";
    assertEquals(ns, schema.getNamespace());
    // shiporder element
    Collection<XmlElement> elements = getElementsWithNS(ns, schema.getElements().values());
    assertEquals(1, elements.size());
    XmlElement shiporder = elements.iterator().next();
    assertNotNull(shiporder);
    TypeDefinition type = shiporder.getType();
    assertEquals(5, type.getChildren().size());
    // contained shiporder element
    PropertyDefinition s2 = type.getChild(new QName(ns, "shiporder")).asProperty();
    assertNotNull(s2);
    assertEquals(type, s2.getPropertyType());
}
Also used : DefaultInputSupplier(eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier) QName(javax.xml.namespace.QName) XmlIndex(eu.esdihumboldt.hale.io.xsd.model.XmlIndex) XmlElement(eu.esdihumboldt.hale.io.xsd.model.XmlElement) URI(java.net.URI) GroupPropertyDefinition(eu.esdihumboldt.hale.common.schema.model.GroupPropertyDefinition) PropertyDefinition(eu.esdihumboldt.hale.common.schema.model.PropertyDefinition) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) Test(org.junit.Test)

Example 8 with PropertyDefinition

use of eu.esdihumboldt.hale.common.schema.model.PropertyDefinition in project hale by halestudio.

the class XmlSchemaReaderTest method testRead_definitive_attributegroup.

/**
 * Test reading a simple XML schema that uses an attribute group and an
 * attribute with xs:date type.
 *
 * @throws Exception if reading the schema fails
 */
@Test
public void testRead_definitive_attributegroup() throws Exception {
    URI location = getClass().getResource("/testdata/definitive/attributegroup.xsd").toURI();
    LocatableInputSupplier<? extends InputStream> input = new DefaultInputSupplier(location);
    XmlIndex schema = (XmlIndex) readSchema(input);
    // ShirtType
    TypeDefinition type = schema.getType(new QName("ShirtType"));
    assertNotNull(type);
    // not there any more because it is flattened away
    // // IdentifierGroup
    // GroupPropertyDefinition group = type.getChild(new QName("IdentifierGroup")).asGroup();
    // assertNotNull(group);
    // // not a choice
    // assertFalse(group.getConstraint(ChoiceFlag.class).isEnabled());
    // id
    PropertyDefinition id = type.getChild(new QName("id")).asProperty();
    assertNotNull(id);
    // property type must be a simple type
    assertTrue(id.getPropertyType().getConstraint(HasValueFlag.class).isEnabled());
    // binding must be string
    assertEquals(String.class, id.getPropertyType().getConstraint(Binding.class).getBinding());
    // required
    Cardinality cc = id.getConstraint(Cardinality.class);
    assertEquals(1, cc.getMinOccurs());
    assertEquals(1, cc.getMaxOccurs());
    // version
    PropertyDefinition version = type.getChild(new QName("version")).asProperty();
    assertNotNull(version);
    // property type must be a simple type
    assertTrue(version.getPropertyType().getConstraint(HasValueFlag.class).isEnabled());
    // effDate
    PropertyDefinition effDate = type.getChild(new QName("effDate")).asProperty();
    assertNotNull(effDate);
    // binding must be compatible to Date
    assertTrue(Date.class.isAssignableFrom(effDate.getPropertyType().getConstraint(Binding.class).getBinding()));
}
Also used : Binding(eu.esdihumboldt.hale.common.schema.model.constraint.type.Binding) DefaultInputSupplier(eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier) Cardinality(eu.esdihumboldt.hale.common.schema.model.constraint.property.Cardinality) QName(javax.xml.namespace.QName) XmlIndex(eu.esdihumboldt.hale.io.xsd.model.XmlIndex) URI(java.net.URI) GroupPropertyDefinition(eu.esdihumboldt.hale.common.schema.model.GroupPropertyDefinition) PropertyDefinition(eu.esdihumboldt.hale.common.schema.model.PropertyDefinition) Date(java.util.Date) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) Test(org.junit.Test)

Example 9 with PropertyDefinition

use of eu.esdihumboldt.hale.common.schema.model.PropertyDefinition in project hale by halestudio.

the class CustomTypeContentHelper method applyConfiguration.

/**
 * Apply a custom type configuration to the given schema.
 *
 * @param index the XML schema
 * @param association the custom type configuration for an individual
 *            property
 */
public static void applyConfiguration(XmlIndex index, CustomTypeContentAssociation association) {
    CustomTypeContent config = association.getConfig();
    // property identified by a list of qualified names
    List<QName> property = association.getProperty();
    PropertyDefinition propDef = null;
    DefinitionGroup propParent = null;
    if (property != null && property.size() > 1) {
        QName typeName = property.get(0);
        TypeDefinition type = index.getType(typeName);
        if (type != null) {
            LinkedList<QName> nameQueue = new LinkedList<>(property.subList(1, property.size()));
            Definition<?> parent = null;
            Definition<?> child = type;
            while (!nameQueue.isEmpty() && child != null) {
                parent = child;
                QName name = nameQueue.pollFirst();
                child = DefinitionUtil.getChild(parent, name);
            }
            if (nameQueue.isEmpty() && child instanceof PropertyDefinition) {
                propDef = (PropertyDefinition) child;
                if (parent instanceof TypeDefinition) {
                    propParent = (DefinitionGroup) parent;
                } else if (parent instanceof ChildDefinition<?>) {
                    ChildDefinition<?> pc = (ChildDefinition<?>) parent;
                    if (pc.asProperty() != null) {
                        propParent = pc.asProperty().getPropertyType();
                    } else if (pc.asGroup() != null) {
                        propParent = pc.asGroup();
                    }
                } else {
                    log.error("Illegal parent for custom type content property");
                    return;
                }
            } else {
                log.warn("Cannot apply custom type content configuration due to invalid property path");
                return;
            }
        } else {
            log.warn("Cannot apply custom type content configuration due because the type {} starting the property path could not be found", typeName);
            return;
        }
    } else {
        log.warn("Cannot apply custom type content configuration due to missing property path");
        return;
    }
    switch(config.getMode()) {
        case simple:
            applySimpleMode(propDef, propParent, config);
        case elements:
            applyElementsMode(propDef, propParent, config, index);
        default:
            log.error("Unrecognized custom type content mode {}", config.getMode().name());
    }
}
Also used : QName(javax.xml.namespace.QName) ChildDefinition(eu.esdihumboldt.hale.common.schema.model.ChildDefinition) DefaultPropertyDefinition(eu.esdihumboldt.hale.common.schema.model.impl.DefaultPropertyDefinition) DefaultGroupPropertyDefinition(eu.esdihumboldt.hale.common.schema.model.impl.DefaultGroupPropertyDefinition) PropertyDefinition(eu.esdihumboldt.hale.common.schema.model.PropertyDefinition) DefinitionGroup(eu.esdihumboldt.hale.common.schema.model.DefinitionGroup) LinkedList(java.util.LinkedList) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) DefaultTypeDefinition(eu.esdihumboldt.hale.common.schema.model.impl.DefaultTypeDefinition)

Example 10 with PropertyDefinition

use of eu.esdihumboldt.hale.common.schema.model.PropertyDefinition in project hale by halestudio.

the class SubstitutionGroupProperty method initChildren.

/**
 * @see LazyGroupPropertyDefinition#initChildren()
 */
@Override
protected void initChildren() {
    if (property != null) {
        TypeDefinition propertyType = property.getPropertyType();
        // add property and substitutions
        // collect substitution types and elements
        List<XmlElement> substitutions = collectSubstitutions(property.getName(), propertyType);
        if (substitutions == null || substitutions.isEmpty()) {
            // add property (XXX even if the property type is abstract)
            // no redeclaration necessary as this
            super.addChild(property);
        // is already the declaring group
        } else {
            // add property if the type is not abstract
            if (!propertyType.getConstraint(AbstractFlag.class).isEnabled()) {
                // no redeclaration necessary as
                super.addChild(property);
            // this is already the declaring
            // group
            }
            // add substitutions
            for (XmlElement substitution : substitutions) {
                PropertyDefinition p = new SubstitutionProperty(substitution, property, this);
                // must call super add
                super.addChild(p);
            }
        }
    }
// else empty group
}
Also used : XmlElement(eu.esdihumboldt.hale.io.xsd.model.XmlElement) DefaultPropertyDefinition(eu.esdihumboldt.hale.common.schema.model.impl.DefaultPropertyDefinition) DefaultGroupPropertyDefinition(eu.esdihumboldt.hale.common.schema.model.impl.DefaultGroupPropertyDefinition) PropertyDefinition(eu.esdihumboldt.hale.common.schema.model.PropertyDefinition) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition)

Aggregations

PropertyDefinition (eu.esdihumboldt.hale.common.schema.model.PropertyDefinition)64 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)38 QName (javax.xml.namespace.QName)24 ChildContext (eu.esdihumboldt.hale.common.align.model.ChildContext)12 PropertyEntityDefinition (eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition)12 GroupPropertyDefinition (eu.esdihumboldt.hale.common.schema.model.GroupPropertyDefinition)11 ArrayList (java.util.ArrayList)11 EntityDefinition (eu.esdihumboldt.hale.common.align.model.EntityDefinition)10 DefaultTypeDefinition (eu.esdihumboldt.hale.common.schema.model.impl.DefaultTypeDefinition)10 DefaultPropertyDefinition (eu.esdihumboldt.hale.common.schema.model.impl.DefaultPropertyDefinition)9 Test (org.junit.Test)9 Cell (eu.esdihumboldt.hale.common.align.model.Cell)8 TypeEntityDefinition (eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition)8 Cardinality (eu.esdihumboldt.hale.common.schema.model.constraint.property.Cardinality)7 XmlIndex (eu.esdihumboldt.hale.io.xsd.model.XmlIndex)7 DefaultInputSupplier (eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier)6 URI (java.net.URI)6 Entity (eu.esdihumboldt.hale.common.align.model.Entity)5 Property (eu.esdihumboldt.hale.common.align.model.Property)5 MutableAlignment (eu.esdihumboldt.hale.common.align.model.MutableAlignment)4