Search in sources :

Example 1 with GroupPropertyDefinition

use of eu.esdihumboldt.hale.common.schema.model.GroupPropertyDefinition 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 2 with GroupPropertyDefinition

use of eu.esdihumboldt.hale.common.schema.model.GroupPropertyDefinition 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 3 with GroupPropertyDefinition

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

the class DefinitionImages method getImage.

/**
 * Get the image for the given definition
 *
 * @param entityDef the entity definition, may be <code>null</code>
 * @param def the definition
 * @return the image, may be <code>null</code>
 */
protected Image getImage(EntityDefinition entityDef, Definition<?> def) {
    Classification clazz = Classification.getClassification(def);
    String imageName = getImageForClassification(clazz);
    // retrieve image for key
    Image image;
    if (imageName == null) {
        // default
        imageName = ISharedImages.IMG_OBJ_ELEMENT;
        image = PlatformUI.getWorkbench().getSharedImages().getImage(imageName);
    } else {
        image = CommonSharedImages.getImageRegistry().get(imageName);
    }
    // XXX not supported yet
    if (def instanceof TypeDefinition && !((TypeDefinition) def).getConstraint(AbstractFlag.class).isEnabled() && hasGeometry((TypeDefinition) def)) {
        TypeDefinition type = (TypeDefinition) def;
        DataSet dataSet = DataSet.SOURCE;
        // defined styles
        if (entityDef != null) {
            dataSet = DataSet.forSchemaSpace(entityDef.getSchemaSpace());
        }
        String typeKey = dataSet.name() + "::" + type.getIdentifier();
        // XXX check if style image is already there?
        // XXX how to handle style changes?
        BufferedImage img = getLegendImage(type, dataSet, true);
        if (img != null) {
            // replace image with style image
            ImageData imgData = SwingRcpUtilities.convertToSWT(img);
            image = new Image(Display.getCurrent(), imgData);
            final Image old;
            if (styleImages.containsKey(typeKey)) {
                old = styleImages.get(typeKey);
            } else {
                old = null;
            }
            styleImages.put(typeKey, image);
            if (old != null) {
                // schedule image to be disposed when there are no
                // references to it
                handles.addReference(image);
            }
        }
    } else // check for inline attributes
    {
        boolean attribute = (def instanceof PropertyDefinition) && ((PropertyDefinition) def).getConstraint(XmlAttributeFlag.class).isEnabled();
        boolean mandatory = false;
        if (!suppressMandatory) {
            if (def instanceof PropertyDefinition) {
                Cardinality cardinality = ((PropertyDefinition) def).getConstraint(Cardinality.class);
                mandatory = cardinality.getMinOccurs() > 0 && !((PropertyDefinition) def).getConstraint(NillableFlag.class).isEnabled();
            } else if (def instanceof GroupPropertyDefinition) {
                Cardinality cardinality = ((GroupPropertyDefinition) def).getConstraint(Cardinality.class);
                mandatory = cardinality.getMinOccurs() > 0;
            }
        }
        boolean deflt = false;
        boolean faded = false;
        if (entityDef != null) {
            // entity definition needed to determine if item is a default
            // geometry
            deflt = DefaultGeometryUtil.isDefaultGeometry(entityDef);
            // and to determine population
            PopulationService ps = PlatformUI.getWorkbench().getService(PopulationService.class);
            if (ps != null && ps.hasPopulation(entityDef.getSchemaSpace())) {
                Population pop = ps.getPopulation(entityDef);
                faded = (pop != null && pop.getOverallCount() == 0);
            }
        }
        if (deflt || mandatory || attribute || faded) {
            // overlayed image
            ImageConf conf = new ImageConf(imageName, attribute, deflt, mandatory, faded);
            Image overlayedImage = overlayedImages.get(conf);
            if (overlayedImage == null) {
                // apply overlays to image
                Image copy = new Image(image.getDevice(), image.getBounds());
                // draw on image
                GC gc = new GC(copy);
                try {
                    gc.drawImage(image, 0, 0);
                    if (attribute) {
                        gc.drawImage(attribOverlay, 0, 0);
                    }
                    if (deflt) {
                        gc.drawImage(defOverlay, 0, 0);
                    }
                    if (mandatory) {
                        gc.drawImage(mandatoryOverlay, 0, 0);
                    }
                } finally {
                    gc.dispose();
                }
                if (faded) {
                    ImageData imgData = copy.getImageData();
                    imgData.alpha = 150;
                    Image copy2 = new Image(image.getDevice(), imgData);
                    copy.dispose();
                    copy = copy2;
                }
                image = copy;
                overlayedImages.put(conf, copy);
            } else {
                image = overlayedImage;
            }
        }
    }
    return image;
}
Also used : GroupPropertyDefinition(eu.esdihumboldt.hale.common.schema.model.GroupPropertyDefinition) Cardinality(eu.esdihumboldt.hale.common.schema.model.constraint.property.Cardinality) DataSet(eu.esdihumboldt.hale.common.instance.model.DataSet) Image(org.eclipse.swt.graphics.Image) BufferedImage(java.awt.image.BufferedImage) GroupPropertyDefinition(eu.esdihumboldt.hale.common.schema.model.GroupPropertyDefinition) PropertyDefinition(eu.esdihumboldt.hale.common.schema.model.PropertyDefinition) BufferedImage(java.awt.image.BufferedImage) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) ImageData(org.eclipse.swt.graphics.ImageData) Classification(eu.esdihumboldt.hale.common.schema.Classification) PopulationService(eu.esdihumboldt.hale.ui.common.service.population.PopulationService) Population(eu.esdihumboldt.hale.ui.common.service.population.Population) GC(org.eclipse.swt.graphics.GC)

Example 4 with GroupPropertyDefinition

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

the class AbstractGeometrySchemaService method determineDefaultGeometry.

/**
 * Determine the path to a geometry property to be used as default geometry
 * for the given type. By default the first geometry property found with a
 * breadth first search is used.
 *
 * @param type the type definition
 * @return the path to the default geometry property or <code>null</code> if
 *         unknown
 */
protected List<QName> determineDefaultGeometry(TypeDefinition type) {
    // breadth first search for geometry properties
    Queue<Pair<List<QName>, DefinitionGroup>> groups = new LinkedList<Pair<List<QName>, DefinitionGroup>>();
    groups.add(new Pair<List<QName>, DefinitionGroup>(new ArrayList<QName>(), type));
    List<QName> firstGeometryPath = null;
    while (firstGeometryPath == null && !groups.isEmpty()) {
        // for each parent group...
        Pair<List<QName>, DefinitionGroup> group = groups.poll();
        DefinitionGroup parent = group.getSecond();
        List<QName> parentPath = group.getFirst();
        // max depth for default geometries
        if (parentPath.size() > 5)
            continue;
        // queue
        for (ChildDefinition<?> child : DefinitionUtil.getAllChildren(parent)) {
            // path for child
            List<QName> path = new ArrayList<QName>(parentPath);
            path.add(child.getName());
            if (child.asProperty() != null) {
                PropertyDefinition property = child.asProperty();
                TypeDefinition propertyType = property.getPropertyType();
                // check if we found a geometry property
                if (propertyType.getConstraint(GeometryType.class).isGeometry()) {
                    // match
                    firstGeometryPath = path;
                } else {
                    // test children later on
                    groups.add(new Pair<List<QName>, DefinitionGroup>(path, propertyType));
                }
            } else if (child.asGroup() != null) {
                // test group later on
                GroupPropertyDefinition childGroup = child.asGroup();
                groups.add(new Pair<List<QName>, DefinitionGroup>(path, childGroup));
            } else {
                throw new IllegalStateException("Invalid child definition encountered");
            }
        }
    }
    if (firstGeometryPath != null) {
        // a geometry property was found
        return generalizeGeometryProperty(type, firstGeometryPath);
    } else {
        return null;
    }
}
Also used : GroupPropertyDefinition(eu.esdihumboldt.hale.common.schema.model.GroupPropertyDefinition) QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList) GroupPropertyDefinition(eu.esdihumboldt.hale.common.schema.model.GroupPropertyDefinition) PropertyDefinition(eu.esdihumboldt.hale.common.schema.model.PropertyDefinition) LinkedList(java.util.LinkedList) DefinitionGroup(eu.esdihumboldt.hale.common.schema.model.DefinitionGroup) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) GeometryType(eu.esdihumboldt.hale.common.schema.model.constraint.type.GeometryType) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) Pair(eu.esdihumboldt.util.Pair)

Example 5 with GroupPropertyDefinition

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

the class GroupPath method allowAdd.

/**
 * Determines if the adding a property value for the given property to the
 * last element in the path is allowed.
 *
 * @param propertyName the property name
 * @param strict states if additional checks are applied apart from whether
 *            the property exists
 * @param ignoreNamespaces if a property with a differing namespace may be
 *            accepted
 * @return if adding the property value to the last element in the path is
 *         allowed
 */
public boolean allowAdd(QName propertyName, boolean strict, boolean ignoreNamespaces) {
    if (children == null || children.isEmpty()) {
        // check last parent
        MutableGroup parent = parents.get(parents.size() - 1);
        ChildDefinition<?> child = GroupUtil.findChild(parent.getDefinition(), propertyName, ignoreNamespaces);
        if (child.asProperty() != null) {
            return !strict || GroupUtil.allowAdd(parent, null, child.asProperty().getName());
        } else {
            return false;
        }
    } else {
        // check last child
        DefinitionGroup child = children.get(children.size() - 1);
        ChildDefinition<?> property = GroupUtil.findChild(child, propertyName, ignoreNamespaces);
        if (property == null) {
            return false;
        }
        if (child instanceof GroupPropertyDefinition && ((GroupPropertyDefinition) child).getConstraint(ChoiceFlag.class).isEnabled()) {
            // group is a choice
            return true;
        }
        return !strict || GroupUtil.allowAdd(null, child, property.getName());
    }
}
Also used : GroupPropertyDefinition(eu.esdihumboldt.hale.common.schema.model.GroupPropertyDefinition) MutableGroup(eu.esdihumboldt.hale.common.instance.model.MutableGroup) DefinitionGroup(eu.esdihumboldt.hale.common.schema.model.DefinitionGroup)

Aggregations

GroupPropertyDefinition (eu.esdihumboldt.hale.common.schema.model.GroupPropertyDefinition)12 PropertyDefinition (eu.esdihumboldt.hale.common.schema.model.PropertyDefinition)7 QName (javax.xml.namespace.QName)7 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)6 Cardinality (eu.esdihumboldt.hale.common.schema.model.constraint.property.Cardinality)6 DefinitionGroup (eu.esdihumboldt.hale.common.schema.model.DefinitionGroup)5 DefaultInputSupplier (eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier)4 XmlIndex (eu.esdihumboldt.hale.io.xsd.model.XmlIndex)4 URI (java.net.URI)4 Test (org.junit.Test)4 MutableGroup (eu.esdihumboldt.hale.common.instance.model.MutableGroup)3 Pair (eu.esdihumboldt.util.Pair)3 ArrayList (java.util.ArrayList)3 Group (eu.esdihumboldt.hale.common.instance.model.Group)2 List (java.util.List)2 IOMessageImpl (eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl)1 DataSet (eu.esdihumboldt.hale.common.instance.model.DataSet)1 DefaultGroup (eu.esdihumboldt.hale.common.instance.model.impl.DefaultGroup)1 Classification (eu.esdihumboldt.hale.common.schema.Classification)1 ChildDefinition (eu.esdihumboldt.hale.common.schema.model.ChildDefinition)1