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.dao.jdbc;
020:
021: import java.io.StringWriter;
022: import java.io.Writer;
023: import java.util.ArrayList;
024: import java.util.Iterator;
025: import java.util.List;
026: import java.util.Map;
027:
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030: import org.springframework.jdbc.core.support.JdbcDaoSupport;
031: import org.xml.sax.SAXException;
032: import org.xml.sax.helpers.AttributesImpl;
033:
034: import com.sun.org.apache.xml.internal.serialize.OutputFormat;
035: import com.sun.org.apache.xml.internal.serialize.XMLSerializer;
036:
037: import piscator.dao.SearchDao;
038:
039: public class SearchDaoImpl extends JdbcDaoSupport implements SearchDao {
040:
041: private static Log LOG = LogFactory.getLog(SearchDaoImpl.class);
042:
043: /* (non-Javadoc)
044: * @see piscator.dao.SearchDao#getDocIds(java.lang.String)
045: */
046: public List getDocIds(String sql) {
047: List docIds = new ArrayList();
048: Iterator rowIter = getJdbcTemplate().queryForList(sql)
049: .iterator();
050: while (rowIter.hasNext()) {
051: Map map = (Map) rowIter.next();
052: if (map.containsKey("DOCID")) {
053: docIds.add(map.get("DOCID"));
054: }
055: }
056: return docIds;
057: }
058:
059: /* (non-Javadoc)
060: * @see piscator.dao.SearchDao#query(java.lang.String)
061: */
062: public String getXML(String sql) {
063: OutputFormat outputFormat = new OutputFormat();
064: outputFormat.setEncoding("UTF-8");
065: outputFormat.setIndenting(true);
066: outputFormat.setOmitXMLDeclaration(false);
067: Writer writer = new StringWriter();
068: XMLSerializer serializer = new XMLSerializer(writer,
069: outputFormat);
070: try {
071: serializer.startDocument();
072: AttributesImpl queryAttrs = new AttributesImpl();
073: queryAttrs.addAttribute("", "query", "query", "CDATA", sql);
074: serializer.startElement("", "result", "result", queryAttrs);
075: Iterator rowIter = getJdbcTemplate().queryForList(sql)
076: .iterator();
077: while (rowIter.hasNext()) {
078: AttributesImpl attrs = new AttributesImpl();
079: Map map = (Map) rowIter.next();
080: Iterator columnIter = map.keySet().iterator();
081: while (columnIter.hasNext()) {
082: String column = (String) columnIter.next();
083: Object value = map.get(column);
084: if (value != null) {
085: String name = column.toLowerCase();
086: attrs.addAttribute("", name, name, "CDATA",
087: value.toString());
088: }
089: }
090: serializer.startElement("", "row", "row", attrs);
091: serializer.endElement("", "row", null);
092: }
093: serializer.endElement("", "result", "result");
094: serializer.endDocument();
095: } catch (SAXException e) {
096: LOG.error(e.getMessage(), e);
097: }
098: return writer.toString();
099: }
100:
101: /* (non-Javadoc)
102: * @see piscator.dao.SearchDao#getDocuments(java.lang.String)
103: */
104: public String getDocuments(String sql) {
105: StringBuffer documents = new StringBuffer();
106: Iterator rowIter = getJdbcTemplate().queryForList(sql)
107: .iterator();
108: while (rowIter.hasNext()) {
109: Map map = (Map) rowIter.next();
110: if (map.containsKey("XML")) {
111: String document = (String) map.get("XML");
112: if (document != null) {
113: documents.append(document);
114: }
115: }
116: }
117: return documents.toString();
118: }
119:
120: }
|