org.crosswire.jsword.passage
Class PassageKeyFactory

java.lang.Object
  extended by org.crosswire.jsword.passage.PassageKeyFactory
All Implemented Interfaces:
KeyFactory

public final class PassageKeyFactory
extends Object
implements KeyFactory

An implementation of KeyFactory that works for most Bibles that contain all the verses in the Bible.

Author:
Joe Walker [joe at eireneh dot com], DM Smith [dmsmith555 at yahoo dot com]
See Also:
for license details. The copyright to this program is held by it's authors.

Field Summary
private static PassageType defaultType
          The default type
private static KeyFactory keyf
          How we create Passages
private static Passage whole
          The cached whole Bible passage
 
Constructor Summary
private PassageKeyFactory()
          This class implements a Singleton pattern.
 
Method Summary
protected static int binarySize(int max)
          Write to buffer (starting at index) the given number using a set of bytes as required by the max possible value for the number
 Key createEmptyKeyList()
          Fetch an empty Key to which we can add Keys.
protected static int fromBinary(byte[] buffer, int[] index, int max)
          Read and return an int from the buffer (starting at index[0]) using a set of bytes as required by the max possible value for the number, and incrementing index[0] by that number of bytes.
static Passage fromBinaryRepresentation(byte[] buffer)
          Write out the object to the given ObjectOutputStream
static int getDefaultPassage()
          Get the default reference type.
 Key getGlobalKeyList()
          Get a complete list of index entries.
 Key getKey(String name)
          Someone has typed in a reference to find, but we need a Key to actually look it up.
static Passage getReadOnlyPassage(Passage ref, boolean ignore)
          Get a new Passage based on another Passage that synchronizes all access to its members.
static Passage getSynchronizedPassage(Passage ref)
          Get a new Passage based on another Passage that synchronizes all access to its members.
 Key getValidKey(String name)
          Get a Key for the name, if possible.
static KeyFactory instance()
           
private  String mungOsisRef(String name)
          Replace spaces with semi-colons, because the parser expects them.
private  String normalize(String name)
          The internals of a Passage require that references are separated with a reference delimiter.
static Passage readPassage(Reader in)
          Read a passage from a given stream
static void setDefaultPassage(int newDefaultType)
          Set the default reference type.
protected static int toBinary(byte[] buffer, int index, int number, int max)
          Write to buffer (starting at index) the given number using a set of bytes as required by the max possible value for the number
static byte[] toBinaryRepresentation(Passage ref)
          Convert us to a binary representation.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

keyf

private static KeyFactory keyf
How we create Passages


whole

private static Passage whole
The cached whole Bible passage


defaultType

private static PassageType defaultType
The default type

Constructor Detail

PassageKeyFactory

private PassageKeyFactory()
This class implements a Singleton pattern. So the ctor is private

Method Detail

instance

public static KeyFactory instance()

createEmptyKeyList

public Key createEmptyKeyList()
Description copied from interface: KeyFactory
Fetch an empty Key to which we can add Keys. Not all implementations of Key are able to hold any type of Key, It isn't reasonable to expect a Key of Bible verses (=Passage) to hold a dictionary Key. So each KeyFactory must be able to create you an empty Key to which you can safely add other Keys it generates.

Specified by:
createEmptyKeyList in interface KeyFactory
Returns:
An empty Key that can hold other Keys from this factory.

getValidKey

public Key getValidKey(String name)
Description copied from interface: KeyFactory
Get a Key for the name, if possible. Otherwise return an empty Key.

Specified by:
getValidKey in interface KeyFactory
Returns:
a valid key.

getKey

public Key getKey(String name)
           throws NoSuchKeyException
Description copied from interface: KeyFactory
Someone has typed in a reference to find, but we need a Key to actually look it up. So we create a Key from the string if such a translation is possible. The returned Key may be a BranchKey if the string represents more than one Key.

Specified by:
getKey in interface KeyFactory
Parameters:
name - The string to translate into a Key
Returns:
The Key corresponding to the input text
Throws:
NoSuchKeyException - If the name can not be parsed.

getGlobalKeyList

public Key getGlobalKeyList()
Description copied from interface: KeyFactory
Get a complete list of index entries. Create a Key that encompasses all of the known valid keys for the given context. For a dictionary this will include all of the entries in the dictionary, for a Bible this will probably include all the verses in the Bible, but a commentary may well miss some out.

Specified by:
getGlobalKeyList in interface KeyFactory
Returns:
A Key that includes all of the known Keys

setDefaultPassage

public static void setDefaultPassage(int newDefaultType)
Set the default reference type. Must be one of:

Parameters:
newDefaultType - The new default type.

getDefaultPassage

public static int getDefaultPassage()
Get the default reference type.

Returns:
default_type The new default type.
See Also:
setDefaultPassage(int)

getSynchronizedPassage

public static Passage getSynchronizedPassage(Passage ref)
Get a new Passage based on another Passage that synchronizes all access to its members.

Parameters:
ref - The passage to synchronize
Returns:
A new synchronized passage that proxies requests to the original

getReadOnlyPassage

public static Passage getReadOnlyPassage(Passage ref,
                                         boolean ignore)
Get a new Passage based on another Passage that synchronizes all access to its members.

Parameters:
ref - The passage to synchronize
ignore - Do we throw up if someone tries to change us
Returns:
A new synchronized passage that proxies requests to the original

toBinaryRepresentation

public static byte[] toBinaryRepresentation(Passage ref)
Convert us to a binary representation. There are some distinctly endianist happenings here, but that is OK because we are reading the stuff we write here just below.

Parameters:
ref - The Passage to convert
Returns:
a byte array

fromBinaryRepresentation

public static Passage fromBinaryRepresentation(byte[] buffer)
                                        throws NoSuchVerseException
Write out the object to the given ObjectOutputStream

Parameters:
buffer - The stream to read our state from
Returns:
The converted Passage
Throws:
NoSuchVerseException - If the buffer is invalid

readPassage

public static Passage readPassage(Reader in)
                           throws IOException,
                                  NoSuchVerseException
Read a passage from a given stream

Parameters:
in - The stream to read from
Returns:
a newly built Passage
Throws:
IOException - If there was troule reading the stream
NoSuchVerseException - if the data was not a valid passage

binarySize

protected static int binarySize(int max)
Write to buffer (starting at index) the given number using a set of bytes as required by the max possible value for the number

Parameters:
max - The number to write
Returns:
The number of bytes needed

toBinary

protected static int toBinary(byte[] buffer,
                              int index,
                              int number,
                              int max)
Write to buffer (starting at index) the given number using a set of bytes as required by the max possible value for the number

Parameters:
buffer - Where to write to
index - The offset to start at
number - The number to write
max - The max size
Returns:
The number of bytes written

fromBinary

protected static int fromBinary(byte[] buffer,
                                int[] index,
                                int max)
Read and return an int from the buffer (starting at index[0]) using a set of bytes as required by the max possible value for the number, and incrementing index[0] by that number of bytes.

Parameters:
buffer - The buffer to read from
index - The offset to start at
max - The max nuber of bytes to read
Returns:
The converted number

mungOsisRef

private String mungOsisRef(String name)
Replace spaces with semi-colons, because the parser expects them.

Parameters:
name -
Returns:
the munged value

normalize

private String normalize(String name)
The internals of a Passage require that references are separated with a reference delimiter. However, people and other systems may not be so stringent. So we want to allow for "Ge 1:26 3:22 11:7 20:13 31:7, 53 35:7" (which is from Clarke) This should become "Ge 1:26, 3:22, 11:7, 20:13, 31:7, 53, 35:7" Basically, the rule of thumb is that if two numbers are found separated by whitespace then add a comma between them. One note $, and ff are taken to be numbers. But it is complicated by Book names that are like 1 Cor And by verse references like Gen 1.2 Gen.1.2 Gen 1 2 which are all equivalent. So we use a counter when we see a number, if the counter reaches 2 and then we see a name or a number we emit a reference delimiter.

Parameters:
name -
Returns:
the normalized value

Copyright ยจ 2003-2007