Example usage for org.eclipse.jdt.internal.formatter DefaultCodeFormatter DefaultCodeFormatter

List of usage examples for org.eclipse.jdt.internal.formatter DefaultCodeFormatter DefaultCodeFormatter

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.formatter DefaultCodeFormatter DefaultCodeFormatter.

Prototype

public DefaultCodeFormatter() 

Source Link

Usage

From source file:de.tub.tfs.henshin.tgg.interpreter.gui.TranslationJob.java

License:Open Source License

protected IStatus run(IProgressMonitor monitor) {
    if (inputFile == null)
        return Status.OK_STATUS;
    if (this.getThread() != null) {
        this.getThread().setName("TranslationJob " + this.getThread().getName());
    }//from w ww .  ja  va  2s . c  o m
    synchronized (lock) {
        // check that grammar is loaded
        if (LoadHandler.trSystems.size() == 0) {
            return new Status(RUNNING, "tgg-plugin", "Transformation System was not loaded");
        }

        // clear list of rules from previous executions
        //TggUtil.initClassConversions();
        ExecutionTimes executionTimes = new ExecutionTimes();
        try {
            monitor.beginTask("Translating " + inputURI.lastSegment(), 3);
            System.out.println("=====");
            System.out.println("Translating: " + inputURI.lastSegment());
            long time0 = System.currentTimeMillis();
            monitor.subTask("Loading input");
            ResourceSet resSet = new ResourceSetImpl();
            HashMap<String, Object> options = new HashMap<String, Object>();
            options.put(XMLResource.OPTION_EXTENDED_META_DATA, Boolean.TRUE);
            options.put(XMLResource.OPTION_EXTENDED_META_DATA, Boolean.TRUE);
            options.put(XMLResource.OPTION_SCHEMA_LOCATION, Boolean.TRUE);
            options.put(XMLResource.OPTION_USE_ENCODED_ATTRIBUTE_STYLE, Boolean.TRUE);
            options.put(XMLResource.OPTION_USE_ENCODED_ATTRIBUTE_STYLE, Boolean.TRUE);
            options.put(XMLResource.OPTION_USE_LEXICAL_HANDLER, Boolean.TRUE);

            resSet.getLoadOptions().putAll(options);
            Resource res = resSet.getResource(inputURI, true);

            // Print out syntax parsing errors 
            // and abort translation if errors occur
            EList<Diagnostic> errors = res.getErrors();
            if (!errors.isEmpty()) {
                String msg = "===========================\n";
                msg += "Translation failed. No output was generated. The following syntax errors occured while parsing:\n";
                for (Diagnostic d : errors) {
                    msg += "(" + inputURI.lastSegment() + ") line " + d.getLine() + ": " + d.getMessage()
                            + "\n";
                    msg += "-------------------------------\n";
                }
                msg += "===========================\n";
                throw new RuntimeException(msg);
            }

            EObject inputRoot = (EObject) res.getContents().get(0);
            inputEObjects = res.getContents(); // add all of root
            tggTransformation = new TggTransformationImpl();
            tggTransformation.setStartTime(Long.parseLong(engineOptions.get("_StartTime")));
            tggTransformation.setInput(inputEObjects);
            tggTransformation.opRulesList.clear();

            // Validate input AST based on custom constraints 
            org.eclipse.emf.common.util.Diagnostic validation_result = Diagnostician.INSTANCE
                    .validate(inputRoot);
            if (!validation_result.getChildren().isEmpty()) {
                String msg = "===========================\n";
                msg += "Translation failed. No output was generated. The following syntax errors occured while parsing:\n";
                for (org.eclipse.emf.common.util.Diagnostic d : validation_result.getChildren()) {
                    msg += "(" + inputURI.lastSegment() + ") " + d.getMessage() + " (" + d.getSource() + ")\n";
                    msg += "-------------------------------\n";
                }
                msg += "===========================\n";
                throw new RuntimeException(msg);
            }

            emfEngine = tggTransformation.getEmfEngine();
            JavaScriptTggInitializer.registerWithScriptingEngine(emfEngine.getScriptEngine());
            emfEngine.getScriptEngine().put("emfEngine", emfEngine);
            Bindings bindings = emfEngine.getScriptEngine().getBindings(ScriptContext.ENGINE_SCOPE);
            synchronized (this.engineOptions) {
                String init = this.engineOptions.get("Initialisation");
                if (init == null) {
                    for (Entry<String, String> entry : this.engineOptions.entrySet()) {

                        bindings.put(entry.getKey(), entry.getValue());
                    }
                } else {
                    for (Entry<String, String> entry : this.engineOptions.entrySet()) {
                        bindings.put(entry.getKey(), entry.getValue());
                    }

                    init = this.engineOptions.get("Initialisation");
                    if (init != null) {
                        if (init.startsWith("{*")) {
                            init = init.substring(2, init.length() - 2);
                        }
                        try {
                            //org.eclipse.core.runtime.Platform

                            emfEngine.getScriptEngine().eval(init);
                        } catch (Throwable e) {
                            System.err.println("Error while initialising Grammar!");
                            e.printStackTrace();
                        }

                        this.engineOptions.remove("Initialisation");
                    }
                }
            }

            this.emfEngine.updateOptions();

            long time1 = System.currentTimeMillis();
            long stage1 = time1 - time0;
            System.out.println("Stage 1 -- Loading: " + stage1 + " ms");
            monitor.worked(1);
            if (monitor.isCanceled()) {
                monitor.done();
                return Status.CANCEL_STATUS;
            }

            Iterator<TGG> moduleIt = LoadHandler.trSystems.iterator();
            List<String> fileNames = LoadHandler.trFileNames;

            boolean foundApplicationForRound = false;
            boolean foundApplicationForModule = false;

            boolean newMatchesArePossible = true;
            boolean initialRound = true;
            // execute all modules as long as there are matches
            TGG module = null;
            ArrayList<Module> modules = new ArrayList<Module>();
            ArrayList<List<Rule>> opRules = new ArrayList<List<Rule>>();

            // retrieve modules and lists of operational rules
            while (moduleIt.hasNext()) {
                module = moduleIt.next();
                modules.add(module);
                opRules.add(getOpRules(module));
                //for (Module module2 : priorModules) {
                //   opRules.add(getOpRules(module2));
                //}
                //priorModules.add(module);
            }

            while (newMatchesArePossible) {
                foundApplicationForRound = false;
                // execute all modules once
                for (int modulePos = 0; modulePos < modules.size(); modulePos++) {
                    tggTransformation.setOpRuleList(opRules.get(modulePos));
                    tggTransformation.setNullValueMatching(modules.get(modulePos).isNullValueMatching());

                    String trFileName = fileNames.get(modulePos);
                    monitor.subTask("Applying " + trFileName);
                    if (debug)
                        System.out.println("Applying " + trFileName);

                    foundApplicationForModule = tggTransformation.applyRules(monitor, "Applying " + trFileName,
                            debug);
                    monitor.worked(1);
                    if (monitor.isCanceled()) {
                        monitor.done();
                        return Status.CANCEL_STATUS;
                    }
                    foundApplicationForRound = foundApplicationForRound || foundApplicationForModule;

                    for (int i = 0; i < modulePos; i++) {

                        tggTransformation.setOpRuleList(opRules.get(i));
                        tggTransformation.setNullValueMatching(modules.get(i).isNullValueMatching());

                        trFileName = fileNames.get(i);
                        monitor.subTask("Applying " + trFileName);
                        if (debug)
                            System.out.println("Applying " + trFileName);

                        foundApplicationForModule = tggTransformation.applyRules(monitor,
                                "Applying " + trFileName, debug);
                        monitor.worked(1);
                        if (monitor.isCanceled()) {
                            monitor.done();
                            return Status.CANCEL_STATUS;
                        }
                        foundApplicationForRound = foundApplicationForRound || foundApplicationForModule;
                    }
                }

                if (!initialRound && foundApplicationForRound)
                    System.out.println(
                            "Warning: some ruleapplications depend on rules that are applied in a subsequent module. This can cause inefficient executions. "
                                    + "Try to reorder the modules or rules.");

                initialRound = false;
                newMatchesArePossible = foundApplicationForRound;
            }

            //            while (moduleIt.hasNext() && fileNames.hasNext()) {
            //               module = moduleIt.next();
            //               trFileName = fileNames.next();
            //               addFTRules(module);
            //               tggTransformation.setNullValueMatching(module.isNullValueMatching());
            //
            //               monitor.subTask("Applying " + trFileName);
            //
            //               tggTransformation.applyRules(monitor,"Applying " + trFileName,debug);
            //               monitor.worked(1);
            //               if (monitor.isCanceled()) {
            //                  monitor.done();
            //                  return Status.CANCEL_STATUS;
            //               }
            //            }
            if (monitor.isCanceled()) {
                monitor.done();
                return Status.CANCEL_STATUS;
            }
            long time2 = System.currentTimeMillis();
            long stage2 = time2 - time1;
            System.out.println("Stage 2 -- Transformation: " + stage2 + " ms");
            monitor.subTask("Saving result");
            List<EObject> roots = tggTransformation.getGraph().getRoots();

            Iterator<EObject> it = roots.iterator();
            EObject targetRoot = null;
            EObject current = null;
            //TGG tgg = LoadHandler.layoutModels.get(0);
            boolean targetRootFound = false;
            Class<?> targetClass = null;
            if (this.engineOptions.containsKey("TargetClass")) {
                try {
                    String targetClassScript = this.engineOptions.get("TargetClass");
                    if (targetClassScript.startsWith("{*")) {
                        targetClassScript = targetClassScript.substring(2, targetClassScript.length() - 2);
                    }
                    targetClass = (Class) emfEngine.getScriptEngine().eval(targetClassScript);

                } catch (Throwable e) {
                    e.printStackTrace();
                }
            }
            while (it.hasNext() && !targetRootFound) {
                current = it.next();
                if (NodeUtil.isTargetClass(module, current.eClass())) {
                    targetRoot = current;
                    targetRootFound = true;

                    if (targetClass != null && !targetClass.isInstance(targetRoot)) {
                        targetRoot = null;
                        targetRootFound = false;
                    }

                }
            }

            String moduleName = module.getName();
            String[] moduleNameComponents = moduleName.split("2");
            if (moduleNameComponents.length == 2)
                targetExt = moduleNameComponents[1];

            if (targetExt != null && targetExt.isEmpty()) {
                targetExt = DEFAULT_EXT;
            }
            if (this.engineOptions.containsKey("TargetExtension")) {
                targetExt = this.engineOptions.get("TargetExtension");
            }
            if (targetRoot != null) {
                PriorityQueue<AbstractPostProcessorFactory> postProcessorFactories = getPostProcessorFactories();
                PriorityQueue<AbstractPostProcessorFactory> postProcessorFactories2 = new PriorityQueue<AbstractPostProcessorFactory>(
                        postProcessorFactories);
                EObject newRoot = targetRoot;
                while (!postProcessorFactories2.isEmpty()) {
                    AbstractPostProcessorFactory postProcessorFactory = postProcessorFactories2.poll();
                    if (postProcessorFactory.isValid(inputURI)) {
                        AbstractPostProcessor postProcessor = postProcessorFactory.createPostProcessor(newRoot);

                        postProcessor.registerSharedObjects(sharedObjectRegistry);

                        newRoot = postProcessor.process();

                    }
                }

                targetRoot = newRoot;
                // remove all backreferences
                TreeIterator<EObject> nodesIt = targetRoot.eAllContents();
                EObject targetObject = targetRoot;
                //AbstractTarget tNode;

                removeT2C(targetObject);

                while (nodesIt.hasNext()) {
                    targetObject = nodesIt.next();
                    removeT2C(targetObject);
                }
                if (targetExt == null) {
                    Export.saveModel(resSet, roots, xmiURI);
                } else {
                    this.outputURI = this.inputURI.trimFileExtension().appendFileExtension(targetExt);
                    Export.saveTargetModel(resSet, targetRoot, outputURI, postProcessorFactories, inputURI,
                            sharedObjectRegistry);
                }
            } else {
                System.out.println("No target root!");
            }
            monitor.worked(1);
            if (useOutputFolder) {
                this.outputURI = outputURI.trimSegments(1).appendSegment("output")
                        .appendSegment(outputURI.lastSegment());
            }
            Import.unloadModel(resSet, outputURI);
            resSet.getResource(inputURI, true).unload();

            try {
                if (outputURI.isPlatformResource()) {
                    String platformString = outputURI.toPlatformString(true);
                    IFile file = (IFile) ResourcesPlugin.getWorkspace().getRoot().findMember(platformString);
                    Path path = Paths.get(file.getLocation().toString());
                    Charset charset = StandardCharsets.UTF_8;
                    String content = new String(Files.readAllBytes(path), charset);
                    CodeFormatter cf = new DefaultCodeFormatter();
                    TextEdit te = cf.format(CodeFormatter.K_UNKNOWN, content, 0, content.length(), 0, null);
                    IDocument dc = new Document(content);
                    te.apply(dc);
                    Files.write(path, dc.get().getBytes(charset));
                }
            } catch (Exception e) {
                //e.printStackTrace();
            }
            long time3 = System.currentTimeMillis();
            long stage3 = time3 - time2;
            System.out.println("Stage 3 -- Saving: " + stage3 + " ms");
            executionTimes.stage1 = stage1;
            executionTimes.stage2 = stage2;
            executionTimes.stage3 = stage3;
            executionTimes.overall = stage1 + stage2 + stage3;
        } finally {
            monitor.done();
        }
        // put the execution time for this file in the global list
        synchronized (executionTimesMap) {
            executionTimesMap.put(inputURI.path(), executionTimes);
        }
        for (Module m : LoadHandler.trSystems) {
            cleanGrammar(m);
        }

        if (emfEngine != null)
            emfEngine.clearCache();

        if (this.getThread() != null && this.getThread().getName().startsWith("TranslationJob ")) {
            this.getThread().setName(this.getThread().getName().replaceFirst("TranslationJob ", ""));
        }
        if (!monitor.isCanceled()) {
            // Start next Translation Job
            new TranslationJobCreator() {

                @Override
                public Job createJob() {
                    TranslationJob job = new TranslationJob(inputFiles, useOutputFolder, engineOptions, lock);
                    job.setTimesMap(executionTimesMap);
                    job.setPriority(Job.DECORATE);

                    return job;
                }
            }.createJob().schedule();
        }
        return Status.OK_STATUS;
    }
}