use of eu.esdihumboldt.hale.common.schema.model.constraint.property.Cardinality in project hale by halestudio.
the class SubstitutionGroupProperty method setProperty.
/**
* Set the property represented by the group. The property must have been
* created with this group as parent and the {@link Cardinality} constraint
* must have been already set.
*
* @param property the property to set
*/
public void setProperty(DefaultPropertyDefinition property) {
Preconditions.checkArgument(property.getDeclaringGroup() == this);
this.property = property;
// apply cardinality to group
setConstraint(property.getConstraint(Cardinality.class));
// set cardinality to exactly one for the property
property.setConstraint(Cardinality.CC_EXACTLY_ONCE);
// set display name to property name
setConstraint(new DisplayName(property.getDisplayName()));
}
use of eu.esdihumboldt.hale.common.schema.model.constraint.property.Cardinality 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()));
}
use of eu.esdihumboldt.hale.common.schema.model.constraint.property.Cardinality in project hale by halestudio.
the class XmlSchemaReaderTest method testShiporderStructure.
/**
* Test the shiporder structure
*
* @param shiporder the shiporder element
* @param ns the namespace
*/
private void testShiporderStructure(XmlElement shiporder, String ns) {
assertNotNull(shiporder);
assertEquals("shiporder", shiporder.getName().getLocalPart());
// shiporder type
TypeDefinition shiporderType = shiporder.getType();
assertNotNull(shiporderType);
Collection<? extends ChildDefinition<?>> properties = shiporderType.getChildren();
assertEquals(4, properties.size());
// orderperson
PropertyDefinition orderperson = shiporderType.getChild(new QName(ns, "orderperson")).asProperty();
assertNotNull(orderperson);
// property type must be a simple type
assertTrue(orderperson.getPropertyType().getConstraint(HasValueFlag.class).isEnabled());
// binding must be string
assertEquals(String.class, orderperson.getPropertyType().getConstraint(Binding.class).getBinding());
// cardinality
Cardinality cc = orderperson.getConstraint(Cardinality.class);
assertEquals(1, cc.getMinOccurs());
assertEquals(1, cc.getMaxOccurs());
// not nillable
assertFalse(orderperson.getConstraint(NillableFlag.class).isEnabled());
// shipto
PropertyDefinition shipto = shiporderType.getChild(new QName(ns, "shipto")).asProperty();
assertNotNull(shipto);
// property type must be a complex type
assertFalse(shipto.getPropertyType().getConstraint(HasValueFlag.class).isEnabled());
// item
PropertyDefinition item = shiporderType.getChild(new QName(ns, "item")).asProperty();
assertNotNull(item);
// property type must be a complex type
assertFalse(item.getPropertyType().getConstraint(HasValueFlag.class).isEnabled());
// item cardinality
cc = item.getConstraint(Cardinality.class);
assertEquals(1, cc.getMinOccurs());
assertEquals(Cardinality.UNBOUNDED, cc.getMaxOccurs());
// item properties
TypeDefinition itemType = item.getPropertyType();
Collection<? extends ChildDefinition<?>> itemProps = itemType.getChildren();
assertEquals(4, itemProps.size());
// title
assertNotNull(itemType.getChild(new QName(ns, "title")).asProperty());
// note
PropertyDefinition note = itemType.getChild(new QName(ns, "note")).asProperty();
assertNotNull(note);
cc = note.getConstraint(Cardinality.class);
assertEquals(0, cc.getMinOccurs());
assertEquals(1, cc.getMaxOccurs());
// quantity
PropertyDefinition quantity = itemType.getChild(new QName(ns, "quantity")).asProperty();
assertNotNull(quantity);
assertTrue(quantity.getPropertyType().getConstraint(HasValueFlag.class).isEnabled());
assertTrue(Number.class.isAssignableFrom(quantity.getPropertyType().getConstraint(Binding.class).getBinding()));
// price
PropertyDefinition price = itemType.getChild(new QName(ns, "price")).asProperty();
assertNotNull(price);
assertTrue(price.getPropertyType().getConstraint(HasValueFlag.class).isEnabled());
assertTrue(Number.class.isAssignableFrom(price.getPropertyType().getConstraint(Binding.class).getBinding()));
// orderid
PropertyDefinition orderid = shiporderType.getChild(new QName(ns, "orderid")).asProperty();
assertNotNull(orderid);
// binding must be string
assertEquals(String.class, orderid.getPropertyType().getConstraint(Binding.class).getBinding());
// required
cc = orderid.getConstraint(Cardinality.class);
assertEquals(1, cc.getMinOccurs());
assertEquals(1, cc.getMaxOccurs());
}
use of eu.esdihumboldt.hale.common.schema.model.constraint.property.Cardinality 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?
}
use of eu.esdihumboldt.hale.common.schema.model.constraint.property.Cardinality 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;
}
Aggregations