Source of StripQualifiers.java


  1: //: com:bruceeckel:util:StripQualifiers.java
  2: // From 'Thinking in Java, 2nd ed.' by Bruce Eckel
  3: // www.BruceEckel.com. See copyright notice in CopyRight.txt.
  4: package com.bruceeckel.util;
  5: import java.io.*;

  7: public class StripQualifiers {
  8:   private StreamTokenizer st;
  9:   public StripQualifiers(String qualified) {
 10:     st = new StreamTokenizer(
 11:       new StringReader(qualified));
 12:     st.ordinaryChar(' '); // Keep the spaces
 13:   }
 14:   public String getNext() {
 15:     String s = null;
 16:     try {
 17:       int token = st.nextToken();
 18:       if(token != StreamTokenizer.TT_EOF) {
 19:         switch(st.ttype) {
 20:           case StreamTokenizer.TT_EOL:
 21:             s = null;
 22:             break;
 23:           case StreamTokenizer.TT_NUMBER:
 24:             s = Double.toString(st.nval);
 25:             break;
 26:           case StreamTokenizer.TT_WORD:
 27:             s = new String(st.sval);
 28:             break;
 29:           default: // single character in ttype
 30:             s = String.valueOf((char)st.ttype);
 31:         }
 32:       }
 33:     } catch(IOException e) {
 34:       System.err.println("Error fetching token");
 35:     }
 36:     return s;
 37:   }
 38:   public static String strip(String qualified) {
 39:     StripQualifiers sq = 
 40:       new StripQualifiers(qualified);
 41:     String s = "", si;
 42:     while((si = sq.getNext()) != null) {
 43:       int lastDot = si.lastIndexOf('.');
 44:       if(lastDot != -1)
 45:         si = si.substring(lastDot + 1);
 46:       s += si;
 47:     }
 48:     return s;
 49:   }
 50: } ///:~