Index: /trunk/gsdl/packages/mg/java/org/greenstone/mg/MGDocInfo.java
===================================================================
--- /trunk/gsdl/packages/mg/java/org/greenstone/mg/MGDocInfo.java	(revision 3741)
+++ /trunk/gsdl/packages/mg/java/org/greenstone/mg/MGDocInfo.java	(revision 3741)
@@ -0,0 +1,42 @@
+/*
+ *    MGDocInfo.java
+ *    Copyright (C) 2002 New Zealand Digital Library, http://www.nzdl.org
+ *
+ *    This program is free software; you can redistribute it and/or modify
+ *    it under the terms of the GNU General Public License as published by
+ *    the Free Software Foundation; either version 2 of the License, or
+ *    (at your option) any later version.
+ *
+ *    This program is distributed in the hope that it will be useful,
+ *    but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *    GNU General Public License for more details.
+ *
+ *    You should have received a copy of the GNU General Public License
+ *    along with this program; if not, write to the Free Software
+ *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+package org.greenstone.mg;
+
+
+/** DocInfo 'struct' class for a search result from mg
+ *  used by MGQueryResult
+ *
+ * @See MGQueryResult
+ */
+public class MGDocInfo
+{
+    public long num_ = 0;
+    public float rank_ = 0;
+
+    public MGDocInfo(long doc, float rank)
+    {
+	num_ = doc;
+	rank_ = rank;
+    }
+
+    public String toString()
+    {
+	return "" + num_ + " (" + rank_ + ")";
+    }
+}
Index: /trunk/gsdl/packages/mg/java/org/greenstone/mg/MGQueryResult.java
===================================================================
--- /trunk/gsdl/packages/mg/java/org/greenstone/mg/MGQueryResult.java	(revision 3741)
+++ /trunk/gsdl/packages/mg/java/org/greenstone/mg/MGQueryResult.java	(revision 3741)
@@ -0,0 +1,140 @@
+/*
+ *    MGQueryResult.java
+ *    Copyright (C) 2002 New Zealand Digital Library, http://www.nzdl.org
+ *
+ *    This program is free software; you can redistribute it and/or modify
+ *    it under the terms of the GNU General Public License as published by
+ *    the Free Software Foundation; either version 2 of the License, or
+ *    (at your option) any later version.
+ *
+ *    This program is distributed in the hope that it will be useful,
+ *    but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *    GNU General Public License for more details.
+ *
+ *    You should have received a copy of the GNU General Public License
+ *    along with this program; if not, write to the Free Software
+ *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+package org.greenstone.mg;
+
+
+import java.util.Vector;
+
+
+/** a java version of QueryResult for mg
+ *
+ * contains get methods for the java side, and simple
+ * set methods to be called from c side
+ *
+ */
+public class MGQueryResult
+{
+    /** the list of MGDocInfo */
+    protected Vector docs_ = null;
+    /** the list of MGTermInfo */
+    protected Vector terms_ = null;
+    /** the total number of docs found - not likely to be the size of docs_ */
+    protected long total_num_docs_ = 0;
+
+
+    MGQueryResult() {
+	docs_ = new Vector();
+	terms_ = new Vector();
+    }
+
+
+    /** clear the info from the last query
+	should be called before setting any new docs/terms */
+    public void clear() {
+	total_num_docs_ = 0;
+	docs_.clear();
+	terms_.clear();
+    }
+
+
+    /** returns the result as a String - useful for printing out results */
+    public String toString()
+    {
+	String result = "";
+	result += "docs (ranks): ";
+	for (int i = 0; i < docs_.size(); i++) {
+	    if (i > 0) result += ", ";
+	    result += ((MGDocInfo) docs_.elementAt(i)).toString();
+	}
+	result += "\nterms: ";
+	for (int i = 0; i < terms_.size(); i++) {
+	    if (i > 0) result += ", ";
+	    result += ((MGTermInfo) terms_.elementAt(i)).toString();
+	}
+	result += "\nactual number of docs found = " + total_num_docs_;
+	return result;
+    }
+
+
+    /** a shorter representation - just terms and total docs - not the 
+	individual docnums and ranks */
+    public String toShortString() {
+	String result = "";
+	result += "\nterms: ";
+	for (int i = 0; i < terms_.size(); i++) {
+	    if (i > 0) result += ", ";
+	    result += ((MGTermInfo) terms_.elementAt(i)).toString();
+	}
+	result += "\nactual number of docs found = "+total_num_docs_;
+	return result;
+    }
+
+
+    // set methods used by c++ code
+
+    public void setTotalDocs(long num) {
+	total_num_docs_=num;
+    }
+
+
+    public void addDoc(long doc, float rank) {
+	MGDocInfo doc_info = new MGDocInfo(doc, rank);
+	System.out.println("(Java) Added doc " + doc_info);
+
+	docs_.add(doc_info);
+    }
+
+
+    // public void addTerm(String term, int stem, long match, long freq)
+    public void addTerm(String term, String tag, int stem,
+			long match, long freq, String[] equivs)
+    {
+	MGTermInfo ti = new MGTermInfo();
+	ti.term_ = term;
+	ti.tag_ = tag;
+	ti.stem_method_ = stem;
+	ti.match_docs_ = match;
+	ti.term_freq_ = freq;
+	if (equivs != null) {
+	    for (int i = 0; i < equivs.length; i++) {
+		ti.equiv_terms_.add(equivs[i]);
+	    }
+	}
+	System.out.println("(Java) Added term " + ti);
+
+	terms_.add(ti);
+    }
+
+
+    // Get methods for the java side - GS2MGSearch.java, GS2MGRetrieve.java
+    public Vector getDocs() {
+	return docs_;
+    }
+
+
+    public Vector getTerms() {
+	return terms_;
+    }
+
+
+    public long getTotalDocs() {
+	return total_num_docs_;
+    }
+}
+
Index: /trunk/gsdl/packages/mg/java/org/greenstone/mg/MGTermInfo.java
===================================================================
--- /trunk/gsdl/packages/mg/java/org/greenstone/mg/MGTermInfo.java	(revision 3741)
+++ /trunk/gsdl/packages/mg/java/org/greenstone/mg/MGTermInfo.java	(revision 3741)
@@ -0,0 +1,81 @@
+/*
+ *    MGTermInfo.java
+ *    Copyright (C) 2002 New Zealand Digital Library, http://www.nzdl.org
+ *
+ *    This program is free software; you can redistribute it and/or modify
+ *    it under the terms of the GNU General Public License as published by
+ *    the Free Software Foundation; either version 2 of the License, or
+ *    (at your option) any later version.
+ *
+ *    This program is distributed in the hope that it will be useful,
+ *    but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *    GNU General Public License for more details.
+ *
+ *    You should have received a copy of the GNU General Public License
+ *    along with this program; if not, write to the Free Software
+ *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+package org.greenstone.mg;
+
+
+import java.util.Vector;
+
+
+/** TermInfo 'struct' class for a search result from mg
+ *  used by MGQueryResult
+ *
+ * @see MGQueryResult
+ */
+public class MGTermInfo
+{
+    /** the term itself */
+    public String term_ = null;
+
+    /** the tag - level or metadata - for which the query was done */
+    public String tag_ = null;
+
+    /** the stem and casefold method used
+	0 = none
+	1 = casefold only
+	2 = stem only
+	3 = casefold and stem */
+    public int stem_method_ = 0;
+
+    /** the number of documents containing this term
+	(where document is defined by the tag data) */
+    public long match_docs_ = 0;
+
+    /** overall term freq - word level */
+    public long term_freq_ = 0;
+
+    /** list of stemmed and casefolded equivalent terms - if stem_method_ is non-zero 
+      - Vector of Strings */
+    public Vector equiv_terms_ = null;
+
+
+    public MGTermInfo()
+    {
+	equiv_terms_ = new Vector();
+    }
+
+
+    /** output the class as a string */
+    public String toString()
+    {
+	String result = "\"" + term_ + "\"";
+	result += " tag(" + tag_ + ")";
+	result += " stem(" + stem_method_ + ")";
+	result += " docs(" + match_docs_ + ")";
+	result += " freq(" + term_freq_ + ")";
+
+	result += " equiv terms(";
+	for (int i = 0; i < equiv_terms_.size(); i++) {
+	    if (i > 0) result += ", ";
+	    result += (String) equiv_terms_.elementAt(i);
+	}
+	result += ")";
+
+	return result;
+    }
+}
Index: /trunk/gsdl/packages/mg/java/org/greenstone/mg/MGWrapper.java
===================================================================
--- /trunk/gsdl/packages/mg/java/org/greenstone/mg/MGWrapper.java	(revision 3741)
+++ /trunk/gsdl/packages/mg/java/org/greenstone/mg/MGWrapper.java	(revision 3741)
@@ -0,0 +1,102 @@
+/*
+ *    MGWrapper.java
+ *    Copyright (C) 2002 New Zealand Digital Library, http://www.nzdl.org
+ *
+ *    This program is free software; you can redistribute it and/or modify
+ *    it under the terms of the GNU General Public License as published by
+ *    the Free Software Foundation; either version 2 of the License, or
+ *    (at your option) any later version.
+ *
+ *    This program is distributed in the hope that it will be useful,
+ *    but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *    GNU General Public License for more details.
+ *
+ *    You should have received a copy of the GNU General Public License
+ *    along with this program; if not, write to the Free Software
+ *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+package org.greenstone.mg;
+
+
+/** java wrapper class for access to mg in C
+ *
+ * the native side implemented in MGWrapperImpl.c
+ * uses MGQueryResult to hold the result of a query.
+ * the result of a getDocument is a String
+ * uses the jni
+ *
+ *@see MGQueryResult
+ */
+public class MGWrapper
+{
+    /** the query result, filled in by runQuery */
+    protected MGQueryResult mg_query_result_ = null;
+
+    /** pointer to c MGWrapperData class - cached indexData and queryInfo */
+    protected long mg_data_ptr_ = 0;
+
+    static {
+	System.loadLibrary("mgjni");
+	initIDs();
+    }
+
+    public MGWrapper() {
+	mg_query_result_ = new MGQueryResult();
+	initCSide();
+    }
+
+    /** initialises field and method IDs for java side to enable access on C side */
+    private static native void initIDs();
+
+    /** initialises the mg_data_ptr_ */
+    private native boolean initCSide();
+
+    /** returns a document: number docnum at level level 
+     * the base_dir and text_path paths should join together to provide
+     * the absolute location of the mg text files eg ..../index/text/demo
+     * returns the doc in utf-8
+     */
+    public native String getDocument(String base_dir, String text_dir, long docnum);
+
+    /** unloads the data */
+    public native boolean unloadIndexData();
+
+    // query param methods
+
+    /** if on=true, sets default casefolding on - it's off by default */
+    public native void setCase(boolean on);
+    /** if on=true, sets default stemming on - it's off by default */
+    public native void setStem(boolean on);
+    /** default is 50 */
+    public native void setMaxDocs(int num);
+    /** if on=true, sorts by rank, otherwise returns in build order - 
+	default is on */
+    public native void setSortByRank(boolean on);
+    /** if on=true, a query returns term freq info - default is on */
+    public native void setReturnTerms(boolean on);
+    /** sets the index to search - default is 'dtx' */
+    public native void setIndex(String index);
+    /** sets the default boolean operator - AND(=1)/OR(=0) */
+    public native void setMatchMode(int mode);
+
+    /** returns a string with all the current query param settings */
+    public native String getQueryParams();
+
+    /** actually carry out the query.
+	Use the set methods to set query results.
+	Writes the result to query_result.
+     * - maintains state between requests as can be slow  
+     * base_dir and index_path should join together to provide
+     * the absolute location of the mg index files eg ..../index/dtx/demo 
+     * base_dir must end with a file separator (OS dependant)
+     */
+    public native void runQuery(String base_dir, String text_dir, String query_string);
+
+
+    /** get the result out of the wrapper */
+    public MGQueryResult getQueryResult()
+    {
+	return mg_query_result_;
+    }
+}
Index: /trunk/gsdl/packages/mg/java/org/greenstone/mg/Makefile.in
===================================================================
--- /trunk/gsdl/packages/mg/java/org/greenstone/mg/Makefile.in	(revision 3741)
+++ /trunk/gsdl/packages/mg/java/org/greenstone/mg/Makefile.in	(revision 3741)
@@ -0,0 +1,64 @@
+
+SHELL = /bin/sh
+VPATH = @srcdir@
+exec_prefix = @exec_prefix@
+prefix = @prefix@
+bindir = @bindir@
+srcdir = @srcdir@
+libdir = @libdir@
+subdir = java/org/greenstone/mg
+top_srcdir = @top_srcdir@
+transform = @program_transform_name@
+
+AR = ar
+CC = @CC@
+CXX = @CXX@
+JAVAC = javac
+JAVAH = javah
+JAR = jar
+CFLAGS = @CFLAGS@ -ansi -DSILENT -DSHORT_SUFFIX
+CXXFLAGS = @CXXFLAGS@ -DSILENT -DSHORT_SUFFIX
+DEFS = @DEFS@
+RANLIB = @RANLIB@
+INSTALL = @INSTALL@
+INSTALL_DATA = @INSTALL_DATA@
+INSTALL_PROGRAM = @INSTALL_PROGRAM@
+LDFLAGS = @LDFLAGS@
+LIBS =  -lstdc++ @LIBS@
+TOUCH = echo timestamp >
+AWK = @AWK@
+
+MGHOME=../../../..
+JAVALIBDIR=$(libdir)/java
+JAVACLASSDIR=./classes
+JAVASRCDIR=../../..
+JAVACOPTIONS= -deprecation -g -O
+JAVACLASSPATH=$(JAVACLASSDIR):
+
+datadir = $(prefix)/share
+libexecdir = $(exec_prefix)/libexec
+localedir = $(datadir)/locale
+mandir = $(prefix)/man/man1
+
+
+all: setup compile
+
+setup:
+	mkdir -p $(JAVACLASSDIR)
+
+compile: setup
+	$(JAVAC) -d $(JAVACLASSDIR) -sourcepath $(JAVASRCDIR) $(JAVACOPTIONS) *.java
+	$(JAVAH) -classpath $(JAVACLASSPATH) -d $(MGHOME)/jni org.greenstone.mg.MGWrapper
+	$(JAR) cf mg.jar -C $(JAVACLASSDIR) org
+
+clean:
+	rm -rf $(JAVACLASSDIR) mg.jar $(MGHOME)/jni/org_greenstone_mg_MGWrapper.h
+
+install: compile
+	$(MGHOME)/mkinstalldirs $(JAVALIBDIR); \
+	cp mg.jar $(JAVALIBDIR)/
+
+distclean: clean
+	rm -f Makefile
+
+
Index: /trunk/gsdl/packages/mg/java/org/greenstone/mg/Queryer.java
===================================================================
--- /trunk/gsdl/packages/mg/java/org/greenstone/mg/Queryer.java	(revision 3741)
+++ /trunk/gsdl/packages/mg/java/org/greenstone/mg/Queryer.java	(revision 3741)
@@ -0,0 +1,185 @@
+/*
+ *    Queryer.java
+ *    Copyright (C) 2002 New Zealand Digital Library, http://www.nzdl.org
+ *
+ *    This program is free software; you can redistribute it and/or modify
+ *    it under the terms of the GNU General Public License as published by
+ *    the Free Software Foundation; either version 2 of the License, or
+ *    (at your option) any later version.
+ *
+ *    This program is distributed in the hope that it will be useful,
+ *    but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *    GNU General Public License for more details.
+ *
+ *    You should have received a copy of the GNU General Public License
+ *    along with this program; if not, write to the Free Software
+ *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+package org.greenstone.mg;
+
+import java.util.Vector;
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
+
+/** Queryer - java port of c++ Queryer 
+ * - uses MGWrapper to access mg
+ * only has get document and search - full text browse not implemented
+ *
+ * run as:
+ * java org.greenstone.mg.Queryer <basedir> <indexdir> <textdir>
+ *@see MGWrapper
+ */
+public class Queryer {
+  
+    /** outputs to std out a help message describing the commands used by 
+     * Queryer 
+     */
+    public void printHelp()
+    {
+	System.out.println( "commands available are:\n"+
+			    "\t.q\t\tquit\n"+
+			    "\t.h\t\tprint this help message\n"+
+			    "\t.d\t\tprint the current query parameter settings\n" +
+			    "\t.i<index>\tsearch index <index>\n"+
+			    "\t.r0/.r1\t\tranking off/on\n"+
+			    "\t.t0/.t1\t\tquery type some/all\n"+
+			    "\t.c0/.c1\t\tcasefolding off/on\n"+
+			    "\t.s0/.s1\t\tstemming off/on\n"+
+			    "\t.o0/.o1\t\tshort output off/on\n"+
+			    "\t.m<num>\t\tset max docs to return to num\n\n"+
+			    "\t.p<docnum>\tprint document docnum\n"+
+			    "\t<query string>\tdo a query\n");
+    }
+
+   
+    public static void main(String[] args)
+    {
+	if (args.length != 3) {
+	    System.out.println("Usage: java org.greenstone.mg.Queryer <basedir> <indexdir> <textdir>");
+	    return;
+	}
+
+	Queryer self = new Queryer();
+
+	String base_dir = args[0];
+	String index_path = args[1];
+	String text_path = args[2];
+
+	// the jni class to access mg stuff
+	MGWrapper wrapper = new MGWrapper();
+	wrapper.setIndex(index_path);
+
+	System.out.println("Welcome to Java Queryer :-)");
+	self.printHelp();
+
+	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
+	boolean shortOutput = false;
+
+	String command; // user input
+	char x; // the command letter
+	String data = null; // any auxiliary input to a command
+	while (true) {
+	    System.out.print(">"); // the prompt
+            try {
+                command = br.readLine();
+                command = command.trim();
+		if (command.startsWith(".")) {
+		    // a system command
+		    x = command.charAt(1);
+		    if (command.length() > 2) {
+			data = command.substring(2);
+			data = data.trim();
+		    }
+
+		    switch (x) {
+		    case 'q': // clean up and exit
+			wrapper.unloadIndexData();
+			return;
+		    case 'h': // print help message
+			self.printHelp();
+			break;
+		    case 'd': // print query param settings
+			String info = wrapper.getQueryParams();
+			System.out.println(info);
+			break;
+		    case 'p': // print doc
+			int docnum = Integer.parseInt(data);
+			String doc = wrapper.getDocument(base_dir, text_path, docnum);
+			System.out.println(doc);
+			break;
+		    case 'm': //match docs
+			int match = Integer.parseInt(data);
+			wrapper.setMaxDocs(match);
+			break;
+		    case 's': // set stem on/off
+			int stem = Integer.parseInt(data);
+			if (stem==0 ){
+			    wrapper.setStem(false);
+			} else if(stem==1) {
+			    wrapper.setStem(true);
+			} else {
+			    System.err.println("Error: stem should be 0 or 1");
+			}
+			break;
+		    case 'c': // set case on/off
+			int casef = Integer.parseInt(data);
+			if (casef==0) {
+			    wrapper.setCase(false);
+			} else if (casef==1) {
+			    wrapper.setCase(true);
+			} else {
+			    System.err.println("Error: case should be 0 or 1");
+			}
+			break;
+		    case 'i': // set index
+			wrapper.setIndex(data);
+			break;
+		    case 'r': // set rank on/off
+			int rank = Integer.parseInt(data);
+			if (rank==0) {
+			    wrapper.setSortByRank(false);
+			} else if (rank==1) {
+			    wrapper.setSortByRank(true);
+			} else {
+			    System.err.println("Error: rank should be 0 or 1");
+			}
+			break;
+		    case 't': // set query type some/all
+			int type = Integer.parseInt(data);
+			if (type==0 || type==1) {
+			    wrapper.setMatchMode(type);
+			} else {
+			    System.err.println("Error: type should be 0 (some) or 1 (all)");
+			}
+			break;
+		    case 'o': // set output short/long
+			int output = Integer.parseInt(data);
+			if (output==0) {
+			    shortOutput = false;
+			} else if (output==1) {
+			    shortOutput = true;
+			} else {
+			    System.err.println("Error: output should be 0 or 1");
+			}
+			break;
+		    }
+		}
+		else {
+		    // a query
+		    wrapper.runQuery(base_dir, text_path, command);
+		    MGQueryResult res = wrapper.getQueryResult();
+		    System.out.println("(Java) Matching documents: " + res.getTotalDocs());
+		    if (shortOutput) {
+			System.out.println(res.toShortString());
+		    } else {
+			System.out.println(res.toString());
+		    }
+		}
+
+	    } catch (Exception e) {
+		System.out.println("Queryer error: "+e.getClass() + " "+e.getMessage());
+	    }
+	}
+    }
+}
