package org.apache.xindice.examples;
/*
* 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: CreateCollection.java,v 1.8 2004/02/08 01:19:43 vgritsenko Exp $
*/
import org.apache.xindice.client.xmldb.services.CollectionManager;
import org.apache.xindice.xml.dom.DOMParser;
import org.xmldb.api.base.Collection;
import org.xmldb.api.base.XMLDBException;
/**
* Simple XML:DB API example to create a collection.
*/
public class CreateCollection extends AbstractExample {
private static final String COLLECTION_NAME = "mycollection";
public static void main(String[] args) throws Exception {
Collection collection = null;
try {
// collection = getCollection("xmldb:xindice:///db/");
collection = getCollection("xmldb:xindice://localhost:8888/db/");
// collection = getCollection("xmldb:xindice-embed:///db/");
CollectionManager service = (CollectionManager) collection.getService("CollectionManager", "1.0");
try{
service.dropCollection(COLLECTION_NAME);
System.out.println("Dropped existing collection with name: " + COLLECTION_NAME);
}
catch (Exception e) {
; // nothing, this may be the first pass.
}
// Build up the Collection XML configuration.
String collectionConfig =
"<collection compressed=\"true\" name=\"" + COLLECTION_NAME + "\">"
+ " <filer class=\"org.apache.xindice.core.filer.BTreeFiler\"/>"
+ "</collection>";
service.createCollection(COLLECTION_NAME, DOMParser.toDocument(collectionConfig));
System.out.println("Collection " + COLLECTION_NAME + " created.");
} catch (XMLDBException e) {
System.err.println("XML:DB Exception occured " + e.errorCode + " " + e.getMessage());
} finally {
if (collection != null) {
collection.close();
}
}
}
}