1 10 package com.hp.hpl.jena.util; 11 12 import java.util.NoSuchElementException ; 13 14 21 public class Tokenizer { 22 23 24 protected String source; 25 26 27 protected int p; 28 29 30 protected String delim; 31 32 33 protected boolean returnDelims; 34 35 36 protected String literalDelim; 37 38 39 protected int state; 40 41 42 protected String lookahead; 43 44 45 protected static final int NORMAL = 1; 46 47 48 protected static final int LITERAL_START = 2; 49 50 51 protected static final int LITERAL_END = 3; 52 53 60 public Tokenizer(String str, String delim, String literalDelim, boolean returnDelims) { 61 this.source = str; 62 this.delim = delim; 63 this.literalDelim = literalDelim; 64 this.returnDelims = returnDelims; 65 p = 0; 66 state = NORMAL; 67 } 68 69 73 public String nextToken() { 74 String result = null; 75 if (lookahead != null) { 76 result = lookahead; 77 lookahead = null; 78 } else { 79 result = getNextToken(); 80 } 81 if (result == null) { 82 throw new NoSuchElementException ("No more elements in tokenized string"); 83 } 84 if (!returnDelims) { 85 if (result.length() == 1) { 86 char c = result.charAt(0); 87 if (delim.indexOf(c) != -1 || literalDelim.indexOf(c) != -1) { 88 return nextToken(); 89 } 90 } 91 } 92 return result; 93 } 94 95 98 public boolean hasMoreTokens() { 99 if (lookahead != null) lookahead = getNextToken(); 100 return lookahead != null; 101 } 102 103 106 private String getNextToken() { 107 if (p >= source.length()) { 108 return null; 109 } 110 switch(state) { 111 case NORMAL: 112 if (is(literalDelim)) { 113 state = LITERAL_START; 114 p++; 115 return source.substring(p-1, p); 116 } else if (is(delim)) { 117 p++; 118 return source.substring(p-1, p); 119 } else { 120 int start = p; 121 p++; 122 while (p < source.length() && ! is(delim)) p++; 123 return source.substring(start, p); 124 } 125 case LITERAL_START: 126 int start = p; 127 while (isLiteral() && p < source.length()) p++; 128 state = LITERAL_END; 129 return source.substring(start, p); 130 case LITERAL_END: 131 state = NORMAL; 132 p++; 133 return source.substring(p-1, p); 134 } 135 return null; 136 } 137 138 139 142 private boolean is(String classification) { 143 return classification.indexOf(source.charAt(p)) != -1; 144 } 145 146 149 private boolean isLiteral() { 150 if (is(literalDelim)) { 151 if (source.charAt(p-1) == '\\') return true; 153 return false; 154 } else { 155 return true; 156 } 157 } 158 } 159 160 161 | Popular Tags |