WebReference.com - Part 2 of Extending the JXTA Shell, from Early Adopter JXTA (Wrox Press). (3/4)
[previous] [next] |
Early Adopter JXTA
Now, a structured document has been created, we assign it to either the left hand
side of the assignment or a new environment variable. This is taken care of us by the
getReturnVariable()
method courtesy of the ShellApp
class. The
getReturnVariable()
method will fetch the environment variable referred to on the
left hand side of the assignment, if any, or create a new env?
to hold the result
if there isn't one (now you can see why so many JXTA commands create env?
variables as a side effect of their work).
Note also that we can only assign objects of type ShellObject
to an
environment variable. Fortunately, ShellObjects
are simply a data structure, which
can contain any Java object, plus an associated name. In this case, the name is StructuredDocument
.
In this way, one can simply treat environment variables the same way as Java variables. Any
ShellObject
that we assign to an environment variable will become available to the
user of the shell, and any subsequent commands that they may execute.
env.add(getReturnVariable(),
new ShellObject("StructuredDocument", doc));
return ShellApp.appNoError;
}
For the -c
and -e
switches, there is no need to create a
StructuredDocument ShellObject
, so we store them simply as String
typed
ShellObjects
. This will keep them in an easy-to-manipulate form.
env.add(getReturnVariable(),
new ShellObject("String", myElement));
return ShellApp.appNoError;
}
public void stopApp () {
}
The helper getRestOfArgs()
method combines all the specified
arguments into one string. It artificially inserts one blank in between each of the detected
arguments during the construction of the String
.
public String getRestOfArgs(String [] args, int idx) {
int arysize = args.length;
String res = args[idx];
for (int i=(idx + 1); i < arysize; i++) {
res = res + " " + args[i];
}
return res;
}
The helper mergeAllElements()
method works similarly to getRestOfArgs()
,
but treats each of the arguments as a ShellObject
of type String
. This is the way
we store elements using mkdoc
. The mergeAllElements()
method accesses each of the
environment variables specified in the arguments, and concatenates their contents together. Note that
unlike getRestOfArgs()
, there is no need for blank re-insertion.
public String mergeAllElements( String [] args, int idx) {
int arysize = args.length;
String res = "";
try {
for (int i= idx; i <arysize; i++) {
ShellObject obj = env.get (args[i]);
res += (String) obj.getObject();
}
}
catch (Exception ex) {
println("mkdoc: sorry, cannot access some elements.");
res = "";
}
return res;
}
The reportError()
method gives the user some hint on how to use the
command  it is called when the command is invoked incorrectly.
private int reportError() {
println ("usage: mkdoc [-e <element name> <element
content>]");
println (" [-c <element name> <element> .... ]");
println (" [-d <doctype> <element> .... ]");
println (" ");
println ("'man mkdoc' to get more information.");
return ShellApp.appParamError;
}
The getDescription()
method gives a short description of the command,
for man
's summary.
public String getDescription() {
return "Creates a structured document in memory.";
}
The help()
method gives a detailed description of the command, for a
'man
page' of help information.
public void help() {
println("NAME");
println(" mkdoc - create a sructured
document in memory.");
println(" ");
println("SYNOPSIS");
println(" mkdoc [-e <element name>
<element content>]");
println(" [-c <element name> <element>...]");
println(" [-d <doctype> <element> ....]");
println(" ");
println("DESCRIPTION");
println(" ");
println("'mkdoc' provides a way to create complete
structured");
println("documents from scratch, in memory,
without using file.");
println(" ");
println("First, you use -e to create
environment variables that");
println("contains the element you want.
Then you can use -c to");
println("combine them in new elements,
or just create a structured");
println("document using the -d option");
println(" ");
println("EXAMPLE");
println(" JXTA>name=mkdoc -e name Joe Baxer");
println(" JXTA>phone=mkdoc -e phone 223-222-3333");
println(" JXTA>mydoc=mkdoc -d CustData name phone");
println("Explanation");
println(" The commands above creates a structured
document with");
println(" root of CustData containing the name and
phone info.");
println(" ");
}
}
This completes the code analysis, so it's time to give this code a try.
[previous] [next] |
Created: January 28, 2002
Revised: January 28, 2002
URL: https://webreference.com/programming/jxta/chap3/2/3.html