source: default/trunk/de.ugoe.cs.swe.bnftools.xtools/src/resource/tools/xtools/ResourceTool.java

Last change on this file was 68, checked in by hkaulbersch, 10 years ago

1.)Changed the outline
2.)Added a PopupButton? for codegeneration(not running yet)

  • Property svn:mime-type set to text/plain
File size: 7.4 KB
Line 
1package resource.tools.xtools;
2
3import java.io.File;
4import java.io.IOException;
5import java.util.HashMap;
6import java.util.Map;
7import java.util.Properties;
8
9import org.eclipse.emf.common.util.BasicDiagnostic;
10import org.eclipse.emf.common.util.Diagnostic;
11import org.eclipse.emf.common.util.EList;
12import org.eclipse.emf.common.util.TreeIterator;
13import org.eclipse.emf.common.util.URI;
14import org.eclipse.emf.ecore.EObject;
15import org.eclipse.emf.ecore.EOperation;
16import org.eclipse.emf.ecore.EPackage;
17import org.eclipse.emf.ecore.EStructuralFeature;
18import org.eclipse.emf.ecore.EValidator;
19import org.eclipse.emf.ecore.resource.Resource;
20import org.eclipse.emf.ecore.resource.ResourceSet;
21import org.eclipse.emf.ecore.resource.impl.FileURIHandlerImpl;
22import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
23import org.eclipse.emf.ecore.util.Diagnostician;
24import org.eclipse.emf.ecore.util.EObjectValidator;
25import org.eclipse.emf.ecore.util.EcoreUtil;
26import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;
27import org.eclipse.emf.ecore.xmi.impl.XMIResourceImpl;
28import org.eclipse.ocl.common.OCLConstants;
29import org.eclipse.ocl.ecore.delegate.OCLInvocationDelegateFactory;
30import org.eclipse.ocl.ecore.delegate.OCLSettingDelegateFactory;
31import org.eclipse.ocl.ecore.delegate.OCLValidationDelegateFactory;
32import org.eclipse.xtext.resource.SaveOptions;
33import org.eclipse.xtext.resource.XtextResource;
34import org.eclipse.xtext.resource.XtextResourceSet;
35import org.eclipse.xtext.serializer.impl.Serializer;
36import org.slf4j.Logger;
37import org.slf4j.LoggerFactory;
38import org.slf4j.impl.SimpleLogger;
39
40import com.google.inject.Injector;
41
42public class ResourceTool {
43
44        protected static Logger log;
45        protected Injector injector;
46
47        public ResourceTool() {
48                System.setProperty(Logger.class.getName(),SimpleLogger.class.getName());
49                System.setProperty("org.slf4j.simpleLogger.logFile","validation.log");
50                System.setProperty("org.slf4j.simpleLogger.logFile","System.out");
51                log = LoggerFactory.getLogger(ResourceTool.class);     
52        }
53
54        public Resource loadResourceFromXtext(String workspace, String pathName, boolean resolveAll) {
55                // "workspace" is a string that contains the path to the workspace containing the DSL program.
56                new org.eclipse.emf.mwe.utils.StandaloneSetup().setPlatformUri(workspace);
57               
58                XtextResourceSet resourceSet = injector.getInstance(XtextResourceSet.class);
59
60                //TODO:why is this not needed for FAMIX but needed for DAG?
61                if (resolveAll) {
62                        resourceSet.addLoadOption(XtextResource.OPTION_RESOLVE_ALL, Boolean.TRUE);
63                }
64
65                Resource resource = resourceSet.getResource(URI.createURI(pathName), true);
66                for (org.eclipse.emf.ecore.resource.Resource.Diagnostic diagnostic : resource.getErrors()) {
67                        log.warn("xtext: "+diagnostic.getLine() +" :"+diagnostic.getMessage());
68                }
69                return resource;
70        }
71
72        @SuppressWarnings("restriction")
73        public Resource storeResourceInXtext(String workspace, String pathName, EList<EObject> contents) {
74                // "workspace" is a string that contains the path to the workspace containing the DSL program.
75                new org.eclipse.emf.mwe.utils.StandaloneSetup().setPlatformUri(workspace);
76               
77                XtextResourceSet resourceSet = injector.getInstance(XtextResourceSet.class);
78
79                String s = "";
80                Serializer serializer = injector.getInstance(Serializer.class);
81
82                try { 
83                        EObject tdlPackage = contents.get(0);
84                        s += serializer.serialize(tdlPackage); 
85                } catch (Exception ex) { // fall back: 
86                        System.out.println("Model could not be serialized");
87                        ex.printStackTrace();
88                } 
89//              TreeIterator<Object> iterator = EcoreUtil.getAllContents(contents);
90//              while (iterator.hasNext()) {
91//                      EObject next = (EObject) iterator.next();
92//                      try { 
93//                              s += serializer.serialize(next)+"\n"; 
94//                      } catch (Exception ex) { // fall back: 
95//                              System.out.println(next.eClass().getName() +" could not be serialized"); 
96//                      } 
97//              }
98                System.out.println(s);
99               
100//              try {
101////                    FileUtils.writeStringToFile(new File(pathName), s);
102//              } catch (IOException e) {
103//                      // TODO Auto-generated catch block
104//                      e.printStackTrace();
105//              }
106                Resource resource = null;
107                //Resource resource = loadResourceFromXtext(workspace, pathName, true);
108
109                return resource;
110        }
111
112       
113        @SuppressWarnings({ "rawtypes", "unchecked" })
114        public Resource loadResourceFromXMI(String inputPath, String extension) {
115            Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE;
116            Map<String, Object> m = reg.getExtensionToFactoryMap();
117                m.put(extension, new XMIResourceFactoryImpl());
118            ResourceSet resSetIn = new ResourceSetImpl();
119            Resource inputResource = resSetIn.createResource(URI.createURI(inputPath));
120            try {
121                Map options = new HashMap<>();
122                options.put(XMIResourceImpl.OPTION_DEFER_IDREF_RESOLUTION, Boolean.TRUE);
123//              options.put(XMIResourceImpl.OPTION_PROCESS_DANGLING_HREF, XMIResourceImpl.OPTION_PROCESS_DANGLING_HREF_DISCARD);
124                        inputResource.load(options);
125                } catch (IOException e) {
126                        e.printStackTrace();
127                }
128                return inputResource;
129        }
130
131        protected void initializeValidator() {
132        //              OCL.initialize(null);
133                        String oclDelegateURI = OCLConstants.OCL_DELEGATE_URI+"/Pivot";
134                       
135                    EOperation.Internal.InvocationDelegate.Factory.Registry.INSTANCE.put(oclDelegateURI,
136                        new OCLInvocationDelegateFactory(oclDelegateURI));
137                    EStructuralFeature.Internal.SettingDelegate.Factory.Registry.INSTANCE.put(oclDelegateURI,
138                        new OCLSettingDelegateFactory(oclDelegateURI));
139                    EValidator.ValidationDelegate.Registry.INSTANCE.put(oclDelegateURI,
140                        new OCLValidationDelegateFactory(oclDelegateURI));
141                   
142        //          EStructuralFeature.Internal.SettingDelegate.Factory.Registry.INSTANCE.put(oclDelegateURI,
143        //              new OCLSettingDelegateFactory.Global());
144        //          QueryDelegate.Factory.Registry.INSTANCE.put(oclDelegateURI, new OCLQueryDelegateFactory.Global());
145                   
146                }
147
148        public void validateResource(Resource resource) {
149            BasicDiagnostic diagnostics = new BasicDiagnostic();
150            boolean valid = true;
151            for (EObject eo : resource.getContents())
152            {
153                Map<Object, Object> context = new HashMap<Object, Object>();
154                boolean validationResult = Diagnostician.INSTANCE.validate(eo, diagnostics, context);
155                showDiagnostics(diagnostics, "");
156                        valid &= validationResult;
157            }
158           
159            if (!valid){
160                System.out.println("Problem with validation!");
161            }
162        }
163
164        protected void showDiagnostics(Diagnostic diagnostics, String indent) {
165                indent+="  ";
166                for (Diagnostic d : diagnostics.getChildren()){
167                        log.warn(indent+d.getSource());
168                        log.warn(indent+"  "+d.getMessage());
169                        showDiagnostics(d,indent);
170                }
171        }
172
173        @SuppressWarnings({ "unchecked", "rawtypes" })
174        protected void storeResourceContents(EList<EObject> contents, String outputPath, String extension) {
175                //TODO: duplicated from loadResourceFromXMI => move to a more appropriate location
176                Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE;
177            Map<String, Object> m = reg.getExtensionToFactoryMap();
178                m.put(extension, new XMIResourceFactoryImpl());
179               
180            ResourceSet resSet = new ResourceSetImpl();
181                Resource outputResource = resSet.createResource(URI.createURI(outputPath));
182            outputResource.getContents().addAll(contents);
183            try {
184              Map options = new HashMap<>();
185              options.put(XMIResourceImpl.OPTION_ENCODING, "UTF-8");
186//            options.put(XMIResourceImpl.OPTION_PROCESS_DANGLING_HREF, XMIResourceImpl.OPTION_PROCESS_DANGLING_HREF_DISCARD);
187              outputResource.save(options);
188            } catch (IOException e) {
189              e.printStackTrace();
190            }
191        }
192
193}
Note: See TracBrowser for help on using the repository browser.