001: /*
002:
003: * LIUS - Lucene Index Update and Search
004: * http://sourceforge.net/projects/lius/
005: *
006: * Copyright (c) 2005, Laval University Library. All rights reserved.
007: *
008: * This library is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU Lesser General Public
010: * License as published by the Free Software Foundation; either
011: * version 2.1 of the License, or (at your option) any later version.
012: *
013: * This library is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * Lesser General Public License for more details.
017: *
018: * You should have received a copy of the GNU Lesser General Public
019: * License along with this library; if not, write to the Free Software
020: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
021: */
022:
023: package ca.ulaval.bibl.lius.index.Excel;
024:
025: import java.io.File;
026: import java.io.IOException;
027: import java.util.ArrayList;
028: import java.util.Collection;
029: import java.util.Iterator;
030:
031: import jxl.Cell;
032: import jxl.Sheet;
033: import jxl.Workbook;
034:
035: import org.apache.log4j.Logger;
036: import org.apache.lucene.document.Document;
037:
038: import ca.ulaval.bibl.lius.Lucene.LuceneActions;
039: import ca.ulaval.bibl.lius.config.LiusConfig;
040: import ca.ulaval.bibl.lius.config.LiusConfigBuilder;
041: import ca.ulaval.bibl.lius.config.LiusField;
042: import ca.ulaval.bibl.lius.index.Indexer;
043:
044: /**
045: *
046: * Classe permettant d'indexer des fichiers Excel.
047: *
048: * <br/><br/>
049: *
050: * Class for indexing Excel documents.
051: *
052: * @author Rida Benjelloun (rida.benjelloun@bibl.ulaval.ca)
053: *
054: */
055:
056: public class ExcelIndexer
057:
058: extends Indexer {
059:
060: static Logger logger = Logger.getRootLogger();
061:
062: public Object parse(Object file) {
063:
064: String content = "";
065:
066: StringBuffer sb = new StringBuffer();
067:
068: try {
069:
070: Workbook workbook = Workbook.getWorkbook(new File(
071: (String) file));
072:
073: Sheet[] sheets = workbook.getSheets();
074:
075: for (int i = 0; i < sheets.length; i++) {
076:
077: Sheet sheet = sheets[i];
078:
079: int nbCol = sheet.getColumns();
080:
081: for (int j = 0; j < nbCol; j++) {
082:
083: Cell[] cells = sheet.getColumn(j);
084:
085: for (int k = 0; k < cells.length; k++) {
086:
087: sb.append(cells[k].getContents() + " ");
088:
089: }
090:
091: }
092:
093: }
094:
095: }
096:
097: catch (IOException e) {
098:
099: logger.error(e.getMessage());
100:
101: }
102:
103: catch (jxl.read.biff.BiffException e) {
104:
105: logger.error(e.getMessage());
106:
107: }
108:
109: content = sb.toString();
110:
111: return content;
112:
113: }
114:
115: /**
116: *
117: * Méthode retournant un objet de type Lucene document à partir du fichier
118: *
119: * à indexer et du fichier de configuration de Lius exprimé sous forme
120: *
121: * d'objet de type LiusConfig.
122: *
123: * <br/><br/>
124: *
125: * Method that returns a Lucene document object from a file to index and
126: *
127: * the Lius Configuration as a LiusConfig object.
128: *
129: */
130:
131: public Document createLuceneDocument(String file, LiusConfig lc) {
132:
133: Document doc = createLuceneDocument(file, lc.getExcelFields());
134:
135: return doc;
136:
137: }
138:
139: /**
140: *
141: * Méthode retournant un objet de type Lucene document à partir du fichier à
142: *
143: * indexer et d'une collection d'objets de type LiusField. Chaque objet
144: *
145: * LiusField contient de l'information sur le nom du champs Lucene, le type,
146: *
147: * etc.
148: *
149: * <br/><br/>
150: *
151: * Method that returns a Lucene object from the configuration file and a
152: * collection
153: *
154: * of LiusField objects. Each LiusField object contains information about
155: * the Lucene
156: *
157: * field, the type, etc.
158: *
159: */
160:
161: public Collection getPopulatedCollection(Object file,
162: Collection liusFields) {
163:
164: LuceneActions la = LuceneActions.getSingletonInstance();
165:
166: Collection coll = new ArrayList();
167:
168: Iterator it = liusFields.iterator();
169:
170: while (it.hasNext()) {
171:
172: Object field = it.next();
173:
174: if (field instanceof LiusField) {
175:
176: LiusField lf = (LiusField) field;
177:
178: if (lf.getGet() != null) {
179:
180: if (lf.getGet().equalsIgnoreCase("content")) {
181:
182: String text = (String) parse(file);
183:
184: lf.setValue(text);
185:
186: coll.add(lf);
187:
188: }
189:
190: }
191:
192: }
193:
194: else {
195:
196: coll.add(field);
197:
198: }
199:
200: }
201:
202: return coll;
203:
204: }
205:
206: /**
207: *
208: * Permet de récupérer les champs de Lius à partir du fichier de
209: * configuration
210: *
211: * pour effectuer l'indexation.
212: *
213: * <br/><br/>
214: *
215: * Gets Lius fiels from the configuration file for indexation.
216: *
217: */
218:
219: public Collection getLiusFields(LiusConfig lc) {
220:
221: return lc.getExcelFields();
222:
223: }
224:
225: public Collection getPopulatedCollection(Object file,
226: String liusConfig) {
227:
228: LiusConfig lc = LiusConfigBuilder.getSingletonInstance()
229: .getLiusConfig(
230:
231: liusConfig);
232:
233: return getPopulatedCollection(file, lc);
234:
235: }
236:
237: public Collection getPopulatedCollection(Object file, LiusConfig lc) {
238:
239: return getPopulatedCollection(file, lc.getExcelFields());
240:
241: }
242:
243: }
|