if (args.length == 0){
return true;
}
try {
XmldbURI newPath = path;
final XmldbURI currUri = XmldbURI.xmldbUriFor(properties.getProperty("uri")).resolveCollectionPath(path);
if (args[0].equalsIgnoreCase("ls")) {
// list collection contents
getResources();
if ("true".equals(properties.getProperty("permissions"))) {
for (int i = 0; i < resources.length; i++) {
messageln(resources[i]);
}
} else {
for (int i = 0; i < resources.length; i++) {
final StringBuilder buf = new StringBuilder();
int k = 0;
for (int j = 0; i < resources.length && j < 5; i++, j++) {
buf.append(resources[i]);
buf.append('\t');
k = j;
}
if (k == 4 && i < resources.length) {
i--;
}
messageln(buf.toString());
}
}
} else if (args[0].equalsIgnoreCase("cd")) {
// change current collection
completitions.clear();
Collection temp;
XmldbURI collectionPath;
if (args.length < 2 || args[1] == null) {
collectionPath = XmldbURI.ROOT_COLLECTION_URI;
} else {
collectionPath = XmldbURI.xmldbUriFor(URIUtils.urlEncodeUtf8(args[1]));
}
collectionPath = currUri.resolveCollectionPath(collectionPath);
if(collectionPath.numSegments()==0) {
collectionPath = currUri.resolveCollectionPath(XmldbURI.ROOT_COLLECTION_URI);
messageln("cannot go above "+XmldbURI.ROOT_COLLECTION_URI.toString());
}
temp = DatabaseManager.getCollection(
collectionPath.toString(),
properties.getProperty("user"),
properties.getProperty("password"));
if (temp != null) {
current = temp;
newPath = collectionPath.toCollectionPathURI();
if (startGUI) {
frame.setPath(collectionPath.toCollectionPathURI());
}
} else {
messageln("no such collection.");
}
getResources();
} else if (args[0].equalsIgnoreCase("cp")) {
if (args.length != 3) {
messageln("cp requires two arguments.");
return true;
}
final XmldbURI src,dest;
try {
src = URIUtils.encodeXmldbUriFor(args[1]);
dest = URIUtils.encodeXmldbUriFor(args[2]);
} catch(final URISyntaxException e) {
errorln("could not parse collection name into a valid URI: "+e.getMessage());
return false;
}
copy(src,dest);
getResources();
} else if (args[0].equalsIgnoreCase("edit")) {
if (args.length == 2) {
final XmldbURI resource;
try {
resource = URIUtils.encodeXmldbUriFor(args[1]);
} catch(final URISyntaxException e) {
errorln("could not parse resource name into a valid URI: "+e.getMessage());
return false;
}
editResource(resource);
} else {
messageln("Please specify a resource.");
}
} else if (args[0].equalsIgnoreCase("get")) {
if (args.length < 2) {
System.err.println("wrong number of arguments.");
return true;
}
final XmldbURI resource;
try {
resource = URIUtils.encodeXmldbUriFor(args[1]);
} catch(final URISyntaxException e) {
errorln("could not parse resource name into a valid URI: "+e.getMessage());
return false;
}
final Resource res = retrieve(resource);
// display document
if (res != null) {
final String data;
if ("XMLResource".equals(res.getResourceType())) {
data = (String) res.getContent();
} else {
data = new String((byte[]) res.getContent());
}
if (startGUI) {
frame.setEditable(false);
frame.display(data);
frame.setEditable(true);
} else {
final String content = data;
more(content);
}
}
return true;
} else if (args[0].equalsIgnoreCase("find")) {
// search
if (args.length < 2) {
messageln("no query argument found.");
return true;
}
messageln(args[1]);
final long start = System.currentTimeMillis();
result = find(args[1]);
if (result == null) {
messageln("nothing found");
} else {
messageln("found " + result.getSize() + " hits in "
+ (System.currentTimeMillis() - start) + "ms.");
}
nextInSet = 1;
} else if (args[0].equalsIgnoreCase("run")) {
if (args.length < 2) {
messageln("please specify a query file.");
return true;
}
try {
final BufferedReader reader = new BufferedReader(new FileReader(args[1]));
final StringBuilder buf = new StringBuilder();
String nextLine;
while ((nextLine = reader.readLine()) != null) {
buf.append(nextLine);
buf.append(EOL);
}
args[1] = buf.toString();
final long start = System.currentTimeMillis();
result = find(args[1]);
if (result == null) {
messageln("nothing found");
} else {
messageln("found " + result.getSize() + " hits in "
+ (System.currentTimeMillis() - start) + "ms.");
}
nextInSet = 1;
} catch (final Exception e) {
errorln("An error occurred: " + e.getMessage());
}
} else if (args[0].equalsIgnoreCase("show")) {
// show search results
if (result == null) {
messageln("no result set.");
return true;
}
try {
int start = nextInSet;
int count = 1;
if (args.length > 1) {
start = Integer.parseInt(args[1]);
}
if (args.length > 2) {
count = Integer.parseInt(args[2]);
}
final int s = (int) result.getSize();
if (start < 1 || start > s) {
messageln("start offset out of range");
return true;
}
--start;
if (start + count > s) {
count = s - start;
}
nextInSet = start + count + 1;
for (int i = start; i < start + count; i++) {
final Resource r = result.getResource(i);
if (startGUI) {
frame.display((String) r.getContent());
} else {
more((String) r.getContent());
}
}
messageln("displayed items " + (start + 1) + " to "
+ (start + count) + " of " + result.getSize());
} catch (final NumberFormatException nfe) {
errorln("wrong argument");
return true;
}
} else if (args[0].equalsIgnoreCase("mkcol")) {
// create collection
if (args.length < 2) {
messageln("missing argument.");
return true;
}
final XmldbURI collUri;
try {
collUri = URIUtils.encodeXmldbUriFor(args[1]);
} catch(final URISyntaxException e) {
errorln("could not parse collection name into a valid URI: "+e.getMessage());
return false;
}
final CollectionManagementServiceImpl mgtService = (CollectionManagementServiceImpl) current
.getService("CollectionManagementService", "1.0");
final Collection newCollection = mgtService.createCollection(collUri);
if (newCollection == null) {
messageln("could not create collection.");
} else {
messageln("created collection.");
}
// re-read current collection
current = DatabaseManager.getCollection(properties
.getProperty("uri")
+ path, properties.getProperty("user"), properties
.getProperty("password"));
getResources();
} else if (args[0].equalsIgnoreCase("put")) {
// put a document or directory into the database
if (args.length < 2) {
messageln("missing argument.");
return true;
}
final boolean r = parse(args[1]);
getResources();
return r;
} else if (args[0].equalsIgnoreCase("putzip")) {
// put the contents of a zip archive into the database
if (args.length < 2) {
messageln("missing argument.");
return true;
}
final boolean r = parseZip(args[1]);
getResources();
return r;
} else if (args[0].equalsIgnoreCase("putgz")) {
// put the contents of a zip archive into the database
if (args.length < 2) {
messageln("missing argument.");
return true;
}
final boolean r = parseGZip(args[1]);
getResources();
return r;
} else if (args[0].equalsIgnoreCase("blob")) {
// put a document or directory into the database
if (args.length < 2) {
messageln("missing argument.");
return true;
}
storeBinary(args[1]);
getResources();
} else if (args[0].equalsIgnoreCase("rm")) {
// remove document
if (args.length < 2) {
messageln("missing argument.");
return true;
}
remove(args[1]);
// re-read current collection
current = DatabaseManager.getCollection(properties
.getProperty("uri")
+ path, properties.getProperty("user"), properties
.getProperty("password"));
getResources();
} else if (args[0].equalsIgnoreCase("rmcol")) {
// remove collection
if (args.length < 2) {
messageln("wrong argument count.");
return true;
}
final XmldbURI collUri;
try {
collUri = URIUtils.encodeXmldbUriFor(args[1]);
} catch(final URISyntaxException e) {
errorln("could not parse collection name into a valid URI: "+e.getMessage());
return false;