1.18. Element `invoke`
Prev | 1. Reference | Next

### 1.18. Element `invoke`

    <invoke
      method = Qualified name of a Java static method
    >
      Content: [ argument ]+
    </invoke>

    <argument>
      Content: string
    </argument>

Invokes specified Java™ static method, passing it specified arguments.

- The method generally belongs to a class which is contained in a `jar` dynamically discovered by XXE at startup time.

- The method must have one of the following signatures:

  - `method(java.lang.String[] arguments,
    java.io.File workingDir);`
  - `method(java.lang.String[] arguments,
    java.io.File workingDir,
    com.xmlmind.xmledit.util.Console console);`
  - `method(java.lang.String[] arguments,
    java.io.File workingDir,
    com.xmlmind.xmledit.util.Console console,
    com.xmlmind.xmledit.doc.Document docBeingEdited);`
  arguments

    An array of Strings corresponding to the contents of the `argument` child elements. Note that the macro-variables (`%0`, `%1`, `%D`, `%p`, `%C`, etc) are substituted with their values in each argument.

  workingDir

    The temporary directory created each time a process command is executed. Relative paths are generally relative to this directory.

  console

    A simple way to report information and non fatal errors to the user of the process command. Throw an exception to report a fatal error.

  docBeingEdited

    The document being edited.

- The method may return a value. If it returns a value, this value is converted to a `java.lang.String` using `toString()` and then returned by the `invoke` element (à la read, for use in a macro command for example).

- The method may throw any exception.

Examples:

    <invoke method="TestInvoke.echo">
      <argument>args={%*}</argument>
      <argument>doc='%D'</argument>
      <argument>pwd='%W'</argument>
      <argument>conf='%C'</argument>
    </invoke>

    <invoke method="TestInvoke.echo2"/>

    <invoke method="TestInvoke.gzip">
      <argument>__doc.xml</argument>
    </invoke>

Static methods invoked by the above examples:

    public final class TestInvoke {
        public static void echo(String[] arguments, File workingDir,
                                Console console) {
            console.showMessage("arguments={" +
                                StringUtil.joinArguments(arguments) + "}",
                                Console.INFO);
            console.showMessage("workingDir='" + workingDir + "'",
                                Console.INFO);
        }

        public static void echo2(String[] arguments, File workingDir,
                                 Console console, Document docBeingEdited) {
            echo(arguments, workingDir, console);
            console.showMessage("docBeingEdited='" + docBeingEdited.getLocation()
                                + "'", Console.INFO);
        }

        public static File gzip(String[] arguments, File workingDir)
            throws IOException {
            if (arguments.length != 1)
                throw new IllegalArgumentException("arguments");

            File inFile = new File(workingDir, arguments[0].trim());
            if (!inFile.isFile())
                throw new FileNotFoundException(inFile.getPath());

            File outFile = new File(inFile.getPath() + ".gz");
            FileInputStream in = new FileInputStream(inFile);

            try {
                GZIPOutputStream out =
                    new GZIPOutputStream(new FileOutputStream(outFile));

                byte[] bytes = new byte[8192];
                int count;

                while ((count = in.read(bytes)) != -1)
                    out.write(bytes, 0, count);

                out.finish();
                out.close();
            } finally {
                in.close();
            }

            return outFile;
        }
    }

Prev | Up | Next
1.17. Element `shell` | Home | 1.19. Element `subProcess`