-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/smeup/asup.git
- Loading branch information
Showing
156 changed files
with
1,341 additions
and
1,084 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
org.smeup.sys.dk.source.jdt/src/org/smeup/sys/dk/source/jdt/JDTObjectSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/** | ||
* Copyright (c) 2012, 2015 Sme.UP and others. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* | ||
* Contributors: | ||
* Mattia Rocchi - Initial API and implementation | ||
*/ | ||
package org.smeup.sys.dk.source.jdt; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.Collections; | ||
|
||
import org.eclipse.emf.common.util.URI; | ||
import org.eclipse.emf.ecore.EObject; | ||
import org.eclipse.emf.ecore.impl.BasicEObjectImpl; | ||
import org.eclipse.emf.ecore.resource.ResourceSet; | ||
import org.smeup.sys.dk.source.QProject; | ||
import org.smeup.sys.il.core.QObjectNameable; | ||
|
||
public class JDTObjectSerializer { | ||
|
||
private ResourceSet resourceSet; | ||
|
||
public JDTObjectSerializer(ResourceSet resourceSet) { | ||
this.resourceSet = resourceSet; | ||
} | ||
|
||
public <T extends QObjectNameable> ByteArrayOutputStream serialize(QProject project, Class<T> klass, String name, T object) throws IOException { | ||
|
||
URI uri = buildURI(project, klass, name); | ||
|
||
JDTResourceImpl resource = (JDTResourceImpl) resourceSet.getResource(uri, false); | ||
if (resource == null) | ||
resource = (JDTResourceImpl) resourceSet.createResource(uri, "asup"); | ||
else | ||
clearResource(resource); | ||
|
||
((BasicEObjectImpl) object).eSetResource(null, null); | ||
resource.getContents().add((EObject) object); | ||
|
||
ByteArrayOutputStream output = new ByteArrayOutputStream(); | ||
resource.doSave(output, Collections.EMPTY_MAP); | ||
|
||
return output; | ||
|
||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public <T extends QObjectNameable> T deserialize(QProject project, Class<T> klass, String name, InputStream inputStream) throws IOException { | ||
|
||
URI uri = buildURI(project, klass, name); | ||
|
||
JDTResourceImpl resource = (JDTResourceImpl) resourceSet.getResource(uri, false); | ||
if (resource == null) | ||
resource = (JDTResourceImpl) resourceSet.createResource(uri, "asup"); | ||
else | ||
clearResource(resource); | ||
|
||
resource.doLoad(inputStream, resourceSet.getLoadOptions()); | ||
if (resource.getContents().isEmpty()) | ||
return null; | ||
|
||
EObject eObject = resource.getContents().get(0); | ||
|
||
return (T) eObject; | ||
|
||
} | ||
|
||
private void clearResource(JDTResourceImpl resource) { | ||
BasicEObjectImpl oldObject = null; | ||
if (resource.isLoaded() && !resource.getContents().isEmpty()) { | ||
oldObject = (BasicEObjectImpl) resource.getContents().get(0); | ||
resource.getContents().clear(); | ||
oldObject.eSetResource(resource, null); | ||
} | ||
} | ||
|
||
private <T extends QObjectNameable> URI buildURI(QProject project, Class<T> klass, String name) { | ||
|
||
String uri = "asup://" + "TODO" + "/" + project.getName() + "/" + klass.getSimpleName().toLowerCase().substring(1) + "#" + name; | ||
URI eURI = URI.createURI(uri); | ||
eURI.appendFragment(name); | ||
return eURI; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
org.smeup.sys.dk.source.jdt/src/org/smeup/sys/dk/source/jdt/JDTResourceFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/** | ||
* Copyright (c) 2012, 2015 Sme.UP and others. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* | ||
* Contributors: | ||
* Mattia Rocchi - Initial API and implementation | ||
*/ | ||
package org.smeup.sys.dk.source.jdt; | ||
|
||
import org.eclipse.emf.common.util.URI; | ||
import org.eclipse.emf.ecore.resource.Resource; | ||
import org.eclipse.emf.ecore.resource.impl.ResourceFactoryImpl; | ||
|
||
public class JDTResourceFactory extends ResourceFactoryImpl { | ||
|
||
@Override | ||
public Resource createResource(URI uri) { | ||
return new JDTResourceImpl(uri); | ||
} | ||
|
||
} |
33 changes: 33 additions & 0 deletions
33
org.smeup.sys.dk.source.jdt/src/org/smeup/sys/dk/source/jdt/JDTResourceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* Copyright (c) 2012, 2015 Sme.UP and others. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* | ||
* Contributors: | ||
* Mattia Rocchi - Initial API and implementation | ||
*/ | ||
package org.smeup.sys.dk.source.jdt; | ||
|
||
import org.eclipse.emf.common.util.URI; | ||
import org.eclipse.emf.ecore.xmi.impl.XMIResourceImpl; | ||
import org.smeup.sys.il.core.QNameable; | ||
|
||
|
||
public class JDTResourceImpl extends XMIResourceImpl implements QNameable { | ||
|
||
public JDTResourceImpl(URI uri) { | ||
super(uri); | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
URI uri = getURI(); | ||
if (uri.segmentCount() == 0) | ||
return null; | ||
|
||
return uri.segment(0); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.