Search in sources :

Example 1 with Value

use of eu.esdihumboldt.hale.common.core.io.Value in project hale by halestudio.

the class CustomTypeContentConfigurationType method fromDOM.

@Override
public CustomTypeContentConfiguration fromDOM(Element fragment, Void context) {
    Value val = DOMValueUtil.fromTag(fragment);
    ValueList list = val.as(ValueList.class);
    List<CustomTypeContentAssociation> resultList = new ArrayList<>();
    if (list != null) {
        for (Value value : list) {
            CustomTypeContentAssociation assoc = value.as(CustomTypeContentAssociation.class);
            if (assoc != null) {
                resultList.add(assoc);
            }
        }
    }
    return new CustomTypeContentConfiguration(resultList);
}
Also used : CustomTypeContentAssociation(eu.esdihumboldt.hale.io.xsd.anytype.CustomTypeContentAssociation) CustomTypeContentConfiguration(eu.esdihumboldt.hale.io.xsd.anytype.CustomTypeContentConfiguration) ValueList(eu.esdihumboldt.hale.common.core.io.ValueList) Value(eu.esdihumboldt.hale.common.core.io.Value) ArrayList(java.util.ArrayList)

Example 2 with Value

use of eu.esdihumboldt.hale.common.core.io.Value in project hale by halestudio.

the class AbstractAppSchemaConfigurator method updateTargetSchemaResources.

// TODO: code adapted from ArchiveProjectWriter: how to avoid duplication?
private Map<URI, String> updateTargetSchemaResources(File targetDirectory, ProgressIndicator progress, IOReporter reporter) throws IOException {
    progress.begin("Copy resources", ProgressIndicator.UNKNOWN);
    Project project = (Project) getProjectInfo();
    // resource locations mapped to new resource path
    Map<URI, String> handledResources = new HashMap<>();
    try {
        List<IOConfiguration> resources = project.getResources();
        // every resource needs his own directory
        int count = 0;
        Iterator<IOConfiguration> iter = resources.iterator();
        while (iter.hasNext()) {
            IOConfiguration resource = iter.next();
            if (resource.getActionId().equals(SchemaIO.ACTION_LOAD_TARGET_SCHEMA)) {
                // get resource path
                Map<String, Value> providerConfig = resource.getProviderConfiguration();
                String path = providerConfig.get(ImportProvider.PARAM_SOURCE).toString();
                URI pathUri;
                try {
                    pathUri = new URI(path);
                } catch (URISyntaxException e1) {
                    reporter.error(new IOMessageImpl("Skipped resource because of invalid URI: " + path, e1));
                    continue;
                }
                // check if path was already handled
                if (handledResources.containsKey(pathUri)) {
                    // skip copying the resource
                    continue;
                }
                String scheme = pathUri.getScheme();
                LocatableInputSupplier<? extends InputStream> input = null;
                if (scheme != null) {
                    if (scheme.equals("http") || scheme.equals("https") || scheme.equals("file") || scheme.equals("platform") || scheme.equals("bundle") || scheme.equals("jar")) {
                        input = new DefaultInputSupplier(pathUri);
                    } else {
                        continue;
                    }
                } else {
                    // now can't open that, can we?
                    reporter.error(new IOMessageImpl("Skipped resource because it cannot be loaded from " + pathUri.toString(), null));
                    continue;
                }
                progress.setCurrentTask("Copying resource at " + path);
                // every resource file is copied into an own resource
                // directory in the target directory
                String resourceFolder = "_schemas";
                if (count > 0) {
                    resourceFolder += count;
                }
                File newDirectory = new File(targetDirectory, resourceFolder);
                try {
                    newDirectory.mkdir();
                } catch (SecurityException e) {
                    throw new IOException("Can not create directory " + newDirectory.toString(), e);
                }
                // the filename
                String name = path.toString().substring(path.lastIndexOf("/") + 1, path.length());
                // remove any query string from the filename
                int queryIndex = name.indexOf('?');
                if (queryIndex >= 0) {
                    name = name.substring(0, queryIndex);
                }
                if (name.isEmpty()) {
                    name = "file";
                }
                File newFile = new File(newDirectory, name);
                Path target = newFile.toPath();
                // retrieve the resource advisor
                Value ct = providerConfig.get(ImportProvider.PARAM_CONTENT_TYPE);
                IContentType contentType = null;
                if (ct != null) {
                    contentType = HalePlatform.getContentTypeManager().getContentType(ct.as(String.class));
                }
                ResourceAdvisor ra = ResourceAdvisorExtension.getInstance().getAdvisor(contentType);
                // copy the resource
                progress.setCurrentTask("Copying resource at " + path);
                ra.copyResource(input, target, contentType, true, reporter);
                // store new path for resource
                String newPath = resourceFolder + "/" + name;
                handledResources.put(pathUri, newPath);
                count++;
            }
        }
    } finally {
        progress.end();
    }
    return handledResources;
}
Also used : Path(java.nio.file.Path) DefaultInputSupplier(eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier) HashMap(java.util.HashMap) IOConfiguration(eu.esdihumboldt.hale.common.core.io.project.model.IOConfiguration) IOMessageImpl(eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl) IContentType(org.eclipse.core.runtime.content.IContentType) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) URI(java.net.URI) ResourceAdvisor(eu.esdihumboldt.hale.common.core.io.ResourceAdvisor) Project(eu.esdihumboldt.hale.common.core.io.project.model.Project) Value(eu.esdihumboldt.hale.common.core.io.Value) File(java.io.File)

Example 3 with Value

use of eu.esdihumboldt.hale.common.core.io.Value in project hale by halestudio.

the class AbstractScriptedPropertyTransformation method evaluate.

@Override
protected final ListMultimap<String, Object> evaluate(String transformationIdentifier, E engine, ListMultimap<String, PropertyValue> variables, ListMultimap<String, PropertyEntityDefinition> resultNames, Map<String, String> executionParameters, TransformationLog log) throws TransformationException {
    ListMultimap<String, ParameterValue> originalParameters = getParameters();
    ListMultimap<String, Value> transformedParameters = ArrayListMultimap.create();
    if (originalParameters != null) {
        for (Map.Entry<String, ParameterValue> entry : originalParameters.entries()) {
            if (!entry.getValue().needsProcessing()) {
                Value value = entry.getValue().intern();
                if (!value.isRepresentedAsDOM()) {
                    value = Value.simple(getExecutionContext().getVariables().replaceVariables(value.getStringRepresentation()));
                }
                transformedParameters.put(entry.getKey(), value);
            } else {
                // type is a script
                ScriptFactory factory = ScriptExtension.getInstance().getFactory(entry.getValue().getType());
                if (factory == null)
                    throw new TransformationException("Couldn't find factory for script id " + entry.getValue().getType());
                Script script;
                try {
                    script = factory.createExtensionObject();
                } catch (Exception e) {
                    throw new TransformationException("Couldn't create script from factory", e);
                }
                Object result;
                try {
                    String scriptStr = entry.getValue().as(String.class);
                    if (script.requiresReplacedTransformationVariables()) {
                        // replace transformation variables
                        scriptStr = getExecutionContext().getVariables().replaceVariables(scriptStr);
                    }
                    result = script.evaluate(scriptStr, variables.values(), getExecutionContext());
                } catch (ScriptException e) {
                    throw new TransformationException("Couldn't evaluate a transformation parameter", e);
                }
                // XXX use conversion service instead of valueOf?
                transformedParameters.put(entry.getKey(), Value.simple(result));
            }
        }
    }
    this.transformedParameters = Multimaps.unmodifiableListMultimap(transformedParameters);
    return evaluateImpl(transformationIdentifier, engine, variables, resultNames, executionParameters, log);
}
Also used : Script(eu.esdihumboldt.hale.common.scripting.Script) TransformationException(eu.esdihumboldt.hale.common.align.transformation.function.TransformationException) ParameterValue(eu.esdihumboldt.hale.common.align.model.ParameterValue) TransformationException(eu.esdihumboldt.hale.common.align.transformation.function.TransformationException) ScriptException(javax.script.ScriptException) ScriptException(javax.script.ScriptException) ParameterValue(eu.esdihumboldt.hale.common.align.model.ParameterValue) PropertyValue(eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue) Value(eu.esdihumboldt.hale.common.core.io.Value) Map(java.util.Map) ScriptFactory(eu.esdihumboldt.hale.common.scripting.ScriptFactory)

Example 4 with Value

use of eu.esdihumboldt.hale.common.core.io.Value in project hale by halestudio.

the class AbstractCombinedValidatorFactory method restore.

@Override
public Validator restore(Value value) throws Exception {
    ValueList list = value.as(ValueList.class);
    List<Validator> children = new ArrayList<>();
    for (Value childVal : list) {
        children.add(childVal.as(ValidatorValue.class).toValidator());
    }
    return createValidator(children);
}
Also used : ValueList(eu.esdihumboldt.hale.common.core.io.ValueList) ArrayList(java.util.ArrayList) ValidatorValue(eu.esdihumboldt.hale.common.schema.model.validate.factory.ValidatorValue) Value(eu.esdihumboldt.hale.common.core.io.Value) CombinedValidator(eu.esdihumboldt.util.validator.CombinedValidator) Validator(eu.esdihumboldt.util.validator.Validator)

Example 5 with Value

use of eu.esdihumboldt.hale.common.core.io.Value in project hale by halestudio.

the class EnumerationFactory method restore.

@Override
public Enumeration<?> restore(Value value, Definition<?> definition, TypeResolver typeIndex, ClassResolver resolver) throws Exception {
    ValueProperties props = value.as(ValueProperties.class);
    boolean allowOthers = props.get(P_ALLOW_OTHERS).as(Boolean.class, true);
    Collection<Object> values = null;
    if (props.containsKey(P_VALUES)) {
        values = new ArrayList<>();
        ValueList list = props.get(P_VALUES).as(ValueList.class);
        for (Value val : list) {
            // XXX determine value type?
            // XXX for now just use string
            String str = val.as(String.class);
            if (str != null) {
                values.add(str);
            } else {
            // TODO warn?
            }
        }
    }
    return new Enumeration<Object>(values, allowOthers);
}
Also used : Enumeration(eu.esdihumboldt.hale.common.schema.model.constraint.type.Enumeration) ValueProperties(eu.esdihumboldt.hale.common.core.io.ValueProperties) ValueList(eu.esdihumboldt.hale.common.core.io.ValueList) Value(eu.esdihumboldt.hale.common.core.io.Value)

Aggregations

Value (eu.esdihumboldt.hale.common.core.io.Value)81 ValueList (eu.esdihumboldt.hale.common.core.io.ValueList)12 LookupTable (eu.esdihumboldt.hale.common.lookup.LookupTable)12 HashMap (java.util.HashMap)12 ValueProperties (eu.esdihumboldt.hale.common.core.io.ValueProperties)11 LookupTableImpl (eu.esdihumboldt.hale.common.lookup.impl.LookupTableImpl)11 IOConfiguration (eu.esdihumboldt.hale.common.core.io.project.model.IOConfiguration)10 ParameterValue (eu.esdihumboldt.hale.common.align.model.ParameterValue)9 ArrayList (java.util.ArrayList)9 URI (java.net.URI)8 Test (org.junit.Test)6 File (java.io.File)5 StyledString (org.eclipse.jface.viewers.StyledString)5 DefaultCustomPropertyFunction (eu.esdihumboldt.hale.common.align.custom.DefaultCustomPropertyFunction)4 PropertyValue (eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue)4 IOMessageImpl (eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl)4 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)4 ProjectService (eu.esdihumboldt.hale.ui.service.project.ProjectService)4 IOException (java.io.IOException)4 Locale (java.util.Locale)4