KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > metadata > parser > JdoClass


1
2 /*
3  * Copyright (c) 1998 - 2005 Versant Corporation
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * Versant Corporation - initial API and implementation
11  */

12 package com.versant.core.metadata.parser;
13
14 import com.versant.core.metadata.MDStatics;
15 import com.versant.core.metadata.MDStaticUtils;
16 import com.versant.core.common.Debug;
17
18 import java.io.PrintStream JavaDoc;
19 import java.util.ArrayList JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.Map JavaDoc;
22
23 /**
24  * Class element from a .jdo file.
25  */

26 public final class JdoClass extends JdoElement implements MDStatics {
27
28     public String JavaDoc name;
29     public int identityType;
30     public String JavaDoc objectIdClass;
31     public boolean requiresExtent;
32     public String JavaDoc pcSuperclass;
33     public JdoPackage parent;
34
35     /** The field and extension elements in order or null if none. */
36     public JdoElement[] elements;
37
38     /** The query elements or null if none. */
39     public ArrayList JavaDoc queries;
40     /**
41      * This can be used to override the need for a objectIdClass for appid instances
42      */

43     public boolean objectIdClasssRequired = true;
44
45     public JdoElement getParent() { return parent; }
46
47     /**
48      * Get information for this element to be used in building up a
49      * context string.
50      * @see #getContext
51      */

52     public String JavaDoc getSubContext() {
53         return "class[" + name + "]";
54     }
55
56     public String JavaDoc toString() {
57         StringBuffer JavaDoc s = new StringBuffer JavaDoc();
58         s.append("class[");
59         s.append(name);
60         s.append("] identityType=");
61         s.append(MDStaticUtils.toIdentityTypeString(identityType));
62         s.append(" objectIdClass=");
63         s.append(objectIdClass);
64         s.append(" requiresExtent=");
65         s.append(requiresExtent);
66         s.append(" pcSuperclass=");
67         s.append(pcSuperclass);
68         return s.toString();
69     }
70
71     public void dump() {
72         dump(Debug.OUT, "");
73     }
74
75     public void dump(PrintStream JavaDoc out, String JavaDoc indent) {
76         out.println(indent + this);
77         if (elements != null) {
78             for (int i = 0; i < elements.length; i++) {
79                 Object JavaDoc o = elements[i];
80                 if (o instanceof JdoField) {
81                     ((JdoField)o).dump(out, indent + " ");
82                 } else if (o instanceof JdoExtension) {
83                     ((JdoExtension)o).dump(out, indent + " ");
84                 } else if (o instanceof JdoQuery) {
85                     ((JdoQuery) o).dump(out, indent + " ");
86                 } else {
87                     out.println("unknown " + o);
88                 }
89             }
90         }
91         if (queries != null) {
92             for (Iterator JavaDoc i = queries.iterator(); i.hasNext(); ) {
93                 ((JdoQuery)i.next()).dump(out, indent + " ");
94             }
95         }
96     }
97
98     /**
99      * Get the fully qualified name of this class.
100      */

101     public String JavaDoc getQName() {
102         return getQName(name);
103     }
104
105     private String JavaDoc getQName(String JavaDoc n) {
106         if (n == null) return null;
107         int i = n.indexOf('.');
108         if (i >= 0 || parent.name.length() == 0) return n;
109         return parent.name + '.' + n;
110     }
111
112     /**
113      * Get the fully qualified name of our PC super class or null if none.
114      */

115     public String JavaDoc getPCSuperClassQName() {
116         return getQName(pcSuperclass);
117     }
118
119     /**
120      * Get the fully qualified name of our objectid-class or null if none.
121      */

122     public String JavaDoc getObjectIdClassQName() {
123         return getQName(objectIdClass);
124     }
125
126     /**
127      * Does this class has a JDBC_KEY_GENERATOR set on it?
128      */

129     public boolean hasKeyGen(){
130         if (elements != null) {
131             JdoExtension ext = null;
132             for (int i = 0; i < elements.length; i++) {
133                 Object JavaDoc o = elements[i];
134                 if (o instanceof JdoExtension) {
135                     ext = (JdoExtension)o;
136                     if (ext.key == JdoExtension.JDBC_KEY_GENERATOR) {
137                         return true;
138                     } else if (ext.contains(JdoExtension.JDBC_KEY_GENERATOR)){
139                         return true;
140                     }
141
142                 }
143             }
144         }
145         return false;
146     }
147
148     /**
149      * Add a JdoQuery to this class. This is called when queries declared
150      * in a separate .jdoquery resource file are moved to the main JdoClass
151      * definition and when queries are originally parsed. The q.parent field
152      * must have already been set.
153      */

154     public void addJdoQuery(JdoQuery q) {
155         // Note that this code must not change q.parent to reference this
156
// class. The parent link is used to construct parsing error messages
157
// and so must lead back to the original JdoRoot for the resource the
158
// query was declared in.
159
if (queries == null) queries = new ArrayList JavaDoc();
160         queries.add(q);
161     }
162
163     public int getInheritance(Map JavaDoc enumMap) {
164         if (elements != null) {
165             JdoExtension ext = null;
166             for (int i = 0; i < elements.length; i++) {
167                 Object JavaDoc o = elements[i];
168                 if (o instanceof JdoExtension) {
169                     ext = (JdoExtension)o;
170                     if (ext.key == JdoExtension.JDBC_INHERITANCE) {
171                         if (ext.value == null) return -1;
172                         return ext.getEnum(enumMap);
173                     }
174                 }
175             }
176         }
177         return -1;
178     }
179
180     public void addElement(JdoElement jdoElement) {
181         if (elements == null) {
182             elements = new JdoElement[] {jdoElement,};
183         } else {
184             JdoElement[] tmp = new JdoElement[elements.length + 1];
185             System.arraycopy(elements, 0, tmp, 0, elements.length);
186             tmp[elements.length] = jdoElement;
187             elements = tmp;
188         }
189
190     }
191 }
192
Popular Tags