use of org.eclipse.emf.ecore.impl.EObjectImpl in project tdi-studio-se by Talend.
the class ImportItemUtil method resolveItem.
public void resolveItem(ResourcesManager manager, ItemRecord itemRecord) {
if (itemRecord.isResolved()) {
return;
}
InputStream stream = null;
try {
final Item item = itemRecord.getItem();
boolean byteArray = (item instanceof FileItem);
IPath itemPath = getItemPath(itemRecord.getPath(), item);
Set<IPath> paths = manager.getPaths();
// check the item file
if (!paths.contains(itemPath)) {
itemRecord.addError(itemRecord.getItemName() + " " + Messages.getString("ImportItemUtil.MissingItemFile") + " - " + itemPath);
log.error(itemRecord.getItemName() + " " + Messages.getString("ImportItemUtil.MissingItemFile") + " - " + //$NON-NLS-1$
itemPath);
return;
}
stream = manager.getStream(itemPath);
Resource resource = createResource(itemRecord, itemPath, byteArray);
if (byteArray) {
// TDI-24612
// This part fixes a problem of import of routines from .tar.gz.
// Seems either the Tar stream or emf got problems to read this.
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int i = 0;
while ((i = stream.read(buf)) != -1) {
baos.write(buf, 0, i);
}
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
resource.load(bais, null);
} else {
resource.load(stream, null);
}
for (ReferenceFileItem rfItem : (List<ReferenceFileItem>) item.getReferenceResources()) {
itemPath = getReferenceItemPath(itemRecord.getPath(), rfItem.getExtension());
stream = manager.getStream(itemPath);
Resource rfResource = createResource(itemRecord, itemPath, true);
rfResource.load(stream, null);
}
Iterator<EObject> itRef = item.eCrossReferences().iterator();
IPath parentPath = itemRecord.getPath().removeLastSegments(1);
while (itRef.hasNext()) {
EObject object = itRef.next();
String linkedFile = EcoreUtil.getURI(object).toFileString();
IPath linkedPath = parentPath.append(linkedFile);
if (!paths.contains(linkedPath)) {
if (linkedFile != null && !linkedFile.equals(itemPath.lastSegment()) && linkedFile.endsWith(itemPath.getFileExtension())) {
if (object.eIsProxy()) {
// if original href of the item point to some missing item file
// and if we can get the original item file from the name, recover it, but add a warning
((EObjectImpl) object).eSetProxyURI(URI.createFileURI(itemPath.lastSegment()));
log.warn(itemRecord.getItemName() + " " + Messages.getString("ImportItemUtil.NotHrefCurrentItemFile") + " - " + //$NON-NLS-1$
itemRecord.getPath());
}
}
}
EcoreUtil.resolve(object, resource);
}
} catch (IOException e) {
// ignore
} finally {
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
// ignore
}
}
}
itemRecord.setResolved(true);
}
Aggregations