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 * DescriptionModel.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: DescriptionModel.java,v 1.3 2005/10/18 13:32:37 mungady Exp $
036 *
037 * Changes
038 * -------
039 * 21-Jun-2003 : Initial version (TM);
040 *
041 */
042
043package org.jfree.xml.generator.model;
044
045import java.util.ArrayList;
046import java.util.HashMap;
047
048import org.jfree.util.Log;
049
050/**
051 * A model containing class descriptions.
052 */
053public class DescriptionModel {
054
055    /** The sources. */
056    private ArrayList sources;
057    
058    /** The classes. */
059    private ArrayList classes;
060
061    /** Maps classes to class descriptions. */
062    private HashMap classesMap;
063    
064    /** The mapping model. */
065    private MappingModel mappingModel;
066    
067    /** Model comments. */
068    private Comments modelComments;
069    
070    /** Include comments. */
071    private HashMap includeComments;
072
073    /**
074     * Creates a new class description model.
075     */
076    public DescriptionModel() {
077        this.classes = new ArrayList();
078        this.classesMap = new HashMap();
079        this.mappingModel = new MappingModel();
080        this.sources = new ArrayList();
081        this.includeComments = new HashMap();
082    }
083
084    /**
085     * Adds a class description to the model.
086     * 
087     * @param cd  the class description.
088     */
089    public void addClassDescription(final ClassDescription cd) {
090        this.classesMap.put(cd.getObjectClass(), cd);
091        if (!this.classes.contains(cd)) {
092            this.classes.add(cd);
093        }
094    }
095
096    /**
097     * Removes a class description from the model.
098     * 
099     * @param cd  the class description.
100     */
101    public void removeClassDescription(final ClassDescription cd) {
102        this.classesMap.remove(cd.getObjectClass());
103        this.classes.remove(cd);
104    }
105
106    /**
107     * Returns a class description.
108     * 
109     * @param index  the description index (zero-based).
110     * 
111     * @return a class description.
112     */
113    public ClassDescription get(final int index) {
114        return (ClassDescription) this.classes.get(index);
115    }
116
117    /**
118     * Returns a class description for the given class name.
119     * 
120     * @param key  the class name.
121     * 
122     * @return the class description.
123     */
124    public ClassDescription get(final Class key) {
125        return (ClassDescription) this.classesMap.get(key);
126    }
127
128    /**
129     * Returns the number of classes in the model.
130     * 
131     * @return the number of classes in the model.
132     */
133    public int size() {
134        return this.classes.size();
135    }
136
137    /**
138     * Returns the mapping model.
139     * 
140     * @return the mapping model.
141     */
142    public MappingModel getMappingModel() {
143        return this.mappingModel;
144    }
145
146    /**
147     * Adds a source to the model description.
148     * 
149     * @param source  the source.
150     */
151    public void addSource(final String source) {
152        this.sources.add(source);
153    }
154
155    /**
156     * Returns the sources for the model description.
157     * 
158     * @return The sources.
159     */
160    public String[] getSources() {
161        return (String[]) this.sources.toArray(new String[this.sources.size()]);
162    }
163
164    /**
165     * Removes any class descriptions that are not fully defined.
166     */
167    public void prune() {
168        final ClassDescription[] cds = (ClassDescription[]) this.classes.toArray(new ClassDescription[0]);
169        for (int i = 0; i < cds.length; i++) {
170            if (cds[i].isUndefined()) {
171                removeClassDescription(cds[i]);
172                Log.debug("Pruned: " + cds[i].getName());
173            }
174        }
175    }
176
177    /**
178     * Adds an include comment.
179     * 
180     * @param source  the source.
181     * @param comments  the comments.
182     */
183    public void addIncludeComment(final String source, final Comments comments) {
184        this.includeComments.put(source, comments);
185    }
186
187    /**
188     * Returns the include comment for the specified source.
189     * 
190     * @param source  the source.
191     * 
192     * @return The include comment.
193     */
194    public Comments getIncludeComment(final String source) {
195        return (Comments) this.includeComments.get(source);
196    }
197
198    /**
199     * Returns the model comments.
200     * 
201     * @return The model comments.
202     */
203    public Comments getModelComments() {
204        return this.modelComments;
205    }
206
207    /**
208     * Sets the model comments.
209     * 
210     * @param modelComments  the model comments.
211     */
212    public void setModelComments(final Comments modelComments) {
213        Log.debug ("Model: Comment set: " + modelComments);
214        this.modelComments = modelComments;
215    }
216    
217}