001: /*
002: * Project: LIUS Package: de.teamskill.util.parser
003: *
004: * Copyright (c) 2004 by Jens Fendler <jf@teamskill.de>
005: *
006: * This program is a free software; you can redistribute it and/or modify it
007: * under the terms of the GNU General Public License as published by the Free
008: * Software Foundation; either version 2 of the License, or (at your option) any
009: * later version. This program is distributed in the hope that it will be
010: * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
012: * Public License for more details. You should have received a copy of the GNU
013: * General Public License along with this program; if not, write to the Free
014: * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
015: * USA
016: */
017:
018: package de.teamskill.util.parser;
019:
020: import java.io.BufferedReader;
021: import java.io.File;
022: import java.io.FileInputStream;
023: import java.io.IOException;
024: import java.io.InputStream;
025: import java.io.InputStreamReader;
026: import java.util.List;
027: import java.util.Vector;
028:
029: import de.teamskill.util.VCard;
030:
031: /**
032: * Class: VCardParser <br>
033: *
034: * Changelog:
035: * <ul>
036: * <li>02.06.2005: Initial implementation (jf)</li>
037: * <li>03.06.2005: Fixed card detection code (no reg-ex anymore) (jf)</li>
038: * </ul>
039: *
040: * @author <a href="mailto:jf@teamskill.de">Jens Fendler </a>
041: */
042: public class VCardParser {
043:
044: private static final String lineSep = System
045: .getProperty("line.separator");
046:
047: private static final String BEGIN_PATTERN = "BEGIN:VCARD";
048:
049: private static final String END_PATTERN = "END:VCARD";
050:
051: private InputStream stream = null;
052:
053: private List cards = new Vector();
054:
055: public VCardParser(InputStream stream) throws IOException {
056: // setup the input stream and parse it into the cards List
057: this .stream = stream;
058: parse(stream);
059: }
060:
061: public VCardParser(File file) throws IOException {
062: this (new FileInputStream(file));
063: }
064:
065: public VCardParser(String filename) throws IOException {
066: this (new File(filename));
067: }
068:
069: /**
070: * parse
071: *
072: * @param stream2
073: * @throws IOException
074: */
075: private void parse(InputStream stream) throws IOException {
076: BufferedReader br = new BufferedReader(new InputStreamReader(
077: stream));
078: StringBuffer sb = new StringBuffer();
079: String line = null;
080: while ((line = br.readLine()) != null) {
081: if (line.startsWith(BEGIN_PATTERN)) {
082: StringBuffer cardBuffer = new StringBuffer();
083: // new VCard begins
084: cardBuffer.append(line).append(lineSep);
085: // finish reading of card content..
086: String cardString = finishCard(br, cardBuffer);
087: // add the new card to our container as a new VCard object
088: cards.add(new VCard(cardString));
089: }
090: }
091: }
092:
093: /**
094: * finishCard
095: *
096: * @param br
097: * @param cardBuffer
098: * @return
099: * @throws IOException
100: */
101: private String finishCard(BufferedReader br, StringBuffer cardBuffer)
102: throws IOException {
103: String line = null;
104: // simply loop until EOF is reached. if a valid card end marker is found
105: // earlier we'll
106: // return directly from within the loop..
107: while ((line = br.readLine()) != null) {
108: // adjust for (invalid) short lines (e.g. containing only CR/LF)
109: if (line.length() > 2)
110: cardBuffer.append(line).append(lineSep);
111:
112: if (line.startsWith(END_PATTERN)) {
113: return cardBuffer.toString().trim();
114: }
115: }
116: // if a card is not correctly ending, fix it and return what we have so
117: // far
118: cardBuffer.append(END_PATTERN);
119: return cardBuffer.toString();
120: }
121:
122: public List getCards() {
123: return cards;
124: }
125:
126: public void setCards(List cards) {
127: this .cards = cards;
128: }
129:
130: public InputStream getStream() {
131: return stream;
132: }
133:
134: public void setStream(InputStream stream) {
135: this.stream = stream;
136: }
137: }
|