001: /**********************************************************************
002: Copyright (c) 2004 Andy Jefferson and others. All rights reserved.
003: Licensed under the Apache License, Version 2.0 (the "License");
004: you may not use this file except in compliance with the License.
005: You may obtain a copy of the License at
006:
007: http://www.apache.org/licenses/LICENSE-2.0
008:
009: Unless required by applicable law or agreed to in writing, software
010: distributed under the License is distributed on an "AS IS" BASIS,
011: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: See the License for the specific language governing permissions and
013: limitations under the License.
014:
015: Contributors:
016: Kikuchi Kousuke - org.jpox.enhancer.conf.JDOConfigPackage
017: ...
018: **********************************************************************/package org.jpox.metadata;
019:
020: import java.util.ArrayList;
021: import java.util.Collection;
022: import java.util.HashSet;
023: import java.util.Iterator;
024: import java.util.List;
025:
026: import org.jpox.exceptions.JPOXUserException;
027: import org.jpox.util.StringUtils;
028:
029: /**
030: * Representation of the Meta-Data for a package.
031: *
032: * @since 1.1
033: * @version $Revision: 1.15 $
034: */
035: public class PackageMetaData extends MetaData {
036: /** List of interfaces (uses List to retain positioning). */
037: protected List interfaces = null;
038:
039: /** List of classes (uses List to retain positioning). */
040: protected List classes = null;
041:
042: /** Sequence generators. */
043: protected Collection sequences = null;
044:
045: /** Table generators. */
046: protected Collection tableGenerators = null;
047:
048: /** Package name */
049: protected final String name;
050:
051: /** Catalog name for all classes in this package */
052: protected String catalog;
053:
054: /** Schema name for all classes in this package */
055: protected String schema;
056:
057: /**
058: * Constructor.
059: * @param parent The FileMetaData owner
060: * @param name Name of package
061: * @param catalog Catalog name for all classes in the package
062: * @param schema Schema name for all classes in the package
063: */
064: public PackageMetaData(FileMetaData parent, final String name,
065: final String catalog, final String schema) {
066: super (parent);
067:
068: this .name = name;
069: if (this .name == null) {
070: throw new JPOXUserException(LOCALISER.msg("044041", "name",
071: getFileMetaData().getFilename(), "package"));
072: }
073:
074: this .catalog = (StringUtils.isWhitespace(catalog) ? null
075: : catalog);
076: this .schema = (StringUtils.isWhitespace(schema) ? null : schema);
077: if (this .catalog == null && parent.getCatalog() != null) {
078: // Nothing specified for this package, but file has a value
079: this .catalog = parent.getCatalog();
080: }
081: if (this .schema == null && parent.getSchema() != null) {
082: // Nothing specified for this package, but file has a value
083: this .schema = parent.getSchema();
084: }
085: }
086:
087: // -------------------------------- Accessors ------------------------------
088:
089: /**
090: * Accessor for the parent FileMetaData.
091: * @return File MetaData.
092: */
093: public FileMetaData getFileMetaData() {
094: if (parent != null) {
095: return (FileMetaData) parent;
096: }
097: return null;
098: }
099:
100: /**
101: * Accessor for the name of the package
102: * @return package name
103: */
104: public String getName() {
105: return name;
106: }
107:
108: /**
109: * Accessor for the catalog name for all classes in this package
110: * @return Catalog name to use.
111: */
112: public String getCatalog() {
113: return catalog;
114: }
115:
116: /**
117: * Accessor for the schema name for all classes in this package
118: * @return Schema name to use.
119: */
120: public String getSchema() {
121: return schema;
122: }
123:
124: /**
125: * Accessor for the number of interfaces.
126: * @return Number of interfaces.
127: */
128: public int getNoOfInterfaces() {
129: return interfaces != null ? interfaces.size() : 0;
130: }
131:
132: /**
133: * Accessor for the Meta-Data of a interface in this package.
134: * @param i interface index
135: * @return Meta-Data for the interface
136: */
137: public InterfaceMetaData getInterface(int i) {
138: return (InterfaceMetaData) interfaces.get(i);
139: }
140:
141: /**
142: * Accessor for the Meta-Data of an interface with the specified name.
143: * @param name the name of the interface
144: * @return Meta-Data for the interface
145: */
146: public InterfaceMetaData getInterface(String name) {
147: Iterator iter = interfaces.iterator();
148: while (iter.hasNext()) {
149: InterfaceMetaData imd = (InterfaceMetaData) iter.next();
150: if (imd.getName().equals(name)) {
151: return imd;
152: }
153: }
154: return null;
155: }
156:
157: /**
158: * Accessor for the number of classes.
159: * @return Number of classes.
160: */
161: public int getNoOfClasses() {
162: return classes != null ? classes.size() : 0;
163: }
164:
165: /**
166: * Accessor for the Meta-Data of a class in this package.
167: * @param i class index
168: * @return Meta-Data for the class
169: */
170: public ClassMetaData getClass(int i) {
171: return (ClassMetaData) classes.get(i);
172: }
173:
174: /**
175: * Accessor for the Meta-Data of a class with the specified name.
176: * @param name the name of the class
177: * @return Meta-Data for the class.
178: */
179: public ClassMetaData getClass(String name) {
180: Iterator iter = classes.iterator();
181: while (iter.hasNext()) {
182: ClassMetaData cmd = (ClassMetaData) iter.next();
183: if (cmd.getName().equals(name)) {
184: return cmd;
185: }
186: }
187: return null;
188: }
189:
190: /**
191: * Accessor for the number of sequences.
192: * @return Number of sequences.
193: */
194: public int getNoOfSequences() {
195: return sequences != null ? sequences.size() : 0;
196: }
197:
198: /**
199: * Accessor for the Meta-Data for the sequences in this package.
200: * @return Meta-Data for the sequences
201: */
202: public SequenceMetaData[] getSequences() {
203: return (sequences == null ? null
204: : (SequenceMetaData[]) sequences
205: .toArray(new SequenceMetaData[sequences.size()]));
206: }
207:
208: /**
209: * Accessor for the Meta-Data of an sequence with the specified name.
210: * @param name the name of the sequence
211: * @return Meta-Data for the sequence
212: */
213: public SequenceMetaData getSequence(String name) {
214: Iterator iter = sequences.iterator();
215: while (iter.hasNext()) {
216: SequenceMetaData seqmd = (SequenceMetaData) iter.next();
217: if (seqmd.getName().equals(name)) {
218: return seqmd;
219: }
220: }
221: return null;
222: }
223:
224: /**
225: * Accessor for the number of table generators.
226: * @return Number of table generators.
227: */
228: public int getNoOfTableGenerators() {
229: return tableGenerators != null ? tableGenerators.size() : 0;
230: }
231:
232: /**
233: * Accessor for the Meta-Data for the table generators in this package.
234: * @return Meta-Data for the table generators
235: */
236: public TableGeneratorMetaData[] getTableGenerators() {
237: return (tableGenerators == null ? null
238: : (TableGeneratorMetaData[]) tableGenerators
239: .toArray(new TableGeneratorMetaData[tableGenerators
240: .size()]));
241: }
242:
243: /**
244: * Accessor for the Meta-Data of a table generator with the specified name.
245: * @param name the name of the table generator
246: * @return Meta-Data for the table generator
247: */
248: public TableGeneratorMetaData getTableGenerator(String name) {
249: Iterator iter = tableGenerators.iterator();
250: while (iter.hasNext()) {
251: TableGeneratorMetaData tgmd = (TableGeneratorMetaData) iter
252: .next();
253: if (tgmd.getName().equals(name)) {
254: return tgmd;
255: }
256: }
257: return null;
258: }
259:
260: // -------------------------------- Mutators -------------------------------
261:
262: /**
263: * Method to add a class Meta-Data to the package.
264: * @param cmd Meta-Data for the class
265: */
266: public void addClass(ClassMetaData cmd) {
267: if (cmd == null) {
268: return;
269: }
270: if (classes == null) {
271: classes = new ArrayList();
272: }
273: classes.add(cmd);
274: }
275:
276: /**
277: * Method to add a interface Meta-Data to the package.
278: * @param imd Meta-Data for the interface
279: */
280: public void addInterface(InterfaceMetaData imd) {
281: if (imd == null) {
282: return;
283: }
284: if (interfaces == null) {
285: interfaces = new ArrayList();
286: }
287: interfaces.add(imd);
288: }
289:
290: /**
291: * Method to add a sequence Meta-Data to the package.
292: * @param seqmd Meta-Data for the sequence
293: */
294: public void addSequence(SequenceMetaData seqmd) {
295: if (seqmd == null) {
296: return;
297: }
298: if (sequences == null) {
299: sequences = new HashSet();
300: }
301: sequences.add(seqmd);
302: seqmd.parent = this ;
303: }
304:
305: /**
306: * Method to add a table generator Meta-Data to the package.
307: * @param tgmd Meta-Data for the table generator
308: */
309: public void addTableGenerator(TableGeneratorMetaData tgmd) {
310: if (tgmd == null) {
311: return;
312: }
313: if (tableGenerators == null) {
314: tableGenerators = new HashSet();
315: }
316: tableGenerators.add(tgmd);
317: tgmd.parent = this ;
318: }
319:
320: // ------------------------------ Utilities --------------------------------
321:
322: /**
323: * Returns a string representation of the object.
324: * @param prefix prefix string
325: * @param indent indent string
326: * @return a string representation of the object.
327: */
328: public String toString(String prefix, String indent) {
329: StringBuffer sb = new StringBuffer();
330: sb.append(prefix).append("<package name=\"" + name + "\"");
331: if (catalog != null) {
332: sb.append(" catalog=\"" + catalog + "\"");
333: }
334: if (schema != null) {
335: sb.append(" schema=\"" + schema + "\"");
336: }
337: sb.append(">\n");
338:
339: // Add interfaces
340: if (interfaces != null) {
341: Iterator int_iter = interfaces.iterator();
342: while (int_iter.hasNext()) {
343: sb.append(((InterfaceMetaData) int_iter.next())
344: .toString(prefix + indent, indent));
345: }
346: }
347:
348: // Add classes
349: if (classes != null) {
350: Iterator cls_iter = classes.iterator();
351: while (cls_iter.hasNext()) {
352: sb.append(((ClassMetaData) cls_iter.next()).toString(
353: prefix + indent, indent));
354: }
355: }
356:
357: // Add sequences
358: if (sequences != null) {
359: Iterator seq_iter = sequences.iterator();
360: while (seq_iter.hasNext()) {
361: sb.append(((SequenceMetaData) seq_iter.next())
362: .toString(prefix + indent, indent));
363: }
364: }
365:
366: // Add extensions
367: sb.append(super .toString(prefix + indent, indent));
368:
369: sb.append(prefix).append("</package>\n");
370: return sb.toString();
371: }
372: }
|