001: // kelondroColumn.java
002: // (C) 2006 by Michael Peter Christen; mc@anomic.de, Frankfurt a. M., Germany
003: // first published 24.05.2006 on http://www.anomic.de
004: //
005: // This is a part of the kelondro database,
006: // which is a part of YaCy, a peer-to-peer based web search engine
007: //
008: // $LastChangedDate: 2006-04-02 22:40:07 +0200 (So, 02 Apr 2006) $
009: // $LastChangedRevision: 1986 $
010: // $LastChangedBy: orbiter $
011: //
012: // LICENSE
013: //
014: // This program is free software; you can redistribute it and/or modify
015: // it under the terms of the GNU General Public License as published by
016: // the Free Software Foundation; either version 2 of the License, or
017: // (at your option) any later version.
018: //
019: // This program is distributed in the hope that it will be useful,
020: // but WITHOUT ANY WARRANTY; without even the implied warranty of
021: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
022: // GNU General Public License for more details.
023: //
024: // You should have received a copy of the GNU General Public License
025: // along with this program; if not, write to the Free Software
026: // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
027:
028: package de.anomic.kelondro;
029:
030: public class kelondroColumn {
031:
032: public static final int celltype_undefined = 0;
033: public static final int celltype_boolean = 1;
034: public static final int celltype_binary = 2;
035: public static final int celltype_string = 3;
036: public static final int celltype_cardinal = 4;
037: public static final int celltype_bitfield = 5;
038:
039: public static final int encoder_none = 0;
040: public static final int encoder_b64e = 1;
041: public static final int encoder_b256 = 2;
042: public static final int encoder_bytes = 3;
043:
044: public int celltype, cellwidth, encoder;
045: public String nickname, description;
046:
047: public kelondroColumn(String nickname, int celltype, int encoder,
048: int cellwidth, String description) {
049: this .celltype = celltype;
050: this .cellwidth = cellwidth;
051: this .encoder = encoder;
052: this .nickname = nickname;
053: this .description = description;
054: }
055:
056: public kelondroColumn(String celldef) {
057: // define column with column syntax
058: // example: <UDate-3>
059:
060: // cut quotes etc.
061: celldef = celldef.trim();
062: if (celldef.startsWith("<"))
063: celldef = celldef.substring(1);
064: if (celldef.endsWith(">"))
065: celldef = celldef.substring(0, celldef.length() - 1);
066:
067: // parse type definition
068: int p = celldef.indexOf(' ');
069: String typename = "";
070: if (p < 0) {
071: // no typedef
072: this .celltype = celltype_undefined;
073: this .cellwidth = -1;
074: } else {
075: typename = celldef.substring(0, p);
076: celldef = celldef.substring(p + 1).trim();
077:
078: if (typename.equals("boolean")) {
079: this .celltype = celltype_boolean;
080: this .cellwidth = 1;
081: } else if (typename.equals("byte")) {
082: this .celltype = celltype_cardinal;
083: this .cellwidth = 1;
084: } else if (typename.equals("short")) {
085: this .celltype = celltype_cardinal;
086: this .cellwidth = 2;
087: } else if (typename.equals("int")) {
088: this .celltype = celltype_cardinal;
089: this .cellwidth = 4;
090: } else if (typename.equals("long")) {
091: this .celltype = celltype_cardinal;
092: this .cellwidth = 8;
093: } else if (typename.equals("byte[]")) {
094: this .celltype = celltype_binary;
095: this .cellwidth = -1; // yet undefined
096: } else if (typename.equals("char")) {
097: this .celltype = celltype_string;
098: this .cellwidth = 1;
099: } else if (typename.equals("String")) {
100: this .celltype = celltype_string;
101: this .cellwidth = -1; // yet undefined
102: } else if (typename.equals("Cardinal")) {
103: this .celltype = celltype_cardinal;
104: this .cellwidth = -1; // yet undefined
105: } else if (typename.equals("Bitfield")) {
106: this .celltype = celltype_bitfield;
107: this .cellwidth = -1; // yet undefined
108: } else {
109: throw new kelondroException(
110: "kelondroColumn - undefined type def '"
111: + typename + "'");
112: }
113: }
114:
115: // parse length
116: p = celldef.indexOf('-');
117: if (p < 0) {
118: // if the cell was defined with a type, we dont need to give an explicit with definition
119: if (this .cellwidth < 0)
120: throw new kelondroException(
121: "kelondroColumn - no cell width definition given");
122: int q = celldef.indexOf(' ');
123: if (q < 0) {
124: this .nickname = celldef;
125: celldef = "";
126: } else {
127: this .nickname = celldef.substring(0, p);
128: celldef = celldef.substring(q + 1);
129: }
130: } else {
131: this .nickname = celldef.substring(0, p);
132: int q = celldef.indexOf(' ');
133: if (q < 0) {
134: try {
135: this .cellwidth = Integer.parseInt(celldef
136: .substring(p + 1));
137: } catch (NumberFormatException e) {
138: throw new kelondroException(
139: "kelondroColumn - cellwidth description wrong:"
140: + celldef.substring(p + 1));
141: }
142: celldef = "";
143: } else {
144: try {
145: this .cellwidth = Integer.parseInt(celldef
146: .substring(p + 1, q));
147: } catch (NumberFormatException e) {
148: throw new kelondroException(
149: "kelondroColumn - cellwidth description wrong:"
150: + celldef.substring(p + 1, q));
151: }
152: celldef = celldef.substring(q + 1);
153: }
154: }
155:
156: // check length constraints
157: if (this .cellwidth < 0)
158: throw new kelondroException(
159: "kelondroColumn - no cell width given for "
160: + this .nickname);
161: if (((typename.equals("boolean")) && (this .cellwidth > 1))
162: || ((typename.equals("byte")) && (this .cellwidth > 1))
163: || ((typename.equals("short")) && (this .cellwidth > 2))
164: || ((typename.equals("int")) && (this .cellwidth > 4))
165: || ((typename.equals("long")) && (this .cellwidth > 8))
166: || ((typename.equals("char")) && (this .cellwidth > 1)))
167: throw new kelondroException("kelondroColumn - cell width "
168: + this .cellwidth + " too wide for type " + typename);
169: if (((typename.equals("short")) && (this .cellwidth <= 1))
170: || ((typename.equals("int")) && (this .cellwidth <= 2))
171: || ((typename.equals("long")) && (this .cellwidth <= 4)))
172: throw new kelondroException("kelondroColumn - cell width "
173: + this .cellwidth + " not appropriate for type "
174: + typename);
175:
176: // parse/check encoder type
177: if ((celldef.length() > 0) && (celldef.charAt(0) == '{')) {
178: p = celldef.indexOf('}');
179: String expf = celldef.substring(1, p);
180: celldef = celldef.substring(p + 1).trim();
181: if (expf.equals("b64e"))
182: this .encoder = encoder_b64e;
183: else if (expf.equals("b256"))
184: this .encoder = encoder_b256;
185: else if (expf.equals("bytes"))
186: this .encoder = encoder_bytes;
187: else {
188: if (this .celltype == celltype_undefined)
189: this .encoder = encoder_bytes;
190: else if (this .celltype == celltype_boolean)
191: this .encoder = encoder_bytes;
192: else if (this .celltype == celltype_binary)
193: this .encoder = encoder_bytes;
194: else if (this .celltype == celltype_string)
195: this .encoder = encoder_bytes;
196: else if (this .celltype == celltype_cardinal)
197: throw new kelondroException(
198: "kelondroColumn - encoder missing for cell "
199: + this .nickname);
200: }
201: } else {
202: if (this .celltype == celltype_cardinal)
203: throw new kelondroException(
204: "kelondroColumn - encoder missing for cell "
205: + this .nickname);
206: this .encoder = encoder_bytes;
207: }
208:
209: assert (this .celltype != celltype_cardinal)
210: || (this .encoder == encoder_b64e)
211: || (this .encoder == encoder_b256);
212:
213: // parse/check description
214: if ((celldef.length() > 0) && (celldef.charAt(0) == '"')) {
215: p = celldef.indexOf('"', 1);
216: this .description = celldef.substring(1, p);
217: celldef = celldef.substring(p + 1).trim();
218: } else {
219: this .description = this .nickname;
220: }
221: }
222:
223: public void setAttributes(String nickname, int celltype, int encoder) {
224: this .celltype = celltype;
225: this .encoder = encoder;
226: this .nickname = nickname;
227: }
228:
229: public String toString() {
230: StringBuffer s = new StringBuffer();
231: switch (celltype) {
232: case celltype_undefined:
233: s.append(nickname);
234: s.append('-');
235: s.append(cellwidth);
236: break;
237: case celltype_boolean:
238: s.append("boolean ");
239: s.append(nickname);
240: break;
241: case celltype_binary:
242: s.append("byte[] ");
243: s.append(nickname);
244: s.append('-');
245: s.append(cellwidth);
246: break;
247: case celltype_string:
248: s.append("String ");
249: s.append(nickname);
250: s.append('-');
251: s.append(cellwidth);
252: break;
253: case celltype_cardinal:
254: s.append("Cardinal ");
255: s.append(nickname);
256: s.append('-');
257: s.append(cellwidth);
258: break;
259: case celltype_bitfield:
260: s.append("Bitfield ");
261: s.append(nickname);
262: s.append('-');
263: s.append(cellwidth);
264: break;
265: }
266:
267: switch (encoder) {
268: case encoder_b64e:
269: s.append(" {b64e}");
270: break;
271: case encoder_b256:
272: s.append(" {b256}");
273: break;
274: }
275: return new String(s);
276: }
277:
278: public boolean equals(kelondroColumn otherCol) {
279: return (this.celltype == otherCol.celltype)
280: && (this.cellwidth == otherCol.cellwidth)
281: && (this.encoder == otherCol.encoder)
282: && (this.nickname.equals(otherCol.nickname));
283: }
284:
285: }
|