Index: trunk/java-client/org/nzdl/gsdl/classification/LibraryOfCongressClassification.java
===================================================================
--- trunk/java-client/org/nzdl/gsdl/classification/LibraryOfCongressClassification.java	(revision 2194)
+++ trunk/java-client/org/nzdl/gsdl/classification/LibraryOfCongressClassification.java	(revision 2194)
@@ -0,0 +1,199 @@
+/*
+ *    TODO.java
+ *    Copyright (C) 2000 Stuart Yeates 
+ *
+ *    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.
+ */
+
+// the package we're in
+package org.nzdl.gsdl.classification;
+
+// java standard library classes used
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.Serializable;
+import java.lang.Cloneable;
+import java.util.Vector;
+import java.util.Hashtable;
+import java.util.Enumeration;
+import java.util.Properties;
+
+/**
+ * The Library of Congress Classification System
+ * 
+ * @author stuart yeates (say1@cs.waikato.ac.nz)
+ * @version $Revision$
+ * @see org.nzdl.gsdl.classification.Classification
+ * @see org.nzdl.gsdl.classification.ClassificationSystem
+ * @see org.nzdl.gsdl.classification.LibraryOfCongressClassification
+ * @see java.io.Serializable
+ * @see java.lang.Cloneable
+ */
+
+/**
+ * A  Dewey Decimal Subject Classification Category
+ *
+ * @author stuart yeates (say1@cs.waikato.ac.nz)
+ * @version $Revision$
+ * @see org.nzdl.gsdl.classification.ClassificationSystem
+ * @see java.io.Serializable
+ * @see java.lang.Cloneable
+ * @see
+ */
+public class LibraryOfCongressClassification implements Classification {
+  LibraryOfCongressClassificationSystem system = null;
+  LibraryOfCongressClassification parent = null;
+  String key = null;
+  String value = null;
+  String parentValue = null;  // parentValue  + " --- " + myValue == value
+  String myValue = null;      //
+  Vector subClassifications = null;
+  int level;
+
+  LibraryOfCongressClassification(LibraryOfCongressClassificationSystem system,
+			     LibraryOfCongressClassification parent,
+			     int level,
+			     String key, 
+			     String value)
+  {
+    this.system = system;
+    this.parent = parent;
+    this.key = key;
+    this.value = value;
+    this.level = level;
+    subClassifications = new Vector();
+    for(int i = 0; i < 10;i++)
+      subClassifications.add(null);
+  }
+
+  boolean add (String key, String value) {
+    if (key.length() < level+1)
+      throw new Error("blarg " + key.length() + " " + level);
+    int next = Integer.parseInt(key.substring(level,level+1));
+    if (subClassifications.elementAt(next) == null) {
+      LibraryOfCongressClassification classification = 
+	new LibraryOfCongressClassification(system,
+				       this,
+				       level+1,
+				       key,
+				       value);
+      subClassifications.setElementAt(classification,next);
+      return true;
+    } else {
+      LibraryOfCongressClassification classification = 
+	(LibraryOfCongressClassification) subClassifications.elementAt(next);
+     return classification.add(key,value);
+    }
+  }
+
+  void clean() {
+    String common = null;
+
+    if (value == null) {
+      for (int i = 0; i < 10; i++){
+	LibraryOfCongressClassification classification = 
+	  (LibraryOfCongressClassification) subClassifications.elementAt(i);
+	if (classification != null){
+	  classification.clean();
+	  if (common == null)
+	    common = clean (classification.getFullTextualDescription());
+	  else {
+	    String diff = clean (classification.getFullTextualDescription());
+	    int min = Math.min(common.length(),diff.length());
+	    StringBuffer com = new StringBuffer(min);
+	    int j = 0;
+	    for (j = 0; j < min && (diff.charAt(j) == common.charAt(j)); j++){
+	      com.append(diff.charAt(j));
+	    }
+	    common = com.toString(); 
+	  }
+	}
+      }
+      value = common;
+    }
+    if (parent == null)
+      parentValue = "";
+    else
+      parentValue = parent.value;
+    myValue = clean (value.substring(parentValue.length(),value.length())); 
+  }
+
+  String clean(String in) {
+    int first = 0;
+    int last = in.length();
+    for (; first < last-1 && 
+	   (in.charAt(first) == ' ' || 
+	    in.charAt(first) == '-'); first++)
+      ;
+    for (; last > 1 && 
+	   (in.charAt(last-1) == ' ' || 
+	    in.charAt(last-1) == '-'); last--)
+      ;
+
+    return in.substring(first,last);
+  }
+
+
+  /** What is the textual description of this Classification? */
+  public Classification getClassificationFor(String string) {
+    if (key.length() < level)
+      return this;
+    
+    int next = Integer.parseInt(key.substring(level,level+1));
+    if (subClassifications.elementAt(next) == null) {
+      return this;
+    } else {
+      LibraryOfCongressClassification classification = 
+	(LibraryOfCongressClassification) subClassifications.elementAt(next);
+      return classification.getClassificationFor(string);
+    }
+  }
+  
+  /** What is the textual description of this Classification? */
+  public String getTextualDescription() {
+    return value;
+  }
+    /** Verbose textual description */
+  public String getFullTextualDescription() {
+    if (parent == null)
+      return value;
+    else
+      return parent.getFullTextualDescription() + value;
+  }
+  /** What is the code representing this Classification */
+  public String getLabel() {
+    return key;
+  }
+  /** What System is this Classification part of? */
+  public ClassificationSystem getSystem(){
+    return system;
+  }
+
+  /** Does this Classification contain this sub-Classification? */
+  public boolean contains(Classification instance){
+    return false;
+  }
+  /** What direct sub-Classifications does this Classification have? */
+  public Enumeration getSubClassifications() {
+    return subClassifications.elements();
+  }
+  /** The parent Classification */
+  public Classification getParent() {
+    return parent;
+  }
+  
+}
Index: trunk/java-client/org/nzdl/gsdl/classification/LibraryOfCongressClassificationSystem.java
===================================================================
--- trunk/java-client/org/nzdl/gsdl/classification/LibraryOfCongressClassificationSystem.java	(revision 2194)
+++ trunk/java-client/org/nzdl/gsdl/classification/LibraryOfCongressClassificationSystem.java	(revision 2194)
@@ -0,0 +1,114 @@
+/*
+ *    LibraryOfCongressClassificationSystem.java
+ *    Copyright (C) 2000 Stuart Yeates 
+ *
+ *    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.
+ */
+
+// the package we're in
+package org.nzdl.gsdl.classification;
+
+// java standard library classes used
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.Serializable;
+import java.lang.Cloneable;
+import java.util.Vector;
+import java.util.Hashtable;
+import java.util.Enumeration;
+import java.util.Properties;
+
+/**
+ * The Library of Congress Classification System
+ * 
+ * @author stuart yeates (say1@cs.waikato.ac.nz)
+ * @version $Revision$
+ * @see org.nzdl.gsdl.classification.Classification
+ * @see org.nzdl.gsdl.classification.ClassificationSystem
+ * @see org.nzdl.gsdl.classification.LibraryOfCongressClassification
+ * @see java.io.Serializable
+ * @see java.lang.Cloneable
+ */
+
+public class LibraryOfCongressClassificationSystem 
+  implements ClassificationSystem {
+  
+  static LibraryOfCongressClassificationSystem instance = null;
+  
+  static final protected String name = "Dewey Decimal Classification System";
+  LibraryOfCongressClassification root = null;
+  
+  protected LibraryOfCongressClassificationSystem() {
+    root = new LibraryOfCongressClassification(this, null,0,"",getName());
+    init();
+    root.clean();
+  }
+  static public LibraryOfCongressClassificationSystem getInstance() {
+    if (instance == null)
+      instance = new LibraryOfCongressClassificationSystem();
+    return instance;
+  }
+
+  protected boolean add (String key, String value) {
+    return root.add(key,value);
+  }
+  
+  protected void init() {
+  }
+  /** */
+  public Classification getClassificationFor(String string) {
+    return root.getClassificationFor(string);
+  }
+
+
+  /** Returns the name of the ClassificationSystem*/
+  public String getName() { return name; };
+
+  /** 
+   * Returns an enumeration of all top-level Classifications 
+   * (or all Classifications if this is a non-heirarchical 
+   * ClassificationSystem) 
+   */
+  public Enumeration getClassifications() {return root.getSubClassifications();};
+ 
+  /**
+   * Parse a string containing one or more Classifications and
+   * return an Enumeration of them.
+   */
+  public Enumeration  parse(String rawDesc) {
+    Vector vec = new Vector();
+    StringBuffer tmp = new StringBuffer();
+    for(int i = 0; i <rawDesc.length();i++) {
+      if (Character.isDigit((char)i) || i == '.') {
+	tmp.append(i);
+      } else {
+	if (tmp.length() != 0)
+	  vec.add(getClassificationFor(tmp.toString()));
+	tmp.delete(0,tmp.length());
+      }
+    }      
+    return vec.elements();
+
+  };
+
+  public static void main(String args[]) throws IOException
+  {
+    LibraryOfCongressClassificationSystem system = 
+      new LibraryOfCongressClassificationSystem();
+  }
+
+}
Index: trunk/java-client/org/nzdl/gsdl/classification/LibraryOfCongressCollectionsPolicy.java
===================================================================
--- trunk/java-client/org/nzdl/gsdl/classification/LibraryOfCongressCollectionsPolicy.java	(revision 2194)
+++ trunk/java-client/org/nzdl/gsdl/classification/LibraryOfCongressCollectionsPolicy.java	(revision 2194)
@@ -0,0 +1,148 @@
+/*
+ *    TODO.java
+ *    Copyright (C) 2000 Stuart Yeates 
+ *
+ *    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.
+ */
+
+// the package we're in
+package org.nzdl.gsdl.classification;
+
+// java standard library classes used
+import java.util.Enumeration;
+import java.util.Vector;
+import java.util.Properties;
+
+/**
+ * Class ...
+ *
+ * Based on algorithms in ...
+ *
+ * @author stuart yeates (say1@cs.waikato.ac.nz)
+ * @version $Revision$
+ * @see
+ * @see
+ * @see
+ * 
+ */
+
+public class LibraryOfCongressCollectionsPolicy implements CollectionsPolicy
+{
+  Properties byClass = null;
+
+  LibraryOfCongressCollectionsPolicy() {
+    byClass = new Properties();
+  }
+  LibraryOfCongressCollectionsPolicy(Properties p) {
+    byClass = p;
+  }
+
+  /**
+   * Get the ClassificationSystem used by the CollectionsPolicy
+   * 
+   * @return ClassificationSystem
+   * @see ClassificationSystem
+   */  
+  public ClassificationSystem getClassificationSystem(){
+    return LibraryOfCongressClassificationSystem.getInstance();
+  }
+  /**
+   * Assign a level to a piece of metadata. 
+   *
+   * The metadata must be in a format understood by the 
+   * ClassificationSystem used by the CollectionsPolicy
+   * 
+   * @param metadata the metadata
+   * @return the level
+   * @see ClassificationSystem
+   */
+  public int level(String metadata){
+    Enumeration e = getClassificationSystem().parse(metadata);
+    if (e.hasMoreElements())
+      return ((Integer)byClass.get(e.nextElement())).intValue();
+    else
+      return 0;
+  }
+
+  /**
+   * Assign a level to a collection of metadata. 
+   *
+   * The metadata must be in a format understood by the 
+   * ClassificationSystem used by the CollectionsPolicy
+   * 
+   * @exception java.io.IOException when ...
+   * @param metadata the metadata
+   * @return the level
+   * @see ClassificationSystem
+   */
+  public int level(String[] metadata) {
+    int top = 0;
+    for (int i = 0; i < metadata.length; i++)
+      top = Math.max(top, level(metadata[i]));
+    return top;
+  }
+  
+  /**
+   * Get all Classifications for a given level
+   * 
+   * @exception java.io.IOException when ...
+   * @param level the level for which Classifications are returned
+   * @return the Classifications
+   * @see Classification
+   */
+  public Enumeration getClassificationsForLevel(int level){
+    Vector vec = new Vector ();
+    Enumeration e = byClass.keys();
+    while (e.hasMoreElements()) {
+      Classification c = (Classification) e.nextElement();
+      Integer I = (Integer) byClass.get(c);
+      int i = I.intValue();
+      if (i == level)
+	vec.add(c);
+    }
+    return vec.elements();
+  }
+
+  /**
+   * Tests...
+   *
+   * 
+   * @param args the arguments ...
+   */
+
+  public static void main(String args[])
+  {
+    Properties p = new Properties();
+    p.put("004",new Integer(5));
+    p.put("005",new Integer(5));
+    p.put("006",new Integer(5));
+    p.put("001",new Integer(4));
+    p.put("002",new Integer(4));
+    p.put("003",new Integer(4));
+    LibraryOfCongressCollectionsPolicy policy = new LibraryOfCongressCollectionsPolicy(p);
+
+    System.out.println("the level of 001 should is: " + policy.level("001") + " should be 4");
+    System.out.println("the level of 002 should is: " + policy.level("002") + " should be 4");
+    System.out.println("the level of 003 should is: " + policy.level("003") + " should be 4");
+    System.out.println("the level of 004 should is: " + policy.level("004") + " should be 5");
+    System.out.println("the level of 005 should is: " + policy.level("005") + " should be 5");
+    System.out.println("the level of 006 should is: " + policy.level("006") + " should be 5");
+    System.out.println("the level of 010 should is: " + policy.level("010") + " should be 0");
+    System.out.println("the level of 005.5 should is: " + policy.level("005.5") + " should be 5");
+    
+    
+  }
+}
+
