1 9 10 package org.jboss.portal.format.parser.html; 11 12 import java.util.Iterator ; 13 14 import org.jboss.portal.format.parser.AbstractParser; 15 import org.jboss.portal.format.parser.ParseEvent; 16 import org.jboss.portal.format.parser.TextEvent; 17 import org.jboss.portal.format.parser.Token; 18 import org.jboss.portal.format.parser.bbcode.BBCodeParser; 19 import org.jboss.portal.format.parser.chars.MutableChars; 20 import org.jboss.portal.format.util.Stack; 21 22 26 public class TagParser 27 extends AbstractParser 28 { 29 30 public static final int EVENT_NORMAL = 0; 31 public static final int EVENT_TAG = 1; 32 33 private MutableChars buffer = new MutableChars(); 34 private Analyzer analyzer = new Analyzer(BBCodeParser.NULL_READER); 35 36 private OpenEvent openEvent = new OpenEvent(); 37 private TextEvent textEvent = new TextEvent(); 38 39 private Stack stack = new Stack(10) 40 { 41 protected Stack.Key createKey() 42 { 43 return new CloseEvent(); 44 } 45 protected boolean equals(Stack.Key o1, Stack.Key o2) 46 { 47 HTMLKey key1 = (HTMLKey)o1; 48 HTMLKey key2 = (HTMLKey)o2; 49 if (key1.getType() != key2.getType()) 50 { 51 return false; 52 } 53 if (key1.getType() == 1) 54 { 55 return key1.getTag().equals(key2.getTag()); 56 } 57 return false; 58 } 59 }; 60 61 public TagParser() 62 { 63 } 64 65 private HTMLKey temporaryKey = new HTMLKey(); 66 67 public void parse(char[] chars, int off, int len) 68 { 69 70 stack.reset(); 72 analyzer.reset(chars, off, len); 73 buffer.reset(); 74 75 CloseEvent closeEvent = (CloseEvent)stack.push(); 77 closeEvent.type = openEvent.type = EVENT_NORMAL; 78 closeEvent.tag = openEvent.tag = null; 79 openEvent.attributes = null; 80 handler.handle(openEvent); 81 82 while (true) 83 { 84 Token t = analyzer.next(); 86 87 if (t == null) 88 { 89 break; 91 } 92 93 switch (t.type) 94 { 95 case Analyzer.STAG: 96 { 97 closeEvent = (CloseEvent)stack.push(); 98 String tag = t.value; 99 int start = 1; 100 int end = tag.indexOf(' ', 1); 101 if (end == -1) 102 { 103 openEvent.type = closeEvent.type = EVENT_TAG; 104 openEvent.tag = closeEvent.tag = tag.substring(start, tag.length() - 1); 105 openEvent.attributes = ""; 106 } 107 else 108 { 109 openEvent.type = closeEvent.type = EVENT_TAG; 110 openEvent.tag = closeEvent.tag = tag.substring(start, end); 111 openEvent.attributes = tag.substring(end, tag.length() - 1); 112 } 113 text(); 114 handler.handle(openEvent); 115 break; 116 } 117 case Analyzer.ETAG: 118 { 119 temporaryKey.type = 1; 120 temporaryKey.tag = t.value.substring(2, t.value.length() - 1); 121 Iterator i = stack.pop(temporaryKey); 122 if (i.hasNext()) 123 { 124 text(); 125 do 126 { 127 handler.handle((CloseEvent)i.next()); 128 } 129 while (i.hasNext()); 130 } 131 break; 132 } 133 case Analyzer.EMPTY: 134 { 135 break; 145 } 146 case Analyzer.CDATA: 147 buffer.append(t.value.charAt(0)); 148 break; 149 default: 150 throw new IllegalStateException ("Unexpected token : " + t.type); 151 } 152 } 153 154 text(); 156 temporaryKey.type = EVENT_NORMAL; 157 Iterator i = stack.pop(temporaryKey); 158 while (i.hasNext()) 159 { 160 handler.handle((CloseEvent)i.next()); 161 } 162 } 163 164 private void text() 165 { 166 if (buffer.length() > 0) 167 { 168 textEvent.setText(buffer.chars(), 0, buffer.length()); 169 buffer.reset(); 170 handler.handle(textEvent); 171 } 172 } 173 174 private static class HTMLKey implements Stack.Key 175 { 176 int type; 177 String tag; 178 private HTMLKey() 179 { 180 type = -1; 181 tag = null; 182 } 183 public int getType() 184 { 185 return type; 186 } 187 public String getTag() 188 { 189 return tag; 190 } 191 } 192 193 public static class OpenEvent extends HTMLKey implements ParseEvent 194 { 195 private String attributes; 196 public String getAttribute() 197 { 198 return attributes; 199 } 200 } 201 202 public static class CloseEvent extends HTMLKey implements ParseEvent 203 { 204 } 205 } 206 | Popular Tags |