use of eu.esdihumboldt.hale.common.core.io.supplier.FileIOSupplier in project hale by halestudio.
the class URILink method createDefaultSelectionAdapter.
// create the the SelectionAdapter for the UriLink
private SelectionAdapter createDefaultSelectionAdapter(final URI uri) {
return new SelectionAdapter() {
private URI removeFragment(URI uri) throws URISyntaxException {
String uristring = uri.toString();
uristring = uristring.substring(0, uristring.indexOf("#"));
return new URI(uristring);
}
// the URI has to be an existing file on the local drive or on a
// server
@Override
public void widgetSelected(SelectionEvent e) {
URI newuri = uri;
try {
// online resource
if (uri.getScheme().equals("http") || uri.getScheme().equals("https")) {
try {
Desktop.getDesktop().browse(uri);
} catch (IOException e1) {
MessageDialog.openWarning(Display.getCurrent().getActiveShell(), "Opening Error", "No default application set!");
}
return;
}
if (uri.toString().contains("#")) {
newuri = removeFragment(uri);
}
// local resource or bundle resource
if (DefaultInputSupplier.SCHEME_LOCAL.equals(newuri.getScheme()) || "bundleentry".equals(newuri.getScheme())) {
// cannot be opened by system
// so copy resource to temporary file
String name = newuri.getPath();
int index = name.lastIndexOf('/');
if (index >= 0) {
name = name.substring(index + 1);
}
if (!name.isEmpty()) {
File tmpFile = Files.createTempFile("resource", name).toFile();
try (OutputStream out = new FileIOSupplier(tmpFile).getOutput();
InputStream in = new DefaultInputSupplier(newuri).getInput()) {
ByteStreams.copy(in, out);
}
tmpFile.deleteOnExit();
newuri = tmpFile.toURI();
}
}
// try creating a file
File file = new File(newuri);
if (file.exists()) {
try {
Desktop.getDesktop().open(file);
} catch (IOException e2) {
try {
Desktop.getDesktop().browse(newuri);
} catch (IOException e1) {
MessageDialog.openWarning(Display.getCurrent().getActiveShell(), "Opening Error", "No default application set!");
}
}
} else {
try {
Desktop.getDesktop().browse(newuri);
} catch (IOException e1) {
MessageDialog.openWarning(Display.getCurrent().getActiveShell(), "Opening Error", "No default application set!");
}
}
} catch (Exception e1) {
// ignore
}
}
};
}
use of eu.esdihumboldt.hale.common.core.io.supplier.FileIOSupplier in project hale by halestudio.
the class XLSInstanceWriterTest method testWriteComplexSchema.
/**
* Test - write data of complex schema and analyze result
*
* @throws Exception , if an error occurs
*/
@Test
public void testWriteComplexSchema() throws Exception {
TransformationExample example = TransformationExamples.getExample(TransformationExamples.SIMPLE_COMPLEX);
// alternative the data could be generated by iterating through the
// exempleproject's sourcedata
List<String> header = Arrays.asList("id", "name", "details.age", "details.income", "details.address.street", "details.address.city");
List<String> firstDataRow = Arrays.asList("id0", "name0", "age0", "income0", "street0", "city0");
// set instances to xls instance writer
XLSInstanceWriter writer = new XLSInstanceWriter();
IContentType contentType = HalePlatform.getContentTypeManager().getContentType("eu.esdihumboldt.hale.io.xls.xls");
writer.setParameter(InstanceTableIOConstants.SOLVE_NESTED_PROPERTIES, Value.of(true));
File tmpFile = tmpFolder.newFile("excelTestWriteComplexSchema.xls");
writer.setInstances(example.getSourceInstances());
// write instances to a temporary XLS file
writer.setTarget(new FileIOSupplier(tmpFile));
writer.setContentType(contentType);
IOReport report = writer.execute(null);
assertTrue(report.isSuccess());
Workbook wb = WorkbookFactory.create(tmpFile);
Sheet sheet = wb.getSheetAt(0);
checkHeader(sheet, header);
checkSheetName(sheet, "person");
checkFirstDataRow(sheet, firstDataRow);
}
use of eu.esdihumboldt.hale.common.core.io.supplier.FileIOSupplier in project hale by halestudio.
the class StreamGmlWriterTest method fillFeatureTest.
/**
* Create a feature, fill it with values, write it as GML, validate the GML
* and load the GML file again to compare the loaded values with the ones
* that were written
*
* @param elementName the element name of the feature type to use, if
* <code>null</code> a random element will be used
* @param targetSchema the schema to use, the first element will be used for
* the type of the feature
* @param values the values to set on the feature
* @param testName the name of the test
* @param srsName the SRS name
* @param skipValueTest if the check for equality shall be skipped
* @param expectWriteFail if the GML writing is expected to fail
* @param windingOrderParam winding order parameter or <code>null</code>
* @return the validation report or the GML writing report if writing
* expected to fail
* @throws Exception if any error occurs
*/
private IOReport fillFeatureTest(String elementName, URI targetSchema, Map<List<QName>, Object> values, String testName, String srsName, boolean skipValueTest, boolean expectWriteFail, EnumWindingOrderTypes windingOrderParam) throws Exception {
// load the sample schema
XmlSchemaReader reader = new XmlSchemaReader();
reader.setSharedTypes(null);
reader.setSource(new DefaultInputSupplier(targetSchema));
IOReport schemaReport = reader.execute(null);
assertTrue(schemaReport.isSuccess());
XmlIndex schema = reader.getSchema();
XmlElement element = null;
if (elementName == null) {
element = schema.getElements().values().iterator().next();
if (element == null) {
// $NON-NLS-1$
fail("No element found in the schema");
}
} else {
for (XmlElement candidate : schema.getElements().values()) {
if (candidate.getName().getLocalPart().equals(elementName)) {
element = candidate;
break;
}
}
if (element == null) {
// $NON-NLS-1$ //$NON-NLS-2$
fail("Element " + elementName + " not found in the schema");
}
}
if (element == null) {
throw new IllegalStateException();
}
// create feature
MutableInstance feature = new DefaultInstance(element.getType(), null);
// set some values
for (Entry<List<QName>, Object> entry : values.entrySet()) {
MutableGroup parent = feature;
List<QName> properties = entry.getKey();
for (int i = 0; i < properties.size() - 1; i++) {
QName propertyName = properties.get(i);
DefinitionGroup def = parent.getDefinition();
Object[] vals = parent.getProperty(propertyName);
if (vals != null && vals.length > 0) {
Object value = vals[0];
if (value instanceof MutableGroup) {
parent = (MutableGroup) value;
} else {
MutableGroup child;
ChildDefinition<?> childDef = def.getChild(propertyName);
if (childDef.asProperty() != null || value != null) {
// create instance
child = new DefaultInstance(childDef.asProperty().getPropertyType(), null);
} else {
// create group
child = new DefaultGroup(childDef.asGroup());
}
if (value != null) {
// wrap value
((MutableInstance) child).setValue(value);
}
parent = child;
}
}
}
parent.addProperty(properties.get(properties.size() - 1), entry.getValue());
}
InstanceCollection instances = new DefaultInstanceCollection(Collections.singleton(feature));
// write to file
InstanceWriter writer = new GmlInstanceWriter();
if (windingOrderParam != null) {
writer.setParameter(GeoInstanceWriter.PARAM_UNIFY_WINDING_ORDER, Value.of(windingOrderParam));
}
writer.setInstances(instances);
DefaultSchemaSpace schemaSpace = new DefaultSchemaSpace();
schemaSpace.addSchema(schema);
writer.setTargetSchema(schemaSpace);
// $NON-NLS-1$
File outFile = File.createTempFile(testName, ".gml");
writer.setTarget(new FileIOSupplier(outFile));
if (windingOrderParam != null && windingOrderParam == EnumWindingOrderTypes.counterClockwise) {
assertTrue(writer.getParameter(GeoInstanceWriter.PARAM_UNIFY_WINDING_ORDER).as(EnumWindingOrderTypes.class) == EnumWindingOrderTypes.counterClockwise);
}
// new LogProgressIndicator());
IOReport report = writer.execute(null);
if (expectWriteFail) {
assertFalse("Writing the GML output should not be successful", report.isSuccess());
return report;
} else {
assertTrue("Writing the GML output not successful", report.isSuccess());
}
List<? extends Locatable> validationSchemas = writer.getValidationSchemas();
System.out.println(outFile.getAbsolutePath());
System.out.println(targetSchema.toString());
// if (!DEL_TEMP_FILES && Desktop.isDesktopSupported()) {
// Desktop.getDesktop().open(outFile);
// }
IOReport valReport = validate(outFile.toURI(), validationSchemas);
// load file
InstanceCollection loaded = loadGML(outFile.toURI(), schema);
ResourceIterator<Instance> it = loaded.iterator();
try {
assertTrue(it.hasNext());
if (!skipValueTest) {
Instance l = it.next();
// test values
for (Entry<List<QName>, Object> entry : values.entrySet()) {
// XXX conversion?
Object expected = entry.getValue();
// String propertyPath = Joiner.on('.').join(Collections2.transform(entry.getKey(), new Function<QName, String>() {
//
// @Override
// public String apply(QName input) {
// return input.toString();
// }
// }));
// Collection<Object> propValues = PropertyResolver.getValues(
// l, propertyPath, true);
// assertEquals(1, propValues.size());
// Object value = propValues.iterator().next();
Collection<GeometryProperty<?>> geoms = GeometryUtil.getAllGeometries(l);
assertEquals(1, geoms.size());
Object value = geoms.iterator().next().getGeometry();
if (expected instanceof Geometry && value instanceof Geometry) {
if (windingOrderParam == null || windingOrderParam == EnumWindingOrderTypes.noChanges) {
matchGeometries((Geometry) expected, (Geometry) value);
}
// Winding Order Test.
if (windingOrderParam != null) {
if (windingOrderParam == EnumWindingOrderTypes.counterClockwise) {
assertTrue(((Geometry) expected).getNumGeometries() == ((Geometry) value).getNumGeometries());
assertTrue(WindingOrder.isCounterClockwise((Geometry) value));
} else if (windingOrderParam == EnumWindingOrderTypes.clockwise) {
assertFalse(WindingOrder.isCounterClockwise((Geometry) value));
} else {
assertTrue(WindingOrder.isCounterClockwise((Geometry) value) == WindingOrder.isCounterClockwise((Geometry) expected));
}
} else {
// TODO check winding order is CCW
if (value instanceof Polygon || value instanceof MultiPolygon)
assertTrue(WindingOrder.isCounterClockwise((Geometry) value));
}
} else {
assertEquals(expected.toString(), value.toString());
}
}
assertFalse(it.hasNext());
}
} finally {
it.close();
}
if (DEL_TEMP_FILES) {
outFile.deleteOnExit();
}
return valReport;
}
use of eu.esdihumboldt.hale.common.core.io.supplier.FileIOSupplier in project hale by halestudio.
the class ExamplesContent method getMappingContent.
/**
* Get the mapping documentation content for an example project.
*
* @param projectId the project ID
* @return the mapping documentation content stream or <code>null</code>
*/
private InputStream getMappingContent(String projectId) {
if (!mappingDocExportInitialized) {
mappingDocExport = HaleIO.createIOProvider(AlignmentWriter.class, null, ID_MAPPING_EXPORT);
if (mappingDocExport == null) {
log.error("Could not create mapping documentation exporter.");
}
mappingDocExportInitialized = true;
}
if (mappingDocExport == null) {
// no mapping documentation export possible
return null;
}
if (tempMappingDir == null) {
tempMappingDir = Files.createTempDir();
tempMappingDir.deleteOnExit();
}
// the file of the mapping documentation
File mappingDoc = new File(tempMappingDir, projectId + ".html");
if (!mappingDoc.exists()) {
ATransaction trans = log.begin("Generate example mapping documentation");
try {
// create the mapping documentation
ExampleProject exampleProject = ExampleProjectExtension.getInstance().get(projectId);
final Project project = (Project) exampleProject.getInfo();
// determine alignment location - contained in project file, not
// a resource
URI alignmentLoc = exampleProject.getAlignmentLocation();
if (alignmentLoc == null) {
// no alignment present
return null;
}
// store configurations per action ID
Multimap<String, IOConfiguration> confs = HashMultimap.create();
for (IOConfiguration conf : project.getResources()) {
confs.put(conf.getActionId(), conf);
}
// load schemas
// source schemas
LoadSchemaAdvisor source = new LoadSchemaAdvisor(SchemaSpaceID.SOURCE);
for (IOConfiguration conf : confs.get(SchemaIO.ACTION_LOAD_SOURCE_SCHEMA)) {
source.setConfiguration(conf);
executeProvider(source, conf.getProviderId(), null);
}
// target schemas
LoadSchemaAdvisor target = new LoadSchemaAdvisor(SchemaSpaceID.TARGET);
for (IOConfiguration conf : confs.get(SchemaIO.ACTION_LOAD_TARGET_SCHEMA)) {
target.setConfiguration(conf);
executeProvider(target, conf.getProviderId(), null);
}
// load alignment
// manual loading needed, as we can't rely on the environment
// alignment advisor
DefaultInputSupplier alignmentIn = new DefaultInputSupplier(alignmentLoc);
AlignmentReader reader = HaleIO.findIOProvider(AlignmentReader.class, alignmentIn, alignmentLoc.getPath());
LoadAlignmentAdvisor alignmentAdvisor = new LoadAlignmentAdvisor(null, source.getSchemaSpace(), target.getSchemaSpace(), exampleProject.getUpdater());
reader.setSource(alignmentIn);
executeProvider(alignmentAdvisor, null, reader);
Alignment alignment = alignmentAdvisor.getAlignment();
if (alignment != null) {
// save alignment docu
synchronized (mappingDocExport) {
// only a single instance
mappingDocExport.setAlignment(alignment);
mappingDocExport.setTarget(new FileIOSupplier(mappingDoc));
if (mappingDocExport instanceof ProjectInfoAware) {
ProjectInfo smallInfo = new ProjectInfo() {
@Override
public String getName() {
return project.getName();
}
@Override
public Date getModified() {
return null;
}
@Override
public Version getHaleVersion() {
return null;
}
@Override
public String getDescription() {
return project.getDescription();
}
@Override
public Date getCreated() {
return null;
}
@Override
public String getAuthor() {
return project.getAuthor();
}
};
// project);
((ProjectInfoAware) mappingDocExport).setProjectInfo(smallInfo);
}
mappingDocExport.execute(null);
}
mappingDoc.deleteOnExit();
}
} catch (Throwable e) {
log.error("Error generating mapping documentation for example project", e);
return null;
} finally {
trans.end();
}
}
if (mappingDoc.exists()) {
try {
return new FileInputStream(mappingDoc);
} catch (FileNotFoundException e) {
return null;
}
} else
return null;
}
use of eu.esdihumboldt.hale.common.core.io.supplier.FileIOSupplier in project hale by halestudio.
the class GenerateDefaults method writeAlignment.
private void writeAlignment() throws Exception {
System.out.println("Writing alignment to " + context.getOut().getAbsolutePath());
// create alignment writer
IContentType contentType = HalePlatform.getContentTypeManager().getContentType(ALIGNMENT_CONTENT_TYPE);
IOProviderDescriptor factory = HaleIO.findIOProviderFactory(AlignmentWriter.class, contentType, null);
AlignmentWriter writer = (AlignmentWriter) factory.createExtensionObject();
// configure alignment writer
writer.setTargetSchema(new DefaultSchemaSpace().addSchema(schema));
writer.setTarget(new FileIOSupplier(context.getOut()));
writer.setAlignment(alignment);
IOReport report = writer.execute(new NullProgressIndicator());
if (!report.isSuccess() || !report.getErrors().isEmpty()) {
throw new IllegalStateException("Errors while writing the alignment.");
} else {
System.out.println("Completed successfully.");
}
}
Aggregations