Source for org.jfree.xml.generator.model.DescriptionModel

   1: /* ========================================================================
   2:  * JCommon : a free general purpose class library for the Java(tm) platform
   3:  * ========================================================================
   4:  *
   5:  * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
   6:  * 
   7:  * Project Info:  http://www.jfree.org/jcommon/index.html
   8:  *
   9:  * This library is free software; you can redistribute it and/or modify it 
  10:  * under the terms of the GNU Lesser General Public License as published by 
  11:  * the Free Software Foundation; either version 2.1 of the License, or 
  12:  * (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but 
  15:  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
  16:  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
  17:  * License for more details.
  18:  *
  19:  * You should have received a copy of the GNU Lesser General Public
  20:  * License along with this library; if not, write to the Free Software
  21:  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
  22:  * USA.  
  23:  *
  24:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
  25:  * in the United States and other countries.]
  26:  * 
  27:  * ---------------------
  28:  * DescriptionModel.java
  29:  * ---------------------
  30:  * (C)opyright 2003, 2004, by Thomas Morgner and Contributors.
  31:  *
  32:  * Original Author:  Thomas Morgner;
  33:  * Contributor(s):   David Gilbert (for Object Refinery Limited);
  34:  *
  35:  * $Id: DescriptionModel.java,v 1.3 2005/10/18 13:32:37 mungady Exp $
  36:  *
  37:  * Changes
  38:  * -------
  39:  * 21-Jun-2003 : Initial version (TM);
  40:  *
  41:  */
  42: 
  43: package org.jfree.xml.generator.model;
  44: 
  45: import java.util.ArrayList;
  46: import java.util.HashMap;
  47: 
  48: import org.jfree.util.Log;
  49: 
  50: /**
  51:  * A model containing class descriptions.
  52:  */
  53: public class DescriptionModel {
  54: 
  55:     /** The sources. */
  56:     private ArrayList sources;
  57:     
  58:     /** The classes. */
  59:     private ArrayList classes;
  60: 
  61:     /** Maps classes to class descriptions. */
  62:     private HashMap classesMap;
  63:     
  64:     /** The mapping model. */
  65:     private MappingModel mappingModel;
  66:     
  67:     /** Model comments. */
  68:     private Comments modelComments;
  69:     
  70:     /** Include comments. */
  71:     private HashMap includeComments;
  72: 
  73:     /**
  74:      * Creates a new class description model.
  75:      */
  76:     public DescriptionModel() {
  77:         this.classes = new ArrayList();
  78:         this.classesMap = new HashMap();
  79:         this.mappingModel = new MappingModel();
  80:         this.sources = new ArrayList();
  81:         this.includeComments = new HashMap();
  82:     }
  83: 
  84:     /**
  85:      * Adds a class description to the model.
  86:      * 
  87:      * @param cd  the class description.
  88:      */
  89:     public void addClassDescription(final ClassDescription cd) {
  90:         this.classesMap.put(cd.getObjectClass(), cd);
  91:         if (!this.classes.contains(cd)) {
  92:             this.classes.add(cd);
  93:         }
  94:     }
  95: 
  96:     /**
  97:      * Removes a class description from the model.
  98:      * 
  99:      * @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: }