summaryrefslogtreecommitdiffstats
path: root/java/rasj/RasType.java
blob: 1e3afdaed1897b496a1cc5f34a4d7f8344798769 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
package rasj;

import rasj.*;
import rasj.global.*;
import java.util.*;


/*
* This file is part of rasdaman community.
*
* Rasdaman community is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Rasdaman community is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with rasdaman community.  If not, see <http://www.gnu.org/licenses/>.
*
* Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009 Peter Baumann /
rasdaman GmbH.
*
* For more information please see <http://www.rasdaman.org>
* or contact Peter Baumann via <baumann@rasdaman.com>.
*/
/** ***********************************************************
 * <pre>
 *
 * PURPOSE:
 * This class the superclass for all types in the ODMG conformant
 * representation of the RasDaMan type system.
 * @version $Revision: 1.10 $
 *
 *
 *
 * COMMENTS:
 *
 * </pre>
 *********************************************************** */


public class RasType
{
    static final String rcsid = "@(#)Package rasj, class RasType: $Header: /home/rasdev/CVS-repository/rasdaman/java/rasj/RasType.java,v 1.10 2003/12/10 21:04:23 rasdev Exp $";

    protected String typeName;
    protected int typeID;

    /**
     * Default constructor.
     **/
    public RasType()
    {
	typeName = "";
	typeID = 0;
    }

    /**
     * Constructor getting the name of the new type.
     * @param newTypeName the name of the new type
     **/
    public RasType(String newTypeName)
    {
	typeName = newTypeName;
	typeID = 0;
    }

    /**
     * Retrieves the id of this type.
     * @return the type id
     **/
    public int getTypeID()
    {
	return typeID;
    }

    /**
     * Retrieves the name of this type.
     * @return the type name
     **/
    public String  getName()
    {
	return typeName;
    }

    /**
     * Checks if this type is a primitive or structured type.
     * @return true if this is a structured type, otherwise false
     **/
    public boolean isStructType()
    {
	return false;
    }

    /**
     * Checks if this type is a base type (primitive type or structure type).
     * @return true for a primitive or structured type, otherwise false
     **/
    public boolean isBaseType()
    {
	return false;
    }


    /**
     * Builds the type schema from a string representation.
     * @param typeString the string representation of the type
     **/
    public static RasType getAnyType(String typeString)
    {
	StringTokenizer strTok = new StringTokenizer(typeString, "{}[]<>,: ");
	String currentStr = "";
	RasType returnValue = null;
	if(strTok.hasMoreTokens())
	    {
		currentStr = strTok.nextToken();
		if(currentStr.equals("set"))
		    {
	              //System.out.println("getSet");
		      returnValue = getSet(strTok.nextToken(""));
		    }
		else if(currentStr.equals("marray"))
		    {
                      //System.out.println("getMArray");
		      returnValue = getMArrayType(strTok.nextToken(""));
		    }
		else
		    {
		      //System.out.println("getType");
		      returnValue = getType(typeString);
		    }
	    }
	return returnValue;
    }

    // converts array of cells from NT byte order to Unix byte order.
    //public void convertToLittleEndian(String cells, int noCells)
    //{
    //}

    // converts array of cells from Unix byte order to NT byte order.
    //public void convertToBigEndian(String cells, int noCells)
    //{
    //}

    private static RasType getSet(String setStr)
    {
	StringTokenizer setTok = new StringTokenizer(setStr, "{}[]<>,: ");
	String currentStr = "";
	RasType returnValue = null;
	RasType elementType = null;
	if(setTok.hasMoreTokens())
	    {
		currentStr = setTok.nextToken();
		//Fehler falls kein <!
		if(currentStr.equals("marray"))
		    elementType = getMArrayType(setTok.nextToken(""));
		else elementType = getType(setStr);
	    }

	returnValue = new RasCollectionType(elementType);

	return returnValue;
    }

    private static RasType getType(String typeStr)
    {
	StringTokenizer typeTok = new StringTokenizer(typeStr, "{}[]<>,: ");
	String currentStr = "";
	RasType returnValue = null;
	if(typeTok.hasMoreTokens())
	    {
		currentStr = typeTok.nextToken();
		if(currentStr.equals("struct"))
		    {
			//System.out.println("getStructureType");
			returnValue = getStructureType(typeTok.nextToken(""));
		    }
		else if(currentStr.equals("interval"))
		    {
			returnValue = getSIntervalType(typeTok.nextToken(""));
                        returnValue.typeID = RasGlobalDefs.RAS_SINTERVAL;
		    }
		else if(currentStr.equals("minterval"))
		    {
			returnValue = getMIntervalType(typeTok.nextToken(""));
                        returnValue.typeID = RasGlobalDefs.RAS_MINTERVAL;
		    }
		else if(currentStr.equals("point"))
		    {
			returnValue = getPointType(typeTok.nextToken(""));
                        returnValue.typeID = RasGlobalDefs.RAS_POINT;
		    }
		else if(currentStr.equals("oid"))
		    {
			returnValue = getOIDType(typeTok.nextToken(""));
                        returnValue.typeID = RasGlobalDefs.RAS_OID;
		    }
		else
		    {
			//System.out.println("getPrimitiveType");
			returnValue = getPrimitiveType(typeStr);
		    }
	    }
	return returnValue;
    }

    private static RasMArrayType getMArrayType(String mArrayStr)
    {
	//StringTokenizer mArrayTok = new StringTokenizer(mArrayStr, "{}[]<>,: ");
        //System.out.println("enter getMArrayType" + mArrayStr);
	RasMArrayType returnValue = null;
	RasBaseType baseType = null;
	//Fehler falls kein <!
	// get base type (structure or primitive type)
	baseType = getBaseType(mArrayStr);
	//System.out.println("base type: " + baseType);
	returnValue = new RasMArrayType(baseType);

	return returnValue;
    }


    private static RasBaseType getBaseType(String baseStr)
    {
	StringTokenizer baseTok = new StringTokenizer(baseStr, "{}[]<>,: ");
	String currentStr = "";
        //System.out.println(baseStr);
	RasBaseType returnValue = null;
	if(baseTok.hasMoreTokens())
	    {
		currentStr = baseTok.nextToken();
		if(currentStr.equals("struct"))
		    {
			returnValue = getStructureType(baseTok.nextToken(""));
		    }
		else
		    {
			returnValue = getPrimitiveType(baseStr);
			//System.out.println("getPrimitiveType");
		    }
	    }

	return returnValue;
    }


    private static RasPrimitiveType getPrimitiveType(String primStr) throws RasTypeUnknownException
    {
	StringTokenizer primTok = new StringTokenizer(primStr, "{}[]<>,: ");
	String currentStr = "";
	RasPrimitiveType returnValue = null;
	if(primTok.hasMoreTokens())
	    {
		currentStr = primTok.nextToken();
		if(currentStr.equals("char"))
		    returnValue = new RasPrimitiveType("RAS_CHAR", RasGlobalDefs.RAS_CHAR);
		else if(currentStr.equals("octet"))
		    returnValue = new RasPrimitiveType("RAS_BYTE", RasGlobalDefs.RAS_BYTE);
		else if(currentStr.equals("short"))
		    returnValue = new RasPrimitiveType("RAS_SHORT", RasGlobalDefs.RAS_SHORT);
		else if(currentStr.equals("ushort"))
		    returnValue = new RasPrimitiveType("RAS_USHORT", RasGlobalDefs.RAS_USHORT);
		else if(currentStr.equals("long"))
		    returnValue = new RasPrimitiveType("RAS_LONG", RasGlobalDefs.RAS_LONG);
		else if(currentStr.equals("ulong"))
		    returnValue = new RasPrimitiveType("RAS_ULONG", RasGlobalDefs.RAS_ULONG);
		else if(currentStr.equals("bool"))
		    returnValue = new RasPrimitiveType("RAS_BOOLEAN", RasGlobalDefs.RAS_BOOLEAN);
		else if(currentStr.equals("float"))
		    returnValue = new RasPrimitiveType("RAS_FLOAT", RasGlobalDefs.RAS_FLOAT);
		else if(currentStr.equals("double"))
		    returnValue = new RasPrimitiveType("RAS_DOUBLE", RasGlobalDefs.RAS_DOUBLE);
		else
		    throw new RasTypeUnknownException(currentStr);
	    }

	return returnValue;
    }


    private static RasStructureType getStructureType(String structStr) throws RasTypeUnknownException
    {
	StringTokenizer structTok = new StringTokenizer(structStr, "[]<>,: ");
	RasStructureType returnValue = null;
	String structName = "";
	RasBaseType[] baseType = new RasBaseType[structTok.countTokens()/2];
	String[] attributeName = new String[structTok.countTokens()/2];
	//System.out.println("enter getStructureType");
	// get struct name
	if(structTok.hasMoreTokens())
		    {
			structName = structTok.nextToken();
                        if(structName.equals("{"))
                          structName="";
                        else
                          structTok.nextToken();
			//System.out.println("StructName:" + structName);
		    }
	else
	    {
		// no struct name
		throw new RasTypeUnknownException("");
	    }

	//Fehler falls kein { so lange bis } dazwischen ,!!!
	for(int i=0; i < attributeName.length; i++)
	    {
		// get type
                String more = structTok.nextToken();
                if(more.equals("}"))
                  break;
                else
                {
		  baseType[i] = getBaseType(more);
		  //System.out.println("BaseType:"+ i + baseType[i]);
		  // get attribut name
		  if(structTok.hasMoreTokens())
		    {
			attributeName[i] = structTok.nextToken();
			//System.out.println("Attribut:"+ i + attributeName[i]);
		    }
		  else
		    // no attribut name
		    throw new RasTypeUnknownException("");
                }
	    }

	returnValue = new RasStructureType(structName, baseType, attributeName);
	return returnValue;
    }

    private static RasSIntervalType getSIntervalType(String sintStr)
    {
	return new RasSIntervalType();
    }

    private static RasMIntervalType getMIntervalType(String mintStr)
    {
	return new RasMIntervalType();
    }

    private static RasPointType getPointType(String pointStr)
    {
	return new RasPointType();
    }

    private static RasOIDType getOIDType(String oidStr)
    {
	return new RasOIDType();
    }

    /**
     * @return the string represntation of this type
     **/
    public String toString()
    {
	return "typeName: " + typeName + "\n typeID:  " + typeID +"\n ";
    }

}