1 50 51 package org.openlaszlo.iv.flash.util; 52 53 58 public final class Tokenizer { 59 60 private String line; 61 private int cur; 62 private int length; 63 64 public Tokenizer( String line ) { 65 this.line = line; 66 cur = 0; 67 length = line.length(); 68 } 69 70 private boolean skipBlanks() { 71 while( cur < length && line.charAt(cur) == ' ' ) cur++; 72 return cur >= length; 73 } 74 75 public String getToken() { 76 if( skipBlanks() ) return null; 77 78 boolean quotes; 79 int start; 80 81 char ch = line.charAt(cur); 82 if( ch == '\"' ) { 83 quotes = true; 84 start = ++cur; 85 if( cur >= length ) return null; 86 ch = line.charAt(cur); 87 } else { 88 quotes = false; 89 start = cur; 90 } 91 int end; 92 93 mainLoop: 94 for(;;) { 95 switch( ch ) { 96 case '\\': 97 cur++; 98 if( cur >= length ) { 99 end = cur; 100 break mainLoop; 101 } 102 break; 103 case '\"': 104 if( quotes ) { 105 end = cur++; 106 if( skipBlanks() ) break mainLoop; 107 if( line.charAt(cur) == ',' ) cur++; 108 break mainLoop; 109 } 110 break; 111 case ',': 112 if( !quotes ) { 113 end = cur++; 114 break mainLoop; 115 } 116 break; 117 default: 118 break; 119 } 120 cur++; 121 if( cur >= length ) { 122 end = cur; 123 break; 124 } 125 ch = line.charAt(cur); 126 } 127 128 while( start < end && line.charAt(end-1) == ' ' ) end--; 130 131 return line.substring(start, end); 132 } 133 134 } 135 | Popular Tags |