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     * MappingModel.java
029     * -----------------
030     * (C)opyright 2003, 2004, by Thomas Morgner and Contributors.
031     *
032     * Original Author:  Thomas Morgner;
033     * Contributor(s):   David Gilbert (for Object Refinery Limited);
034     *
035     * $Id: MappingModel.java,v 1.3 2005/10/18 13:32:37 mungady Exp $
036     *
037     * Changes 
038     * -------
039     * 20-Nov-2003 : Initial version
040     *  
041     */
042    
043    package org.jfree.xml.generator.model;
044    
045    import java.util.ArrayList;
046    import java.util.HashMap;
047    
048    import org.jfree.util.Log;
049    
050    /**
051     * A mapping model.
052     */
053    public class MappingModel {
054        
055        /** Mapping infos. */
056        private HashMap mappingInfos;
057        
058        /** The manual mappings. */
059        private ArrayList manualMappings;
060        
061        /** The multiplex mappings. */
062        private ArrayList multiplexMappings;
063    
064        /**
065         * Creates a new instance.
066         */
067        public MappingModel() {
068            this.mappingInfos = new HashMap();
069            this.manualMappings = new ArrayList();
070            this.multiplexMappings = new ArrayList();
071        }
072    
073        /**
074         * Returns the multiplex mapping info.
075         * 
076         * @return The multiplex mapping info.
077         */
078        public MultiplexMappingInfo[] getMultiplexMapping() {
079            return (MultiplexMappingInfo[]) this.multiplexMappings.toArray(
080                new MultiplexMappingInfo[this.multiplexMappings.size()]
081            );
082        }
083    
084        /**
085         * Returns the manual mapping info.
086         * 
087         * @return The manual mapping info.
088         */
089        public ManualMappingInfo[] getManualMapping() {
090            return (ManualMappingInfo[]) this.manualMappings.toArray(
091                new ManualMappingInfo[this.manualMappings.size()]
092            );
093        }
094    
095        /**
096         * Adds a manual mapping.
097         * 
098         * @param mappingInfo  the mapping.
099         */
100        public void addManualMapping(final ManualMappingInfo mappingInfo) {
101            if (!this.mappingInfos.containsKey(mappingInfo.getBaseClass())) {
102                this.manualMappings.add(mappingInfo);
103                this.mappingInfos.put(mappingInfo.getBaseClass(), mappingInfo);
104            }
105            else {
106                final Object o = this.mappingInfos.get(mappingInfo.getBaseClass());
107                if (o instanceof ManualMappingInfo) {
108                    Log.info ("Duplicate manual mapping: " + mappingInfo.getBaseClass());
109                }
110                else {
111                    throw new IllegalArgumentException
112                        ("This mapping is already a multiplex mapping.");
113                }
114            }
115        }
116    
117        /**
118         * Adds a multiplex mapping.
119         * 
120         * @param mappingInfo  the mapping.
121         */
122        public void addMultiplexMapping(final MultiplexMappingInfo mappingInfo) {
123            if (!this.mappingInfos.containsKey(mappingInfo.getBaseClass())) {
124                this.multiplexMappings.add(mappingInfo);
125                this.mappingInfos.put(mappingInfo.getBaseClass(), mappingInfo);
126            }
127            else {
128                final Object o = this.mappingInfos.get(mappingInfo.getBaseClass());
129                if (o instanceof ManualMappingInfo) {
130                    throw new IllegalArgumentException
131                        ("This mapping is already a manual mapping.");
132                }
133                else {
134                    Log.info(
135                        "Duplicate Multiplex mapping: " + mappingInfo.getBaseClass(), new Exception()
136                    );
137                }
138            }
139    
140        }
141    
142        /**
143         * Returns a multiplex mapping for the specified class.
144         * 
145         * @param baseClass  the base class.
146         * 
147         * @return The mapping.
148         */
149        public MultiplexMappingInfo lookupMultiplexMapping(final Class baseClass) {
150            final Object o = this.mappingInfos.get(baseClass);
151            if (o instanceof MultiplexMappingInfo) {
152                return (MultiplexMappingInfo) o;
153            }
154            return null;
155        }
156    
157    }