1 /*
2  ------------------------------------------------------------------------
3 Copyright (C) 2002-2004 Keith Stribley
4 
5 Distributable under the terms of either the Common Public License or the
6 GNU Lesser General Public License, as specified in the LICENSING.txt file.
7 
8 File: Engine.cp
9 Responsibility: Keith Stribley
10 Last reviewed: Not yet.
11 
12 Description:
13     Implements a JNI interface to the TECkit conversion engine.
14 -------------------------------------------------------------------------*/
15 
16 package org.sil.scripts.teckit;
17 import java.net.URL;
18 import java.io.File;
19 /**
20  *
21  * @author  keith
22  */
23 public class TecKitJni
24 {
25     private static final String LIBRARY = "TecKitJni";
26     private static boolean libraryLoaded = false;
loadLibrary(File libraryPath)27     public static boolean loadLibrary(File libraryPath)
28     {
29         if (!libraryLoaded)
30         {
31             try
32             {
33                 File library = new File
34                     (libraryPath,
35                     System.mapLibraryName(LIBRARY));
36                 System.load(library.getAbsolutePath());
37                 System.out.println("Loaded " + System.mapLibraryName(LIBRARY));
38                 libraryLoaded = true;
39             }
40             catch (UnsatisfiedLinkError e)
41             {
42                 System.out.println(e);
43                 System.out.println(e.getMessage() + " " +
44                     System.mapLibraryName(LIBRARY) + " " +
45                     System.getProperty("java.library.path"));
46             }
47             catch (Exception e)
48             {
49                 e.printStackTrace();
50             }
51         }
52         return libraryLoaded;
53     }
54     /**
55      * Checks whether the library is already loaded.
56      * @return true if the library has been loaded.
57      */
isLibraryLoaded()58     public static boolean isLibraryLoaded() { return libraryLoaded; }
59 
60     /** Creates a new instance of TecKitJni */
TecKitJni()61     public TecKitJni()
62     {
63 
64     }
65     /**
66      * Creates a converter for a given tec file.
67      * @param path the full path to the .tec file
68      * @param toUnicode true if the direction is from bytes to Unicode
69      * false if the direction is from Unicode to bytes
70      * @return id of the converter created. This must be used in subsequent
71      * invocations to convert.
72      */
createConverter(String path, boolean toUnicode)73     public native long createConverter(String path, boolean toUnicode);
74     /**
75      * Converts the input byte array using the specified converter, which must
76      * have already be created with #createConverter.
77      * @param convId returned by #createConverter
78      * @param inputArray of bytes or UTF-8 encoded unicode.
79      * @return byte array of converted string as UTF-8 or bytes depending on direction.
80      */
convert(long convId, byte [] inputArray)81     public native byte [] convert(long convId, byte [] inputArray);
82     /**
83      * Flushes the converter.
84      * @param convId returned by #createConverter
85      */
flush(long convId)86     public native void flush(long convId);
87     /**
88      * Destroys the specified converter, releasing any resources.
89      * @param convId returned by #createConverter
90      */
destroyConverter(long convId)91     public native void destroyConverter(long convId);
92 }
93