From: <je...@sv...> - 2005-09-30 00:40:16
|
Author: jeichar Date: 2005-09-29 17:39:43 -0700 (Thu, 29 Sep 2005) New Revision: 15955 Modified: geotools/branches/2.1.x/plugin/shapefile/src/org/geotools/data/shapefi= le/dbf/DbaseFileWriter.java geotools/branches/2.1.x/plugin/shapefile/test/org/geotools/data/shapef= ile/DbaseFileTest.java Log: GEOT-696 Modified: geotools/branches/2.1.x/plugin/shapefile/src/org/geotools/data/= shapefile/dbf/DbaseFileWriter.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- geotools/branches/2.1.x/plugin/shapefile/src/org/geotools/data/shapef= ile/dbf/DbaseFileWriter.java 2005-09-30 00:25:04 UTC (rev 15954) +++ geotools/branches/2.1.x/plugin/shapefile/src/org/geotools/data/shapef= ile/dbf/DbaseFileWriter.java 2005-09-30 00:39:43 UTC (rev 15955) @@ -106,12 +106,14 @@ =20 for (int i =3D 0; i < header.getNumFields(); i++) { String fieldString =3D fieldString(record[i], i); -// if ( header.getFieldLength(i) !=3D fieldString.getBytes().length= ) { -// System.out.println(i + " : " + header.getFieldName(i)); -// } + if ( header.getFieldLength(i) !=3D fieldString.getBytes().length) = { + System.out.println(i + " : " + header.getFieldName(i)+" value = =3D "+fieldString+""); + buffer.put(new byte[header.getFieldLength(i)]); + }else{ + buffer.put(fieldString.getBytes()); + } =20 =20 - buffer.put(fieldString.getBytes()); =20 } =20 @@ -202,7 +204,7 @@ private StringBuffer buffer =3D new StringBuffer(255); private NumberFormat numFormat =3D NumberFormat.getNumberInstance(Lo= cale.US); private Calendar calendar =3D Calendar.getInstance(Locale.US); - private String emtpyString; + private String emptyString; private static final int MAXCHARS =3D 255; =20 public FieldFormatter() { @@ -216,23 +218,40 @@ sb.setCharAt(i, ' '); } =20 - emtpyString =3D sb.toString(); + emptyString =3D sb.toString(); } =20 public String getFieldString(int size, String s) { - buffer.replace(0, size, emtpyString); + buffer.replace(0, size, emptyString); buffer.setLength(size); - =20 + int unit=3D" ".getBytes().length; + //international characters must be accounted for so size !=3D leng= th. + int maxSize=3Dsize; if(s !=3D null) { buffer.replace(0, size, s); - if(s.length() <=3D size) { - for(int i =3D s.length(); i < size; i++) { - buffer.append(' '); - } + int currentBytes=3Ds.substring(0, Math.min(size, s.length())).ge= tBytes().length; + if( currentBytes>size ){ + char[] c=3Dnew char[1]; + for( int index=3Dsize-1; currentBytes>size; index--){ + c[0] =3D buffer.charAt(index); + String string=3Dnew String(c); + buffer.deleteCharAt(index); + currentBytes-=3Dstring.getBytes().length; + maxSize--; + } + }else{ + if(s.length() < size) { + maxSize=3Dsize-(currentBytes-s.length()); + for(int i =3D s.length(); i < size; i++) { + buffer.append(' '); + } + } } } =20 - buffer.setLength(size); + buffer.setLength(maxSize); + + System.out.println(buffer.toString().getBytes().length); return buffer.toString(); } =20 @@ -268,7 +287,7 @@ buffer.append(day); } else { buffer.setLength(8); - buffer.replace(0, 8, emtpyString); + buffer.replace(0, 8, emptyString); } =20 buffer.setLength(8); Modified: geotools/branches/2.1.x/plugin/shapefile/test/org/geotools/data= /shapefile/DbaseFileTest.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- geotools/branches/2.1.x/plugin/shapefile/test/org/geotools/data/shape= file/DbaseFileTest.java 2005-09-30 00:25:04 UTC (rev 15954) +++ geotools/branches/2.1.x/plugin/shapefile/test/org/geotools/data/shape= file/DbaseFileTest.java 2005-09-30 00:39:43 UTC (rev 15955) @@ -113,4 +113,24 @@ f.delete(); } =20 + public void testFieldFormatter() throws Exception { + DbaseFileWriter.FieldFormatter formatter=3Dnew DbaseFileWriter.Fie= ldFormatter(); + =20 + String stringWithInternationChars=3D"hello "+'\u20ac'; + =20 + System.out.println(stringWithInternationChars); + =20 + String formattedString=3Dformatter.getFieldString(10, stringWithIn= ternationChars); + =20 + assertEquals(" ".getBytes().length, formattedString.getBy= tes().length); + =20 + //test when the string is too big. + stringWithInternationChars=3D'\u20ac'+"1234567890"; + formattedString=3Dformatter.getFieldString(10, stringWithInternati= onChars); + =20 + assertEquals(" ".getBytes().length, formattedString.getBy= tes().length); + =20 + =20 + } + =20 } |