| KeyUtil.java |
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: KeyUtil.java 2223 2012-01-26 21:28:02Z dmsmith $
21 */
22 package org.crosswire.jsword.passage;
23
24 import org.crosswire.common.util.Logger;
25 import org.crosswire.jsword.versification.Versification;
26 import org.crosswire.jsword.versification.system.Versifications;
27
28 /**
29 * .
30 *
31 * @see gnu.lgpl.License for license details.<br>
32 * The copyright to this program is held by it's authors.
33 * @author Joe Walker [joe at eireneh dot com]
34 * @author DM Smith [dmsmith555 at yahoo dot com]
35 */
36 public final class KeyUtil {
37 /**
38 * Prevent instantiation
39 */
40 private KeyUtil() {
41 }
42
43 /**
44 * Walk through a tree visiting the nodes and branches in the tree
45 *
46 * @param key
47 * The node tree to walk through
48 * @param visitor
49 * The visitor to notify whenever a node is found
50 */
51 public static void visit(Key key, KeyVisitor visitor) {
52 for (Key subkey : key) {
53 if (subkey.canHaveChildren()) {
54 visitor.visitBranch(subkey);
55 visit(subkey, visitor);
56 } else {
57 visitor.visitLeaf(subkey);
58 }
59 }
60 }
61
62 /**
63 * Not all keys represent verses, but we ought to be able to get something
64 * close to a verse from anything that does verse like work.
65 */
66 public static Verse getVerse(Key key) {
67 if (key instanceof Verse) {
68 return (Verse) key;
69 }
70
71 if (key instanceof Passage) {
72 Passage ref = getPassage(key);
73 return ref.getVerseAt(0);
74 }
75
76 // AV11N(DMS): Is this right?
77 Versification v11n = Versifications.instance().getDefaultVersification();
78 try {
79 return VerseFactory.fromString(v11n, key.getName());
80 } catch (NoSuchVerseException ex) {
81 log.warn("Key can't be a verse: " + key.getName());
82 return Verse.DEFAULT;
83 }
84 }
85
86 /**
87 * Not all keys represent passages, but we ought to be able to get something
88 * close to a passage from anything that does passage like work. If you pass
89 * a null key into this method, you get a null Passage out.
90 */
91 public static Passage getPassage(Key key) {
92 if (key == null) {
93 return null;
94 }
95
96 if (key instanceof Passage) {
97 return (Passage) key;
98 }
99
100 Key ref = null;
101 // AV11N(DMS): Is this right?
102 Versification v11n = Versifications.instance().getDefaultVersification();
103 try {
104 ref = keyf.getKey(v11n, key.getName());
105 } catch (NoSuchKeyException ex) {
106 log.warn("Key can't be a passage: " + key.getName());
107 ref = keyf.createEmptyKeyList(v11n);
108 }
109 return (Passage) ref;
110 }
111
112 /**
113 * How we create Passages
114 */
115 private static PassageKeyFactory keyf = PassageKeyFactory.instance();
116
117 /**
118 * The log stream
119 */
120 private static final Logger log = Logger.getLogger(KeyUtil.class);
121 }
122