    001    /* ========================================================================
    002     * JCommon : a free general purpose class library for the Java(tm) platform
    003     * ========================================================================
    004     *
    005     * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
    006     *
    007     * Project Info:  http://www.jfree.org/jcommon/index.html
    008     *
    009     * This library is free software; you can redistribute it and/or modify it
    010     * under the terms of the GNU Lesser General Public License as published by
    011     * the Free Software Foundation; either version 2.1 of the License, or
    012     * (at your option) any later version.
    013     *
    014     * This library is distributed in the hope that it will be useful, but
    015     * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
    016     * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
    017     * License for more details.
    018     *
    019     * You should have received a copy of the GNU Lesser General Public
    020     * License along with this library; if not, write to the Free Software
    021     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
    022     * USA.
    023     *
    024     * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
    025     * in the United States and other countries.]
    026     *
    027     * ----------------
    028     * SafeTagList.java
    029     * ----------------
    030     * (C)opyright 2003-2005, by Thomas Morgner and Contributors.
    031     *
    032     * Original Author:  Thomas Morgner;
    033     * Contributor(s):   David Gilbert (for Object Refinery Limited);
    034     *
    035     * $Id: SafeTagList.java,v 1.3 2005/10/18 13:35:06 mungady Exp $
    036     *
    037     * Changes
    038     * -------
    039     * 21-Feb-2003 : Added standard header and Javadocs (DG);
    040     *
    041     */
    042
    043    package org.jfree.xml.writer;
    044
    045    import java.util.HashMap;
    046
    047    /**
    048     * A container for information relating to the tags in the JFreeReport XML report files.  Some tags
    049     * cannot be spread across multiple lines, because it causes problems for the parser.
    050     *
    051     * @author Thomas Morgner.
    052     */
    053    public class SafeTagList {
    054
    055        /** Storage for the tag information. */
    056        private HashMap safeTags;
    057
    058        /**
    059         * A tag description.
    060         */
    061        private static class SafeDescription {
    062
    063            /** A flag indicating whether or not it is safe to put a new line after the open tag. */
    064            private boolean open;
    065
    066            /** A flag indicating whether or not it is safe to put a new line before the close tag. */
    067            private boolean close;
    068
    069            /**
    070             * Creates a new tag description.
    071             *
    072             * @param open  the 'open' flag.
    073             * @param close  the 'close' flag.
    074             */
    075            public SafeDescription(final boolean open, final boolean close) {
    076                this.open = open;
    077                this.close = close;
    078            }
    079
    080            /**
    081             * Returns the 'open' flag.
    082             *
    083             * @return <code>true</code> or <code>false</code>.
    084             */
    085            public boolean isOpen() {
    086                return this.open;
    087            }
    088
    089            /**
    090             * Returns the 'close' flag.
    091             *
    092             * @return <code>true</code> or <code>false</code>.
    093             */
    094            public boolean isClose() {
    095                return this.close;
    096            }
    097        }
    098
    099        /**
    100         * Creates a new list.
    101         */
    102        public SafeTagList() {
    103            this.safeTags = new HashMap();
    104        }
    105
    106        /**
    107         * Adds a tag with both the 'open' and 'close' flags set to <code>true</code>.
    108         *
    109         * @param tag  the tag name.
    110         */
    111        public void add (final String tag) {
    112            this.safeTags.put(tag, new SafeDescription(true, true));
    113        }
    114
    115        /**
    116         * Adds a tag.
    117         *
    118         * @param tag  the tag name.
    119         * @param open  the 'open' flag.
    120         * @param closed  the 'close' flag.
    121         */
    122        public void add (final String tag, final boolean open, final boolean closed) {
    123            this.safeTags.put(tag, new SafeDescription(open, closed));
    124        }
    125
    126        /**
    127         * Returns <code>true</code> if it is safe to start a new line
    128         * immediately after an open tag.
    129         *
    130         * @param tag  the tag name.
    131         *
    132         * @return A boolean.
    133         */
    134        public boolean isSafeForOpen (final String tag) {
    135            final SafeDescription sd = (SafeDescription) this.safeTags.get(tag);
    136            if (sd == null) {
    137                return false;
    138            }
    139            return sd.isOpen();
    140        }
    141
    142        /**
    143         * Returns <code>true</code> if it is safe to start a new
    144         * line immediately after a close tag.
    145         *
    146         * @param tag  the tag name.
    147         *
    148         * @return A boolean.
    149         */
    150        public boolean isSafeForClose (final String tag) {
    151            final SafeDescription sd = (SafeDescription) this.safeTags.get(tag);
    152            if (sd == null) {
    153                return false;
    154            }
    155            return sd.isClose();
    156        }
    157
    158    }