use of org.eclipse.emf.common.util.URI in project mechanoid by robotoworks.
the class NewMechanoidOpsFileWizard method createElementResource.
@Override
protected IResource createElementResource(IProgressMonitor monitor, IPath path) {
try {
if (monitor == null) {
monitor = new NullProgressMonitor();
}
monitor.beginTask(Messages.NewMechanoidOpsFileWizard_Progress_Message, 1);
URI newEmfResourceURI = URI.createURI(//$NON-NLS-1$
"platform:/resource" + path.toPortableString());
Resource emfResource = mResourceSet.createResource(newEmfResourceURI);
Model model = OpServiceModelFactory.eINSTANCE.createModel();
model.setPackageName(mSelectedPackageName);
emfResource.getContents().add(model);
ServiceBlock service = (ServiceBlock) OpServiceModelFactory.eINSTANCE.createServiceBlock();
service.setName(mSelectedElementName);
model.setService(service);
emfResource.save(Collections.EMPTY_MAP);
IResource resource = ResourcesPlugin.getWorkspace().getRoot().findMember(newEmfResourceURI.toPlatformString(true));
monitor.worked(1);
return resource;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
use of org.eclipse.emf.common.util.URI in project mechanoid by robotoworks.
the class NewMechanoidNetFileWizard method createElementResource.
@Override
protected IResource createElementResource(IProgressMonitor monitor, IPath path) {
try {
if (monitor == null) {
monitor = new NullProgressMonitor();
}
monitor.beginTask(Messages.NewMechanoidNetFileWizard_Progress_Message, 1);
URI newEmfResourceURI = URI.createURI(//$NON-NLS-1$
"platform:/resource" + path.toPortableString());
Resource emfResource = mResourceSet.createResource(newEmfResourceURI);
Model model = NetModelFactory.eINSTANCE.createModel();
model.setPackageName(mSelectedPackageName);
emfResource.getContents().add(model);
Client client = (Client) NetModelFactory.eINSTANCE.createClient();
client.setName(mSelectedElementName);
if (!Strings.isNullOrEmpty(mSelectedBaseUrl)) {
client.setBaseUrl(mSelectedBaseUrl);
}
model.getDeclarations().add(client);
emfResource.save(Collections.EMPTY_MAP);
IResource resource = ResourcesPlugin.getWorkspace().getRoot().findMember(newEmfResourceURI.toPlatformString(true));
monitor.worked(1);
return resource;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
use of org.eclipse.emf.common.util.URI in project tdi-studio-se by Talend.
the class ComponentsFactory method loadComponentResource.
/**
* DOC guanglong.du Comment method "loadComponentResource".
*
* @param eclipseProject
* @return
* @throws IOException
*/
private ComponentsCache loadComponentResource(String installLocation) throws IOException {
String filePath = ComponentsFactory.TALEND_COMPONENT_CACHE + LanguageManager.getCurrentLanguage().toString().toLowerCase() + ComponentsFactory.TALEND_FILE_NAME;
URI uri = URI.createFileURI(installLocation).appendSegment(filePath);
ComponentCacheResourceFactoryImpl compFact = new ComponentCacheResourceFactoryImpl();
Resource resource = compFact.createResource(uri);
Map optionMap = new HashMap();
optionMap.put(XMLResource.OPTION_DEFER_ATTACHMENT, Boolean.TRUE);
optionMap.put(XMLResource.OPTION_DEFER_IDREF_RESOLUTION, Boolean.TRUE);
optionMap.put(XMLResource.OPTION_USE_PARSER_POOL, new XMLParserPoolImpl());
optionMap.put(XMLResource.OPTION_USE_XML_NAME_TO_FEATURE_MAP, new HashMap());
optionMap.put(XMLResource.OPTION_USE_DEPRECATED_METHODS, Boolean.FALSE);
resource.load(optionMap);
ComponentsCache cache = (ComponentsCache) EcoreUtil.getObjectByType(resource.getContents(), ComponentCachePackage.eINSTANCE.getComponentsCache());
return cache;
}
use of org.eclipse.emf.common.util.URI in project tdi-studio-se by Talend.
the class MergeTosMetadataMigrationTask method execute.
@Override
public ExecutionResult execute(Item item) {
if (item instanceof ConnectionItem) {
try {
URI itemURI = getItemURI(item);
if (itemURI != null) {
URI itemResourceURI = getItemResourceURI(itemURI);
if (metadata400to410 == null) {
metadata400to410 = new TosMetadataMigrationFrom400to410();
}
Resource migratedResource = metadata400to410.migrate(itemResourceURI.toString(), new NullProgressMonitor());
HashMap<String, Object> xmlSaveOtions = XML_SAVE_OTIONS_1_0;
if (migratedResource != null) {
// check for DB connection caus we need to setup Schema and Catalog properly
EObject content = migratedResource.getContents().get(0);
if (content != null && "DatabaseConnection".equals(content.eClass().getName())) {
// resource is dynamic EMF so convert it to static model by serialising it and reloading it
ByteArrayOutputStream tempStream = new ByteArrayOutputStream();
try {
// serialize into memory
try {
migratedResource.save(tempStream, XML_SAVE_OTIONS_1_0);
} catch (Exception e) {
// try with version 1.1
tempStream = new ByteArrayOutputStream();
xmlSaveOtions = XML_SAVE_OTIONS_1_1;
migratedResource.save(tempStream, xmlSaveOtions);
} finally {
tempStream.close();
}
// create a resource to laod the inmemory resource that should be a static EMF model
migratedResource = resourceSet.createResource(URI.createURI(//$NON-NLS-1$
"http://talend/dummy_static.metadata"));
migratedResource.load(new ByteArrayInputStream(tempStream.toByteArray()), xmlSaveOtions);
// check that DBConnection is firdt element
DatabaseConnection databaseConnection = SwitchHelpers.DATABASECONNECTION_SWITCH.doSwitch(migratedResource.getContents().get(0));
// do not check for null caus DB connection is already check above
String databaseType = databaseConnection.getDatabaseType();
databaseConnection.setDriverClass(ExtractMetaDataUtils.getInstance().getDriverClassByDbType(databaseType));
EDatabaseTypeName currentType = EDatabaseTypeName.getTypeFromDbType(databaseType);
EDatabaseSchemaOrCatalogMapping curCatalog = currentType.getCatalogMappingField();
EDatabaseSchemaOrCatalogMapping curSchema = currentType.getSchemaMappingField();
// all the DB connection are migrated with a Schema by default
if (!curCatalog.equals(EDatabaseSchemaOrCatalogMapping.None)) {
List<Schema> schemas = ConnectionHelper.getSchema(databaseConnection);
if (!curSchema.equals(EDatabaseSchemaOrCatalogMapping.None)) {
// we need to place the current schemas into a catalogs
ConnectionHelper.removeSchemas(schemas, databaseConnection);
for (Schema schema : schemas) {
// compute the name of the schema and the catalogs
String schemaName = computeSchemaName(schema, databaseConnection, curSchema);
String catalogName = computeCatalogName(databaseConnection, curCatalog);
schema.setName(schemaName);
Catalog catalog = RelationalFactory.eINSTANCE.createCatalog();
// catalogs are not in a contained reference
migratedResource.getContents().add(catalog);
catalog.setName(catalogName);
// add the schema to the catalog and the the catalog to the connection
CatalogHelper.addSchemas(Collections.singleton(schema), catalog);
ConnectionHelper.addCatalog(catalog, databaseConnection);
}
} else {
// we need to replace the Schemas with a Catalogs
for (Schema schema : schemas) {
// compute the name the catalog
String catalogName = computeCatalogName(databaseConnection, curCatalog);
// use owned elements to get everything regardless of tables or views or
// else
Catalog catalog = RelationalFactory.eINSTANCE.createCatalog();
// catalogs are not in a contained reference
migratedResource.getContents().add(catalog);
catalog.setName(catalogName);
catalog.getOwnedElement().addAll(schema.getOwnedElement());
ConnectionHelper.addCatalog(catalog, databaseConnection);
ConnectionHelper.removeSchemas(Collections.singleton(schema), databaseConnection);
}
}
} else if (!curSchema.equals(EDatabaseSchemaOrCatalogMapping.None)) {
List<Schema> schemas = ConnectionHelper.getSchema(databaseConnection);
for (Schema schema : schemas) {
String schemaName = computeSchemaName(schema, databaseConnection, curSchema);
schema.setName(schemaName);
}
}
// else no catalog so we keep the schema as is
} catch (Exception e) {
// we have an exception finalising the migration but we trap it caus we still try to
// save it
log.error("Cannot complete merge metadata migration on file:" + itemResourceURI.toString(), e);
ExceptionHandler.process(e);
} finally {
tempStream.close();
}
}
// else not a DB connection so persist
OutputStream outputStream = item.eResource().getResourceSet().getURIConverter().createOutputStream(itemResourceURI, null);
try {
migratedResource.save(outputStream, xmlSaveOtions);
} finally {
outputStream.close();
}
}
return ExecutionResult.SUCCESS_WITH_ALERT;
}
} catch (ATLCoreException e) {
log.error(e);
ExceptionHandler.process(e);
return ExecutionResult.SUCCESS_NO_ALERT;
} catch (IOException e) {
log.error(e);
ExceptionHandler.process(e);
return ExecutionResult.SUCCESS_NO_ALERT;
} finally {
resourceSet.getResources().clear();
}
}
return ExecutionResult.NOTHING_TO_DO;
}
use of org.eclipse.emf.common.util.URI in project tdi-studio-se by Talend.
the class MetadataEmfFactory method getOutputStream.
@SuppressWarnings("unchecked")
public OutputStream getOutputStream() throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
//$NON-NLS-1$
URI uri = URI.createURI("");
Resource res = new TalendFileResourceImpl(uri);
res.getContents().add(metadataType);
res.save(out, null);
return out;
}
Aggregations