1

我有一个 Xtext/Xpand(oAW 4.3、Eclipse 3.4)生成器插件,我在第二个工作台中与编辑器插件一起运行。在那里,我想在我创建的模型文件上以编程方式运行 Xpand 工作流程。如果我使用我拥有的 IFile 的绝对路径设置模型文件,例如:

String dslFile = file.getLocation().makeAbsolute().toOSString();

或者,如果我使用通过以下方式检索的文件 URI:

String dslFile = file.getLocationURI().toString();

找不到文件:

org.eclipse.emf.ecore.resource.Resource$IOWrappedException: Resource '/absolute/path/to/my/existing/dsl.file' does not exist. 
at org.openarchitectureware.xtext.parser.impl.AbstractParserComponent.invokeInternal(AbstractParserComponent.java:55)

在我交给 WorkflowRunner 的地图中,我应该将模型文件属性 (dslFile) 设置为什么值:

Map properties = new HashMap();
properties.put("modelFile", dslFile);

我还尝试将属性留空并引用相对于工作流文件(在工作流文件内)的模型文件,但这会产生 FileNotFoundException。在普通应用程序(而不是在第二个工作台)中运行所有这些工作正常。

4

3 回答 3

1

对于在这里看的人来说有两件重要的事情...... TE 使用 IFLE 来表示“file.get....”,并且路径的正确语法是“file:/c:/myOSbla”。

于 2009-05-28T08:42:03.293 回答
0

我在openArchitectureWare 论坛上找到了帮助。基本上使用

properties.put("modelFile", file.getLocation().makeAbsolute().toOSString());

有效,但您需要在您调用的工作流程中指定通过 URI 进行查找:

<component class="org.eclipse.mwe.emf.Reader">
    <uri value='${modelFile}'/>
    <modelSlot value='theModel'/>
</component>
于 2008-10-10T16:00:24.160 回答
0

这是一个示例应用Launcher.java(位于默认包中):

import gnu.getopt.Getopt;
import gnu.getopt.LongOpt;

import java.io.File;
import java.io.IOException;

import org.eclipse.emf.mwe2.launch.runtime.Mwe2Launcher;

public class Launcher implements Runnable {

    //
    // main program
    //

    public static void main(final String[] args) {
        new Launcher(args).run();
    }


    //
    // private final fields
    //

    private static final String defaultModelDir     = "src/main/resources/model";
    private static final String defaultTargetDir    = "target/generated/pageflow-maven-plugin/java";
    private static final String defaultFileEncoding = "UTF-8";

    private static final LongOpt[] longopts = new LongOpt[] {
        new LongOpt("baseDir",   LongOpt.REQUIRED_ARGUMENT, new StringBuffer(), 'b'),
        new LongOpt("modelDir",  LongOpt.REQUIRED_ARGUMENT, new StringBuffer(), 'm'),
        new LongOpt("targetDir", LongOpt.REQUIRED_ARGUMENT, new StringBuffer(), 't'),
        new LongOpt("encoding",  LongOpt.REQUIRED_ARGUMENT, new StringBuffer(), 'e'),
        new LongOpt("help",      LongOpt.NO_ARGUMENT,       null,               'h'),
        new LongOpt("verbose",   LongOpt.NO_ARGUMENT,       null,               'v'),
    };


    private final String[] args;

    //
    // public constructors
    //

    public Launcher(final String[] args) {
        this.args = args;
    }


    public void run() {
        final String cwd = System.getProperty("user.dir");
        String baseDir   = cwd;
        String modelDir  = defaultModelDir;
        String targetDir = defaultTargetDir;
        String encoding  = defaultFileEncoding;
        boolean verbose = false;

        final StringBuffer sb = new StringBuffer();
        final Getopt g = new Getopt("pageflow-dsl-generator", this.args, "b:m:t:e:hv;", longopts);
        g.setOpterr(false); // We'll do our own error handling
        int c;
        while ((c = g.getopt()) != -1)
            switch (c) {
                case 'b':
                    baseDir = g.getOptarg();
                    break;
                case 'm':
                    modelDir = g.getOptarg();
                    break;
                case 't':
                    targetDir = g.getOptarg();
                    break;
                case 'e':
                    encoding = g.getOptarg();
                    break;
                case 'h':
                    printUsage();
                    System.exit(0);
                    break;
                case 'v':
                    verbose = true;
                    break;
                case '?':
                default:
                    System.out.println("The option '" + (char) g.getOptopt() + "' is not valid");
                    printUsage();
                    System.exit(1);
                    break;
            }

        String absoluteModelDir;
        String absoluteTargetDir;

        try {
            absoluteModelDir  = checkDir(baseDir, modelDir, false, true);
            absoluteTargetDir = checkDir(baseDir, targetDir, true, true);
        } catch (final IOException e) {
            throw new RuntimeException(e.getMessage(), e.getCause());
        }

        if (verbose) {
            System.err.println(String.format("modeldir  = %s", absoluteModelDir));
            System.err.println(String.format("targetdir = %s", absoluteTargetDir));
            System.err.println(String.format("encoding  = %s", encoding));
        }

        Mwe2Launcher.main(
                new String[] {
                        "workflow.PageflowGenerator",
                        "-p", "modelDir=".concat(absoluteModelDir),
                        "-p", "targetDir=".concat(absoluteTargetDir),
                        "-p", "fileEncoding=".concat(encoding)
                });

    }


    private void printUsage() {
        System.err.println("Syntax: [-b <baseDir>] [-m <modelDir>] [-t <targetDir>] [-e <encoding>] [-h] [-v]");
        System.err.println("Options:");
        System.err.println("  -b, --baseDir     project home directory, e.g: /home/workspace/myapp");
        System.err.println("  -m, --modelDir    default is: ".concat(defaultModelDir));
        System.err.println("  -t, --targetDir   default is: ".concat(defaultTargetDir));
        System.err.println("  -e, --encoding    default is: ".concat(defaultFileEncoding));
        System.err.println("  -h, --help        this help text");
        System.err.println("  -v, --verbose     verbose mode");
    }

    private String checkDir(final String basedir, final String dir, final boolean create, final boolean fail) throws IOException {
        final StringBuilder sb = new StringBuilder();
        sb.append(basedir).append('/').append(dir);
        final File f = new File(sb.toString()).getCanonicalFile();
        final String absolutePath = f.getAbsolutePath();
        if (create) {
            if (f.isDirectory()) return absolutePath;
            if (f.mkdirs()) return absolutePath;
        } else {
            if (f.isDirectory()) return absolutePath;
        }
        if (!fail) return null;
        throw new IOException(String.format("Failed to locate or create directory %s", absolutePath));
    }

    private String checkFile(final String basedir, final String file, final boolean fail) throws IOException {
        final StringBuilder sb = new StringBuilder();
        sb.append(basedir).append('/').append(file);
        final File f = new File(sb.toString()).getCanonicalFile();
        final String absolutePath = f.getAbsolutePath();
        if (f.isFile()) return absolutePath;
        if (!fail) return null;
        throw new IOException(String.format("Failed to find or locate directory %s", absolutePath));
    }

}

...这是它的pom.xml

<project
    xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>com.vaadin</groupId>
    <artifactId>pageflow-dsl-generator</artifactId>
    <version>0.1.0-SNAPSHOT</version>

    <build>
        <sourceDirectory>src</sourceDirectory>

        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <index>true</index>
                        <manifest>
                            <mainClass>Launcher</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>

    </build>



    <dependencies>
        <dependency>
            <groupId>urbanophile</groupId>
            <artifactId>java-getopt</artifactId>
            <version>1.0.9</version>
        </dependency>
    </dependencies>

</project>

不幸的是,这个 pom.xml 并不打算打包它(至少现在还没有)。有关包装的说明,请查看 链接文本

玩得开心 :)

理查德·戈麦斯

于 2010-06-30T02:27:12.903 回答