Indexer.java :  » Search-Engine » piscator » Piscator » service » Java Open Source

Java Open Source » Search Engine » piscator 
piscator » Piscator » service » Indexer.java
/*
 *  Piscator: a small SQL/XML search engine
 *  Copyright (C) 2007  Luk Morbee
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package piscator.service;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import piscator.dao.DocumentTableDao;
import piscator.dao.SideTableDao;
import piscator.util.DomUtils;

import com.sun.org.apache.xpath.internal.XPathAPI;

/**
 * The indexer parses each stored document and extracts (+ stores) its search fields in the side tables.
 */
public class Indexer {
  
  private static Log LOG = LogFactory.getLog(Indexer.class);
  
  private DocumentTableDao documentTableDao = null;
  private SideTableDao sideTableDao = null;
  
  public DocumentTableDao getDocumentTableDao() {
    return documentTableDao;
  }
  
  public void setDocumentTableDao(DocumentTableDao documentTableDao) {
    this.documentTableDao = documentTableDao;
  }
  
  public SideTableDao getSideTableDao() {
    return sideTableDao;
  }
  
  public void setSideTableDao(SideTableDao sideTableDao) {
    this.sideTableDao = sideTableDao;
  }
  
  /**
   * Indexes the stored documents according to side table configuration settings.
   */
  public void index(Element config) throws ParserConfigurationException, SAXException, IOException, TransformerException {
    if (config != null) {
      sideTableDao.create(config);
      String documentTable = DomUtils.getValue(config, "./document-table/attribute::name");
      List docids = documentTableDao.getDocIds(documentTable);
      Iterator iter = docids.iterator();
      while(iter.hasNext()) {
        String docId = (String)iter.next();
        String xml = documentTableDao.getDocument(documentTable, docId);
        DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        ByteArrayInputStream in = new ByteArrayInputStream(xml.getBytes("UTF-8"));
              Document document = documentBuilder.parse(in);
              index(docId, document, config);
              in.close();
      }
    }
  }

  /**
   * Indexes the parsed document according to side table configuration settings.
   */
  private void index(String docId, Document document, Element config) throws TransformerException {
    LOG.info("Indexing document " + docId + " ...");
    NodeList sideTableNodes = config.getElementsByTagName("side-table");
    for (int i = 0; i < sideTableNodes.getLength(); i++) {
      Map map = new LinkedHashMap();
      map.put("doc_id", docId);
      Element tableElement = (Element)sideTableNodes.item(i);
      String tableName = tableElement.getAttribute("name");
      String tableXPath = tableElement.getAttribute("path");
      NodeList columnNodes = tableElement.getElementsByTagName("column");
      NodeList nodeList = XPathAPI.selectNodeList(document, tableXPath);
      for (int j = 0; j < nodeList.getLength(); j++) {
        Node node = nodeList.item(j);
        for (int k = 0; k < columnNodes.getLength(); k++) {
          Element columnElement = (Element)columnNodes.item(k);
          String columnName = columnElement.getAttribute("name");
          String columnXPath = columnElement.getAttribute("path");
          process(node, columnXPath, map, columnName);
        }
      }
      sideTableDao.insert(tableName, map);
    }
  }
  
  
  private void process(Node node, String xpath, Map map, String searchField) throws TransformerException {
    String value = DomUtils.getValue(node, xpath);
    if (value != null) {
      map.put(searchField, value);
    }
  }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.