All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.astamuse.asta4d.template.Template Maven / Gradle / Ivy

Go to download

core functionalities of asta4d framework, including template and snippt implemention

There is a newer version: 1.2-M2
Show newest version
/*
 * Copyright 2012 astamuse company,Ltd.
 * 
 * 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 com.astamuse.asta4d.template;

import java.io.IOException;
import java.io.InputStream;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.parser.BlockTagSupportHtmlTreeBuilder;
import org.jsoup.parser.Parser;

import com.astamuse.asta4d.Configuration;
import com.astamuse.asta4d.extnode.ExtNodeConstants;
import com.astamuse.asta4d.util.ElementUtil;
import com.astamuse.asta4d.util.IdGenerator;
import com.astamuse.asta4d.util.SelectorUtil;

public class Template {

    private String path;

    private Document doc;

    /**
     * 
     * @param path
     *            not being used, just for debug purpose
     * @param input
     * @throws IOException
     */
    public Template(String path, InputStream input) throws TemplateException, TemplateNotFoundException {
        try {
            this.path = path;
            this.doc = Jsoup.parse(input, "UTF-8", "", new Parser(new BlockTagSupportHtmlTreeBuilder()));
            // this.doc = Jsoup.parse(input, "UTF-8", "");
            initDocument();
        } catch (TemplateNotFoundException e) {
            throw e;
        } catch (Exception e) {
            String msg = String.format("Template %s initialize failed.", path);
            throw new TemplateException(msg, e);
        }
    }

    private void initDocument() throws TemplateException, TemplateNotFoundException {
        clearCommentNode();
        processExtension();
        TemplateUtil.regulateElement(doc);
    }

    private void clearCommentNode() throws TemplateException {
        String commentSelector = SelectorUtil.tag(ExtNodeConstants.COMMENT_NODE_TAG);
        ElementUtil.removeNodesBySelector(doc, commentSelector, false);
    }

    private void processExtension() throws TemplateException, TemplateNotFoundException {
        Element extension = doc.select(ExtNodeConstants.EXTENSION_NODE_TAG_SELECTOR).first();
        if (extension != null) {
            String parentPath = extension.attr(ExtNodeConstants.EXTENSION_NODE_ATTR_PARENT);
            if (parentPath == null || parentPath.isEmpty()) {
                throw new TemplateException(String.format("You must specify the parent of an extension (%s).", this.path));
            }
            Configuration conf = Configuration.getConfiguration();
            Template parent = conf.getTemplateResolver().findTemplate(parentPath);
            Document parentDoc = parent.getDocumentClone();
            TemplateUtil.mergeBlock(parentDoc, extension);

            doc = parentDoc;
        }

    }

    public String getPath() {
        return path;
    }

    public Document getDocumentClone() {
        Document newDoc = doc.clone();
        newDoc.attr(ExtNodeConstants.ATTR_DOC_REF, "doc-" + IdGenerator.createId());
        return newDoc;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy