1 17 18 19 20 package org.apache.lenya.lucene.index; 21 22 import java.io.File ; 23 import java.io.FileFilter ; 24 import java.io.StringWriter ; 25 import java.io.Writer ; 26 import java.net.URI ; 27 import java.net.URL ; 28 29 import javax.xml.transform.Transformer ; 30 import javax.xml.transform.TransformerFactory ; 31 import javax.xml.transform.dom.DOMSource ; 32 import javax.xml.transform.stream.StreamResult ; 33 import javax.xml.transform.stream.StreamSource ; 34 35 import org.apache.cocoon.util.NetUtils; 36 import org.apache.lenya.xml.DocumentHelper; 37 import org.apache.log4j.Category; 38 import org.w3c.dom.Document ; 39 import org.w3c.dom.Element ; 40 import org.w3c.dom.Node ; 41 import org.w3c.dom.NodeList ; 42 43 public class ConfigurableIndexer extends AbstractIndexer { 44 Category log = Category.getInstance(ConfigurableIndexer.class); 45 46 55 public DocumentCreator createDocumentCreator(Element indexer, String configFileName) throws Exception { 56 log.debug(".createDocumentCreatort(): Element name: " + indexer.getNodeName()); 57 58 String configurationFileName = new File (configFileName).getParent() + File.separator + getLuceneDocConfigFileName(indexer); 60 File configurationFile = new File (configurationFileName); 61 String stylesheet = getStylesheet(configurationFile); 62 return new ConfigurableDocumentCreator(stylesheet); 63 } 64 65 public static final String CONFIGURATION_CREATOR_STYLESHEET = "org/apache/lenya/lucene/index/configuration2xslt.xsl"; 66 67 70 protected String getStylesheet(File configurationFile) throws Exception { 71 log.debug(".getStylesheet(): Configuration file: " + configurationFile.getAbsolutePath()); 72 73 URL configurationCreatorURL = ConfigurableIndexer.class.getClassLoader().getResource(CONFIGURATION_CREATOR_STYLESHEET); 74 File configurationStylesheetFile = new File (new URI (NetUtils.encodePath(configurationCreatorURL.toString()))); 75 Document configurationDocument = DocumentHelper.readDocument(configurationFile); 76 77 TransformerFactory tFactory = TransformerFactory.newInstance(); 78 Transformer configurationTransformer = tFactory.newTransformer(new StreamSource (configurationStylesheetFile)); 79 80 DOMSource source = new DOMSource (configurationDocument); 81 Writer stylesheetWriter = new StringWriter (); 82 configurationTransformer.transform(source, new StreamResult (stylesheetWriter)); 83 84 log.debug(".getStylesheet(): Meta Stylesheet: " + stylesheetWriter.toString()); 86 87 return stylesheetWriter.toString(); 88 } 89 90 93 public FileFilter getFilter(Element indexer, String configFileName) { 94 if (extensionsExists(indexer)) { 95 String [] indexableExtensions = new String [1]; 96 indexableExtensions[0] = getExtensions(indexer); 97 return new AbstractIndexer.DefaultIndexFilter(indexableExtensions); 98 } else if (filterExists(indexer)) { 99 return getFilterFromConfiguration(indexer); 100 } 101 102 return new AbstractIndexer.DefaultIndexFilter(); 103 } 104 105 108 private String getLuceneDocConfigFileName(Element indexer) { 109 String luceneDocConfigFileName = null; 110 111 NodeList nl = indexer.getChildNodes(); 112 for (int i = 0; i < nl.getLength(); i++) { 113 Node node = nl.item(i); 114 if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equals("configuration")) { 115 log.debug(".getLuceneDocConfigFileName(): Node configuration exists!"); 116 luceneDocConfigFileName = ((Element )node).getAttribute("src"); 117 } 118 } 119 if (luceneDocConfigFileName == null) { 120 log.error(".getLuceneDocConfigFileName(): ERROR: Lucene Document Configuration is not specified (indexer/configuration/@src)"); 121 } 122 log.debug(".getLuceneDocConfigFileName(): Lucene Document Configuration: " + luceneDocConfigFileName); 123 return luceneDocConfigFileName; 124 } 125 126 129 private String getExtensions(Element indexer) { 130 String extensions = null; 131 132 NodeList nl = indexer.getChildNodes(); 133 for (int i = 0; i < nl.getLength(); i++) { 134 Node node = nl.item(i); 135 if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equals("extensions")) { 136 log.debug("Node extensions exists!"); 137 extensions = ((Element )node).getAttribute("src"); 138 } 139 } 140 if (extensions == null) { 141 log.error("Extensions have not been specified (indexer/extensions/@src)"); 142 } 143 log.debug("Extensions: " + extensions); 144 return extensions; 145 } 146 147 150 private FileFilter getFilterFromConfiguration(Element indexer) { 151 String className = null; 152 153 NodeList nl = indexer.getChildNodes(); 154 for (int i = 0; i < nl.getLength(); i++) { 155 Node node = nl.item(i); 156 if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equals("filter")) { 157 log.debug("Node filter exists!"); 158 className = ((Element )node).getAttribute("class"); 159 } 160 } 161 if (className == null) { 162 log.error("Class name has not been specified (indexer/filter/@class)"); 163 return null; 164 } 165 log.debug("Class name: " + className); 166 try { 167 return (FileFilter )Class.forName(className).newInstance(); 168 } catch(Exception e) { 169 log.error("" + e); 170 } 171 return null; 172 } 173 174 177 private boolean extensionsExists(Element indexer) { 178 NodeList nl = indexer.getChildNodes(); 179 for (int i = 0; i < nl.getLength(); i++) { 180 Node node = nl.item(i); 181 if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equals("extensions")) { 182 log.debug("Node <extensions SRC=\"...\"/> exist"); 183 return true; 184 } 185 } 186 return false; 187 } 188 189 192 private boolean filterExists(Element indexer) { 193 NodeList nl = indexer.getChildNodes(); 194 for (int i = 0; i < nl.getLength(); i++) { 195 Node node = nl.item(i); 196 if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equals("filter")) { 197 log.debug("Node filter exists!"); 198 return true; 199 } 200 } 201 return false; 202 } 203 } 204 | Popular Tags |