1 |
/* |
2 |
openisis - an open implementation of the CDS/ISIS database |
3 |
Version 0.8.x (patchlevel see file Version) |
4 |
Copyright (C) 2001-2003 by Erik Grziwotz, erik@openisis.org |
5 |
|
6 |
This library is free software; you can redistribute it and/or |
7 |
modify it under the terms of the GNU Lesser General Public |
8 |
License as published by the Free Software Foundation; either |
9 |
version 2.1 of the License, or (at your option) any later version. |
10 |
|
11 |
This library is distributed in the hope that it will be useful, |
12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 |
Lesser General Public License for more details. |
15 |
|
16 |
You should have received a copy of the GNU Lesser General Public |
17 |
License along with this library; if not, write to the Free Software |
18 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
19 |
|
20 |
see README for more information |
21 |
EOH */ |
22 |
|
23 |
package org.openisis; |
24 |
|
25 |
/** |
26 |
Record implementation of openisis java binding. |
27 |
<p> |
28 |
$Id: Rec.java,v 1.4 2003/04/08 00:20:53 kripke Exp $ |
29 |
@version $Revision: 1.4 $ |
30 |
@author $Author: kripke $ |
31 |
*/ |
32 |
public class Rec { |
33 |
|
34 |
private static final int APPEND_LENGTH=10; |
35 |
|
36 |
private int myRowId; |
37 |
private Field[] myFields; |
38 |
private int myLength; |
39 |
|
40 |
public Rec ( int rowid, Field[] fields ) { |
41 |
myRowId = rowid; |
42 |
myFields = fields; |
43 |
myLength=fields.length; |
44 |
} |
45 |
|
46 |
public Rec (int rowid) { |
47 |
myRowId=rowid; |
48 |
myFields=new Field[0]; |
49 |
myLength=0; |
50 |
} |
51 |
|
52 |
public int getRowId () { return myRowId; } |
53 |
|
54 |
public int getLen () { return myLength; } |
55 |
|
56 |
public Field getField ( int i ) { return myFields[i]; } |
57 |
|
58 |
public Field[] getFields () { |
59 |
int len = getLen(); |
60 |
Field[] clone = new Field[ len ]; |
61 |
System.arraycopy( myFields, 0, clone, 0, len ); |
62 |
return clone; |
63 |
} |
64 |
|
65 |
public Field[] getFields(int offset,int len) { |
66 |
Field[] clone=new Field[len]; |
67 |
System.arraycopy(myFields,offset,clone,0,len); |
68 |
return clone; |
69 |
} |
70 |
|
71 |
|
72 |
public boolean append(int tag,String value) { |
73 |
return append(new Field(tag,value)); |
74 |
} |
75 |
|
76 |
public boolean append(Field field) { |
77 |
if (field==null) { |
78 |
System.err.println("FIELD==null"); |
79 |
field=new Field(0,""); |
80 |
} |
81 |
try { |
82 |
if (myFields.length<=myLength) { |
83 |
if (myFields.length<myLength) { |
84 |
System.err.println("Rec.append:myFields.length<myLength !!! ("+myFields.length+","+myLength+")"); |
85 |
myLength=myFields.length; |
86 |
} |
87 |
Field[] tfields=new Field[myLength+APPEND_LENGTH]; |
88 |
System.arraycopy( myFields, 0, tfields, 0, myLength ); |
89 |
myFields=tfields; |
90 |
} |
91 |
myFields[myLength]=field; |
92 |
myLength++; |
93 |
return true; |
94 |
} catch(Exception e) { |
95 |
e.printStackTrace(System.err); |
96 |
return false; |
97 |
} |
98 |
} |
99 |
|
100 |
public boolean append(Field[] fields) { |
101 |
//return false; |
102 |
if (fields!=null && fields.length>0) { |
103 |
for(int i=0;i<fields.length;i++) { |
104 |
if (!append(fields[i])) return false; |
105 |
} |
106 |
} |
107 |
return true; |
108 |
} |
109 |
|
110 |
|
111 |
public boolean wrap(Rec record,int tag) { |
112 |
boolean ok=append(tag,""+record.getLen()); |
113 |
if (!ok) return false; |
114 |
return append(record.getFields()); |
115 |
} |
116 |
|
117 |
public Rec unwrap(int tag) { |
118 |
|
119 |
return null; |
120 |
} |
121 |
|
122 |
protected void set(int pos,Field f) { |
123 |
if (myLength>pos) myFields[pos]=f; |
124 |
} |
125 |
|
126 |
protected Field get(int pos) { |
127 |
if (myLength>pos) return myFields[pos]; |
128 |
else return null; |
129 |
} |
130 |
|
131 |
/** the field selector. |
132 |
format one or more occurences of a field to a StringBuffer, |
133 |
mimicking the ISIS Vn-operator. |
134 |
@param b the StringBuffer to fill |
135 |
@param cpre conditional prefix: printed first, if anything is printed |
136 |
@param rpre repeated prefix: printed before every occ |
137 |
(but not the first, if mode include PP) |
138 |
@param tag the field number (0 = all) |
139 |
@param mode several mode bits as defined in Db + optional subfield char |
140 |
@param occ a 16bit occ | end << 16. |
141 |
if occ is 0, all occurences are printed. |
142 |
if the end (higher bytes) are 0, only this occ is printed, |
143 |
else all up to incl. end (use Db.LOCC for all). |
144 |
@param len a 16bit length constraint | offset << 16 |
145 |
this is passed to Field.v and applied for each occ. |
146 |
@param rsuf repeated suffix: printed after every occ |
147 |
(but not the last, if mode include PS) |
148 |
@param csuf conditional suffix: printed last, if anything was printed |
149 |
@return the number of formatted occurences |
150 |
@see Field#v(StringBuffer,int,String,int) |
151 |
*/ |
152 |
public int v ( StringBuffer b, |
153 |
String cpre, String rpre, |
154 |
int tag, int mode, int occ, int len, |
155 |
String rsuf, String csuf ) |
156 |
{ |
157 |
int end = occ >>> 16; occ &= 0xffff; |
158 |
int o = 0; // occurences seen |
159 |
int did = 0; // occurences done (after range and subfield selection) |
160 |
int lrsuf = -1; // last pos of repeated suffix |
161 |
|
162 |
// data mode ending should not be used with suffix ? |
163 |
// if ( null != rsuf || null != csuf ) mode &= ~Db.MD; |
164 |
|
165 |
// loop all fields |
166 |
int flen=getLen(); |
167 |
for ( int i=0; i<flen; i++ ) { |
168 |
if ( 0 != tag && myFields[i].tag != tag ) |
169 |
continue; |
170 |
o++; // this is the oth occ, counting from 1 |
171 |
System.err.println( "tag: "+tag+" occ: "+o ); |
172 |
if ( 0 != occ ) // occ range limit |
173 |
if ( o < occ ) |
174 |
continue; |
175 |
else if ( o > occ && o > end ) |
176 |
break; |
177 |
System.err.println( "in range"); |
178 |
// seems we are in range |
179 |
int l = b.length(); |
180 |
if ( 0 == did && null != cpre ) |
181 |
b.append( cpre ); |
182 |
if ( null != rpre && (0<did || 0==(mode&Db.PP)) ) |
183 |
b.append( rpre ); |
184 |
boolean ok; |
185 |
if ( 0 == ((Db.DS|Db.NS) & mode) ) // no dummy |
186 |
ok = myFields[i].v( b, mode, len ); // really print it |
187 |
else if ( 0 == (mode & 0xffff) || '*' == (mode & 0xffff) ) |
188 |
ok = true; // no or 1st subfield |
189 |
else { |
190 |
char[] sf = new char[2]; |
191 |
sf[0] = '^'; sf[1] = (char)(mode & 0xffff); |
192 |
ok = 0 > myFields[i].val.indexOf( new String(sf) ); |
193 |
if ( 0 != (Db.NS & mode) ) // the N absence dummy |
194 |
ok = !ok; |
195 |
} |
196 |
if ( ! ok ) { |
197 |
// subfield not found -- however, it counts as an field occ ... |
198 |
// backup to where we were |
199 |
b.delete( l, Integer.MAX_VALUE ); |
200 |
continue; |
201 |
} |
202 |
did++; // gotcha |
203 |
if ( null == rsuf ) |
204 |
continue; |
205 |
lrsuf = b.length(); |
206 |
b.append( rsuf ); |
207 |
} |
208 |
if ( 0 <= lrsuf && 0 != (Db.PS & mode) ) // backup over lrsuf |
209 |
b.delete( lrsuf, Integer.MAX_VALUE ); |
210 |
if ( 0 != did && null != csuf ) |
211 |
b.append( csuf ); |
212 |
return did; |
213 |
} // v |
214 |
|
215 |
/** format fields to a new StringBuffer, return it's content as String. |
216 |
@return a new String or the String Db.EMPTY, iff no fields where done. |
217 |
@see #v(StringBuffer,String,String,int,int,int,int,String,String) |
218 |
*/ |
219 |
public String v ( String cpre, String rpre, |
220 |
int tag, int mode, int occ, int len, |
221 |
String rsuf, String csuf ) |
222 |
{ |
223 |
StringBuffer b = new StringBuffer(); |
224 |
return 0 < v( b, cpre, rpre, tag, mode, occ, len, rsuf, csuf ) |
225 |
? b.toString() : Db.EMPTY; |
226 |
} // v |
227 |
|
228 |
|
229 |
/** format fields to a new StringBuffer, return it's content as String. |
230 |
the conditional pre and suf are null here. |
231 |
@return a new String or the String Db.EMPTY, iff no fields where done. |
232 |
@see #v(StringBuffer,String,String,int,int,int,int,String,String) |
233 |
*/ |
234 |
public String v ( |
235 |
String rpre, int tag, int mode, int occ, int len, String rsuf ) |
236 |
{ |
237 |
return v( null, rpre, tag, mode, occ, len, rsuf, null ); |
238 |
} // v |
239 |
|
240 |
} // Rec |