Frames | No Frames |
1: /* GnuParserDelegator.java -- The parser delegator which uses Swing DTD 2: Copyright (C) 2006 Free Software Foundation, Inc. 3: 4: This file is part of GNU Classpath. 5: 6: GNU Classpath is free software; you can redistribute it and/or modify 7: it under the terms of the GNU General Public License as published by 8: the Free Software Foundation; either version 2, or (at your option) 9: any later version. 10: 11: GNU Classpath is distributed in the hope that it will be useful, but 12: WITHOUT ANY WARRANTY; without even the implied warranty of 13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14: General Public License for more details. 15: 16: You should have received a copy of the GNU General Public License 17: along with GNU Classpath; see the file COPYING. If not, write to the 18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 19: 02110-1301 USA. 20: 21: Linking this library statically or dynamically with other modules is 22: making a combined work based on this library. Thus, the terms and 23: conditions of the GNU General Public License cover the whole 24: combination. 25: 26: As a special exception, the copyright holders of this library give you 27: permission to link this library with independent modules to produce an 28: executable, regardless of the license terms of these independent 29: modules, and to copy and distribute the resulting executable under 30: terms of your choice, provided that you also meet, for each linked 31: independent module, the terms and conditions of the license of that 32: module. An independent module is a module which is not derived from 33: or based on this library. If you modify this library, you may extend 34: this exception to your version of the library, but you are not 35: obligated to do so. If you do not wish to do so, delete this 36: exception statement from your version. */ 37: 38: 39: package gnu.javax.swing.text.html.parser; 40: 41: import java.io.IOException; 42: import java.io.Reader; 43: import java.io.Serializable; 44: 45: import javax.swing.text.BadLocationException; 46: import javax.swing.text.html.HTMLEditorKit; 47: import javax.swing.text.html.HTMLEditorKit.ParserCallback; 48: import javax.swing.text.html.parser.DTD; 49: import javax.swing.text.html.parser.ParserDelegator; 50: import javax.swing.text.html.parser.TagElement; 51: 52: /** 53: * This parser delegator uses the different DTD ({@link HTML_401Swing}). 54: * It is derived from the ParserDelegator for the compatibility reasons. 55: * 56: * @author Audrius Meskauskas (AudriusA@Bioinformatics.org) 57: */ 58: public class GnuParserDelegator extends ParserDelegator implements Serializable 59: { 60: class gnuParser 61: extends gnu.javax.swing.text.html.parser.support.Parser 62: { 63: private static final long serialVersionUID = 1; 64: 65: gnuParser(DTD d) 66: { 67: super(d); 68: } 69: 70: protected final void handleComment(char[] comment) 71: { 72: callBack.handleComment(comment, hTag.where.startPosition); 73: } 74: 75: protected final void handleEmptyTag(TagElement tag) 76: throws javax.swing.text.ChangedCharSetException 77: { 78: callBack.handleSimpleTag(tag.getHTMLTag(), getAttributes(), 79: hTag.where.startPosition 80: ); 81: } 82: 83: protected final void handleEndTag(TagElement tag) 84: { 85: callBack.handleEndTag(tag.getHTMLTag(), hTag.where.startPosition); 86: } 87: 88: protected final void handleError(int line, String message) 89: { 90: callBack.handleError(message, hTag.where.startPosition); 91: } 92: 93: protected final void handleStartTag(TagElement tag) 94: { 95: htmlAttributeSet attributes = gnu.getAttributes(); 96: 97: if (tag.fictional()) 98: attributes.addAttribute(ParserCallback.IMPLIED, Boolean.TRUE); 99: 100: callBack.handleStartTag(tag.getHTMLTag(), attributes, 101: hTag.where.startPosition 102: ); 103: } 104: 105: protected final void handleText(char[] text) 106: { 107: callBack.handleText(text, hTag.where.startPosition); 108: } 109: 110: DTD getDTD() 111: { 112: // Accessing the inherited gnu.javax.swing.text.html.parser.support.Parser 113: // field. super. is a workaround, required to support JDK1.3's javac. 114: return super.dtd; 115: } 116: } 117: 118: /** 119: * Use serialVersionUID for interoperability. 120: */ 121: private static final long serialVersionUID = -1276686502624777206L; 122: 123: private DTD theDtd; 124: 125: /** 126: * The callback. 127: * This is package-private to avoid an accessor method. 128: */ 129: HTMLEditorKit.ParserCallback callBack; 130: 131: /** 132: * The reference to the working class of HTML parser that is 133: * actually used to parse the document. 134: * This is package-private to avoid an accessor method. 135: */ 136: gnuParser gnu; 137: 138: /** 139: * Create the parser that uses the given DTD to parse the document. 140: * 141: * @param theDtd the DTD 142: */ 143: public GnuParserDelegator(DTD theDtd) 144: { 145: this.theDtd = theDtd; 146: gnu = new gnuParser(theDtd); 147: } 148: 149: /** 150: * Parses the HTML document, calling methods of the provided callback. This 151: * method must be multithread - safe. 152: * 153: * @param reader The reader to read the HTML document from 154: * @param a_callback The callback that is notifyed about the presence of HTML 155: * elements in the document. 156: * @param ignoreCharSet If thrue, any charset changes during parsing are 157: * ignored. 158: * @throws java.io.IOException 159: */ 160: public void parse(Reader reader, 161: HTMLEditorKit.ParserCallback a_callback, 162: boolean ignoreCharSet) throws IOException 163: { 164: callBack = a_callback; 165: gnu.parse(reader); 166: 167: callBack.handleEndOfLineString(gnu.getEndOfLineSequence()); 168: try 169: { 170: callBack.flush(); 171: } 172: catch (BadLocationException ex) 173: { 174: // Convert this into the supported type of exception. 175: throw new IOException(ex.getMessage()); 176: } 177: } 178: }