
import java.io.*;

import org.xmldb.api.base.*;
import org.xmldb.api.modules.*;
import org.xmldb.api.*;

public class XMLDatabase
{
	  
	public XMLDatabase()
	{

	}//Constructor()
    
	
	public String search( String xpath, String collectionName )
	{
		StringBuffer strBuf = new StringBuffer();
		
		System.out.println("xpath is: "+xpath);
		
		Collection col = null;
		
	    try
	     {
	     String driver = "org.apache.xindice.client.xmldb.DatabaseImpl";
	     Class c = Class.forName(driver);
	         
	     Database database = (Database) c.newInstance();
	     DatabaseManager.registerDatabase(database);
	     col = DatabaseManager.getCollection("xmldb:xindice:///db/"+collectionName);
	     
	     //String xpath = "//phonebook[name='"+name+"' and number='"+number+"']";
	     
	     XPathQueryService service = (XPathQueryService) col.getService("XPathQueryService", "1.0");
	     
	     service.setNamespace("xmlns","http://www.loc.gov/METS/");
	     service.setNamespace("xmlns:xlink","http://www.w3.org/TR/xlink");
	     service.setNamespace("xmlns:DC","http://purl.org/dc/elements/1.1/");
	     service.setNamespace("xmlns:ETOC","http://localhost:8080/Project/xsd/etoc.xsd");
	     service.setNamespace("xmlns:xsi","http://www.w3.org/2001/XMLSchema-instance");
	     service.setNamespace("xsi:schemaLocation","http://www.loc.gov/METS/ http://www.loc.gov/standards/mets/mets.xsd http://purl.org/dc/elements/1.1/http://dublincore.org/schemas/xmls/simpledc20021212.xsd");
	     
	     //copy all the namespaces here!
	     
	     
	     System.out.println("xmlns: "+ service.getNamespace("xmlns"));
	     System.out.println("xlink: "+ service.getNamespace("xlink"));
	     System.out.println("DC: "+ service.getNamespace("DC"));
	     System.out.println("ETOC: "+ service.getNamespace("ETOC"));
	     System.out.println("xsi: "+ service.getNamespace("xsi"));
	     System.out.println("schemaLocation: "+ service.getNamespace("schemaLocation"));



	     ResourceSet resultSet = service.query(xpath);
	     ResourceIterator results = resultSet.getIterator();

	     while(results.hasMoreResources())
	       {
	       Resource res = results.nextResource();

	       String outcome = (String) res.getContent();
	       
	       strBuf.append(outcome);
	       strBuf.append("\n");
	       }//while

	     }//try
	    catch (XMLDBException e) {
	     e.printStackTrace();
	     }//catch
	    catch (ClassNotFoundException cnfe) {
	     System.err.println("XML:DB Exception occured " + cnfe.getMessage());
	     }//catch
	    catch (Exception ie) {
	     ie.printStackTrace();
	     }//catch
	    finally
	     {
	     if(col != null) {
	        try{
	         col.close();
	         }
	        catch( XMLDBException xmle ) {
	         xmle.printStackTrace();
	        }
	       }//if
	     }//try-catch-finally

		return strBuf.toString();
	}//search

	public void add( String data, String collectionName )
    {
	Collection col = null;
   
      try
       {
       String driver = "org.apache.xindice.client.xmldb.DatabaseImpl";
       Class c = Class.forName(driver);

       Database database = (Database) c.newInstance();
       DatabaseManager.registerDatabase(database);
       col = DatabaseManager.getCollection("xmldb:xindice:///db/"+collectionName);

       //String data = "<person><name>"+name+"</name><address type=\"number\">999</address></person>";

       XMLResource document = (XMLResource) col.createResource("aaa", "XMLResource");
       document.setContent(data);
       col.storeResource(document);
       
       System.out.println("Document \n" + data + "\n inserted");
       }//try
      catch (XMLDBException e)  {
       e.printStackTrace();
       }//catch
      catch(Exception ne) {
       ne.printStackTrace();
       }//catch
      finally {
       if(col != null)  {
          try{
           col.close();
           }
          catch( XMLDBException xmle ) {
           System.out.println("XMLDBException 2nd");
          }
         }//if
       }//try-catch-finally
       
    }//add()


	private String readFileData(String filename)
	{
		StringBuffer result = new StringBuffer();
		
        try {

         FileReader fis = new FileReader( filename );
         BufferedReader buf = new BufferedReader( fis );
         
         String text;
            
         while((text=buf.readLine()) != null) {

			//text = text.replace('\n', ' ');

		  result.append(text);
          }//while

		 fis.close();
		 buf.close();
         }//try
        catch(IOException ioe) {
         ioe.printStackTrace();
         }//catch
		
		return result.toString();
	}//readFileData()

	public static void main(String args[])
	{
		XMLDatabase database = new XMLDatabase();
		
		String collectionName = "Test";
		
		String data = database.readFileData("C:\\c.xml");
		
		database.add(data,collectionName);
		
		
		//String xPath = "//Test[ /person/name='Gul Akbar']";
		
		//String result = database.search(xPath,collectionName);
		
		//System.out.println(result);
		System.out.println("finished");
		
	}//static-main

}//end-all




