WebReference.com - Part 2 of Extending the JXTA Shell, from Early Adopter JXTA (Wrox Press). (2/4)
[previous] [next] |
Early Adopter JXTA
Coding the mkdoc Command
Now, we can follow the procedure that we have seen with the
trivial date command to create this new command.
You can, once more, find
the source code in the src
directory of the distribution.
Being a shell command, of course, it resides in its own package under the bin
directory, called net.jxta.impl.shell.bin.mkdoc
.
package net.jxta.impl.shell.bin.mkdoc;
import net.jxta.impl.shell.*;
import net.jxta.endpoint.*;
import net.jxta.document.*;
Any shell command must inherit from
net.jxta.impl.shell.ShellApp
, and mkdoc
is no exception.
public class mkdoc extends ShellApp {
We will store a reference to the shell environment, which we can obtain through the
inherited getenv()
method from the ShellApp
class, in the variable called
env
. It is through this reference that we can access all the environment variables
that we will use.
ShellEnv env;
The switches used to tell the command to define an element, combine elements, and make a document are defined next.
private static final String elementDefine = "-e";
private static final String combineElements = "-c";
private static final String makeDoc = "-d";
Next, some temporary working variables for constructing our output.
private String myDocType = null;
private String elementName = null;
private String elementContent = null;
private String myElement = null;
public mkdoc() {
}
The core logic implementation, as with the date
command, begins in the
startApp()
method. The startApp()
method is equivalent to main()
for JXTA shell commands.
public int startApp (String[] args) {
We make sure that there are at least more than three arguments, since all switch alternatives requires at least this number of arguments to be meaningful. If the user has less than this, we print out a help message.
if ((args == null) || (args.length < 3)) {
return reportError();
}
Here, we take advantage of helper methods in ShellApp
to easily
obtain the environment instance.
env = getEnv();
We check the first argument to see if it is -e
, which tells us to define
a new element. If it is, we lift off the second argument and store it as elementName
.
The rest of the command line is captured into the variable elementContent
, and can
contain embedded blanks.
if (elementDefine.equals(args[0])) {
// define a new element
elementName = args[1];
elementContent = getRestOfArgs(args,2);
So now we create a new element by adding the XML angled brackets for an opening and closing tag. This resulting element will be assigned out to an environment variable later.
myElement = "<" + elementName + ">" + elementContent +
"</" + elementName + ">";
}
The next set of logic handles the -c
switch for combining multiple
elements as children of a new element.
if (combineElements.equals(args[0])) {
// combine elements into new element
The first argument after this switch is always the new element name.
elementName = args[1];
We call our own mergeAllElements()
helper method here to do the work.
The mergeAllElements()
method will examine every subsequent argument, access them
as environment variables, extract their values, and concatenate them.
elementContent = mergeAllElements(args,2);
Finally, we create a new element based on the concatenation of the content of the specified elements.
myElement = "<" + elementName + ">" +elementContent +
"</"+elementName +">";
}
For the third switch, -d
, we need to create a new structured document
instance. args[1]
in this case contains the document type, which is typically the
name of the root element. For example, a JXTA pipe advertisement will have document type of
Jxta:PipeAdv
.
if (makeDoc.equals(args[0])) {
// create a structured document
myDocType = args[1];
Here, we use the mergeAllElements()
helper method again to extract
elements from each of the specified environment variables.
myElement = mergeAllElements(args,2);
A structured document is created using the static newStructuredDocument()
method of the StructuredDocumentFactory
. This class takes registration of different
structured document classes from the platform, and hides the construction details from the user.
The version we use here simply takes a string as an argument and uses it as the content of the
document.
StructuredDocument doc = null;
try {
doc = StructuredDocumentFactory.newStructuredDocument (
new MimeMediaType ("text/xml"), myDocType,
myElement);
} catch (Exception ex) {
println("mkdoc: " + ex.toString());
return ShellApp.appMiscError;
}
[previous] [next] |
Created: January 28, 2002
Revised: January 28, 2002
URL: https://webreference.com/programming/jxta/chap3/2/2.html