/*
* Copyright 1999-2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* CVS $Id: ListCollections.java,v 1.12 2004/02/08 02:57:35 vgritsenko Exp $
*/
package org.apache.xindice.tools.command;
import org.apache.xindice.tools.XMLTools;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.Log;
import org.xmldb.api.DatabaseManager;
import org.xmldb.api.base.Collection;
import java.util.Hashtable;
/**
* ListCollections.java is designed to let the user list all Collections from
* a parent Collection or a nested Collection.
*
* NOTE: Collection names are not returned in any specific order
*
* @version CVS $Revision: 1.12 $, $Date: 2004/02/08 02:57:35 $
*/
public class ListCollections extends Command {
private static final Log log = LogFactory.getLog(ListCollections.class);
public boolean execute(Hashtable table) throws Exception {
String[] colarray = null;
String colstring = null;
Collection col = null;
try {
if (table.get(XMLTools.COLLECTION) == null) {
System.out.println("ERROR : Collection context required");
return false;
}
// list the COLLECTIONURI + The collection passed in
colstring = normalizeCollectionURI((String) table.get(XMLTools.COLLECTION),
(String) table.get(XMLTools.LOCAL));
// Get a Collection reference
col = DatabaseManager.getCollection(colstring);
if (col == null) {
// log a debug message
log.debug("Error fetching collection '" + colstring + "'");
System.out.println("ERROR : Collection not found!");
return false;
} else {
colarray = col.listChildCollections();
System.out.println();
for (int i = 0; i < colarray.length; i++) {
System.out.println("\t" + colarray[i]);
}
System.out.println("\nTotal collections: " + colarray.length);
}
} finally {
// Close collection objects and release
if (col != null) {
col.close();
}
}
return true;
}
}