Hi, Vadim,

    I made the class to extract collection List. (see attached) The class
contain 2 static methods,

    public static ArrayList getCollectionList(Hashtable t) {...}
and
    public static ArrayList getCollectionList(String s) {..}

The first one is to be used for commandline query
and the second is to be used for programing query

I tested it, it seemed work just fine.

Also, is there a login control in the xindice?

Honglin
package xproposal.dm;

import java.util.Hashtable;
import java.util.ArrayList;
import java.util.StringTokenizer;

import org.xmldb.api.base.*;
import org.xmldb.api.modules.*;
import org.xmldb.api.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.StringTokenizer;

import org.apache.xindice.tools.XMLTools;

import org.apache.xindice.client.xmldb.embed.DatabaseImpl;
import org.apache.xindice.client.xmldb.services.CollectionManager;
//import org.apache.xindice.xml.dom.DocumentImpl;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xmldb.api.DatabaseManager;
import org.xmldb.api.base.Collection;
import org.xmldb.api.base.Resource;
import org.xmldb.api.base.XMLDBException;
import org.xmldb.api.modules.CollectionManagementService;

import org.xmldb.api.base.*;
import org.xmldb.api.modules.*;
import org.xmldb.api.*;
import org.w3c.dom.*;
//import org.jdom.*;
//import org.jdom.input.*;
//import org.jdom.output.*;
import java.io.File;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.NodeList;

import org.apache.xerces.parsers.*;
import java.io.*;
import org.xml.sax.*;
import org.xml.sax.ContentHandler;

import org.xmldb.api.base.*;
import org.xmldb.api.modules.*;
import org.xmldb.api.*;

/**
 * <p>Title: NRAO Proposal Tool</p>
 * <p>Description: The proposal submission and handling tool for NRAO telescopes</p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: National Radio Astronomy Observatory</p>
 * @author Honglin Ye
 * @version 1.0
 */

public class CollectionList {
  public static final String XINDICEURI = "xindice://";
  public static final String XINDICELOCALURI = "xindice-embed://";
  public static final String XMLDBURI = "xmldb:";
  public static final String XMLDBAPIVERSION = "1.0";
  public static final String LOCAL = "local";

  private static String local = "false";
  private static ArrayList workTable = new ArrayList(3);

  public static ArrayList getCollectionList(Hashtable table) {
    local = (String) table.get(XMLTools.LOCAL);
    workTable.clear();
    String col = (String) table.get(XMLTools.COLLECTION);
    return getCollectionList(col);
  }
  public static ArrayList getCollectionList(String col) {
    local = "false";
    workTable.clear();
    StringTokenizer tok = new StringTokenizer(col, ";, ");
    for (int i = 0; tok.hasMoreTokens(); i++) {
      workTable.add(tok.nextToken());
    }
    expandColStr();
    return workTable;
  }
  private static void expandColStr() {
    for (int i = 0; i < workTable.size(); i++) {
      String argStr = (String)workTable.get(i);
      int starPos = argStr.indexOf("/*");
      if (starPos > 0) {
        String head = argStr.substring(0, starPos);
        String tail = argStr.substring(starPos + 2);
        String[] children = getChildCollections(head);
        if (children != null && children.length > 0) {
          workTable.remove(i);
          for (int j = 0; j < children.length; j++) {
            workTable.add(head + "/" + children[j] + tail);
          }
          expandColStr();
        }
      }
    }
  }
  private static String[] getChildCollections(String parent) {
    Collection coll = null;
    String[] colarray = null;
    String colstring = normalizeCollectionURI(parent, local);
    try {
      coll = DatabaseManager.getCollection(colstring);
      if (coll != null) {
        colarray = coll.listChildCollections();
      }
    }
    catch (XMLDBException ex) {
    }
    finally {
      if (coll != null) {
        try {
          coll.close();
        }
        catch (XMLDBException ex) {
        }
      }
    }
    return colarray;
  }
  private static String normalizeCollectionURI(String uri, String local) {
    // Check to see if this uri starts with "xmldb:" , if so treat as absolute
    if (uri.startsWith("xmldb:")) {
      // URI is absolute, leave alone
      return uri;
    } else if (uri.startsWith("xindice:") || uri.startsWith("xindice-embed:")) {
      return (XMLDBURI + uri);
    } else {
      if ((local != null) && local.equals("true")) {
        return (XMLDBURI + XINDICELOCALURI + uri);
      } else {
        // URI passed in is not absoulte, build the full URI
        return (XMLDBURI + XINDICEURI + uri);
      }
    }
  }
}

Reply via email to