Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
PlainTextFilter |
|
| 2.5;2.5 |
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 or later | |
5 | * as published by the Free Software Foundation. This program is distributed | |
6 | * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even | |
7 | * the 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 | * © CrossWire Bible Society, 2005 - 2016 | |
18 | * | |
19 | */ | |
20 | package org.crosswire.jsword.book.filter.plaintext; | |
21 | ||
22 | import java.util.List; | |
23 | ||
24 | import org.crosswire.common.util.StringUtil; | |
25 | import org.crosswire.jsword.book.Book; | |
26 | import org.crosswire.jsword.book.OSISUtil; | |
27 | import org.crosswire.jsword.book.filter.SourceFilter; | |
28 | import org.crosswire.jsword.passage.Key; | |
29 | import org.jdom2.Content; | |
30 | import org.jdom2.Element; | |
31 | ||
32 | /** | |
33 | * Filter to convert plain text to OSIS format. Plain text is nothing more than | |
34 | * lines without markup. Unfortunately, it often uses whitespace for markup. We | |
35 | * will use OSIS lb to mark lines. | |
36 | * | |
37 | * @see gnu.lgpl.License The GNU Lesser General Public License for details. | |
38 | * @author Joe Walker | |
39 | */ | |
40 | 0 | public class PlainTextFilter implements SourceFilter { |
41 | /* (non-Javadoc) | |
42 | * @see org.crosswire.jsword.book.filter.Filter#toOSIS(org.crosswire.jsword.book.Book, org.crosswire.jsword.passage.Key, java.lang.String) | |
43 | */ | |
44 | public List<Content> toOSIS(Book book, Key key, String plain) { | |
45 | 0 | OSISUtil.OSISFactory factory = OSISUtil.factory(); |
46 | 0 | Element ele = factory.createDiv(); |
47 | ||
48 | 0 | String[] lines = StringUtil.splitAll(plain, '\n'); |
49 | 0 | int lastIndex = lines.length - 1; |
50 | 0 | for (int i = 0; i < lastIndex; i++) { |
51 | // TODO(DMS): Preserve whitespace, in a smart manner. | |
52 | 0 | ele.addContent(lines[i]); |
53 | 0 | ele.addContent(factory.createLB()); |
54 | } | |
55 | // Don't add a line break after the last line. | |
56 | 0 | if (lastIndex >= 0) { |
57 | 0 | ele.addContent(lines[lastIndex]); |
58 | } | |
59 | ||
60 | 0 | return ele.removeContent(); |
61 | } | |
62 | ||
63 | @Override | |
64 | public PlainTextFilter clone() { | |
65 | 0 | PlainTextFilter clone = null; |
66 | try { | |
67 | 0 | clone = (PlainTextFilter) super.clone(); |
68 | 0 | } catch (CloneNotSupportedException e) { |
69 | 0 | assert false : e; |
70 | 0 | } |
71 | 0 | return clone; |
72 | } | |
73 | } |