001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package javax.swing.text.html.parser;
018:
019: import java.io.IOException;
020: import java.io.Reader;
021: import javax.swing.text.ChangedCharSetException;
022: import javax.swing.text.html.HTML;
023: import javax.swing.text.html.HTMLEditorKit;
024:
025: public class DocumentParser extends Parser {
026:
027: private static final HTML.Attribute HTTP_EQUIV = HTML
028: .getAttributeKey("http-equiv");
029:
030: private static final String CONTENT_TYPE = "content-type";
031:
032: private static final HTML.Attribute CONTENT = HTML
033: .getAttributeKey("content");
034:
035: private static final String CHARSET = "charset";
036:
037: private HTMLEditorKit.ParserCallback callback;
038:
039: private boolean ignoreCharSet;
040:
041: public DocumentParser(final DTD dtd) {
042: super (dtd);
043: }
044:
045: protected void handleError(final int ln, final String errorMsg) {
046: callback.handleError(errorMsg, ln);
047: }
048:
049: protected void handleText(final char[] data) {
050: callback.handleText(data, getCurrentPos());
051: }
052:
053: protected void handleEndTag(final TagElement tag) {
054: callback.handleEndTag(tag.getHTMLTag(), getCurrentPos());
055: }
056:
057: protected void handleEmptyTag(final TagElement tag)
058: throws ChangedCharSetException {
059: if (!ignoreCharSet && (tag.getHTMLTag() == HTML.Tag.META)) {
060: String httpEquivValue = (String) getAttributes()
061: .getAttribute(HTTP_EQUIV);
062: String contentValue = (String) getAttributes()
063: .getAttribute(CONTENT);
064:
065: if (httpEquivValue != null && contentValue != null
066: && httpEquivValue.equalsIgnoreCase(CONTENT_TYPE)
067: && contentValue.toLowerCase().contains(CHARSET)) {
068: // notice that always here ignoreCharSet will be false
069: throw new ChangedCharSetException(contentValue,
070: ignoreCharSet);
071: }
072: }
073: callback.handleSimpleTag(tag.getHTMLTag(), getAttributes(),
074: getCurrentPos());
075: }
076:
077: protected void handleComment(final char[] text) {
078: callback.handleComment(text, getCurrentPos());
079: }
080:
081: protected void handleStartTag(final TagElement tag) {
082: callback.handleStartTag(tag.getHTMLTag(), getAttributes(),
083: getCurrentPos());
084: }
085:
086: public void parse(final Reader in,
087: final HTMLEditorKit.ParserCallback callback,
088: final boolean ignoreCharSet) throws IOException {
089: /*
090: * TODO
091: * when the Reader in is null handle implied methods are invoked
092: * and the handleError report a nullPointerException
093: *
094: * Should be handled in Parser class without calling cup class ?
095: */
096:
097: this .callback = callback;
098: this .ignoreCharSet = ignoreCharSet;
099: super .parse(in);
100:
101: callback.handleEndOfLineString(super .getEOLString());
102:
103: // a close invocation flush the remaining bytes
104: in.close();
105: }
106: }
|