Search in sources :

Example 11 with GroupPropertyDefinition

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

the class StreamGmlWriter method writeProperties.

/**
 * Write attribute or element properties.
 *
 * @param parent the parent group
 * @param children the child definitions
 * @param attributes <code>true</code> if attribute properties shall be
 *            written, <code>false</code> if element properties shall be
 *            written
 * @param parentIsNil if the parent property is nil
 * @param report the reporter
 * @throws XMLStreamException if writing the attributes/elements fails
 */
private void writeProperties(Group parent, Collection<? extends ChildDefinition<?>> children, boolean attributes, boolean parentIsNil, IOReporter report) throws XMLStreamException {
    if (parent == null) {
        return;
    }
    boolean parentIsChoice = parent.getDefinition() instanceof GroupPropertyDefinition && ((GroupPropertyDefinition) parent.getDefinition()).getConstraint(ChoiceFlag.class).isEnabled();
    for (ChildDefinition<?> child : children) {
        Object[] values = parent.getProperty(child.getName());
        if (child.asProperty() != null) {
            PropertyDefinition propDef = child.asProperty();
            boolean isAttribute = propDef.getConstraint(XmlAttributeFlag.class).isEnabled();
            if (attributes && isAttribute) {
                if (values != null && values.length > 0) {
                    boolean allowWrite = true;
                    // special case handling: omit nilReason
                    if (getParameter(PARAM_OMIT_NIL_REASON).as(Boolean.class, true)) {
                        Cardinality propCard = propDef.getConstraint(Cardinality.class);
                        if ("nilReason".equals(propDef.getName().getLocalPart()) && propCard.getMinOccurs() < 1) {
                            allowWrite = parentIsNil;
                        }
                    }
                    // write attribute
                    if (allowWrite) {
                        // nilReason "unpopulated"
                        if ("nilReason".equals(propDef.getName().getLocalPart()) && "unpopulated".equals(values[0])) {
                            // TODO more strict check to ensure that this is
                            // a GML nilReason? (check property type and
                            // parent types)
                            writeAttribute("other:unpopulated", propDef);
                        } else {
                            // default
                            writeAttribute(values[0], propDef);
                        }
                    }
                    if (values.length > 1) {
                    // TODO warning?!
                    }
                }
            } else if (!attributes && !isAttribute) {
                int numValues = 0;
                if (values != null) {
                    // write element
                    for (Object value : values) {
                        writeElement(value, propDef, report);
                    }
                    numValues = values.length;
                }
                // only if parent is not a choice
                if (!parentIsChoice) {
                    Cardinality cardinality = propDef.getConstraint(Cardinality.class);
                    if (cardinality.getMinOccurs() > numValues) {
                        if (propDef.getConstraint(NillableFlag.class).isEnabled()) {
                            // nillable element
                            for (int i = numValues; i < cardinality.getMinOccurs(); i++) {
                                // write nil element
                                writeElement(null, propDef, report);
                            }
                        } else {
                            for (int i = numValues; i < cardinality.getMinOccurs(); i++) {
                                // write empty element
                                GmlWriterUtil.writeEmptyElement(writer, propDef.getName());
                            }
                        // TODO add warning to report
                        }
                    }
                }
            }
        } else if (child.asGroup() != null) {
            // handle to child groups
            if (values != null) {
                for (Object value : values) {
                    if (value instanceof Group) {
                        writeProperties((Group) value, DefinitionUtil.getAllChildren(child.asGroup()), attributes, parentIsNil, report);
                    } else {
                    // TODO warning/error?
                    }
                }
            }
        }
    }
}
Also used : GroupPropertyDefinition(eu.esdihumboldt.hale.common.schema.model.GroupPropertyDefinition) DefinitionGroup(eu.esdihumboldt.hale.common.schema.model.DefinitionGroup) Group(eu.esdihumboldt.hale.common.instance.model.Group) Cardinality(eu.esdihumboldt.hale.common.schema.model.constraint.property.Cardinality) XmlAttributeFlag(eu.esdihumboldt.hale.io.xsd.constraint.XmlAttributeFlag) GroupPropertyDefinition(eu.esdihumboldt.hale.common.schema.model.GroupPropertyDefinition) PropertyDefinition(eu.esdihumboldt.hale.common.schema.model.PropertyDefinition)

Example 12 with GroupPropertyDefinition

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

the class PropertyBean method findChild.

/**
 * The function to look for a child as ChildDefinition or as Group
 *
 * @param parent the starting point to traverse from
 * @param childName the name of the parent's child
 * @return a pair of child and a list with the full path from parent to the
 *         child or <code>null</code> if no such child was found
 */
public static Pair<ChildDefinition<?>, List<ChildDefinition<?>>> findChild(DefinitionGroup parent, QName childName) {
    ChildDefinition<?> child = parent.getChild(childName);
    if (child == null) {
        // if the namespace is not null
        if (childName.getNamespaceURI().equals(XMLConstants.NULL_NS_URI)) {
            // get all children and iterate over them
            Collection<? extends ChildDefinition<?>> children = DefinitionUtil.getAllChildren(parent);
            for (ChildDefinition<?> _child : children) {
                // different namespace we overwrite child
                if (_child.getName().getLocalPart().equals(childName.getLocalPart())) {
                    child = _child;
                    break;
                }
            }
        }
    }
    if (child != null) {
        return new Pair<ChildDefinition<?>, List<ChildDefinition<?>>>(child, null);
    }
    Collection<? extends ChildDefinition<?>> children = DefinitionUtil.getAllChildren(parent);
    for (ChildDefinition<?> groupChild : children) {
        if (groupChild.asGroup() != null) {
            GroupPropertyDefinition temp = groupChild.asGroup();
            if (findChild(temp, childName) != null) {
                Pair<ChildDefinition<?>, List<ChildDefinition<?>>> recTemp = findChild(temp, childName);
                if (recTemp.getSecond() == null) {
                    List<ChildDefinition<?>> second = new ArrayList<ChildDefinition<?>>();
                    second.add(temp);
                    ChildDefinition<?> first = recTemp.getFirst();
                    return new Pair<ChildDefinition<?>, List<ChildDefinition<?>>>(first, second);
                } else {
                    recTemp.getSecond().add(0, temp);
                }
            }
        }
    }
    return null;
}
Also used : GroupPropertyDefinition(eu.esdihumboldt.hale.common.schema.model.GroupPropertyDefinition) ChildDefinition(eu.esdihumboldt.hale.common.schema.model.ChildDefinition) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Pair(eu.esdihumboldt.util.Pair)

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