001 /*
002 * Copyright (c) 2008-2010 Mozilla Foundation
003 *
004 * Permission is hereby granted, free of charge, to any person obtaining a
005 * copy of this software and associated documentation files (the "Software"),
006 * to deal in the Software without restriction, including without limitation
007 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
008 * and/or sell copies of the Software, and to permit persons to whom the
009 * Software is furnished to do so, subject to the following conditions:
010 *
011 * The above copyright notice and this permission notice shall be included in
012 * all copies or substantial portions of the Software.
013 *
014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
017 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
019 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
020 * DEALINGS IN THE SOFTWARE.
021 */
022
023 package nu.validator.htmlparser.impl;
024
025 import nu.validator.htmlparser.annotation.NoLength;
026
027 import org.xml.sax.SAXException;
028
029 /**
030 * A common superclass for tree builders that coalesce their text nodes.
031 *
032 * @version $Id$
033 * @author hsivonen
034 */
035 public abstract class CoalescingTreeBuilder<T> extends TreeBuilder<T> {
036
037 protected final void accumulateCharacters(@NoLength char[] buf, int start,
038 int length) throws SAXException {
039 int newLen = charBufferLen + length;
040 if (newLen > charBuffer.length) {
041 char[] newBuf = new char[newLen];
042 System.arraycopy(charBuffer, 0, newBuf, 0, charBufferLen);
043 charBuffer = null; // release the old buffer in C++
044 charBuffer = newBuf;
045 }
046 System.arraycopy(buf, start, charBuffer, charBufferLen, length);
047 charBufferLen = newLen;
048 }
049
050 /**
051 * @see nu.validator.htmlparser.impl.TreeBuilder#appendCharacters(java.lang.Object, char[], int, int)
052 */
053 @Override protected final void appendCharacters(T parent, char[] buf, int start,
054 int length) throws SAXException {
055 appendCharacters(parent, new String(buf, start, length));
056 }
057
058 /**
059 * @see nu.validator.htmlparser.impl.TreeBuilder#appendIsindexPrompt(java.lang.Object)
060 */
061 @Override protected void appendIsindexPrompt(T parent) throws SAXException {
062 appendCharacters(parent, "This is a searchable index. Enter search keywords: ");
063 }
064
065 protected abstract void appendCharacters(T parent, String text) throws SAXException;
066
067 /**
068 * @see nu.validator.htmlparser.impl.TreeBuilder#appendComment(java.lang.Object, char[], int, int)
069 */
070 @Override final protected void appendComment(T parent, char[] buf, int start,
071 int length) throws SAXException {
072 appendComment(parent, new String(buf, start, length));
073 }
074
075 protected abstract void appendComment(T parent, String comment) throws SAXException;
076
077 /**
078 * @see nu.validator.htmlparser.impl.TreeBuilder#appendCommentToDocument(char[], int, int)
079 */
080 @Override protected final void appendCommentToDocument(char[] buf, int start,
081 int length) throws SAXException {
082 // TODO Auto-generated method stub
083 appendCommentToDocument(new String(buf, start, length));
084 }
085
086 protected abstract void appendCommentToDocument(String comment) throws SAXException;
087
088 /**
089 * @see nu.validator.htmlparser.impl.TreeBuilder#insertFosterParentedCharacters(char[], int, int, java.lang.Object, java.lang.Object)
090 */
091 @Override protected final void insertFosterParentedCharacters(char[] buf, int start,
092 int length, T table, T stackParent) throws SAXException {
093 insertFosterParentedCharacters(new String(buf, start, length), table, stackParent);
094 }
095
096 protected abstract void insertFosterParentedCharacters(String text, T table, T stackParent) throws SAXException;
097 }