/*
 * Copyright 2004-2010 the Seasar Foundation and the Others.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */
package org.seasar.mayaa.impl.builder.parser;

import org.apache.xerces.parsers.AbstractSAXParser;
import org.apache.xerces.xni.parser.XMLDocumentFilter;
import org.cyberneko.html.HTMLConfiguration;
import org.cyberneko.html.HTMLScanner;
import org.seasar.mayaa.impl.CONST_IMPL;

/**
 * @author Masataka Kurihara (Gluegent, Inc.)
 */
public class TemplateParser extends AbstractSAXParser implements CONST_IMPL {

    public TemplateParser(HTMLScanner scanner, String templateDefaultCharset) {
        super(new TemplateParserConfiguration(scanner, templateDefaultCharset));
    }

    private static class TemplateParserConfiguration extends HTMLConfiguration {

        /** Ignore outside content. copy from org.cyberneko.html.HTMLTagBalancer */
        protected static final String IGNORE_OUTSIDE_CONTENT =
            "http://cyberneko.org/html/features/balance-tags/ignore-outside-content";
        /** Document fragment balancing only. copy from org.cyberneko.html.HTMLTagBalancer */
        protected static final String DOCUMENT_FRAGMENT =
            "http://cyberneko.org/html/features/balance-tags/document-fragment";

        public TemplateParserConfiguration(HTMLScanner scanner, String templateDefaultCharset) {
            AdditionalHandlerFilter starter = new AdditionalHandlerFilter();
            addComponent(starter);
            setProperty(TemplateScanner.HTML_NAMES_ELEMS, "match");
            setProperty(TemplateScanner.HTML_NAMES_ATTRS, "no-change");
            /* テンプレート上にエンコーディング指定がなければUTF-8と見なす */
            setProperty(TemplateScanner.HTML_DEFAULT_ENCODING, templateDefaultCharset);
            setProperty(TemplateScanner.FILTERS, new XMLDocumentFilter[] { starter });
            /* 元のテンプレート内容を忠実に再現させるオプション。
               ただし、</html>の後ろは無視される。false(デフォルト)の場合は、
               </body>と</html>の後につづくものをnekoがむりやり前に持ってくる */
            setFeature(IGNORE_OUTSIDE_CONTENT, true);
            /* <html>や<body>が無い場合もそのままにするオプション。
               これが無いと勝手に付与されてしまう。 */
            setFeature(DOCUMENT_FRAGMENT, true);
            fDocumentScanner = scanner;
            fDocumentScanner.reset(this);
        }

    }

}
