Search in sources :

Example 1 with DefinitionAccessor

use of eu.esdihumboldt.hale.common.schema.groovy.DefinitionAccessor in project hale by halestudio.

the class InstanceBuilderCode method appendBuildProperties.

/**
 * Append example code to build the properties identified by the given path
 * tree.
 *
 * @param example the example code to append to
 * @param baseIndent the base indent to use
 * @param tree the path tree representing a specific segment
 * @param parent the parent of the segment
 * @param useBrackets if brackets should be used in the generated code
 * @param startWithIndent if at the beginning the indent should be added
 * @param endWithNewline if at the end a new line break should be added
 * @param useExampleValues if example values should be used
 * @return the relative offset where editing should be continued
 */
public static int appendBuildProperties(StringBuilder example, String baseIndent, PathTree tree, DefinitionGroup parent, final boolean useBrackets, final boolean startWithIndent, final boolean endWithNewline, final boolean useExampleValues) {
    Definition<?> def = (Definition<?>) tree.getSegment();
    final String indent = baseIndent;
    int cursor = 0;
    boolean opened = false;
    if (def instanceof PropertyDefinition) {
        // property name
        if (startWithIndent) {
            example.append(indent);
        }
        // TODO test if property must be accessed explicitly through
        // builder?
        example.append(def.getName().getLocalPart());
        // test if uniquely accessible from parent
        boolean useNamespace = true;
        if (parent instanceof Definition<?>) {
            try {
                new DefinitionAccessor((Definition<?>) parent).findChildren(def.getName().getLocalPart()).eval();
                useNamespace = false;
            } catch (IllegalStateException e) {
            // ignore - namespace needed
            }
        }
        boolean needComma = false;
        // add namespace if necessary
        if (useNamespace) {
            if (useBrackets && !needComma) {
                example.append('(');
            }
            example.append(" namespace: '");
            example.append(def.getName().getNamespaceURI());
            example.append('\'');
            needComma = true;
        }
        TypeDefinition propertyType = ((PropertyDefinition) def).getPropertyType();
        boolean hasValue = propertyType.getConstraint(HasValueFlag.class).isEnabled();
        if (hasValue) {
            // add an example value
            if (useBrackets && !needComma) {
                example.append('(');
            }
            if (needComma) {
                example.append(',');
            }
            example.append(' ');
            if (useExampleValues) {
                switch(Classification.getClassification(def)) {
                    case NUMERIC_PROPERTY:
                        example.append("42");
                        break;
                    case STRING_PROPERTY:
                        example.append("'some value'");
                        break;
                    default:
                        example.append("some_value");
                }
            }
            needComma = true;
        }
        if (DefinitionUtil.hasChildren(propertyType) && (!tree.getChildren().isEmpty() || !needComma || !hasValue)) {
            if (needComma) {
                if (useBrackets) {
                    example.append(" )");
                } else {
                    example.append(',');
                }
            }
            example.append(" {");
            example.append('\n');
            opened = true;
        } else {
            cursor = example.length();
            if (useBrackets && needComma) {
                example.append(" )");
            }
            if (endWithNewline) {
                example.append('\n');
            }
        }
    } else {
    // groups are ignored
    }
    if (opened) {
        // set the new parent
        parent = DefinitionUtil.getDefinitionGroup(def);
        // create child properties
        String newIndent = indent + createIndent(1);
        if (tree.getChildren().isEmpty()) {
            example.append(newIndent);
            cursor = example.length();
            example.append('\n');
        } else {
            for (PathTree child : tree.getChildren()) {
                cursor = appendBuildProperties(example, newIndent, child, parent, useBrackets, true, true, useExampleValues);
            }
        }
        // close bracket
        example.append(indent);
        example.append('}');
        if (endWithNewline) {
            example.append('\n');
        }
    }
    return cursor;
}
Also used : HasValueFlag(eu.esdihumboldt.hale.common.schema.model.constraint.type.HasValueFlag) PropertyDefinition(eu.esdihumboldt.hale.common.schema.model.PropertyDefinition) Definition(eu.esdihumboldt.hale.common.schema.model.Definition) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) DefinitionAccessor(eu.esdihumboldt.hale.common.schema.groovy.DefinitionAccessor) PropertyDefinition(eu.esdihumboldt.hale.common.schema.model.PropertyDefinition) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition)

Example 2 with DefinitionAccessor

use of eu.esdihumboldt.hale.common.schema.groovy.DefinitionAccessor in project hale by halestudio.

the class InspireInstanceWriter method writeAdditionalElements.

/**
 * @see StreamGmlWriter#writeAdditionalElements(XMLStreamWriter,
 *      TypeDefinition, IOReporter)
 */
@Override
protected void writeAdditionalElements(XMLStreamWriter writer, TypeDefinition containerDefinition, IOReporter reporter) throws XMLStreamException {
    super.writeAdditionalElements(writer, containerDefinition, reporter);
    // determine INSPIRE identifier and metadata names
    Path<Definition<?>> localIdPath = new DefinitionAccessor(containerDefinition).findChildren("identifier").findChildren("Identifier").findChildren("localId").eval(false);
    QName identifierName = localIdPath.getElements().get(1).getName();
    Definition<?> internalIdentifierDef = localIdPath.getElements().get(2);
    QName internalIdentifierName = internalIdentifierDef.getName();
    QName localIdName = localIdPath.getElements().get(3).getName();
    Path<Definition<?>> namespacePath = new DefinitionAccessor(internalIdentifierDef).findChildren("namespace").eval(false);
    QName namespaceName = namespacePath.getElements().get(1).getName();
    Path<Definition<?>> metadataPath = new DefinitionAccessor(containerDefinition).findChildren("metadata").eval(false);
    QName metadataName = metadataPath.getElements().get(1).getName();
    // write INSPIRE identifier
    writer.writeStartElement(identifierName.getNamespaceURI(), identifierName.getLocalPart());
    writer.writeStartElement(internalIdentifierName.getNamespaceURI(), internalIdentifierName.getLocalPart());
    writer.writeStartElement(localIdName.getNamespaceURI(), localIdName.getLocalPart());
    writer.writeCharacters(getParameter(PARAM_SPATIAL_DATA_SET_LOCALID).as(String.class, ""));
    writer.writeEndElement();
    writer.writeStartElement(namespaceName.getNamespaceURI(), namespaceName.getLocalPart());
    writer.writeCharacters(getParameter(PARAM_SPATIAL_DATA_SET_NAMESPACE).as(String.class, ""));
    writer.writeEndElement();
    writer.writeEndElement();
    writer.writeEndElement();
    // write metadata
    writer.writeStartElement(metadataName.getNamespaceURI(), metadataName.getLocalPart());
    // retrieve metadata element (if any)
    Element metadataElement = getParameter(PARAM_SPATIAL_DATA_SET_METADATA_DOM).as(Element.class);
    // metadata from file (if any)
    if (metadataElement == null) {
        String metadataFile = getParameter(PARAM_SPATIAL_DATA_SET_METADATA_FILE).as(String.class);
        if (metadataFile != null && !metadataFile.isEmpty()) {
            try (InputStream input = new BufferedInputStream(new FileInputStream(new File(metadataFile)))) {
                metadataElement = findMetadata(input, reporter);
            } catch (IOException e) {
                reporter.warn(new IOMessageImpl("Could not load specified metadata file.", e));
            }
        }
    }
    if (metadataElement != null) {
        try {
            writeElement(metadataElement, writer);
        } catch (TransformerException e) {
            reporter.warn(new IOMessageImpl("Couldn't include specified metadata file.", e));
        }
    } else {
        writer.writeAttribute(XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, "nil", "true");
    }
    writer.writeEndElement();
}
Also used : QName(javax.xml.namespace.QName) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) XmlElement(eu.esdihumboldt.hale.io.xsd.model.XmlElement) Element(org.w3c.dom.Element) CRSDefinition(eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition) Definition(eu.esdihumboldt.hale.common.schema.model.Definition) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) IOMessageImpl(eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) BufferedInputStream(java.io.BufferedInputStream) DefinitionAccessor(eu.esdihumboldt.hale.common.schema.groovy.DefinitionAccessor) File(java.io.File) TransformerException(javax.xml.transform.TransformerException)

Example 3 with DefinitionAccessor

use of eu.esdihumboldt.hale.common.schema.groovy.DefinitionAccessor in project hale by halestudio.

the class SpatialDataSetConfigurationPage method createContent.

/**
 * @see HaleWizardPage#createContent(Composite)
 */
@Override
protected void createContent(Composite page) {
    GridLayoutFactory.swtDefaults().numColumns(2).equalWidth(false).spacing(6, 12).applyTo(page);
    // Get the property definitions of localId and namespace.
    String action = getWizard().getActionId();
    SchemaService ss = PlatformUI.getWorkbench().getService(SchemaService.class);
    SchemaSpaceID ssid = SchemaSpaceID.TARGET;
    if (InstanceIO.ACTION_SAVE_SOURCE_DATA.equals(action)) {
        ssid = SchemaSpaceID.SOURCE;
    }
    SchemaSpace target = ss.getSchemas(ssid);
    XmlIndex index = StreamGmlWriter.getXMLIndex(target);
    XmlElement sdsElement = InspireUtil.findSpatialDataSet(index);
    TypeDefinition sdsType = null;
    if (sdsElement != null) {
        sdsType = sdsElement.getType();
    }
    // applicable.
    if (sdsType == null)
        return;
    // find localId and namespace definitions
    Path<Definition<?>> localIdPath = new DefinitionAccessor(sdsType).findChildren("identifier").findChildren("Identifier").findChildren("localId").eval(false);
    List<Definition<?>> localIdDefs = localIdPath.getElements();
    PropertyDefinition localIdDef = (PropertyDefinition) localIdDefs.get(localIdDefs.size() - 1);
    Path<Definition<?>> namespacePath = new DefinitionAccessor(sdsType).findChildren("identifier").findChildren("Identifier").findChildren("namespace").eval(false);
    List<Definition<?>> namespaceDefs = namespacePath.getElements();
    PropertyDefinition nsDef = (PropertyDefinition) namespaceDefs.get(namespaceDefs.size() - 1);
    IPropertyChangeListener changeListener = new IPropertyChangeListener() {

        @Override
        public void propertyChange(PropertyChangeEvent event) {
            if (event.getProperty().equals(AttributeEditor.IS_VALID) || event.getProperty().equals(FileFieldEditor.IS_VALID))
                updateState();
        }
    };
    // inspire identifier
    Label inspireId = new Label(page, SWT.NONE);
    inspireId.setText("Please specify the local ID and the namespace as part of the INSPIRE identifier of the Spatial Data Set:");
    GridDataFactory.fillDefaults().span(2, 1).applyTo(inspireId);
    AttributeEditorFactory aef = PlatformUI.getWorkbench().getService(AttributeEditorFactory.class);
    Composite localIdtitle = new Composite(page, SWT.NONE);
    localIdtitle.setLayout(GridLayoutFactory.swtDefaults().numColumns(2).margins(0, 0).create());
    DefinitionLabelFactory dlf = PlatformUI.getWorkbench().getService(DefinitionLabelFactory.class);
    dlf.createLabel(localIdtitle, localIdDef, false);
    Label label = new Label(localIdtitle, SWT.NONE);
    label.setText(" = ");
    localIdEditor = aef.createEditor(page, localIdDef, null, false);
    localIdEditor.getControl().setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false));
    localIdEditor.setPropertyChangeListener(changeListener);
    Composite namespacetitle = new Composite(page, SWT.NONE);
    namespacetitle.setLayout(GridLayoutFactory.swtDefaults().numColumns(2).margins(0, 0).create());
    dlf.createLabel(namespacetitle, nsDef, false);
    label = new Label(namespacetitle, SWT.NONE);
    label.setText(" = ");
    namespaceEditor = aef.createEditor(page, nsDef, null, false);
    namespaceEditor.getControl().setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false));
    namespaceEditor.setPropertyChangeListener(changeListener);
    // spacer
    Composite spacer = new Composite(page, SWT.NONE);
    GridDataFactory.fillDefaults().hint(0, 8).applyTo(spacer);
    // metadata file
    Label metadataLabel = new Label(page, SWT.NONE);
    metadataLabel.setText("You can include metadata in the Spatial Data Set from a XML file with a MD_Metadata element:");
    GridDataFactory.fillDefaults().span(2, 1).applyTo(metadataLabel);
    // source file
    Composite fileFieldComposite = new Composite(page, SWT.NONE);
    fileFieldComposite.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false, 2, 1));
    metadataFile = new OpenFileFieldEditor("metadataFile", "ISO Geographic MetaData XML", true, FileFieldEditor.VALIDATE_ON_KEY_STROKE, fileFieldComposite);
    metadataFile.setPage(this);
    metadataFile.setEmptyStringAllowed(true);
    metadataFile.setFileExtensions(new String[] { "*.xml" });
    metadataFile.setPropertyChangeListener(changeListener);
    // isValid starts with false even if emptyStringAllowed is true.
    // -> force validation hack
    metadataFile.setStringValue(" ");
    metadataFile.setStringValue("");
    updateState();
}
Also used : IPropertyChangeListener(org.eclipse.jface.util.IPropertyChangeListener) PropertyChangeEvent(org.eclipse.jface.util.PropertyChangeEvent) DefinitionLabelFactory(eu.esdihumboldt.hale.ui.common.definition.DefinitionLabelFactory) Composite(org.eclipse.swt.widgets.Composite) SchemaSpace(eu.esdihumboldt.hale.common.schema.model.SchemaSpace) Definition(eu.esdihumboldt.hale.common.schema.model.Definition) PropertyDefinition(eu.esdihumboldt.hale.common.schema.model.PropertyDefinition) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) Label(org.eclipse.swt.widgets.Label) XmlIndex(eu.esdihumboldt.hale.io.xsd.model.XmlIndex) AttributeEditorFactory(eu.esdihumboldt.hale.ui.common.definition.AttributeEditorFactory) PropertyDefinition(eu.esdihumboldt.hale.common.schema.model.PropertyDefinition) SchemaSpaceID(eu.esdihumboldt.hale.common.schema.SchemaSpaceID) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) OpenFileFieldEditor(eu.esdihumboldt.hale.ui.util.io.OpenFileFieldEditor) SchemaService(eu.esdihumboldt.hale.ui.service.schema.SchemaService) DefinitionAccessor(eu.esdihumboldt.hale.common.schema.groovy.DefinitionAccessor) GridData(org.eclipse.swt.layout.GridData) XmlElement(eu.esdihumboldt.hale.io.xsd.model.XmlElement)

Aggregations

DefinitionAccessor (eu.esdihumboldt.hale.common.schema.groovy.DefinitionAccessor)3 Definition (eu.esdihumboldt.hale.common.schema.model.Definition)3 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)3 PropertyDefinition (eu.esdihumboldt.hale.common.schema.model.PropertyDefinition)2 XmlElement (eu.esdihumboldt.hale.io.xsd.model.XmlElement)2 IOMessageImpl (eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl)1 SchemaSpaceID (eu.esdihumboldt.hale.common.schema.SchemaSpaceID)1 CRSDefinition (eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition)1 SchemaSpace (eu.esdihumboldt.hale.common.schema.model.SchemaSpace)1 HasValueFlag (eu.esdihumboldt.hale.common.schema.model.constraint.type.HasValueFlag)1 XmlIndex (eu.esdihumboldt.hale.io.xsd.model.XmlIndex)1 AttributeEditorFactory (eu.esdihumboldt.hale.ui.common.definition.AttributeEditorFactory)1 DefinitionLabelFactory (eu.esdihumboldt.hale.ui.common.definition.DefinitionLabelFactory)1 SchemaService (eu.esdihumboldt.hale.ui.service.schema.SchemaService)1 OpenFileFieldEditor (eu.esdihumboldt.hale.ui.util.io.OpenFileFieldEditor)1 BufferedInputStream (java.io.BufferedInputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1