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:
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 model containing class descriptions.
052: */
053: public 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
162: .size()]);
163: }
164:
165: /**
166: * Removes any class descriptions that are not fully defined.
167: */
168: public void prune() {
169: final ClassDescription[] cds = (ClassDescription[]) this .classes
170: .toArray(new ClassDescription[0]);
171: for (int i = 0; i < cds.length; i++) {
172: if (cds[i].isUndefined()) {
173: removeClassDescription(cds[i]);
174: Log.debug("Pruned: " + cds[i].getName());
175: }
176: }
177: }
178:
179: /**
180: * Adds an include comment.
181: *
182: * @param source the source.
183: * @param comments the comments.
184: */
185: public void addIncludeComment(final String source,
186: final Comments comments) {
187: this .includeComments.put(source, comments);
188: }
189:
190: /**
191: * Returns the include comment for the specified source.
192: *
193: * @param source the source.
194: *
195: * @return The include comment.
196: */
197: public Comments getIncludeComment(final String source) {
198: return (Comments) this .includeComments.get(source);
199: }
200:
201: /**
202: * Returns the model comments.
203: *
204: * @return The model comments.
205: */
206: public Comments getModelComments() {
207: return this .modelComments;
208: }
209:
210: /**
211: * Sets the model comments.
212: *
213: * @param modelComments the model comments.
214: */
215: public void setModelComments(final Comments modelComments) {
216: Log.debug("Model: Comment set: " + modelComments);
217: this.modelComments = modelComments;
218: }
219:
220: }
|