1 7 8 package javax.swing.text.html.parser; 9 10 28 class ContentModelState { 29 ContentModel model; 30 long value; 31 ContentModelState next; 32 33 36 public ContentModelState(ContentModel model) { 37 this(model, null, 0); 38 } 39 40 44 ContentModelState(Object content, ContentModelState next) { 45 this(content, next, 0); 46 } 47 48 52 ContentModelState(Object content, ContentModelState next, long value) { 53 this.model = (ContentModel )content; 54 this.next = next; 55 this.value = value; 56 } 57 58 61 public ContentModel getModel() { 62 ContentModel m = model; 63 for (int i = 0; i < value; i++) { 64 if (m.next != null) { 65 m = m.next; 66 } else { 67 return null; 68 } 69 } 70 return m; 71 } 72 73 78 public boolean terminate() { 79 switch (model.type) { 80 case '+': 81 if ((value == 0) && !(model).empty()) { 82 return false; 83 } 84 case '*': 85 case '?': 86 return (next == null) || next.terminate(); 87 88 case '|': 89 for (ContentModel m = (ContentModel )model.content ; m != null ; m = m.next) { 90 if (m.empty()) { 91 return (next == null) || next.terminate(); 92 } 93 } 94 return false; 95 96 case '&': { 97 ContentModel m = (ContentModel )model.content; 98 99 for (int i = 0 ; m != null ; i++, m = m.next) { 100 if ((value & (1L << i)) == 0) { 101 if (!m.empty()) { 102 return false; 103 } 104 } 105 } 106 return (next == null) || next.terminate(); 107 } 108 109 case ',': { 110 ContentModel m = (ContentModel )model.content; 111 for (int i = 0 ; i < value ; i++, m = m.next); 112 113 for (; (m != null) && m.empty() ; m = m.next); 114 if (m != null) { 115 return false; 116 } 117 return (next == null) || next.terminate(); 118 } 119 120 default: 121 return false; 122 } 123 } 124 125 130 public Element first() { 131 switch (model.type) { 132 case '*': 133 case '?': 134 case '|': 135 case '&': 136 return null; 137 138 case '+': 139 return model.first(); 140 141 case ',': { 142 ContentModel m = (ContentModel )model.content; 143 for (int i = 0 ; i < value ; i++, m = m.next); 144 return m.first(); 145 } 146 147 default: 148 return model.first(); 149 } 150 } 151 152 157 public ContentModelState advance(Object token) { 158 switch (model.type) { 159 case '+': 160 if (model.first(token)) { 161 return new ContentModelState (model.content, 162 new ContentModelState (model, next, value + 1)).advance(token); 163 } 164 if (value != 0) { 165 if (next != null) { 166 return next.advance(token); 167 } else { 168 return null; 169 } 170 } 171 break; 172 173 case '*': 174 if (model.first(token)) { 175 return new ContentModelState (model.content, this).advance(token); 176 } 177 if (next != null) { 178 return next.advance(token); 179 } else { 180 return null; 181 } 182 183 case '?': 184 if (model.first(token)) { 185 return new ContentModelState (model.content, next).advance(token); 186 } 187 if (next != null) { 188 return next.advance(token); 189 } else { 190 return null; 191 } 192 193 case '|': 194 for (ContentModel m = (ContentModel )model.content ; m != null ; m = m.next) { 195 if (m.first(token)) { 196 return new ContentModelState (m, next).advance(token); 197 } 198 } 199 break; 200 201 case ',': { 202 ContentModel m = (ContentModel )model.content; 203 for (int i = 0 ; i < value ; i++, m = m.next); 204 205 if (m.first(token) || m.empty()) { 206 if (m.next == null) { 207 return new ContentModelState (m, next).advance(token); 208 } else { 209 return new ContentModelState (m, 210 new ContentModelState (model, next, value + 1)).advance(token); 211 } 212 } 213 break; 214 } 215 216 case '&': { 217 ContentModel m = (ContentModel )model.content; 218 boolean complete = true; 219 220 for (int i = 0 ; m != null ; i++, m = m.next) { 221 if ((value & (1L << i)) == 0) { 222 if (m.first(token)) { 223 return new ContentModelState (m, 224 new ContentModelState (model, next, value | (1L << i))).advance(token); 225 } 226 if (!m.empty()) { 227 complete = false; 228 } 229 } 230 } 231 if (complete) { 232 if (next != null) { 233 return next.advance(token); 234 } else { 235 return null; 236 } 237 } 238 break; 239 } 240 241 default: 242 if (model.content == token) { 243 if (next == null && (token instanceof Element ) && 244 ((Element )token).content != null) { 245 return new ContentModelState (((Element )token).content); 246 } 247 return next; 248 } 249 266 } 267 268 return null; 277 } 278 } 279 | Popular Tags |