You are viewing a plain text version of this content. The canonical link for it is here.
Posted to xindice-users@xml.apache.org by "webhiker@tiscali.fr" <we...@tiscali.fr> on 2004/01/15 17:06:38 UTC
To create indexes (warning: Xindice only)
/**
* Xindice only method for adding indexes to a Collection.
* Pattern uses the following format :
* Pattern Description
* ===========
====================================================
* elem The value of the named element
* elem@attr The value of the attribute for the named element
* * The value for all elements
* *@attr The value of the named attribute for all elements
* elem@* The value of all attributes for the named element
* *@* The value of all attributes for all elements
*/
public void addIndex(Collection collection, String name, String pattern)
throws XMLDBException,
javax.xml.parsers.ParserConfigurationException {
Service collectionManager; // should be of type
org.apache.xindice.client.xmldb.services.CollectionManager
collectionManager = collection.getService("CollectionManager", "1.0");
logger.info("Creating indexer");
try {
DocumentBuilder documentBuilder =
DocumentBuilderFactory.newInstance().newDocumentBuilder();
org.w3c.dom.Document document = documentBuilder.newDocument();
org.w3c.dom.Element element = document.createElement("index");
document.appendChild(element);
element.setAttribute("class","org.apache.xindice.core.indexer.ValueIndexer");
element.setAttribute("name",name);
element.setAttribute("pattern",pattern);
//element.setAttribute("type",""); // optional
//element.setAttribute("pagesize",""); // optional
//element.setAttribute("maxkeysize","");// optional
Method createIndexerMethod =
collectionManager.getClass().getDeclaredMethod("createIndexer",
new
Class[]{org.w3c.dom.Document.class});
Object args[] = new Object[]{document};
createIndexerMethod.invoke(collectionManager,args);
// logger.debug("Listing indexes:");
// String[] idx = collectionManager.listIndexers();
// for (int i = 0; i < idx.length; i++) {
// logger.debug("initializeXMLDB - " + i + " - " + idx[i]);
// }
// logger.debug("initializeXMLDB - Total Indexes: " + idx.length);
}
catch (Throwable t) {
t.printStackTrace();
}
}
Re: To create indexes (warning: Xindice only)
Posted by "webhiker@tiscali.fr" <we...@tiscali.fr>.
Yes, it was in response to your question - thought if i change the title
people will find the mesage more freqenetly in the forums search.
I need the reflection because I compile my classes using the xml:db api,
not a specific db like eXist or Xindice.
I prefer coding to the Xml:Db api, and using reflection for anything
inbetween which is not standard, since you can easily just trap the
exception at runtime if the underlying service
doesn't exist.
Cheers.
wh
Upayavira wrote:
> If this was in response to my comment, thanks. It seems that my code
> is fine. Only thing I notice about yours is that you use reflection
> unnecessarily near the end. Instead of:
>
> Method createIndexerMethod =
> collectionManager.getClass().getDeclaredMethod("createIndexer",
> new
> Class[]{org.w3c.dom.Document.class});
> Object args[] = new Object[]{document};
> createIndexerMethod.invoke(collectionManager,args);
>
> You could just do collectionManager.createIndexer(document); which I
> think would be easier ;-)
>
> Regards, Upayavira
>
> webhiker@tiscali.fr wrote:
>
>> /**
>> * Xindice only method for adding indexes to a Collection.
>> * Pattern uses the following format :
>> * Pattern Description
>> * ===========
>> ====================================================
>> * elem The value of the named element
>> * elem@attr The value of the attribute for the named
>> element
>> * * The value for all elements
>> * *@attr The value of the named attribute for all
>> elements
>> * elem@* The value of all attributes for the named
>> element
>> * *@* The value of all attributes for all elements
>> */
>> public void addIndex(Collection collection, String name, String
>> pattern)
>> throws XMLDBException,
>> javax.xml.parsers.ParserConfigurationException {
>> Service collectionManager; // should be of type
>> org.apache.xindice.client.xmldb.services.CollectionManager
>> collectionManager = collection.getService("CollectionManager",
>> "1.0");
>> logger.info("Creating indexer");
>> try {
>> DocumentBuilder documentBuilder =
>> DocumentBuilderFactory.newInstance().newDocumentBuilder();
>> org.w3c.dom.Document document = documentBuilder.newDocument();
>> org.w3c.dom.Element element = document.createElement("index");
>> document.appendChild(element);
>>
>> element.setAttribute("class","org.apache.xindice.core.indexer.ValueIndexer");
>>
>> element.setAttribute("name",name);
>> element.setAttribute("pattern",pattern);
>> //element.setAttribute("type",""); // optional
>> //element.setAttribute("pagesize",""); // optional
>> //element.setAttribute("maxkeysize","");// optional
>> Method createIndexerMethod =
>> collectionManager.getClass().getDeclaredMethod("createIndexer",
>> new
>> Class[]{org.w3c.dom.Document.class});
>> Object args[] = new Object[]{document};
>> createIndexerMethod.invoke(collectionManager,args);
>> // logger.debug("Listing indexes:");
>> // String[] idx = collectionManager.listIndexers();
>> // for (int i = 0; i < idx.length; i++) {
>> // logger.debug("initializeXMLDB - " + i + " - " + idx[i]);
>> // }
>> // logger.debug("initializeXMLDB - Total Indexes: " +
>> idx.length);
>> }
>> catch (Throwable t) {
>> t.printStackTrace();
>> }
>> }
>>
>>
>>
>>
>
>
>
Re: To create indexes (warning: Xindice only)
Posted by Upayavira <uv...@upaya.co.uk>.
If this was in response to my comment, thanks. It seems that my code is
fine. Only thing I notice about yours is that you use reflection
unnecessarily near the end. Instead of:
Method createIndexerMethod =
collectionManager.getClass().getDeclaredMethod("createIndexer",
new
Class[]{org.w3c.dom.Document.class});
Object args[] = new Object[]{document};
createIndexerMethod.invoke(collectionManager,args);
You could just do collectionManager.createIndexer(document); which I
think would be easier ;-)
Regards, Upayavira
webhiker@tiscali.fr wrote:
> /**
> * Xindice only method for adding indexes to a Collection.
> * Pattern uses the following format :
> * Pattern Description
> * ===========
> ====================================================
> * elem The value of the named element
> * elem@attr The value of the attribute for the named
> element
> * * The value for all elements
> * *@attr The value of the named attribute for all
> elements
> * elem@* The value of all attributes for the named
> element
> * *@* The value of all attributes for all elements
> */
> public void addIndex(Collection collection, String name, String
> pattern)
> throws XMLDBException,
> javax.xml.parsers.ParserConfigurationException {
> Service collectionManager; // should be of type
> org.apache.xindice.client.xmldb.services.CollectionManager
> collectionManager = collection.getService("CollectionManager", "1.0");
> logger.info("Creating indexer");
> try {
> DocumentBuilder documentBuilder =
> DocumentBuilderFactory.newInstance().newDocumentBuilder();
> org.w3c.dom.Document document = documentBuilder.newDocument();
> org.w3c.dom.Element element = document.createElement("index");
> document.appendChild(element);
>
> element.setAttribute("class","org.apache.xindice.core.indexer.ValueIndexer");
>
> element.setAttribute("name",name);
> element.setAttribute("pattern",pattern);
> //element.setAttribute("type",""); // optional
> //element.setAttribute("pagesize",""); // optional
> //element.setAttribute("maxkeysize","");// optional
> Method createIndexerMethod =
> collectionManager.getClass().getDeclaredMethod("createIndexer",
> new
> Class[]{org.w3c.dom.Document.class});
> Object args[] = new Object[]{document};
> createIndexerMethod.invoke(collectionManager,args);
> // logger.debug("Listing indexes:");
> // String[] idx = collectionManager.listIndexers();
> // for (int i = 0; i < idx.length; i++) {
> // logger.debug("initializeXMLDB - " + i + " - " + idx[i]);
> // }
> // logger.debug("initializeXMLDB - Total Indexes: " +
> idx.length);
> }
> catch (Throwable t) {
> t.printStackTrace();
> }
> }
>
>
>
>