1   /**
2    * Distribution License:
3    * JSword is free software; you can redistribute it and/or modify it under
4    * the terms of the GNU Lesser General Public License, version 2.1 as published by
5    * the Free Software Foundation. This program is distributed in the hope
6    * that it will be useful, but WITHOUT ANY WARRANTY; without even the
7    * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
8    * See the GNU Lesser General Public License for more details.
9    *
10   * The License is available on the internet at:
11   *       http://www.gnu.org/copyleft/lgpl.html
12   * or by writing to:
13   *      Free Software Foundation, Inc.
14   *      59 Temple Place - Suite 330
15   *      Boston, MA 02111-1307, USA
16   *
17   * Copyright: 2005
18   *     The copyright to this program is held by it's authors.
19   *
20   * ID: $Id: PlainTextFilter.java 2110 2011-03-08 13:55:32Z dmsmith $
21   */
22  package org.crosswire.jsword.book.filter.plaintext;
23  
24  import java.util.List;
25  
26  import org.crosswire.common.util.StringUtil;
27  import org.crosswire.jsword.book.Book;
28  import org.crosswire.jsword.book.OSISUtil;
29  import org.crosswire.jsword.book.filter.Filter;
30  import org.crosswire.jsword.passage.Key;
31  import org.jdom.Content;
32  import org.jdom.Element;
33  
34  /**
35   * Filter to convert plain text to OSIS format. Plain text is nothing more than
36   * lines without markup. Unfortunately, it often uses whitespace for markup. We
37   * will use OSIS lb to mark lines.
38   * 
39   * @see gnu.lgpl.License for license details.<br>
40   *      The copyright to this program is held by it's authors.
41   * @author Joe Walker [joe at eireneh dot com]
42   */
43  public class PlainTextFilter implements Filter {
44      /* (non-Javadoc)
45       * @see org.crosswire.jsword.book.filter.Filter#toOSIS(org.crosswire.jsword.book.Book, org.crosswire.jsword.passage.Key, java.lang.String)
46       */
47      public List<Content> toOSIS(Book book, Key key, String plain) {
48          OSISUtil.OSISFactory factory = OSISUtil.factory();
49          Element ele = factory.createDiv();
50  
51          String[] lines = StringUtil.splitAll(plain, '\n');
52          int lastIndex = lines.length - 1;
53          for (int i = 0; i < lastIndex; i++) {
54              // TODO(DMS): Preserve whitespace, in a smart manner.
55              ele.addContent(lines[i]);
56              ele.addContent(factory.createLB());
57          }
58          // Don't add a line break after the last line.
59          if (lastIndex >= 0) {
60              ele.addContent(lines[lastIndex]);
61          }
62  
63          return ele.removeContent();
64      }
65  
66      @Override
67      public PlainTextFilter clone() {
68          PlainTextFilter clone = null;
69          try {
70              clone = (PlainTextFilter) super.clone();
71          } catch (CloneNotSupportedException e) {
72              assert false : e;
73          }
74          return clone;
75      }
76  }
77