1 23 24 package org.enhydra.xml.xmlc.html.parsers.swing; 25 26 import java.io.IOException ; 27 import java.io.Reader ; 28 29 import javax.swing.text.ChangedCharSetException ; 30 import javax.swing.text.SimpleAttributeSet ; 31 import javax.swing.text.html.HTMLEditorKit ; 32 import javax.swing.text.html.parser.DTD ; 33 import javax.swing.text.html.parser.DocumentParser ; 34 import javax.swing.text.html.parser.Element ; 35 import javax.swing.text.html.parser.ParserDelegator ; 36 import javax.swing.text.html.parser.TagElement ; 37 38 import org.enhydra.xml.xmlc.XMLCError; 39 40 53 class ParserAdaptor { 54 55 public static final int SWING_1_1 = 11; 56 public static final int SWING_1_2 = 12; 57 public static final int SWING_1_3 = 13; 58 59 60 private static int swingVersion; 61 62 67 public static final String MAGIC_END_TAG = "__EndOfLineTag__"; 68 69 73 public static final String IMPLIED_PSEUDO_ATTR = "_implied_"; 74 75 78 static { 79 String specVersion = System.getProperty("java.specification.version"); 81 if ((specVersion == null) || specVersion.startsWith("1.1")) { 82 swingVersion = SWING_1_1; 83 } else if (specVersion.startsWith("1.2")) { 84 swingVersion = SWING_1_2; 85 } else { 86 swingVersion = SWING_1_3; 88 } 89 } 90 91 94 private static class DTDCreator extends ParserDelegator { 95 96 private static String DTD_NAME = "html32"; 97 98 99 private static DTD dtd = null; 100 101 104 private static synchronized void buildDTD() throws IOException { 105 DTD newDTD = DTD.getDTD(DTD_NAME); 106 newDTD = createDTD(newDTD, DTD_NAME); 107 108 dtd = newDTD; 110 } 111 112 115 public static DTD getDTD() throws IOException { 116 if (dtd == null) { 117 buildDTD(); 118 } 119 return dtd; 120 } 121 } 122 123 126 class DocumentParserBase extends DocumentParser { 127 128 protected HTMLEditorKit.ParserCallback fCallback; 129 130 133 public DocumentParserBase(DTD dtd) { 134 super(dtd); 135 } 136 137 140 public void parse(Reader in, 141 HTMLEditorKit.ParserCallback callback, 142 boolean ignoreCharSet) throws IOException { 143 fCallback = callback; 144 super.parse(in, callback, ignoreCharSet); 145 } 146 } 147 148 151 class DocumentParser11 extends DocumentParserBase { 152 155 public DocumentParser11(DTD dtd) { 156 super(dtd); 157 } 158 159 162 protected void handleEmptyTag(TagElement tag) throws ChangedCharSetException { 163 Element elem = tag.getElement(); 164 if (elem.name.equalsIgnoreCase("link")) { 165 if (tag.fictional()) { 166 fCallback.handleSimpleTag(tag.getHTMLTag(), new SimpleAttributeSet (), getCurrentPos()); 167 } else { 168 fCallback.handleSimpleTag(tag.getHTMLTag(), getAttributes(), getCurrentPos()); 169 flushAttributes(); 170 } 171 } else { 172 super.handleEmptyTag(tag); 173 } 174 } 175 } 176 177 180 public int getSwingVersion() { 181 return swingVersion; 182 } 183 184 187 public DocumentParser getParser() { 188 try { 189 DTD dtd = DTDCreator.getDTD(); 190 if (swingVersion == SWING_1_1) { 191 return new DocumentParser11(dtd); 192 } else if (swingVersion == SWING_1_2) { 193 return new DocumentParser (dtd); 194 } else { 195 return new DocumentParser (dtd); 196 } 197 } catch (Exception except) { 198 throw new XMLCError("Can't created Swing HTML parser", except); 199 } 200 } 201 } 202 | Popular Tags |