001: /*
002: * Piscator: a small SQL/XML search engine
003: * Copyright (C) 2007 Luk Morbee
004: *
005: * This program is free software; you can redistribute it and/or modify
006: * it under the terms of the GNU General Public License as published by
007: * the Free Software Foundation; either version 2 of the License, or
008: * (at your option) any later version.
009: *
010: * This program is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: * GNU General Public License for more details.
014: *
015: * You should have received a copy of the GNU General Public License
016: * along with this program; if not, write to the Free Software
017: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
018: */
019: package piscator.service;
020:
021: import java.io.ByteArrayInputStream;
022: import java.io.IOException;
023: import java.util.Iterator;
024: import java.util.LinkedHashMap;
025: import java.util.List;
026: import java.util.Map;
027:
028: import javax.xml.parsers.DocumentBuilder;
029: import javax.xml.parsers.DocumentBuilderFactory;
030: import javax.xml.parsers.ParserConfigurationException;
031: import javax.xml.transform.TransformerException;
032:
033: import org.apache.commons.logging.Log;
034: import org.apache.commons.logging.LogFactory;
035: import org.w3c.dom.Document;
036: import org.w3c.dom.Element;
037: import org.w3c.dom.Node;
038: import org.w3c.dom.NodeList;
039: import org.xml.sax.SAXException;
040:
041: import piscator.dao.DocumentTableDao;
042: import piscator.dao.SideTableDao;
043: import piscator.util.DomUtils;
044:
045: import com.sun.org.apache.xpath.internal.XPathAPI;
046:
047: /**
048: * The indexer parses each stored document and extracts (+ stores) its search fields in the side tables.
049: */
050: public class Indexer {
051:
052: private static Log LOG = LogFactory.getLog(Indexer.class);
053:
054: private DocumentTableDao documentTableDao = null;
055: private SideTableDao sideTableDao = null;
056:
057: public DocumentTableDao getDocumentTableDao() {
058: return documentTableDao;
059: }
060:
061: public void setDocumentTableDao(DocumentTableDao documentTableDao) {
062: this .documentTableDao = documentTableDao;
063: }
064:
065: public SideTableDao getSideTableDao() {
066: return sideTableDao;
067: }
068:
069: public void setSideTableDao(SideTableDao sideTableDao) {
070: this .sideTableDao = sideTableDao;
071: }
072:
073: /**
074: * Indexes the stored documents according to side table configuration settings.
075: */
076: public void index(Element config)
077: throws ParserConfigurationException, SAXException,
078: IOException, TransformerException {
079: if (config != null) {
080: sideTableDao.create(config);
081: String documentTable = DomUtils.getValue(config,
082: "./document-table/attribute::name");
083: List docids = documentTableDao.getDocIds(documentTable);
084: Iterator iter = docids.iterator();
085: while (iter.hasNext()) {
086: String docId = (String) iter.next();
087: String xml = documentTableDao.getDocument(
088: documentTable, docId);
089: DocumentBuilder documentBuilder = DocumentBuilderFactory
090: .newInstance().newDocumentBuilder();
091: ByteArrayInputStream in = new ByteArrayInputStream(xml
092: .getBytes("UTF-8"));
093: Document document = documentBuilder.parse(in);
094: index(docId, document, config);
095: in.close();
096: }
097: }
098: }
099:
100: /**
101: * Indexes the parsed document according to side table configuration settings.
102: */
103: private void index(String docId, Document document, Element config)
104: throws TransformerException {
105: LOG.info("Indexing document " + docId + " ...");
106: NodeList sideTableNodes = config
107: .getElementsByTagName("side-table");
108: for (int i = 0; i < sideTableNodes.getLength(); i++) {
109: Map map = new LinkedHashMap();
110: map.put("doc_id", docId);
111: Element tableElement = (Element) sideTableNodes.item(i);
112: String tableName = tableElement.getAttribute("name");
113: String tableXPath = tableElement.getAttribute("path");
114: NodeList columnNodes = tableElement
115: .getElementsByTagName("column");
116: NodeList nodeList = XPathAPI.selectNodeList(document,
117: tableXPath);
118: for (int j = 0; j < nodeList.getLength(); j++) {
119: Node node = nodeList.item(j);
120: for (int k = 0; k < columnNodes.getLength(); k++) {
121: Element columnElement = (Element) columnNodes
122: .item(k);
123: String columnName = columnElement
124: .getAttribute("name");
125: String columnXPath = columnElement
126: .getAttribute("path");
127: process(node, columnXPath, map, columnName);
128: }
129: }
130: sideTableDao.insert(tableName, map);
131: }
132: }
133:
134: private void process(Node node, String xpath, Map map,
135: String searchField) throws TransformerException {
136: String value = DomUtils.getValue(node, xpath);
137: if (value != null) {
138: map.put(searchField, value);
139: }
140: }
141:
142: }
|