corner.orm.tapestry.pdf.PdfTemplateSourceDelegate.java Source code

Java tutorial

Introduction

Here is the source code for corner.orm.tapestry.pdf.PdfTemplateSourceDelegate.java

Source

// Copyright 2007 the original author or authors.
// site: http://www.bjmaxinfo.com
// file: $Id: PdfTemplateSourceDelegate.java 3919 2008-01-16 04:36:52Z xf $
// created at:2007-07-31
//
// 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 corner.orm.tapestry.pdf;

import java.util.List;
import java.util.Locale;

import org.apache.hivemind.ApplicationRuntimeException;
import org.apache.hivemind.Resource;
import org.apache.tapestry.IComponent;
import org.apache.tapestry.IRequestCycle;
import org.apache.tapestry.engine.ITemplateSourceDelegate;
import org.apache.tapestry.parse.ComponentTemplate;
import org.apache.tapestry.parse.ITemplateParser;
import org.apache.tapestry.parse.ITemplateParserDelegate;
import org.apache.tapestry.parse.TemplateParseException;
import org.apache.tapestry.parse.TemplateParser;
import org.apache.tapestry.parse.TemplateToken;
import org.apache.tapestry.resolver.ComponentSpecificationResolver;
import org.apache.tapestry.services.ComponentPropertySource;
import org.apache.tapestry.services.impl.DefaultParserDelegate;

import com.lowagie.text.Rectangle;

import corner.orm.tapestry.pdf.components.PdfDisplayArea;
import corner.orm.tapestry.pdf.components.PdfOvTable;
import corner.orm.tapestry.pdf.service.AbstractPdfService;

/**
 * ?pdf?,??,?Tapestry??.
 * 
 * @author <a href="mailto:jun.tsai@bjmaxinfo.com">Jun Tsai</a>
 * @author <a href=mailto:Ghostbb@bjmaxinfo.com>Ghostbb</a>
 * @version $Revision: 3919 $
 * @since 2.3.7
 */
public class PdfTemplateSourceDelegate implements ITemplateSourceDelegate {
    /**
     * ? 
     */
    public static final String PDF_PAGE_TYPE_DEFINE_FIELD_NAME = "page-type";
    /**
     * ??
     */
    public static final String TEMPLATE_PAGE_TYPE = "template";

    private ITemplateParser _parser;

    private ComponentPropertySource _componentPropertySource;

    private ComponentSpecificationResolver _componentSpecificationResolver;

    /**
     * pdf
     * 
     * @see org.apache.tapestry.engine.ITemplateSourceDelegate#findTemplate(org.apache.tapestry.IRequestCycle,
     *      org.apache.tapestry.IComponent, java.util.Locale)
     */
    public synchronized ComponentTemplate findTemplate(IRequestCycle cycle, IComponent component, Locale locale) {
        if (component.getNamespace().getId().equals(AbstractPdfService.PDF_PAGE_NAMESPACE)) { // pdf?.
            IPdfPageSpecification spc = (IPdfPageSpecification) component.getSpecification();
            return constructTemplateInstance(cycle, parsePdfTemplate(spc).toCharArray(),
                    spc.getSpecificationLocation(), component);
        }
        return null;
    }

    //?pdf?
    String parsePdfTemplate(IPdfPageSpecification spc) {

        // ?
        StringBuffer sb = new StringBuffer();
        StringBuffer ovTable = new StringBuffer();
        sb.append("<div>");
        int insertPageTypeP = 0;
        String pageType = null;
        for (int i = 1; i <= spc.getNumberOfPage(); i++) {
            ovTable.setLength(0); // OverflowTable.
            pageType = "@PageFragment"; //pageType
            sb.append("<div jwcid=\"page");
            sb.append(i);

            insertPageTypeP = sb.length(); //???

            sb.append("\" pageNum=\"");
            sb.append(i);
            sb.append("\">\n");

            List<PdfBlock> list = spc.getPageFieldsMap().get(i);
            if (list != null) {
                for (PdfBlock block : list) {
                    if (block.getName().indexOf(PdfOvTable.COMPONENT_NAME) > -1
                            || block.getName().indexOf(PdfDisplayArea.COMPONENT_NAME) > -1) { //ov table
                        createTemplateByBlock(block, ovTable);

                    } else if (PDF_PAGE_TYPE_DEFINE_FIELD_NAME.equalsIgnoreCase(block.getName())) { //pdf page type define
                        if (block.getValue().equalsIgnoreCase(TEMPLATE_PAGE_TYPE))
                            pageType = "@TemplatePageFragment";

                    } else {
                        createTemplateByBlock(block, sb);
                    }
                }
            }

            sb.insert(insertPageTypeP, pageType);
            // OverFlowTable.
            sb.append(ovTable.toString());
            sb.append("</div>");
        }
        sb.append("</div>");
        return sb.toString();
    }

    private void createTemplateByBlock(PdfBlock block, StringBuffer sb) {
        sb.append("<span jwcid=\"");
        sb.append(block.getName());
        sb.append("\" ");

        // rectangle
        sb.append("rectangle=\"literal:");
        Rectangle r = block.getRectangle();
        sb.append(r.left()).append(",").append(r.bottom()).append(",").append(r.right()).append(",")
                .append(r.top());
        sb.append("\" ");

        // other parameter
        if (block.getValue() != null)
            sb.append(block.getValue());

        sb.append("/>\n");
    }

    /**
     * 
     * ?Tapestry{@link org.apache.tapestry.services.impl.TemplateSourceImpl#constructTemplateInstance}
     * This method is currently synchronized, because {@link TemplateParser}is
     * not threadsafe. Another good candidate for a pooling mechanism,
     * especially because parsing a template may take a while.
     */

    private synchronized ComponentTemplate constructTemplateInstance(IRequestCycle cycle, char[] templateData,
            Resource resource, IComponent component) {
        String componentAttributeName = _componentPropertySource.getComponentProperty(component,
                "org.apache.tapestry.jwcid-attribute-name");

        ITemplateParserDelegate delegate = new DefaultParserDelegate(component, componentAttributeName, cycle,
                _componentSpecificationResolver);

        TemplateToken[] tokens;

        try {
            tokens = _parser.parse(templateData, delegate, resource);
        } catch (TemplateParseException ex) {
            throw new ApplicationRuntimeException("??:" + resource, ex);
        }

        return new ComponentTemplate(templateData, tokens);
    }

    /** @since 0.7.5 */

    public void setComponentSpecificationResolver(ComponentSpecificationResolver resolver) {
        _componentSpecificationResolver = resolver;
    }

    public void setParser(ITemplateParser parser) {
        _parser = parser;
    }

    /** @since 0.7.5 */
    public void setComponentPropertySource(ComponentPropertySource componentPropertySource) {
        _componentPropertySource = componentPropertySource;
    }
}