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);
}
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;
}
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);
}
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);
}
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);
}
Aggregations