1 7 8 package javax.swing.text.html.parser; 9 10 import javax.swing.text.SimpleAttributeSet ; 11 import javax.swing.text.html.HTMLEditorKit ; 12 import javax.swing.text.html.HTML ; 13 import javax.swing.text.ChangedCharSetException ; 14 15 import java.util.*; 16 import java.io.*; 17 import java.net.*; 18 19 86 public class DocumentParser extends javax.swing.text.html.parser.Parser { 87 88 private int inbody; 89 private int intitle; 90 private int inhead; 91 private int instyle; 92 private int inscript; 93 private boolean seentitle; 94 private HTMLEditorKit.ParserCallback callback = null; 95 private boolean ignoreCharSet = false; 96 private static final boolean debugFlag = false; 97 98 public DocumentParser(DTD dtd) { 99 super(dtd); 100 } 101 102 public void parse(Reader in, HTMLEditorKit.ParserCallback callback, boolean ignoreCharSet) throws IOException { 103 this.ignoreCharSet = ignoreCharSet; 104 this.callback = callback; 105 parse(in); 106 callback.handleEndOfLineString(getEndOfLineString()); 108 } 109 110 113 protected void handleStartTag(TagElement tag) { 114 115 Element elem = tag.getElement(); 116 if (elem == dtd.body) { 117 inbody++; 118 } else if (elem == dtd.html) { 119 } else if (elem == dtd.head) { 120 inhead++; 121 } else if (elem == dtd.title) { 122 intitle++; 123 } else if (elem == dtd.style) { 124 instyle++; 125 } else if (elem == dtd.script) { 126 inscript++; 127 } 128 if (debugFlag) { 129 if (tag.fictional()) { 130 debug("Start Tag: " + tag.getHTMLTag() + " pos: " + getCurrentPos()); 131 } else { 132 debug("Start Tag: " + tag.getHTMLTag() + " attributes: " + 133 getAttributes() + " pos: " + getCurrentPos()); 134 } 135 } 136 if (tag.fictional()) { 137 SimpleAttributeSet attrs = new SimpleAttributeSet (); 138 attrs.addAttribute(HTMLEditorKit.ParserCallback.IMPLIED, 139 Boolean.TRUE); 140 callback.handleStartTag(tag.getHTMLTag(), attrs, 141 getBlockStartPosition()); 142 } else { 143 callback.handleStartTag(tag.getHTMLTag(), getAttributes(), 144 getBlockStartPosition()); 145 flushAttributes(); 146 } 147 } 148 149 150 protected void handleComment(char text[]) { 151 if (debugFlag) { 152 debug("comment: ->" + new String (text) + "<-" 153 + " pos: " + getCurrentPos()); 154 } 155 callback.handleComment(text, getBlockStartPosition()); 156 } 157 158 161 protected void handleEmptyTag(TagElement tag) throws ChangedCharSetException { 162 163 Element elem = tag.getElement(); 164 if (elem == dtd.meta && !ignoreCharSet) { 165 SimpleAttributeSet atts = getAttributes(); 166 if (atts != null) { 167 String content = (String )atts.getAttribute(HTML.Attribute.CONTENT); 168 if (content != null) { 169 if ("content-type".equalsIgnoreCase((String )atts.getAttribute(HTML.Attribute.HTTPEQUIV))) { 170 if (!content.equalsIgnoreCase("text/html") && 171 !content.equalsIgnoreCase("text/plain")) { 172 throw new ChangedCharSetException (content, false); 173 } 174 } else if ("charset" .equalsIgnoreCase((String )atts.getAttribute(HTML.Attribute.HTTPEQUIV))) { 175 throw new ChangedCharSetException (content, true); 176 } 177 } 178 } 179 } 180 if (inbody != 0 || elem == dtd.meta || elem == dtd.base || elem == dtd.isindex || elem == dtd.style || elem == dtd.link) { 181 if (debugFlag) { 182 if (tag.fictional()) { 183 debug("Empty Tag: " + tag.getHTMLTag() + " pos: " + getCurrentPos()); 184 } else { 185 debug("Empty Tag: " + tag.getHTMLTag() + " attributes: " 186 + getAttributes() + " pos: " + getCurrentPos()); 187 } 188 } 189 if (tag.fictional()) { 190 SimpleAttributeSet attrs = new SimpleAttributeSet (); 191 attrs.addAttribute(HTMLEditorKit.ParserCallback.IMPLIED, 192 Boolean.TRUE); 193 callback.handleSimpleTag(tag.getHTMLTag(), attrs, 194 getBlockStartPosition()); 195 } else { 196 callback.handleSimpleTag(tag.getHTMLTag(), getAttributes(), 197 getBlockStartPosition()); 198 flushAttributes(); 199 } 200 } 201 } 202 203 206 protected void handleEndTag(TagElement tag) { 207 Element elem = tag.getElement(); 208 if (elem == dtd.body) { 209 inbody--; 210 } else if (elem == dtd.title) { 211 intitle--; 212 seentitle = true; 213 } else if (elem == dtd.head) { 214 inhead--; 215 } else if (elem == dtd.style) { 216 instyle--; 217 } else if (elem == dtd.script) { 218 inscript--; 219 } 220 if (debugFlag) { 221 debug("End Tag: " + tag.getHTMLTag() + " pos: " + getCurrentPos()); 222 } 223 callback.handleEndTag(tag.getHTMLTag(), getBlockStartPosition()); 224 225 } 226 227 230 protected void handleText(char data[]) { 231 if (data != null) { 232 if (inscript != 0) { 233 callback.handleComment(data, getBlockStartPosition()); 234 return; 235 } 236 if (inbody != 0 || ((instyle != 0) || 237 ((intitle != 0) && !seentitle))) { 238 if (debugFlag) { 239 debug("text: ->" + new String (data) + "<-" + " pos: " + getCurrentPos()); 240 } 241 callback.handleText(data, getBlockStartPosition()); 242 } 243 } 244 } 245 246 249 protected void handleError(int ln, String errorMsg) { 250 if (debugFlag) { 251 debug("Error: ->" + errorMsg + "<-" + " pos: " + getCurrentPos()); 252 } 253 254 callback.handleError(errorMsg, getCurrentPos()); 255 } 256 257 258 261 private void debug(String msg) { 262 System.out.println(msg); 263 } 264 } 265 | Popular Tags |