001: /*
002: * CVS information:
003: * $RCSfile$
004: * $Source$
005: * $Date: 2005-08-18 10:01:39 +0200 (Do, 18 Aug 2005) $
006: * $Author: til132 $
007: * $Revision: 172 $
008: */
009: package net.sf.regain.search.config;
010:
011: import java.io.File;
012: import java.util.ArrayList;
013: import java.util.HashMap;
014: import java.util.Properties;
015:
016: import net.sf.regain.RegainException;
017: import net.sf.regain.XmlToolkit;
018:
019: import org.w3c.dom.Document;
020: import org.w3c.dom.Element;
021: import org.w3c.dom.Node;
022:
023: /**
024: *
025: * @author Tilman Schneider, STZ-IDA an der FH Karlsruhe
026: */
027: public class XmlSearchConfig implements SearchConfig {
028:
029: /** All configured indexes. */
030: private HashMap mIndexHash;
031:
032: /** The names of the default indexes. */
033: private String[] mDefaultIndexNameArr;
034:
035: /**
036: * Creates a new instance of XmlSearchConfig.
037: *
038: * @param xmlFile The XML file to read the config from.
039: * @throws RegainException If reading the config failed.
040: */
041: public XmlSearchConfig(File xmlFile) throws RegainException {
042: Document doc = XmlToolkit.loadXmlDocument(xmlFile);
043: Element config = doc.getDocumentElement();
044:
045: readIndexList(config);
046: }
047:
048: /**
049: * Reads the search indexes from the config.
050: *
051: * @param config The configuration to read from.
052: * @throws RegainException If the configration has errors.
053: */
054: private void readIndexList(Element config) throws RegainException {
055: Node node;
056:
057: Node listNode = XmlToolkit.getChild(config, "indexList", true);
058:
059: // Get the node that holds the default settings for all indexes
060: Node defaultNode = XmlToolkit.getChild(listNode,
061: "defaultSettings");
062:
063: // Get the index nodes
064: mIndexHash = new HashMap();
065: ArrayList defaultIndexNameList = new ArrayList();
066: Node[] nodeArr = XmlToolkit.getChildArr(listNode, "index");
067: for (int indexIdx = 0; indexIdx < nodeArr.length; indexIdx++) {
068: Node indexNode = nodeArr[indexIdx];
069:
070: String indexName = XmlToolkit.getAttribute(indexNode,
071: "name", true);
072: String directory = XmlToolkit.getChildText(indexNode,
073: "dir", true);
074:
075: // Read the openInNewWindowRegex
076: node = XmlToolkit.getCascadedChild(indexNode, defaultNode,
077: "openInNewWindowRegex", true);
078: String openInNewWindowRegex = XmlToolkit
079: .getText(node, true);
080:
081: // Read the useFileToHttpBridge
082: node = XmlToolkit.getCascadedChild(indexNode, defaultNode,
083: "useFileToHttpBridge");
084: boolean useFileToHttpBridge = true;
085: if (node != null) {
086: useFileToHttpBridge = XmlToolkit.getTextAsBoolean(node);
087: }
088:
089: // Read the search field list
090: node = XmlToolkit.getCascadedChild(indexNode, defaultNode,
091: "searchFieldList");
092: String[] searchFieldList = null;
093: if (node != null) {
094: searchFieldList = XmlToolkit.getTextAsWordList(node,
095: true);
096: }
097:
098: // Read the rewrite rules
099: node = XmlToolkit.getCascadedChild(indexNode, defaultNode,
100: "rewriteRules");
101: String[][] rewriteRules = readRewriteRules(node);
102:
103: // Read the SearchAccessController
104: String searchAccessControllerClass = null;
105: String searchAccessControllerJar = null;
106: Properties searchAccessControllerConfig = null;
107: node = XmlToolkit.getCascadedChild(indexNode, defaultNode,
108: "searchAccessController");
109: if (node != null) {
110: Node classNode = XmlToolkit.getChild(node, "class",
111: true);
112: searchAccessControllerClass = XmlToolkit.getText(
113: classNode, true);
114: searchAccessControllerJar = XmlToolkit.getAttribute(
115: classNode, "jar");
116:
117: Node configNode = XmlToolkit.getChild(node, "config");
118: if (configNode != null) {
119: searchAccessControllerConfig = new Properties();
120: Node[] paramNodeArr = XmlToolkit.getChildArr(
121: configNode, "param");
122: for (int i = 0; i < paramNodeArr.length; i++) {
123: String name = XmlToolkit.getAttribute(
124: paramNodeArr[i], "name", true);
125: String value = XmlToolkit.getText(
126: paramNodeArr[i], true);
127: searchAccessControllerConfig.setProperty(name,
128: value);
129: }
130: }
131: }
132:
133: // Create the index config
134: IndexConfig indexConfig = new IndexConfig(indexName,
135: directory, openInNewWindowRegex,
136: useFileToHttpBridge, searchFieldList, rewriteRules,
137: searchAccessControllerClass,
138: searchAccessControllerJar,
139: searchAccessControllerConfig);
140: mIndexHash.put(indexName, indexConfig);
141:
142: // Check whether this index is default
143: boolean isDefault = XmlToolkit.getAttributeAsBoolean(
144: indexNode, "default", false);
145: if (isDefault) {
146: defaultIndexNameList.add(indexName);
147: }
148: }
149:
150: // Store the default indexes into an array
151: mDefaultIndexNameArr = new String[defaultIndexNameList.size()];
152: defaultIndexNameList.toArray(mDefaultIndexNameArr);
153: }
154:
155: /**
156: * Reads the URL rewrite rules from a node
157: *
158: * @param node The node to read from.
159: * @return The rewrite rules. May be null.
160: * @throws RegainException If the configration has errors.
161: */
162: private String[][] readRewriteRules(Node node)
163: throws RegainException {
164: if (node == null) {
165: return null;
166: }
167:
168: Node[] ruleNodeArr = XmlToolkit.getChildArr(node, "rule");
169: String[][] rewriteRules = new String[ruleNodeArr.length][];
170: for (int i = 0; i < ruleNodeArr.length; i++) {
171: String prefix = XmlToolkit.getAttribute(ruleNodeArr[i],
172: "prefix", true);
173: String replacement = XmlToolkit.getAttribute(
174: ruleNodeArr[i], "replacement", true);
175:
176: // Add this rule
177: rewriteRules[i] = new String[] { prefix, replacement };
178: }
179:
180: return rewriteRules;
181: }
182:
183: /**
184: * Gets the configuration for an index.
185: *
186: * @param indexName The name of the index to get the config for.
187: * @return The configuration for the wanted index or <code>null</code> if
188: * there is no such index configured.
189: */
190: public IndexConfig getIndexConfig(String indexName) {
191: return (IndexConfig) mIndexHash.get(indexName);
192: }
193:
194: /**
195: * Gets the names of the default indexes.
196: *
197: * @return The names of the default indexes or an empty array if no default
198: * index was specified.
199: */
200: public String[] getDefaultIndexNameArr() {
201: return mDefaultIndexNameArr;
202: }
203:
204: }
|