Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/smeup/asup.git
Browse files Browse the repository at this point in the history
  • Loading branch information
giugianc73 committed Jan 29, 2016
2 parents e249d16 + 8f01144 commit 17bf422
Show file tree
Hide file tree
Showing 156 changed files with 1,341 additions and 1,084 deletions.
3 changes: 2 additions & 1 deletion org.smeup.sys.dk.source.jdt/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ Bundle-ClassPath: .
Require-Bundle: org.smeup.sys.dk.source;visibility:=reexport,
org.eclipse.core.runtime,
org.eclipse.core.resources,
org.eclipse.jdt.core
org.eclipse.jdt.core,
org.eclipse.emf.ecore.xmi
Bundle-RequiredExecutionEnvironment: JavaSE-1.7
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.smeup.sys.dk.source.QProject;
import org.smeup.sys.dk.source.QSourceNode;

public class JDTProjectAdapter implements QProject {

Expand Down Expand Up @@ -85,11 +84,6 @@ public QProject getProject() {
return this;
}

@Override
public QSourceNode getParent() {
return null;
}

@Override
public boolean isRoot() {
return true;
Expand Down
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);
}

}
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,18 @@
import org.eclipse.core.runtime.CoreException;
import org.smeup.sys.dk.source.QProject;
import org.smeup.sys.dk.source.QSourceEntry;
import org.smeup.sys.il.core.QObjectNameable;

public class JDTSourceEntryFileAdapter implements QSourceEntry {

private static final long serialVersionUID = 1L;

private transient JDTObjectSerializer serializer;
private QProject project;
private transient IFile file;

public JDTSourceEntryFileAdapter(QProject project, IFile file) {
public JDTSourceEntryFileAdapter(JDTObjectSerializer serializer, QProject project, IFile file) {
this.serializer = serializer;
this.project = project;
this.file = file;
}
Expand All @@ -41,12 +44,6 @@ public URI getLocation() {
return file.getRawLocationURI();
}

@Override
public QSourceEntry getParent() {
// TODO Auto-generated method stub
return null;
}

@Override
public QProject getProject() {
return project;
Expand All @@ -64,7 +61,6 @@ public InputStream getInputStream() throws IOException {
} catch (CoreException e) {
throw new IOException(e);
}
// return new FileInputStream(getLocation().getRawPath());
}

@Override
Expand Down Expand Up @@ -100,4 +96,8 @@ public String getText() {
}
}

@Override
public <T extends QObjectNameable> T load(Class<T> type) throws IOException {
return serializer.deserialize(getProject(), type, getName(), getInputStream());
}
}
Loading

0 comments on commit 17bf422

Please sign in to comment.