use of eu.esdihumboldt.hale.common.schema.model.PropertyDefinition in project hale by halestudio.
the class ShapeInstanceReader method checkCompatibility.
/**
* Determines if the compatibility rating between the two Shapefile type
* definitions.
*
* @param schemaType the type to test for compatibility
* @param dataType the type representing the data to read
* @return the percentage of compatibility (value from <code>0</code> to
* <code>100</code>), where <code>100</code> represents an exact
* match and <code>0</code> no compatibility
*/
public static int checkCompatibility(TypeDefinition schemaType, TypeDefinition dataType) {
// Shapefile types are flat, so only regard properties
Collection<? extends PropertyDefinition> children = DefinitionUtil.getAllProperties(dataType);
int count = children.size();
int schemaCount = DefinitionUtil.getAllProperties(schemaType).size();
// check for every property if it exists with the schema, with the same
// name
int num = 0;
for (PropertyDefinition property : children) {
ChildDefinition<?> child = schemaType.getChild(property.getName());
if (child != null && child.asProperty() != null) {
num++;
}
}
if (num == count && count == schemaCount) {
// exact match
return 100;
} else {
int percentage = (int) Math.round((double) (num * 100) / (double) count);
if (percentage > 1) {
// reduce value by one, to ensure 100 is not returned, but only
// return zero if there actually is no match
percentage -= 1;
}
// compatibility measure with a max of 99
return percentage;
}
}
use of eu.esdihumboldt.hale.common.schema.model.PropertyDefinition in project hale by halestudio.
the class XLSInstanceReader method createInstanceCollection.
/**
* create instances, see
* {@link CSVInstanceReader#execute(ProgressIndicator, IOReporter)}
*
* @param row the current row
* @param reporter the reporter of the writer
* @param solveNestedProperties true, if schema should not be flat <b>(not
* implemented yet)</b>
*/
@SuppressWarnings("javadoc")
private void createInstanceCollection(List<String> row, IOReporter reporter) {
MutableInstance instance = new DefaultInstance(type, null);
// int propertyIndex = 0;
for (int index = 0; index < propAr.length; index++) {
String part = null;
if (index < row.size())
part = row.get(index);
if (part != null) {
PropertyDefinition property = propAr[index];
if (part.isEmpty()) {
// FIXME make this configurable
part = null;
}
Object value = part;
if (value != null) {
Binding binding = property.getPropertyType().getConstraint(Binding.class);
try {
if (!binding.getBinding().equals(String.class)) {
ConversionService conversionService = HalePlatform.getService(ConversionService.class);
if (conversionService.canConvert(String.class, binding.getBinding())) {
value = conversionService.convert(part, binding.getBinding());
} else {
throw new IllegalStateException("Conversion not possible!");
}
}
} catch (Exception e) {
reporter.error(new IOMessageImpl("Cannot convert property value to {0}", e, line, -1, binding.getBinding().getSimpleName()));
}
instance.addProperty(property.getName(), value);
}
// propertyIndex++;
}
}
instances.add(instance);
}
use of eu.esdihumboldt.hale.common.schema.model.PropertyDefinition in project hale by halestudio.
the class XLSInstanceReader method execute.
@Override
protected IOReport execute(ProgressIndicator progress, IOReporter reporter) throws IOProviderConfigurationException, IOException {
boolean skipFirst = getParameter(CommonSchemaConstants.PARAM_SKIP_FIRST_LINE).as(Boolean.class);
// first sheet as default
sheetNum = getParameter(InstanceTableIOConstants.SHEET_INDEX).as(int.class, 0);
instances = new DefaultInstanceCollection(new ArrayList<Instance>());
try {
// analyze the excel sheet to get all information
analyser = new AnalyseXLSSchemaTable(getSource().getLocation(), sheetNum);
} catch (Exception e) {
reporter.error(new IOMessageImpl("Reading the excel sheet has failed", e));
return reporter;
}
// get type definition of the schema
type = getSourceSchema().getType(QName.valueOf(getParameter(CommonSchemaConstants.PARAM_TYPENAME).as(String.class)));
// get property definition
propAr = type.getChildren().toArray(new PropertyDefinition[type.getChildren().size()]);
Collection<List<String>> rows = analyser.getRows();
// skip if first row is a header
if (!skipFirst) {
// otherwise first line is also an instance
createInstanceCollection(analyser.getHeader(), reporter);
line++;
}
// iterate over all rows to create the instances
Iterator<List<String>> allRows = rows.iterator();
while (allRows.hasNext()) {
List<String> row = allRows.next();
createInstanceCollection(row, reporter);
line++;
}
reporter.setSuccess(true);
return reporter;
}
use of eu.esdihumboldt.hale.common.schema.model.PropertyDefinition 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.PropertyDefinition in project hale by halestudio.
the class XmlSchemaReaderTest method testRead_definitive_chapter03.
/**
* Test reading a simple XML schema that is split into several files. Tests
* also the {@link XmlElements} and {@link MappingRelevantFlag} constraints
*
* @throws Exception if reading the schema fails
*/
@Test
public void testRead_definitive_chapter03() throws Exception {
URI location = getClass().getResource("/testdata/definitive/chapter03env.xsd").toURI();
LocatableInputSupplier<? extends InputStream> input = new DefaultInputSupplier(location);
XmlIndex schema = (XmlIndex) readSchema(input);
// envelope element
XmlElement envelope = schema.getElements().get(new QName("http://example.org/ord", "envelope"));
assertNotNull(envelope);
TypeDefinition envType = envelope.getType();
// mappable
assertTrue(envType.getConstraint(MappingRelevantFlag.class).isEnabled());
// XmlElements
Collection<? extends XmlElement> elements = envType.getConstraint(XmlElements.class).getElements();
assertEquals(1, elements.size());
assertEquals(envelope, elements.iterator().next());
// order
PropertyDefinition order = envType.getChild(new QName("http://example.org/ord", "order")).asProperty();
assertNotNull(order);
TypeDefinition orderType = order.getPropertyType();
// mappable
assertTrue(orderType.getConstraint(MappingRelevantFlag.class).isEnabled());
// number
PropertyDefinition number = orderType.getChild(new QName("http://example.org/ord", "number")).asProperty();
assertNotNull(number);
// binding must be string
assertEquals(String.class, number.getPropertyType().getConstraint(Binding.class).getBinding());
// items
PropertyDefinition items = orderType.getChild(new QName("http://example.org/ord", "items")).asProperty();
assertNotNull(items);
// not mappable
assertFalse(items.getPropertyType().getConstraint(MappingRelevantFlag.class).isEnabled());
// no elements
assertTrue(items.getPropertyType().getConstraint(XmlElements.class).getElements().isEmpty());
// SpecialOrderType
// extension to OrderType, should be mappable using xsi:type
TypeDefinition specialOrderType = schema.getType(new QName("http://example.org/ord", "SpecialOrderType"));
assertNotNull(specialOrderType);
// number of declared children
assertEquals(1, specialOrderType.getDeclaredChildren().size());
// number of children
assertEquals(3, specialOrderType.getChildren().size());
// mappable
assertTrue(specialOrderType.getConstraint(MappableFlag.class).isEnabled());
// no elements
assertTrue(specialOrderType.getConstraint(XmlElements.class).getElements().isEmpty());
// overall mappable types
Collection<? extends TypeDefinition> mt = schema.getMappingRelevantTypes();
// envelope, order, special order
assertEquals(2, mt.size());
}
Aggregations