package org.paris5.cocoon.transformation; import java.io.File; import java.io.IOException; import java.io.Serializable; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Map; import org.apache.avalon.excalibur.pool.Recyclable; import org.apache.avalon.framework.activity.Disposable; import org.apache.avalon.framework.component.ComponentException; import org.apache.avalon.framework.component.ComponentManager; import org.apache.avalon.framework.configuration.Configurable; import org.apache.avalon.framework.configuration.Configuration; import org.apache.avalon.framework.configuration.ConfigurationException; import org.apache.avalon.framework.context.Context; import org.apache.avalon.framework.context.ContextException; import org.apache.avalon.framework.context.Contextualizable; import org.apache.avalon.framework.parameters.Parameters; import org.apache.cocoon.Constants; import org.apache.cocoon.ProcessingException; import org.apache.cocoon.caching.CacheableProcessingComponent; import org.apache.cocoon.components.search.LuceneCocoonHelper; import org.apache.cocoon.environment.SourceResolver; import org.apache.cocoon.transformation.AbstractTransformer; import org.apache.excalibur.source.SourceValidity; import org.apache.excalibur.source.impl.validity.NOPValidity; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.document.DateField; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.Term; import org.apache.lucene.store.Directory; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.AttributesImpl; /** * A lucene index creation transformer. * @author Nicolas Maisoneuve *
Example of input source:
<page xmlns:lucene="http://apache.org/cocoon/lucene/1.0">
<
lucene:index create="true"
analyzer="org.apache.lucene.analysis.standard.StandardAnalyzer"
directory="d:/indexbase"
merge-factor="merge-factor">
<lucene:document>
<lucene:field name="tile" type="keyword">sqdqsdq</lucene:field>
<lucene:field name="description" type="text"> bla bal
blalael balbal </lucene:field>
<lucene:field name="date" type="date" dateformat="MM/dd/yyyy">10/12/2002</lucene:field>
(see java API Class SimpleDateFormat for more explanation about the dateFormat attribut)
<lucene:field name="date" type="unstored" >just indexed
information (not stored)</lucene:field>
<lucene:field name="date" type="unindexed" >just stored
information (not indexed)</lucene:field>
</lucene:document>
<lucene:document>
<lucene:field name="author" type="keyword" boost="2">Mr
Author</lucene:field>
(boost the field for the search (see Lucene documentation))
<lucene:field name="langage" type="keyword">french</lucene:field>
</lucene:document>
< /lucene:index>
<lucene:delete directory="d:/indexbase" >
<lucene:document field="author" value="Mr Author"/> (delete
all documents with the field author ="Mr Author")
<lucene:document
field="id" value="1E3RFE"/>
< /lucene:delete>
Example of Output Source
<page xmlns:lucene="http://apache.org/cocoon/lucene/1.0">
<
lucene:index nbdocuments="2"/>
<
lucene:delete nbdocuments="1"/>
</page>
null
if the
* component is currently not cacheable.
*/
public SourceValidity getValidity() {
return NOPValidity.SHARED_INSTANCE;
}
public void startDocument() throws SAXException {
super.startDocument();
}
public void endDocument() throws SAXException {
super.endDocument();
}
/**
* Begin the scope of a prefix-URI Namespace mapping.
*
* @param prefix The Namespace prefix being declared.
* @param uri The Namespace URI the prefix is mapped to.
*/
public void startPrefixMapping(String prefix, String uri) throws SAXException {
if (processing == 0) {
super.startPrefixMapping(prefix, uri);
}
}
/**
* End the scope of a prefix-URI mapping.
*
* @param prefix The prefix that was being mapping.
*/
public void endPrefixMapping(String prefix) throws SAXException {
if (processing == 0) {
super.endPrefixMapping(prefix);
}
}
public void startElement(String namespaceURI, String localName, String qName,
Attributes atts) throws SAXException {
//System.out.println("START processing: "+processing+" "+localName);
if (processing == 0) {
if (LUCENE_URI.equals(namespaceURI)) {
// INDEX ACTION
if (LUCENE_QUERY_ELEMENT.equals(localName)) {
action = ADD_ACTION;
// create base parameter
String sCreate = atts.getValue(LUCENE_QUERY_CREATE_ATTRIBUTE);
boolean bCreate = sCreate != null &&
(sCreate.equalsIgnoreCase("yes") ||
sCreate.equalsIgnoreCase("true"));
// analyzer parameter
String analyzerClassname =
atts.getValue(LUCENE_QUERY_ANALYZER_ATTRIBUTE);
if (analyzerClassname == null) {
analyzerClassname = this.ANALYZER_CLASSNAME_DEFAULT;
}
Analyzer analyzer = LuceneCocoonHelper.getAnalyzer(analyzerClassname);
// mergeFactor parameter
String sMergeFactor =
atts.getValue(LUCENE_QUERY_MERGE_FACTOR_ATTRIBUTE);
int mergeFactor = this.mergeFactor;
if (sMergeFactor != null) {
mergeFactor = Integer.parseInt(sMergeFactor);
}
// directory parameter
String directoryName =
atts.getValue(LUCENE_QUERY_DIRECTORY_ATTRIBUTE);
if (directoryName == null) {
directoryName = this.directory;
//System.out.println("QUERY Create=" + bCreate + ", Directory=" + directoryName + ", Analyzer=" + analyzerClassname);
}
try {
Directory directory = LuceneCocoonHelper.getDirectory(new File(
workDir, directoryName), bCreate);
writer = new IndexWriter(directory, analyzer, bCreate);
writer.mergeFactor = mergeFactor;
}
catch (IOException e) {
throw new SAXException(e);
}
processing = 1;
}
// DELETE ACTION
else if (LUCENE_DELETE_ELEMENT.equals(localName)) {
action = DELETE_ACTION;
// directory parameter
String directoryName =
atts.getValue(LUCENE_QUERY_DIRECTORY_ATTRIBUTE);
if (directoryName == null) {
directoryName = this.directory;
}
try {
Directory directory = LuceneCocoonHelper.getDirectory(
new File(workDir, directoryName), false);
reader = LuceneCocoonHelper.getIndexReader(directory);
//System.out.println("DELETE Directory=" + directoryName);
}
catch (IOException e) {
throw new SAXException(e);
}
processing = 1;
}
}
else {
super.startElement(namespaceURI, localName, qName, atts);
}
}
else if (processing == 1) {
if (LUCENE_URI.equals(namespaceURI) &&
LUCENE_DOCUMENT_ELEMENT.equals(localName)) {
if (action == ADD_ACTION) {
this.bodyDocument = new Document();
}
if (action == DELETE_ACTION) {
this.term = new Term(atts.getValue(LUCENE_DOCUMENT_FIELD_ATTRIBUTE),
atts.getValue(LUCENE_DOCUMENT_VALUE_ATTRIBUTE));
}
processing = 2;
}
else {
throw new SAXException(
"