⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 saxitexthandler.java

📁 源码包含生成 PDF 和 HTML 的类库
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * $Id: SAXiTextHandler.java 3427 2008-05-24 18:32:31Z xlv $ * * Copyright 2001, 2002 by Bruno Lowagie. * * The contents of this file are subject to the Mozilla Public License Version 1.1 * (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.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS IS" basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the License. * * The Original Code is 'iText, a free JAVA-PDF library'. * * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie. * All Rights Reserved. * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved. * * Contributor(s): all the names of the contributors are added in the source code * where applicable. * * Alternatively, the contents of this file may be used under the terms of the * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the * provisions of LGPL are applicable instead of those above.  If you wish to * allow use of your version of this file only under the terms of the LGPL * License and not to allow others to use your version of this file under * the MPL, indicate your decision by deleting the provisions above and * replace them with the notice and other provisions required by the LGPL. * If you do not delete the provisions above, a recipient may use your version * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE. * * This library is free software; you can redistribute it and/or modify it * under the terms of the MPL as stated above or under the terms of the GNU * Library General Public License as published by the Free Software Foundation; * either version 2 of the License, or any later version. * * This library is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more * details. * * If you didn't download this code from the following link, you should check if * you aren't using an obsolete version: * http://www.lowagie.com/iText/ */package com.lowagie.text.xml;import java.lang.reflect.Field;import java.util.ArrayList;import java.util.Collections;import java.util.EmptyStackException;import java.util.HashMap;import java.util.Iterator;import java.util.Properties;import java.util.Stack;import org.xml.sax.Attributes;import org.xml.sax.helpers.DefaultHandler;import com.lowagie.text.Anchor;import com.lowagie.text.Annotation;import com.lowagie.text.BadElementException;import com.lowagie.text.Cell;import com.lowagie.text.Chapter;import com.lowagie.text.Chunk;import com.lowagie.text.DocListener;import com.lowagie.text.DocumentException;import com.lowagie.text.Element;import com.lowagie.text.ElementTags;import com.lowagie.text.ExceptionConverter;import com.lowagie.text.Font;import com.lowagie.text.Image;import com.lowagie.text.List;import com.lowagie.text.ListItem;import com.lowagie.text.Meta;import com.lowagie.text.PageSize;import com.lowagie.text.Paragraph;import com.lowagie.text.Rectangle;import com.lowagie.text.Section;import com.lowagie.text.Table;import com.lowagie.text.TextElementArray;import com.lowagie.text.factories.ElementFactory;import com.lowagie.text.html.HtmlTagMap;import com.lowagie.text.pdf.BaseFont;import com.lowagie.text.pdf.draw.LineSeparator;import com.lowagie.text.xml.simpleparser.EntitiesToSymbol;/** * This class is a Handler that controls the iText XML to PDF conversion. * Subclass it, if you want to change the way iText translates XML to PDF. */public class SAXiTextHandler extends DefaultHandler {    /** This is the resulting document. */    protected DocListener document;    /**     * This is a <CODE>Stack</CODE> of objects, waiting to be added to the     * document.     */    protected Stack stack;    /** Counts the number of chapters in this document. */    protected int chapters = 0;    /** This is the current chunk to which characters can be added. */    protected Chunk currentChunk = null;    /** This is the current chunk to which characters can be added. */    protected boolean ignore = false;    /**     * This is a flag that can be set, if you want to open and close the     * Document-object yourself.     */    protected boolean controlOpenClose = true;    /** current margin of a page. */    float topMargin = 36;    /** current margin of a page. */    float rightMargin = 36;    /** current margin of a page. */    float leftMargin = 36;    /** current margin of a page. */    float bottomMargin = 36;    /**     * @param document     */    public SAXiTextHandler(DocListener document) {        super();        this.document = document;        stack = new Stack();    }    /** This hashmap contains all the custom keys and peers. */    protected HashMap myTags;    /**     * @param document     * @param myTags     */    public SAXiTextHandler(DocListener document, HtmlTagMap myTags) {        this(document);        this.myTags = myTags;    }    /**     * @param document     * @param myTags     * @param bf     */    public SAXiTextHandler(DocListener document, HtmlTagMap myTags,            BaseFont bf){        this(document, myTags);        this.bf = bf;    }    /**     * @param document     * @param myTags     */    public SAXiTextHandler(DocListener document, HashMap myTags) {        this(document);        this.myTags = myTags;    }    /**     * Sets the parameter that allows you to enable/disable the control over the     * Document.open() and Document.close() method.     * <P>     * If you set this parameter to true (= default), the parser will open the     * Document object when the start-root-tag is encountered and close it when     * the end-root-tag is met. If you set it to false, you have to open and     * close the Document object yourself.     *      * @param controlOpenClose     *            set this to false if you plan to open/close the Document     *            yourself     */    public void setControlOpenClose(boolean controlOpenClose) {        this.controlOpenClose = controlOpenClose;    }    /**     * This method gets called when a start tag is encountered.     *      * @param uri     *            the Uniform Resource Identifier     * @param lname     *            the local name (without prefix), or the empty string if     *            Namespace processing is not being performed.     * @param name     *            the name of the tag that is encountered     * @param attrs     *            the list of attributes     */    public void startElement(String uri, String lname, String name,            Attributes attrs) {        Properties attributes = new Properties();        if (attrs != null) {            for (int i = 0; i < attrs.getLength(); i++) {                String attribute = attrs.getQName(i);                attributes.setProperty(attribute, attrs.getValue(i));            }        }        handleStartingTags(name, attributes);    }    /**     * This method deals with the starting tags.     *      * @param name     *            the name of the tag     * @param attributes     *            the list of attributes     */    public void handleStartingTags(String name, Properties attributes) {        // System.err.println("Start: " + name);        if (ignore || ElementTags.IGNORE.equals(name)) {            ignore = true;            return;        }        // maybe there is some meaningful data that wasn't between tags        if (currentChunk != null) {            TextElementArray current;            try {                current = (TextElementArray) stack.pop();            } catch (EmptyStackException ese) {            	if (bf == null) {            		current = new Paragraph("", new Font());            	}            	else {            		current = new Paragraph("", new Font(this.bf));            	}            }            current.add(currentChunk);            stack.push(current);            currentChunk = null;        }        // chunks        if (ElementTags.CHUNK.equals(name)) {            currentChunk = ElementFactory.getChunk(attributes);            if (bf != null) {            	currentChunk.setFont(new Font(this.bf));            }            return;        }        // symbols        if (ElementTags.ENTITY.equals(name)) {            Font f = new Font();            if (currentChunk != null) {                handleEndingTags(ElementTags.CHUNK);                f = currentChunk.getFont();            }            currentChunk = EntitiesToSymbol.get(attributes.getProperty(ElementTags.ID),                    f);            return;        }        // phrases        if (ElementTags.PHRASE.equals(name)) {            stack.push(ElementFactory.getPhrase(attributes));            return;        }        // anchors        if (ElementTags.ANCHOR.equals(name)) {            stack.push(ElementFactory.getAnchor(attributes));            return;        }        // paragraphs and titles        if (ElementTags.PARAGRAPH.equals(name) || ElementTags.TITLE.equals(name)) {            stack.push(ElementFactory.getParagraph(attributes));            return;        }        // lists        if (ElementTags.LIST.equals(name)) {            stack.push(ElementFactory.getList(attributes));            return;        }                // listitems        if (ElementTags.LISTITEM.equals(name)) {            stack.push(ElementFactory.getListItem(attributes));            return;        }        // cells        if (ElementTags.CELL.equals(name)) {            stack.push(ElementFactory.getCell(attributes));            return;        }        // tables        if (ElementTags.TABLE.equals(name)) {            Table table = ElementFactory.getTable(attributes);            float widths[] = table.getProportionalWidths();            for (int i = 0; i < widths.length; i++) {                if (widths[i] == 0) {                    widths[i] = 100.0f / widths.length;                }            }            try {                table.setWidths(widths);            } catch (BadElementException bee) {                // this shouldn't happen                throw new ExceptionConverter(bee);            }            stack.push(table);            return;        }        // sections        if (ElementTags.SECTION.equals(name)) {            Element previous = (Element) stack.pop();            Section section;            try {                section = ElementFactory.getSection((Section) previous, attributes);            } catch (ClassCastException cce) {                throw new ExceptionConverter(cce);            }            stack.push(previous);            stack.push(section);            return;        }        // chapters        if (ElementTags.CHAPTER.equals(name)) {            stack.push(ElementFactory.getChapter(attributes));            return;        }        // images        if (ElementTags.IMAGE.equals(name)) {            try {                Image img = ElementFactory.getImage(attributes);                try {                	addImage(img);                    return;                } catch (EmptyStackException ese) {                    // if there is no element on the stack, the Image is added                    // to the document                    try {                        document.add(img);                    } catch (DocumentException de) {                        throw new ExceptionConverter(de);                    }                    return;                }            } catch (Exception e) {                throw new ExceptionConverter(e);            }        }        // annotations        if (ElementTags.ANNOTATION.equals(name)) {            Annotation annotation = ElementFactory.getAnnotation(attributes);            TextElementArray current;            try {                try {                    current = (TextElementArray) stack.pop();                    try {                        current.add(annotation);                    } catch (Exception e) {                        document.add(annotation);                    }                    stack.push(current);                } catch (EmptyStackException ese) {                    document.add(annotation);                }                return;            } catch (DocumentException de) {                throw new ExceptionConverter(de);            }        }        // newlines        if (isNewline(name)) {            TextElementArray current;            try {                current = (TextElementArray) stack.pop();                current.add(Chunk.NEWLINE);                stack.push(current);            } catch (EmptyStackException ese) {                if (currentChunk == null) {                    try {                        document.add(Chunk.NEWLINE);                    } catch (DocumentException de) {                        throw new ExceptionConverter(de);                    }                } else {                    currentChunk.append("\n");                }            }            return;        }        // newpage        if (isNewpage(name)) {            TextElementArray current;            try {                current = (TextElementArray) stack.pop();                Chunk newPage = new Chunk("");                newPage.setNewPage();                if (bf != null) {                	newPage.setFont(new Font(this.bf));                }                current.add(newPage);                stack.push(current);            } catch (EmptyStackException ese) {                document.newPage();            }            return;        }        if (ElementTags.HORIZONTALRULE.equals(name)) {            TextElementArray current;            LineSeparator hr = new LineSeparator(1.0f, 100.0f, null, Element.ALIGN_CENTER, 0);            try {                current = (TextElementArray) stack.pop();                current.add(hr);                stack.push(current);            } catch (EmptyStackException ese) {                try {                    document.add(hr);                } catch (DocumentException de) {                    throw new ExceptionConverter(de);                }            }            return;        }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -