001: /*
002: * Project: LIUS
003: * Package: de.teamskill.lius.index.application
004: *
005: * Copyright (c) 2004 by Jens Fendler <jf@teamskill.de>
006: *
007: * This program is a free software; you can redistribute it and/or modify it
008: * under the terms of the GNU General Public License as published by the Free
009: * Software Foundation; either version 2 of the License, or (at your option) any
010: * later version. This program is distributed in the hope that it will be
011: * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
013: * Public License for more details. You should have received a copy of the GNU
014: * General Public License along with this program; if not, write to the Free
015: * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
016: * USA
017: */
018:
019: package de.teamskill.lius.index.application;
020:
021: import java.io.IOException;
022: import java.util.ArrayList;
023: import java.util.Collection;
024: import java.util.Iterator;
025: import java.util.List;
026:
027: import org.apache.lucene.document.Document;
028:
029: import ca.ulaval.bibl.lius.config.LiusConfig;
030: import ca.ulaval.bibl.lius.config.LiusField;
031: import ca.ulaval.bibl.lius.config.LiusProxyField;
032: import ca.ulaval.bibl.lius.config.LiusValueProxyField;
033: import ca.ulaval.bibl.lius.index.Indexer;
034: import de.teamskill.util.VCard;
035: import de.teamskill.util.parser.VCardParser;
036:
037: /**
038: * Class: VCardIndexer <br>
039: *
040: * This Indexer reads .vcf and .vcard files as exported by various email and PIM
041: * applications (e.g. Mozilla Mail, Evolution, KMail). Fields which can be
042: * indexed are:
043: *
044: * <ul>
045: * <li><code>name</code>: The contact's full name</li>
046: * <li><code>title</code>: Title (e.g. 'Dr.')</li>
047: * <li><code>nickname</code>: nickname</li>
048: * <li><code>birthday</code>: user notes</li>
049: * <li><code>email</code>: email addresses</li>
050: * <li><code>phone</code>: all available phone numbers</li>
051: * <li><code>homephone</code>: home phone number</li>
052: * <li><code>workphone</code>: work phone number</li>
053: * <li><code>cellphone</code>: cell phone number</li>
054: * <li><code>categories</code>: categories as used by the creator-application
055: * </li>
056: * <li><code>address</code>: all available addresses</li>
057: * <li><code>homeaddress</code>: the home address</li>
058: * <li><code>workaddress</code>: the work address</li>
059: * <li><code>url</code>: the URL (usually an http address)</li>
060: * <li><code>organization</code>: the organization</li>
061: * </ul>
062: * <br/>
063: *
064: * Changelog:
065: * <ul>
066: * <li>02.06.2005: Initial implementation (jf)</li>
067: * </ul>
068: *
069: * @author <a href="mailto:jf@teamskill.de">Jens Fendler </a>
070: */
071: public class VCardIndexer extends Indexer {
072:
073: /**
074: * @see ca.ulaval.bibl.lius.index.Indexer#createLuceneDocument(java.lang.String,
075: * ca.ulaval.bibl.lius.config.LiusConfig)
076: */
077: public Document createLuceneDocument(String file, LiusConfig lc) {
078: return createLuceneDocument(file, lc.getVCardFields());
079: }
080:
081: /**
082: * @see ca.ulaval.bibl.lius.index.Indexer#getLiusFields(ca.ulaval.bibl.lius.config.LiusConfig)
083: */
084: public Collection getLiusFields(LiusConfig lc) {
085: return lc.getVCardFields();
086: }
087:
088: /**
089: * @see ca.ulaval.bibl.lius.index.Indexer#getPopulatedCollection(java.lang.Object,
090: * java.util.Collection)
091: */
092: public Collection getPopulatedCollection(Object file,
093: Collection liusFields) {
094: Collection c = new ArrayList();
095: try {
096: VCardParser vcp = new VCardParser((String) file);
097: for (Iterator i = liusFields.iterator(); i.hasNext();) {
098: Object next = i.next();
099: if (next instanceof LiusField) {
100: LiusField lf = (LiusField) next;
101: addVCardProxyFields(vcp, lf, c);
102: } else
103: c.add(next);
104: }
105: } catch (IOException e) {
106: // ignore for now..
107: }
108:
109: return c;
110: }
111:
112: private void addVCardProxyFields(VCardParser vcp, LiusField lf,
113: Collection c) {
114: if ("name".equalsIgnoreCase(lf.getGet())) {
115: addElements(vcp, "FN", null, lf, c);
116: } else if ("title".equalsIgnoreCase(lf.getGet())) {
117: addElements(vcp, "TITLE", null, lf, c);
118: } else if ("nickname".equalsIgnoreCase(lf.getGet())) {
119: addElements(vcp, "NICKNAME", null, lf, c);
120: } else if ("birthday".equalsIgnoreCase(lf.getGet())) {
121: addElements(vcp, "BDAY", null, lf, c);
122: } else if ("notes".equalsIgnoreCase(lf.getGet())) {
123: addElements(vcp, "NOTE", null, lf, c);
124: } else if ("email".equalsIgnoreCase(lf.getGet())) {
125: addElements(vcp, "EMAIL", null, lf, c);
126: } else if ("phone".equalsIgnoreCase(lf.getGet())) {
127: addElements(vcp, "TEL", null, lf, c);
128: } else if ("homephone".equalsIgnoreCase(lf.getGet())) {
129: addElements(vcp, "TEL", "HOME", lf, c);
130: } else if ("workphone".equalsIgnoreCase(lf.getGet())) {
131: addElements(vcp, "TEL", "WORK", lf, c);
132: } else if ("cellphone".equalsIgnoreCase(lf.getGet())) {
133: addElements(vcp, "TEL", "CELL", lf, c);
134: } else if ("categories".equalsIgnoreCase(lf.getGet())) {
135: addElements(vcp, "CATEGORIES", null, lf, c);
136: } else if ("address".equalsIgnoreCase(lf.getGet())) {
137: addElements(vcp, "ADR", null, lf, c);
138: } else if ("homeaddress".equalsIgnoreCase(lf.getGet())) {
139: addElements(vcp, "ADR", "HOME", lf, c);
140: } else if ("workaddress".equalsIgnoreCase(lf.getGet())) {
141: addElements(vcp, "ADR", "WORK", lf, c);
142: } else if ("url".equalsIgnoreCase(lf.getGet())) {
143: addElements(vcp, "URL", null, lf, c);
144: } else if ("organization".equalsIgnoreCase(lf.getGet())) {
145: addElements(vcp, "ORG", null, lf, c);
146: }
147: }
148:
149: private void addElements(VCardParser vcp, String key, String type,
150: LiusField realField, Collection c) {
151: List cards = vcp.getCards();
152: for (int i = 0; i < cards.size(); i++) {
153: VCard card = (VCard) cards.get(i);
154: String data = (type == null) ? card.getElement(key) : card
155: .getElement(key, type);
156: if (data != null) {
157: LiusProxyField lpf = new LiusValueProxyField(realField);
158: lpf.setValue(data);
159: c.add(lpf);
160: }
161: }
162: }
163:
164: /**
165: * @see ca.ulaval.bibl.lius.index.Indexer#getPopulatedCollection(java.lang.Object,
166: * java.lang.String)
167: */
168: public Collection getPopulatedCollection(Object file,
169: String liusConfig) {
170: return getPopulatedCollection(file, liusConfig);
171: }
172:
173: /**
174: * @see ca.ulaval.bibl.lius.index.Indexer#getPopulatedCollection(java.lang.Object,
175: * ca.ulaval.bibl.lius.config.LiusConfig)
176: */
177: public Collection getPopulatedCollection(Object file,
178: LiusConfig liusConfig) {
179: return getPopulatedCollection(file, liusConfig.getVCardFields());
180: }
181:
182: }
|