001: /*
002: This source file is part of Smyle, a database library.
003: For up-to-date information, see http://www.drjava.de/smyle
004: Copyright (C) 2001 Stefan Reich (doc@drjava.de)
005:
006: This library is free software; you can redistribute it and/or
007: modify it under the terms of the GNU Lesser General Public
008: License as published by the Free Software Foundation; either
009: version 2.1 of the License, or (at your option) any later version.
010:
011: This library is distributed in the hope that it will be useful,
012: but WITHOUT ANY WARRANTY; without even the implied warranty of
013: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: Lesser General Public License for more details.
015:
016: You should have received a copy of the GNU Lesser General Public
017: License along with this library; if not, write to the Free Software
018: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019:
020: For full license text, see doc/license/lgpl.txt in this distribution
021: */
022:
023: package drjava.smyle.meta;
024:
025: import java.util.*;
026: import org.artsProject.mcop.*;
027: import org.artsProject.mcop.core.*;
028: import java.lang.Object;
029: import drjava.smyle.*;
030:
031: /** an extension of jMCOP's TypeInfo */
032: public class StructInfo<T extends Struct<T>> extends TypeInfo<T>
033: implements Marshallable {
034: public StructInfo[] fieldTypes;
035:
036: public StructInfo(TypeDef typeDef, Demarshaller<T> demarshaller,
037: Class structClass, StructInfo[] fieldTypes) {
038: super (typeDef, demarshaller, structClass);
039: this .fieldTypes = fieldTypes;
040: }
041:
042: public static final Demarshaller<StructInfo> DEMARSHALLER = new Demarshaller<StructInfo>() {
043: public StructInfo read(Buffer buffer) {
044: return new StructInfo(buffer);
045: }
046: };
047:
048: public int findTypeComponent(String name) {
049: for (int i = 0; i < typeDef.contents.size(); i++) {
050: if (typeDef.contents.get(i).name.equals(name))
051: return i;
052: }
053: return -1;
054: }
055:
056: public StructInfo(Buffer b) {
057: super (new TypeDef(b), null, null);
058: }
059:
060: public void marshal(Buffer b) {
061: typeDef.marshal(b);
062: }
063:
064: private static StructInfo findInCatalog(String name,
065: ArrayList<StructInfo> catalog) {
066: if (name.startsWith("*"))
067: name = name.substring(1);
068: for (int i = 0; i < catalog.size(); i++)
069: if (catalog.get(i).typeDef.name.equals(name))
070: return catalog.get(i);
071: return null;
072: }
073:
074: private static void fillFieldTypes(StructInfo info,
075: ArrayList<StructInfo> catalog) {
076: int n = info.typeDef.contents.size();
077: info.fieldTypes = new StructInfo[n];
078: for (int i = 0; i < n; i++) {
079: TypeComponent c = info.typeDef.contents.get(i);
080: StructInfo s = findInCatalog(c.type, catalog);
081: //System.out.println("findInCatalog "+c.type+" => "+s);
082: info.fieldTypes[i] = s;
083: if (s != null && s.fieldTypes == null)
084: fillFieldTypes(s, catalog);
085: }
086: }
087:
088: public static StructInfo readRecursive(Buffer b) {
089: ArrayList<StructInfo> catalog = new ArrayList<StructInfo>();
090: MCOP.readSeq(b, catalog, StructInfo.DEMARSHALLER);
091: //System.out.println("Read catalog: "+catalog.size());
092:
093: fillFieldTypes(catalog.get(0), catalog);
094: return catalog.get(0);
095: }
096:
097: private void fillCatalog(ArrayList<StructInfo> catalog) {
098: if (findInCatalog(typeDef.name, catalog) == null) {
099: catalog.add(this );
100: if (fieldTypes != null)
101: for (int i = 0; i < fieldTypes.length; i++)
102: if (fieldTypes[i] != null
103: && findInCatalog(
104: fieldTypes[i].typeDef.name, catalog) == null)
105: fieldTypes[i].fillCatalog(catalog);
106: }
107: }
108:
109: public void writeRecursive(Buffer b) {
110: ArrayList<StructInfo> catalog = new ArrayList<StructInfo>();
111: //System.out.println("typeDef: "+typeDef+", fieldTypes: "+fieldTypes);
112: fillCatalog(catalog);
113: //System.out.println("Writing catalog: "+catalog.size());
114: MCOP.writeSeq(b, catalog);
115: }
116:
117: public boolean equals(Object o) {
118: if (!(o instanceof StructInfo)) {
119: //System.out.println("Wrong class: "+(o != null ? o.getClass() : null));
120: return false;
121: }
122: if (o == this )
123: return true;
124: StructInfo s = (StructInfo) o;
125: if ((typeDef != null) != (s.typeDef != null)) {
126: //System.out.println("typeDef null/not-null");
127: return false;
128: }
129: if (typeDef != null) {
130: if (!typeDef.equals(s.typeDef)) {
131: //System.out.println("Different typeDefs: "+typeDef+" != "+s.typeDef);
132: return false;
133: }
134: for (int i = 0; i < fieldTypes.length; i++)
135: if (fieldTypes[i] != null && s.fieldTypes[i] != null
136: && !fieldTypes[i].equals(s.fieldTypes[i])) {
137: //System.out.println("Field type "+i+": "+fieldTypes[i]+" != "+s.fieldTypes[i]);
138: return false;
139: }
140: }
141: //System.out.println("StructInfos are equal");
142: return true;
143: }
144: }
|