Switch to side-by-side view

--- a
+++ b/src/net/timbusproject/context/converter/OWLExportCLI.java
@@ -0,0 +1,149 @@
+/**
+ * Copyright (c) 2013/2014 Verein zur Foerderung der IT-Sicherheit in Oesterreich (SBA).
+ * The work has been developed in the TIMBUS Project and the above-mentioned are Members of the TIMBUS Consortium.
+ * TIMBUS is supported by the European Union under the 7th Framework Programme for research and technological
+ * development and demonstration activities (FP7/2007-2013) under grant agreement no. 269940.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at: http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
+ * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including without
+ * limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTIBITLY, or FITNESS FOR A PARTICULAR
+ * PURPOSE. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise,
+ * unless required by applicable law or agreed to in writing, shall any Contributor be liable for damages, including
+ * any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this
+ * License or out of the use or inability to use the Work.
+ * See the License for the specific language governing permissions and limitation under the License.
+ */
+
+package net.timbusproject.context.converter;
+
+import java.io.File;
+import java.io.IOException;
+
+import org.apache.commons.cli.BasicParser;
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.CommandLineParser;
+import org.apache.commons.cli.HelpFormatter;
+import org.apache.commons.cli.Option;
+import org.apache.commons.cli.Options;
+import org.apache.commons.cli.ParseException;
+import org.eclipse.emf.common.util.URI;
+import org.eclipse.emf.ecore.resource.Resource;
+import org.eclipse.emf.ecore.resource.ResourceSet;
+import org.eclipse.osgi.util.NLS;
+
+import uk.ac.bolton.archimate.compatibility.IncompatibleModelException;
+import uk.ac.bolton.archimate.compatibility.ModelCompatibility;
+import uk.ac.bolton.archimate.editor.model.IArchiveManager;
+import uk.ac.bolton.archimate.editor.model.impl.EditorModelManager;
+import uk.ac.bolton.archimate.model.IArchimateModel;
+import uk.ac.bolton.archimate.model.IArchimatePackage;
+import uk.ac.bolton.archimate.model.util.ArchimateResourceFactory;
+
+/**
+ * Export an Archimate Model to OWL (Web Ontology Language) as standalone application
+ * 
+ * @author Rudolf Mayer (rmayer@sba-research.org)
+ */
+public class OWLExportCLI {
+
+    /** Adapted from {@link EditorModelManager#loadModel(File)}, to work in a stand-alone setting */
+    public static IArchimateModel loadModel(File file) {
+        if (file == null || !file.exists()) {
+            return null;
+        }
+
+        ResourceSet resourceSet = ArchimateResourceFactory.createResourceSet();
+
+        // Changed from EditorModelManager#loadModel(File): need register the Archimate package, if that's not done yet
+        if (!resourceSet.getPackageRegistry().containsKey(IArchimatePackage.eNS_URI)) {
+            resourceSet.getPackageRegistry().put(IArchimatePackage.eNS_URI, IArchimatePackage.eINSTANCE);
+        }
+
+        // Ascertain if this is an archive file
+        boolean useArchiveFormat = IArchiveManager.FACTORY.isArchiveFile(file);
+
+        // Create the Resource
+        Resource resource = resourceSet.createResource(useArchiveFormat ? IArchiveManager.FACTORY
+                .createArchiveModelURI(file) : URI.createFileURI(file.getAbsolutePath()));
+
+        // Load the model file
+        try {
+            resource.load(null);
+        } catch (IOException ex) {
+            // Error occured loading model. Was it a disaster?
+            try {
+                ModelCompatibility.checkErrors(resource);
+            }
+            // Incompatible, don't load it
+            catch (IncompatibleModelException ex1) {
+                System.err.println("Messages.EditorModelManager_2" + ": "
+                        + NLS.bind("Messages.EditorModelManager_3", file) + "\n" + ex1.getMessage());
+                return null;
+            }
+        }
+
+        // Changed: skip "Once loaded - Check version number compatibility with user"
+
+        // Changed: skip the following, as we don't have the extension registry set up
+        // // And then fix any backward compatibility issues
+        // try {
+        // ModelCompatibility.fixCompatibility(resource);
+        // } catch (CompatibilityHandlerException ex) {
+        // }
+
+        IArchimateModel model = (IArchimateModel) resource.getContents().get(0);
+        model.setFile(file);
+        model.setDefaults();
+        return model;
+
+    }
+
+    public static void main(String[] args) throws IOException, ParseException {
+        // create CLI Options
+        Options options = new Options();
+
+        Option optionInput = new Option("i", "input", true, "The Archi archimate model to convert");
+        optionInput.setType(new String());
+        optionInput.setRequired(true);
+
+        Option optionOutput = new Option("o", "output", true, "The name of the OWL output file");
+        optionOutput.setType(new String());
+        optionOutput.setRequired(true);
+
+        options.addOption(optionInput);
+        options.addOption(optionOutput);
+
+        // parse options
+        CommandLineParser parser = new BasicParser();
+        CommandLine cmd;
+        try {
+            cmd = parser.parse(options, args);
+        } catch (Exception e) {
+            // automatically generate the help statement
+            HelpFormatter formatter = new HelpFormatter();
+            formatter.printHelp(OWLExportPlugin.class.getName(), options);
+            return;
+        }
+
+        String inputFilename = cmd.getOptionValue("input");
+        String outputFilename = cmd.getOptionValue("output");
+
+        // load model
+        File archiFile = new File(inputFilename);
+        IArchimateModel model = loadModel(archiFile);
+        System.out.println("Loaded model from " + archiFile.getAbsolutePath());
+
+        // convert model
+        File outputFile = new File(outputFilename);
+        OWLExport.exportToOwl(model, outputFile);
+        System.out.println("Wrote converted file to " + outputFile.getAbsolutePath());
+
+        // also export the layout
+        File layoutFile = OWLExport.getDefaultLayoutFile(outputFile);
+        OWLExport.exportLayout(model, layoutFile, OWLExport.getDefaultIRI(outputFile));
+        System.out.println("Wrote layout file to " + layoutFile.getAbsolutePath());
+
+    }
+}