Beispiel #1
0
 /**
  * Read characters from a Reader and generate SAX characters events.
  *
  * <p>The caller has to close the Reader if needed.
  */
 public static void readerToCharacters(Reader reader, ContentHandler contentHandler) {
   try {
     // Work with buffered Reader
     reader = new BufferedReader(reader);
     // Read and write in chunks
     char[] buf = new char[1024];
     int count;
     while ((count = reader.read(buf)) != -1) contentHandler.characters(buf, 0, count);
   } catch (Exception e) {
     throw new OXFException(e);
   }
 }
Beispiel #2
0
 private static short[] load(String name, Reader input, Model.Translator translator)
     throws java.io.IOException {
   short[] sequence = null;
   char[] buffer = new char[10240];
   int len;
   while ((len = input.read(buffer)) > 0) {
     for (int i = len - 1; i >= 0; i--) {
       if (Character.isWhitespace(buffer[i])) {
         if (i == (len - 1)) {
           len--;
         } else {
           System.arraycopy(buffer, i + 1, buffer, i, len - i - 1);
         }
       }
     }
     int start;
     if (sequence == null) {
       sequence = new short[len];
       start = 0;
     } else {
       start = sequence.length;
       short[] newseq = new short[sequence.length + len];
       System.arraycopy(sequence, 0, newseq, 0, sequence.length);
       sequence = newseq;
     }
     for (int i = 0; i < len; i++) {
       sequence[start + i] = translator.translate(buffer[i]);
       if (sequence[start + i] < 0) {
         throw new IOException(
             "Sequence character '"
                 + buffer[i]
                 + "' ("
                 + ((int) buffer[i])
                 + ") at offset "
                 + (start + i)
                 + " is not in the model's lexicon.");
       }
     }
   }
   return sequence;
 }