diff options
5 files changed, 337 insertions, 22 deletions
diff --git a/plugins/org.eclipse.emf.cdo.edit/src/org/eclipse/emf/cdo/edit/CDOItemProviderAdapter.java b/plugins/org.eclipse.emf.cdo.edit/src/org/eclipse/emf/cdo/edit/CDOItemProviderAdapter.java index e7d5514499..03b67ebec6 100644 --- a/plugins/org.eclipse.emf.cdo.edit/src/org/eclipse/emf/cdo/edit/CDOItemProviderAdapter.java +++ b/plugins/org.eclipse.emf.cdo.edit/src/org/eclipse/emf/cdo/edit/CDOItemProviderAdapter.java @@ -936,7 +936,13 @@ public class CDOItemProviderAdapter extends ItemProviderAdapter if (object instanceof EObject) { CDOObject cdoObject = CDOUtil.getCDOObject((EObject)object); - return cdoObject.eResource().getURI().toString() + "#" + cdoObject.cdoID().toURIFragment(); //$NON-NLS-1$ + Resource resource = cdoObject.eResource(); + if (resource != null) + { + CDOID id = cdoObject.cdoID(); + String fragment = id != null ? id.toURIFragment() : resource.getURIFragment(cdoObject); + return resource.getURI().appendFragment(fragment).toString(); + } } return null; diff --git a/plugins/org.eclipse.emf.cdo.efs/src/org/eclipse/emf/cdo/internal/efs/CDOFileStore.java b/plugins/org.eclipse.emf.cdo.efs/src/org/eclipse/emf/cdo/internal/efs/CDOFileStore.java index 2682a9593b..994199d27d 100644 --- a/plugins/org.eclipse.emf.cdo.efs/src/org/eclipse/emf/cdo/internal/efs/CDOFileStore.java +++ b/plugins/org.eclipse.emf.cdo.efs/src/org/eclipse/emf/cdo/internal/efs/CDOFileStore.java @@ -11,6 +11,8 @@ *******************************************************************************/ package org.eclipse.emf.cdo.internal.efs; +import org.eclipse.emf.cdo.common.revision.CDORevision; +import org.eclipse.emf.cdo.eresource.CDOResource; import org.eclipse.emf.cdo.eresource.CDOResourceFolder; import org.eclipse.emf.cdo.eresource.CDOResourceNode; import org.eclipse.emf.cdo.internal.efs.bundle.OM; @@ -33,7 +35,6 @@ import org.xml.sax.InputSource; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; -import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; @@ -114,7 +115,7 @@ public final class CDOFileStore extends AbstractFileStore if (isProjectDescription()) { - info.setExists(getProjectDescription() != null); + info.setExists(getProjectName() != null); info.setDirectory(false); } else @@ -199,27 +200,17 @@ public final class CDOFileStore extends AbstractFileStore try { monitor.beginTask("", 1); //$NON-NLS-1$ + ByteArrayOutputStream baos = new ByteArrayOutputStream(); if (isProjectDescription()) { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - PrintStream out = new PrintStream(baos); - out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); - out.println("<projectDescription>"); - out.println(" <name>" + OM.getProjectName(root.toURI()) + "</name>"); - out.println(" <comment></comment>"); - out.println(" <projects>"); - out.println(" </projects>"); - out.println(" <buildSpec>"); - out.println(" </buildSpec>"); - out.println(" <natures>"); - out.println(" </natures>"); - out.println("</projectDescription>"); - out.flush(); - - return new ByteArrayInputStream(baos.toByteArray()); + openProjectDescription(baos); + } + else + { + openResource(baos); } - return new FileInputStream(path.toPortableString()); + return new ByteArrayInputStream(baos.toByteArray()); } catch (Exception ex) { @@ -232,7 +223,7 @@ public final class CDOFileStore extends AbstractFileStore } } - private String getProjectDescription() + private String getProjectName() { return OM.getProjectName(root.toURI()); } @@ -242,6 +233,30 @@ public final class CDOFileStore extends AbstractFileStore return path.equals(PROJECT_DESCRIPTION_PATH); } + private void openProjectDescription(ByteArrayOutputStream baos) + { + PrintStream out = new PrintStream(baos); + out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); + out.println("<projectDescription>"); + out.println(" <name>" + OM.getProjectName(root.toURI()) + "</name>"); + out.println(" <comment></comment>"); + out.println(" <projects>"); + out.println(" </projects>"); + out.println(" <buildSpec>"); + out.println(" </buildSpec>"); + out.println(" <natures>"); + out.println(" </natures>"); + out.println("</projectDescription>"); + out.flush(); + } + + private void openResource(ByteArrayOutputStream baos) throws IOException + { + CDOResource resource = (CDOResource)getResourceNode(); + resource.cdoPrefetch(CDORevision.DEPTH_INFINITE); + resource.save(baos, null); + } + @Override public boolean equals(Object obj) { diff --git a/plugins/org.eclipse.emf.cdo.tests/src/org/eclipse/emf/cdo/tests/ResourceTest.java b/plugins/org.eclipse.emf.cdo.tests/src/org/eclipse/emf/cdo/tests/ResourceTest.java index 7cdd23f9d4..a1270c4a18 100644 --- a/plugins/org.eclipse.emf.cdo.tests/src/org/eclipse/emf/cdo/tests/ResourceTest.java +++ b/plugins/org.eclipse.emf.cdo.tests/src/org/eclipse/emf/cdo/tests/ResourceTest.java @@ -22,6 +22,7 @@ import org.eclipse.emf.cdo.session.CDOSession; import org.eclipse.emf.cdo.tests.model1.Category; import org.eclipse.emf.cdo.tests.model1.Company; import org.eclipse.emf.cdo.tests.model1.Order; +import org.eclipse.emf.cdo.tests.model1.OrderDetail; import org.eclipse.emf.cdo.tests.model1.Product1; import org.eclipse.emf.cdo.tests.model1.VAT; import org.eclipse.emf.cdo.transaction.CDOTransaction; @@ -40,7 +41,10 @@ import org.eclipse.emf.ecore.resource.Resource; import org.eclipse.emf.ecore.resource.ResourceSet; import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; import org.eclipse.emf.ecore.util.EcoreUtil; +import org.eclipse.emf.ecore.xmi.XMIResource; +import org.eclipse.emf.ecore.xmi.impl.XMIResourceImpl; +import java.io.ByteArrayOutputStream; import java.util.ArrayList; import java.util.List; @@ -51,6 +55,138 @@ import junit.framework.Assert; */ public class ResourceTest extends AbstractCDOTest { + public void testSaveXMI() throws Exception + { + ByteArrayOutputStream cdoOUT = new ByteArrayOutputStream(); + { + CDOSession session = openSession(); + CDOTransaction transaction = session.openTransaction(); + CDOResource resource = transaction.createResource("/folder/res1"); + + counter = 0; + Category category = createCategoryTree(3); + resource.getContents().add(category); + + transaction.commit(); + resource.save(cdoOUT, null); + } + + ByteArrayOutputStream xmiOUT = new ByteArrayOutputStream(); + { + XMIResource resource = new XMIResourceImpl(URI.createFileURI("/folder/res1")); + + counter = 0; + Category category = createCategoryTree(3); + resource.getContents().add(category); + + resource.save(xmiOUT, null); + } + + String xmiString = xmiOUT.toString(); + msg("XMI:\n\n" + xmiString); + + String cdoString = cdoOUT.toString(); + msg("CDO:\n\n" + cdoString); + + assertEquals(xmiString, cdoString); + } + + public void testSaveXMI_WithXRef() throws Exception + { + ByteArrayOutputStream cdoOUT = new ByteArrayOutputStream(); + { + CDOSession session = openSession(); + CDOTransaction transaction = session.openTransaction(); + CDOResource resource = transaction.createResource("/folder/res1"); + + counter = 0; + Category category = createCategoryTree(3); + resource.getContents().add(category); + + OrderDetail orderDetail = getModel1Factory().createOrderDetail(); + orderDetail.setPrice(147.111f); + orderDetail.setProduct(category.getProducts().get(0)); + resource.getContents().add(orderDetail); + + transaction.commit(); + resource.save(cdoOUT, null); + } + + ByteArrayOutputStream xmiOUT = new ByteArrayOutputStream(); + { + XMIResource resource = new XMIResourceImpl(URI.createFileURI("/folder/res1")); + + counter = 0; + Category category = createCategoryTree(3); + resource.getContents().add(category); + + OrderDetail orderDetail = getModel1Factory().createOrderDetail(); + orderDetail.setPrice(147.111f); + orderDetail.setProduct(category.getProducts().get(0)); + resource.getContents().add(orderDetail); + + resource.save(xmiOUT, null); + } + + String xmiString = xmiOUT.toString(); + msg("XMI:\n\n" + xmiString); + + String cdoString = cdoOUT.toString(); + msg("CDO:\n\n" + cdoString); + + // TODO assertEquals(xmiString, cdoString); + } + + public void testSaveXMI_WithXRef_OtherResource() throws Exception + { + ByteArrayOutputStream cdoOUT = new ByteArrayOutputStream(); + { + CDOSession session = openSession(); + CDOTransaction transaction = session.openTransaction(); + CDOResource resource = transaction.createResource("/folder/res1"); + CDOResource resource2 = transaction.createResource("/folder/res2"); + + counter = 0; + Category category = createCategoryTree(3); + resource.getContents().add(category); + + OrderDetail orderDetail = getModel1Factory().createOrderDetail(); + orderDetail.setPrice(147.111f); + orderDetail.setProduct(category.getProducts().get(0)); + resource2.getContents().add(orderDetail); + + transaction.commit(); + resource.save(cdoOUT, null); + resource2.save(cdoOUT, null); + } + + ByteArrayOutputStream xmiOUT = new ByteArrayOutputStream(); + { + XMIResource resource = new XMIResourceImpl(URI.createFileURI("/folder/res1")); + XMIResource resource2 = new XMIResourceImpl(URI.createFileURI("/folder/res2")); + + counter = 0; + Category category = createCategoryTree(3); + resource.getContents().add(category); + + OrderDetail orderDetail = getModel1Factory().createOrderDetail(); + orderDetail.setPrice(147.111f); + orderDetail.setProduct(category.getProducts().get(0)); + resource2.getContents().add(orderDetail); + + resource.save(xmiOUT, null); + resource2.save(xmiOUT, null); + } + + String xmiString = xmiOUT.toString(); + msg("XMI:\n\n" + xmiString); + + String cdoString = cdoOUT.toString(); + msg("CDO:\n\n" + cdoString); + + // TODO assertEquals(xmiString, cdoString); + } + public void testAttachDetachResourceDepth1_Delete() throws Exception { attachDetachResourceDepth1(1, true, 0); diff --git a/plugins/org.eclipse.emf.cdo/src/org/eclipse/emf/cdo/eresource/CDOResource.java b/plugins/org.eclipse.emf.cdo/src/org/eclipse/emf/cdo/eresource/CDOResource.java index 272fdf7a49..cb691e00c4 100644 --- a/plugins/org.eclipse.emf.cdo/src/org/eclipse/emf/cdo/eresource/CDOResource.java +++ b/plugins/org.eclipse.emf.cdo/src/org/eclipse/emf/cdo/eresource/CDOResource.java @@ -57,6 +57,12 @@ public interface CDOResource extends CDOResourceNode, Resource public static final String OPTION_SAVE_OVERRIDE_TRANSACTION = CDOTransaction.class.getName(); /** + * @ADDED + * @since 4.0 + */ + public static final String OPTION_SAVE_BASE_URI = "OPTION_SAVE_BASE_URI"; + + /** * Returns the value of the '<em><b>Resource Set</b></em>' attribute. <!-- begin-user-doc --> * <p> * If the meaning of the '<em>Resource Set</em>' attribute isn't clear, there really should be more of a description diff --git a/plugins/org.eclipse.emf.cdo/src/org/eclipse/emf/cdo/eresource/impl/CDOResourceImpl.java b/plugins/org.eclipse.emf.cdo/src/org/eclipse/emf/cdo/eresource/impl/CDOResourceImpl.java index 1bcad41f0e..fa3d26d7d7 100644 --- a/plugins/org.eclipse.emf.cdo/src/org/eclipse/emf/cdo/eresource/impl/CDOResourceImpl.java +++ b/plugins/org.eclipse.emf.cdo/src/org/eclipse/emf/cdo/eresource/impl/CDOResourceImpl.java @@ -49,7 +49,12 @@ import org.eclipse.emf.ecore.resource.ResourceSet; import org.eclipse.emf.ecore.resource.URIConverter; import org.eclipse.emf.ecore.resource.impl.ExtensibleURIConverterImpl; import org.eclipse.emf.ecore.resource.impl.ResourceImpl; +import org.eclipse.emf.ecore.util.EcoreUtil; import org.eclipse.emf.ecore.util.InternalEList; +import org.eclipse.emf.ecore.xmi.XMIResource; +import org.eclipse.emf.ecore.xmi.XMLHelper; +import org.eclipse.emf.ecore.xmi.impl.XMIHelperImpl; +import org.eclipse.emf.ecore.xmi.impl.XMIResourceImpl; import org.eclipse.emf.spi.cdo.InternalCDOObject; import org.eclipse.emf.spi.cdo.InternalCDOTransaction; import org.eclipse.emf.spi.cdo.InternalCDOView; @@ -59,7 +64,10 @@ import org.eclipse.core.runtime.IProgressMonitor; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.util.ArrayList; +import java.util.HashMap; import java.util.Iterator; +import java.util.List; import java.util.Map; /** @@ -759,7 +767,151 @@ public class CDOResourceImpl extends CDOResourceNodeImpl implements CDOResource, */ public void save(OutputStream outputStream, Map<?, ?> options) throws IOException { - // Do nothing + final String baseURI; + if (options != null) + { + String uri = (String)options.get(OPTION_SAVE_BASE_URI); + if (uri != null) + { + baseURI = uri; + } + else + { + baseURI = "cdo://"; + } + } + else + { + baseURI = "cdo://"; + } + + final Map<CDOResource, Resource> resourceMappings = new HashMap<CDOResource, Resource>(); + class ExportResource extends XMIResourceImpl + { + private CDOResource delegate; + + public ExportResource(CDOResource delegate) + { + super(URI.createURI(baseURI + delegate.getPath())); + this.delegate = delegate; + } + + @Override + public EList<EObject> getContents() + { + return delegate.getContents(); + } + + @Override + public String getURIFragment(EObject eObject) + { + String id = EcoreUtil.getID(eObject); + if (id != null) + { + return id; + } + + InternalEObject internalEObject = (InternalEObject)eObject; + if (getMappedResource(internalEObject.eDirectResource()) == this) + { + return "/" + getURIFragmentRootSegment(eObject); + } + + List<String> uriFragmentPath = new ArrayList<String>(); + boolean isContained = false; + for (InternalEObject container = internalEObject.eInternalContainer(); container != null; container = internalEObject + .eInternalContainer()) + { + uriFragmentPath.add(container.eURIFragmentSegment(internalEObject.eContainingFeature(), internalEObject)); + internalEObject = container; + if (getMappedResource(container.eDirectResource()) == this) + { + isContained = true; + break; + } + } + + if (!isContained) + { + return "/-1"; + } + + StringBuilder result = new StringBuilder("/"); + result.append(getURIFragmentRootSegment(internalEObject)); + + for (int i = uriFragmentPath.size() - 1; i >= 0; --i) + { + result.append('/'); + result.append(uriFragmentPath.get(i)); + } + + return result.toString(); + } + + @Override + protected XMLHelper createXMLHelper() + { + return new XMIHelperImpl(this) + { + @Override + public String getHREF(EObject obj) + { + InternalEObject o = (InternalEObject)obj; + + if (obj instanceof CDOObject) + { + System.out.println(obj); + } + + URI objectURI = o.eProxyURI(); + if (objectURI == null) + { + Resource otherResource = obj.eResource(); + otherResource = getMappedResource(otherResource); + objectURI = getHREF(otherResource, obj); + } + + objectURI = deresolve(objectURI); + return objectURI.toString(); + } + + @Override + protected URI getHREF(Resource otherResource, EObject obj) + { + String uriFragment = getURIFragment(otherResource, obj); + if (otherResource == ExportResource.this) + { + return URI.createURI(uriFragment); + } + + return otherResource.getURI().appendFragment(uriFragment); + } + + }; + } + + private Resource getMappedResource(Resource otherResource) + { + Resource resource = resourceMappings.get(otherResource); + if (resource != null) + { + return resource; + } + + if (otherResource instanceof CDOResource) + { + CDOResource cdoResource = (CDOResource)otherResource; + otherResource = new ExportResource(cdoResource); + resourceMappings.put(cdoResource, otherResource); + } + + return otherResource; + } + } + + XMIResource xmiResource = new ExportResource(this); + resourceMappings.put(this, xmiResource); + xmiResource.save(outputStream, options); } /** |