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
|
// CodaExample.java - Sample file for the CODA Java library interface
//
// Copyright (C) 2007-2024 S[&]T, The Netherlands.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
import java.lang.reflect.Field;
import java.nio.file.Paths;
import nl.stcorp.coda.Coda;
import nl.stcorp.coda.CodaException;
import nl.stcorp.coda.Cursor;
import nl.stcorp.coda.Expression;
import nl.stcorp.coda.FormatEnum;
import nl.stcorp.coda.NativeTypeEnum;
import nl.stcorp.coda.Product;
import nl.stcorp.coda.SpecialTypeEnum;
import nl.stcorp.coda.Type;
import nl.stcorp.coda.TypeClassEnum;
/**
* CodaExample class.
*
* This is an example class / starting point that illustrates some
* of the techniques of working with CODA through the Java
* interface.
*
* You can compile and run this test program on one or more
* product files by saying
*
* ant run -Dargs="<product file ...>"
*
* This utility will print some global information about CODA, a
* very simple CODA Expression example, and it will print the
* structure of the product files passed on the commandline.
*
* If you encounter an "unsupported product" error in
* coda_open(), it means that the CODA_DEFINITION directory
* (typically $INSTALL/share/coda/definitions) does not have the
* codadef files for your particular product in it.
*
*/
public class CodaExample
{
static
{
loadLibrary("coda_jni");
}
private static void loadLibrary(String libraryName)
{
System.load(Paths.get(System.getProperty("coda.lib.dir", "."), libraryName + ".jnilib").toString());
}
private static void showCodaInfo()
{
System.out.println("Coda Version: " + Coda.version());
System.out.println("Sample FormatEnum: " + Type.getFormatName(FormatEnum.coda_format_xml));
System.out.println("Sample NativeTypeEnum: " + Type.getNativeTypeName(NativeTypeEnum.coda_native_type_uint64));
System.out.println("Sample TypeClassEnum: " + Type.getClassName(TypeClassEnum.coda_raw_class));
System.out.println("Sample SpecialTypeEnum: " + Type.getSpecialTypeName(SpecialTypeEnum.coda_special_vsf_integer));
System.out.println();
}
private static void showProductInfo(String filename) throws CodaException
{
Product product = new Product(filename);
System.out.println("Product:");
System.out.println(" Filename = " + product.getFilename());
System.out.println(" File size = " + product.getFileSize());
System.out.println(" Class = " + product.getProductClass());
System.out.println(" Format = " + product.getFormat());
System.out.println(" Type = " + product.getType());
System.out.println(" Version = " + product.getVersion());
Type type = product.getRootType();
System.out.println(" Root Type:");
System.out.println(" Description = " + type.getDescription());
Cursor cursor = new Cursor(product);
System.out.println(" Number of elements = " + cursor.getNumElements());
TypeClassEnum rootType = cursor.getTypeClass();
System.out.println(" Root Type = " + rootType);
// Uncomment the next line if you also want to print record data.
// printRecord(cursor);
product.close();
}
private static void printRecord(Cursor cursor) throws CodaException
{
int numFields = cursor.getNumElements();
if (numFields > 0)
{
Type recordType = cursor.getType();
cursor.gotoFirstRecordField();
for (int i = 0; i < numFields; i++)
{
// We don't print fields that are hidden, like the first MPH
// field
// (with value 'PRODUCT=')
boolean hidden = recordType.getRecordFieldHiddenStatus(i);
if (!hidden)
{
String fieldName = recordType.getRecordFieldName(i);
System.out.print(String.format("%32s : ", fieldName));
printData(cursor);
System.out.println();
}
if (i < numFields - 1)
cursor.gotoNextRecordField();
}
cursor.gotoParent();
}
}
private static void printData(Cursor cursor) throws CodaException
{
TypeClassEnum typeClass = cursor.getTypeClass();
switch (typeClass)
{
case coda_array_class:
{
int numElements = cursor.getNumElements();
if (numElements > 0)
{
System.out.print("[");
cursor.gotoFirstArrayElement();
for (int i = 0; i < numElements; i++)
{
printData(cursor);
if (i < numElements - 1)
{
System.out.print(", ");
cursor.gotoNextArrayElement();
}
}
System.out.print("]");
cursor.gotoParent();
}
}
break;
case coda_special_class:
{
SpecialTypeEnum specialType = cursor.getSpecialType();
if (specialType == SpecialTypeEnum.coda_special_time)
{
double datetime = cursor.readDouble();
String utcString = Coda.time_double_to_string(datetime, "yyyy-MM-dd HH:mm:ss.SSSSSS");
System.out.print(utcString);
}
else
System.out.print(String.format("*** Unexpected special type (%s) ***", Type.getSpecialTypeName(specialType)));
}
break;
default:
{
NativeTypeEnum readType = cursor.getReadType();
switch (readType)
{
case coda_native_type_not_available:
{
System.out.print("Compound entity that cannot be read directly");
break;
}
case coda_native_type_int8:
case coda_native_type_int16:
case coda_native_type_int32:
{
int data = cursor.readInt32();
System.out.print(data);
break;
}
case coda_native_type_uint8:
case coda_native_type_uint16:
case coda_native_type_uint32:
{
long data = cursor.readUint32();
System.out.print(data);
break;
}
case coda_native_type_int64:
{
long data = cursor.readInt64();
System.out.print(data);
break;
}
case coda_native_type_uint64:
{
long data = cursor.readUint64();
System.out.print(data);
break;
}
case coda_native_type_float:
case coda_native_type_double:
{
double data = cursor.readDouble();
System.out.print(String.format("%g", data));
break;
}
case coda_native_type_char:
{
int data = cursor.readChar();
System.out.print(String.format("%c", data));
break;
}
case coda_native_type_string:
{
String data = cursor.readString();
System.out.print(String.format("\"%s\"", data));
break;
}
case coda_native_type_bytes:
{
int len = cursor.getNumElements();
// byte[] data = cursor.readBytes(0, len);
// String dataString = new String(data);
System.out.print(String.format("%d bytes", len));
break;
}
default:
System.out.print(String.format("*** Unknown native type encountered: %s ***", Type.getNativeTypeName(readType)));
break;
}
}
}
}
private static void showExpressions() throws CodaException
{
showIntegerExpression("2 * 21");
// showIntegerExpression("fnorgle"); // This *should* fail!
// showIntegerExpression("2 / 0"); // This *should* fail!
}
private static void showIntegerExpression(String exprString) throws CodaException
{
Expression expr = new Expression(exprString);
System.out.println("Expression:");
System.out.println(" init string = " + exprString);
System.out.println(" type = " + expr.getType());
System.out.println(" isConstant? = " + expr.isConstant());
System.out.println(" value = " + expr.evalInteger());
expr.delete();
}
public static void main(String argv[])
{
if (argv.length < 1)
{
System.err.println("Usage: CodaExample <product file ...>");
System.exit(1);
}
String[] productFiles = argv;
try
{
Coda.init();
showCodaInfo();
showExpressions();
for (String productFile : productFiles)
{
showProductInfo(productFile);
System.out.println();
}
}
catch (CodaException eeks)
{
System.err.println("Error: " + eeks.getMessage());
}
Coda.done();
}
}
|