} else if (args[0].equalsIgnoreCase("map")) { StringTokenizer tok = new StringTokenizer(args[1], "= "); if (tok.countTokens() < 2) { messageln("please specify a namespace/prefix mapping as: prefix=namespaceURI"); return true; } String prefix = tok.nextToken(); String uri = tok.nextToken(); namespaceMappings.put(prefix, uri);
	protected boolean process(String line) {		if (startGUI)			frame.setPath(path);		String args[];		if (line.startsWith("find")) {			args = new String[2];			args[0] = "find";			args[1] = line.substring(5);		} else {			StringTokenizer tok = new StringTokenizer(line, " ");			args = new String[tok.countTokens()];			if (args.length == 0)				return true;			int i = 0;			while (tok.hasMoreTokens())				args[i++] = tok.nextToken();		}		String newPath = path;		try {			if (args[0].equalsIgnoreCase("ls")) {				// list collection contents				if (properties.getProperty("permissions").equals("true")) {					for (int i = 0; i < resources.length; i++)						messageln(resources[i]);				} else {					StringBuffer buf;					for (int i = 0; i < resources.length; i++) {						buf = new StringBuffer();						int k = 0;						for (int j = 0; i < resources.length && j < 5; i++, j++) {							buf.append(resources[i] + '\t');							k = j;						}						if (k == 4 && i < resources.length)							i--;						messageln(buf.toString());					}				}			} else if (args[0].equalsIgnoreCase("cd")) {				// change current collection				completitions.clear();				String tempPath = newPath;				Collection temp;				if (args.length < 2 || args[1] == null) {					tempPath = "/db";					temp =						DatabaseManager.getCollection(							properties.getProperty("uri") + "/db",							properties.getProperty("user"),							properties.getProperty("password"));				} else {					if (args[1].equals("..")) {						tempPath =							newPath.equals("/db")								? "/db"								: tempPath.substring(0, newPath.lastIndexOf("/"));						if (tempPath.length() == 0)							tempPath = "/db";					} else if (args[1].startsWith("/"))						tempPath = args[1];					else						tempPath = tempPath + '/' + args[1];					temp =						DatabaseManager.getCollection(							properties.getProperty("uri") + tempPath,							properties.getProperty("user"),							properties.getProperty("password"));				}				if (temp != null) {					current = temp;					newPath = tempPath;					if (startGUI)						frame.setPath(newPath);				} else {					messageln("no such collection.");				}				getResources();			} else if (args[0].equalsIgnoreCase("get")) {				if (args.length < 2) {					System.err.println("wrong number of arguments.");					return true;				}				XMLResource res = retrieve(args[1]);				// display document				if (res != null) {					if (startGUI) {						frame.setEditable(false);						frame.display((String) res.getContent());						frame.setEditable(true);					} else {						String content = (String) res.getContent();						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]);				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("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++) {						Resource r = result.getResource((long) 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 (NumberFormatException nfe) {					messageln("wrong argument");					return true;				}			} else if (args[0].equalsIgnoreCase("mkcol")) {				// create collection				if (args.length < 2) {					messageln("missing argument.");					return true;				}				CollectionManagementService mgtService =					(CollectionManagementService) current.getService(						"CollectionManagementService",						"1.0");				Collection newCollection = mgtService.createCollection(args[1]);				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;				}				boolean r = parse(args[1]);				getResources();				return r;			} 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;				}				rmcol(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("adduser")) {				if (args.length < 2) {					System.err.println("Usage: adduser name");					return true;				}				if (startGUI) {					messageln("command not supported in GUI mode. Please use the \"Edit users\" menu option.");					return true;				}				try {					UserManagementService mgtService =						(UserManagementService) current.getService("UserManagementService", "1.0");					String p1;					String p2;					while (true) {						p1 = Readline.readline("password: ");						p2 = Readline.readline("re-enter password: ");						if (p1.equals(p2))							break;						else							System.out.println("\nentered passwords differ. Try again...");					}					String home = Readline.readline("home collection [none]: ");					User user = new User(args[1], p1);					if (home != null && home.length() > 0)						user.setHome(home);					String groups = Readline.readline("enter groups: ");					StringTokenizer tok = new StringTokenizer(groups, " ,");					String group;					while (tok.hasMoreTokens()) {						group = tok.nextToken();						if (group.length() > 0)							user.addGroup(group);					}					mgtService.addUser(user);					System.out.println("user " + user + " created.");				} catch (Exception e) {					System.out.println("ERROR: " + e.getMessage());					e.printStackTrace();				}			} else if (args[0].equalsIgnoreCase("users")) {				UserManagementService mgtService =					(UserManagementService) current.getService("UserManagementService", "1.0");				User users[] = mgtService.getUsers();				System.out.println("User\t\tGroups");				System.out.println("-----------------------------------------");				for (int i = 0; i < users.length; i++) {					System.out.print(users[i].getName() + "\t\t");					for (Iterator j = users[i].getGroups(); j.hasNext();) {						System.out.print(j.next());						if (j.hasNext())							System.out.print(", ");					}					System.out.println();				}			} else if (args[0].equalsIgnoreCase("passwd")) {				if (startGUI) {					messageln("command not supported in GUI mode. Please use the \"Edit users\" menu option.");					return true;				}				if (args.length < 2) {					System.out.println("Usage: passwd username");					return true;				}				try {					UserManagementService mgtService =						(UserManagementService) current.getService("UserManagementService", "1.0");					User user = mgtService.getUser(args[1]);					if (user == null) {						System.out.println("no such user.");						return true;					}					String p1;					String p2;					while (true) {						p1 = Readline.readline("password: ");						p2 = Readline.readline("re-enter password: ");						if (p1.equals(p2))							break;						else							System.out.println("\nentered passwords differ. Try again...");					}					user.setPassword(p1);					mgtService.updateUser(user);					properties.setProperty("password", p1);				} catch (Exception e) {					System.err.println("ERROR: " + e.getMessage());				}			} else if (args[0].equalsIgnoreCase("chmod")) {				if (args.length < 2) {					System.out.println("Usage: chmod [resource] mode");					return true;				}				Collection temp = null;				if (args.length == 3) {					System.out.println("trying collection: " + args[1]);					temp = current.getChildCollection(args[1]);					if (temp == null) {						System.out.println("\ntrying resource: " + args[1]);						Resource r = current.getResource(args[1]);						if (r != null) {							UserManagementService mgtService =								(UserManagementService) current.getService(									"UserManagementService",									"1.0");							mgtService.chmod(r, args[2]);						} else							System.err.println("Resource " + args[1] + " not found.");					} else {						UserManagementService mgtService =							(UserManagementService) temp.getService("UserManagementService", "1.0");						mgtService.chmod(args[2]);					}				} else {					UserManagementService mgtService =						(UserManagementService) current.getService("UserManagementService", "1.0");					mgtService.chmod(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("chown")) {				if (args.length < 3) {					System.out.println("Usage: chown username group [resource]");					return true;				}				Collection temp;				if (args.length == 4)					temp = current.getChildCollection(args[3]);				else					temp = current;				if (temp != null) {					UserManagementService mgtService =						(UserManagementService) temp.getService("UserManagementService", "1.0");					User u = mgtService.getUser(args[1]);					if (u == null) {						System.out.println("unknown user");						return true;					}					mgtService.chown(u, args[2]);					System.out.println("owner changed.");					getResources();					return true;				}				Resource res = current.getResource(args[3]);				if (res != null) {					UserManagementService mgtService =						(UserManagementService) current.getService("UserManagementService", "1.0");					User u = mgtService.getUser(args[1]);					if (u == null) {						System.out.println("unknown user");						return true;					}					mgtService.chown(res, u, args[2]);					getResources();					return true;				}				System.err.println("Resource " + args[3] + " not found.");			} else if (args[0].equalsIgnoreCase("elements")) {				System.out.println("Element occurrences in collection " + current.getName());				System.out.println("--------------------------------------------" + "-----------");				IndexQueryService service =					(IndexQueryService) current.getService("IndexQueryService", "1.0");				Occurrences[] elements = service.getIndexedElements(true);				for (int i = 0; i < elements.length; i++) {					System.out.println(						formatString(							elements[i].getTerm(),							Integer.toString(elements[i].getOccurrences()),							50));				}				return true;			} else if (args[0].equalsIgnoreCase("terms")) {				if (args.length < 3) {					System.out.println("Usage: terms sequence-start sequence-end");					return true;				}				IndexQueryService service =					(IndexQueryService) current.getService("IndexQueryService", "1.0");				Occurrences[] terms = service.scanIndexTerms(args[1], args[2], true);				System.out.println("Element occurrences in collection " + current.getName());				System.out.println("--------------------------------------------" + "-----------");				for (int i = 0; i < terms.length; i++) {					System.out.println(						formatString(							terms[i].getTerm(),							Integer.toString(terms[i].getOccurrences()),							50));				}			} else if (args[0].equalsIgnoreCase("xupdate")) {				if (startGUI) {					messageln("command not supported in GUI mode.");					return true;				}				String lastLine, command = "";				try {					while (true) {						lastLine = Readline.readline("| ");						if (lastLine == null || lastLine.length() == 0)							break;						command += lastLine;					}				} catch (EOFException e) {				} catch (IOException e) {				}				String xupdate =					"<xu:modifications version=\"1.0\" "						+ "xmlns:xu=\"http://www.xmldb.org/xupdate\">"						+ command						+ "</xu:modifications>";				XUpdateQueryService service =					(XUpdateQueryService) current.getService("XUpdateQueryService", "1.0");				long mods = service.update(xupdate);				System.out.println(mods + " modifications processed.");			} else if (args[0].equalsIgnoreCase("set")) {				if (args.length == 1)					properties.list(System.out);				else					try {						StringTokenizer tok = new StringTokenizer(args[1], "= ");						if (tok.countTokens() < 2) {							System.err.println("please specify a key=value pair");							return true;						}						String key = tok.nextToken();						String val = tok.nextToken();						System.out.println(key + " = " + val);						properties.setProperty(key, val);						getResources();					} catch (Exception e) {						System.err.println("Exception: " + e.getMessage());					}			} else if (args[0].equalsIgnoreCase("shutdown")) {				DatabaseInstanceManager mgr =					(DatabaseInstanceManager) current.getService("DatabaseInstanceManager", "1.0");				if (mgr == null) {					System.err.println("service is not available");					return true;				}				mgr.shutdown();				return true;			} else if (args[0].equalsIgnoreCase("help") || args[0].equals("?"))				displayHelp();			else if (args[0].equalsIgnoreCase("quit")) {				return false;			} else {				System.err.println("unknown command");				return true;			}			path = newPath;			return true;		} catch (XMLDBException xde) {			xde.printStackTrace();			if (startGUI)				frame.showErrorMessage("XMLDBException: " + xde.getMessage(), xde);			else				System.err.println(					"XMLDBException: " + xde.getMessage() + " [" + xde.errorCode + "]");			return true;		}	}