1 5 6 package com.hp.hpl.jena.util.tuple ; 7 8 import java.io.* ; 9 import java.util.* ; 10 11 import org.apache.commons.logging.Log; 12 import org.apache.commons.logging.LogFactory; 13 14 18 19 public class TupleSet implements Iterator 20 { 21 BufferedReader in ; 22 public String line = null ; 23 public int lineNumber = 0 ; 24 25 static final char COMMENTCHAR = '#' ; 26 List current = null ; 27 boolean finished = false ; 28 29 protected static Log logger = LogFactory.getLog( TupleSet.class ); 30 31 32 public TupleSet(Reader r) 33 { 34 if ( ! ( r instanceof BufferedReader ) ) 35 in = new BufferedReader(r) ; 36 else 37 in = (BufferedReader)r; 38 } 39 40 public boolean hasNext() 41 { 42 if ( finished ) return false ; 43 44 if ( current == null ) 45 current = tuple() ; 46 return current != null ; 47 } 48 49 public Object next() 50 { 51 if ( hasNext() ) 52 { 53 List x = current ; 54 current = null ; 55 return x ; 56 } 57 else 58 return null ; 59 } 60 61 62 public void remove() 63 { 64 throw new java.lang.UnsupportedOperationException ("TupleSet.remove") ; 65 } 66 67 private List tuple() 68 { 69 70 try { 71 lineNumber ++ ; 72 line = in.readLine() ; 73 } catch (IOException e) {} 74 75 if ( line == null ) 76 { 77 finished = true ; 78 return null ; 79 } 80 81 List tuple = new ArrayList() ; 83 int i = 0 ; 84 int j = 0 ; 85 boolean errorFound = false ; 86 87 tupleLoop: 88 for (;;) 89 { 90 i = skipwhitespace(line, j) ; 92 93 if ( i < 0 ) 94 break ; 95 96 int iStart = -2 ; int jStart = -2 ; int iFinish = -2 ; int jFinish = -2 ; int dtStart = -2 ; int dtFinish = -2 ; int type = TupleItem.UNKNOWN; 103 104 switch (line.charAt(i)) 105 { 106 case COMMENTCHAR: 107 break tupleLoop ; 108 case '<': 109 type = TupleItem.URI ; 110 iStart = i ; 111 jStart = i+1 ; 112 int newPosn = parseURI(i, line) ; 113 if (newPosn < 0) 114 { 115 errorFound = true; 116 break tupleLoop; 117 } 118 j = newPosn ; 119 120 iFinish = j+1 ; 121 jFinish = j ; 122 break ; 123 case '"': 124 type = TupleItem.STRING ; 125 iStart = i ; 126 jStart = i+1 ; 127 boolean inEscape = false ; 128 for ( j = i+1 ; j < line.length() ; j++ ) 129 { 130 char ch = line.charAt(j) ; 131 if ( inEscape ) 132 { 133 inEscape = false ; 135 continue ; 136 } 137 if ( ch == '"' ) 139 break ; 140 141 if ( ch == '\\' ) 142 inEscape = true ; 143 if ( ch == '\n' || ch == '\r' ) 144 { 145 errorFound = true ; 146 break tupleLoop; 147 148 } 149 } 150 151 if ( j == line.length() ) 153 { 154 errorFound = true ; 155 break tupleLoop; 156 } 157 158 iFinish = j+1 ; 159 jFinish = j ; 160 162 if ( j < line.length()-3 163 && line.charAt(j+1) == '^' 164 && line.charAt(j+2) == '^' 165 && line.charAt(j+3) == '<' ) 166 { 167 dtFinish = parseURI(j+3, line) ; 168 dtStart = j+4 ; 169 if (dtFinish < 0) 170 { 171 errorFound = true; 172 break tupleLoop; 173 } 174 j = dtFinish+1 ; 175 } 178 179 break ; 180 case '_': 181 type = TupleItem.ANON ; 182 iStart = i ; 183 for ( j = i+1 ; j < line.length() ; j++ ) 184 { 185 char ch = line.charAt(j) ; 186 if ( ch == ' ' || ch == '\t' || ch == '.' ) 187 break ; 188 if ( ! Character.isLetterOrDigit(ch) && ! (ch == '_') && ! (ch == ':') ) 189 { 190 errorFound = true ; 191 break tupleLoop ; 192 } 193 } 194 iFinish = j ; 195 jStart = iStart ; 196 jFinish = iFinish ; 197 break ; 198 case '.': 199 case '\n': 200 case '\r': 201 return tuple ; 202 default: 203 type = TupleItem.UNQUOTED ; 204 iStart = i ; 205 jStart = i ; 206 for ( j = i+1 ; j < line.length() ; j++ ) 207 { 208 char ch = line.charAt(j) ; 209 if ( ch == ' ' || ch == '\t' || ch == '.' ) 210 break ; 211 212 } 218 if ( j == line.length()+1 ) 220 { 221 errorFound = true ; 222 break tupleLoop; 223 } 224 iFinish = j ; 225 jFinish = j ; 226 break ; 227 } 228 String item = line.substring(jStart, jFinish) ; 229 String literal = line.substring(iStart, iFinish) ; 230 String dt = null ; 231 if ( dtStart > 0 ) 232 dt = line.substring(dtStart, dtFinish) ; 233 234 tuple.add(new TupleItem(item, literal, type, dt)) ; 235 j++ ; 236 } 238 if ( errorFound ) 240 { 241 logger.error( "Error in TupleSet.tuple: " + line ); 242 243 String s = "" ; 244 int k = 0 ; 245 for ( ; k < i ; k++ ) s = s+" " ; 246 s = s+"^" ; 247 for ( ; k < j-1 ; k++ ) s=s+" " ; 248 s = s+"^" ; 249 logger.error( s ) ; 250 return null ; 251 } 252 253 if ( tuple.size() == 0 ) 254 { 255 return tuple() ; 257 } 258 return tuple ; 259 } 260 261 private int skipwhitespace(String s, int i) 262 { 263 for ( ; i < s.length() ; i++ ) 264 { 265 char ch = s.charAt(i) ; 266 if ( ch != ' ' && ch != '\t' ) 268 return i ; 269 } 270 return -1 ; 271 } 272 273 private int parseURI(int i, String line) 274 { 275 int j; 276 for (j = i + 1; j < line.length(); j++) 277 { 278 char ch = line.charAt(j); 279 if (ch == '>') 280 break; 281 if (ch == '\n' || ch == '\r') 282 return -1; 283 } 284 if (j == line.length()) 286 return -2; 287 return j ; 288 } 289 } 290 291 317 | Popular Tags |