53
53
54
54
import javax .xml .XMLConstants ;
55
55
56
+ import java .nio .charset .StandardCharsets ;
57
+ import org .apache .daffodil .japi .infoset .*;
58
+
56
59
public class TestJavaAPI {
57
60
58
61
/**
@@ -262,7 +265,7 @@ public void testJavaAPI2() throws IOException, ClassNotFoundException {
262
265
263
266
/**
264
267
* Verify that we can detect when the parse did not consume all the data.
265
- *
268
+ *
266
269
* @throws IOException
267
270
*/
268
271
@ Test
@@ -393,7 +396,7 @@ public void testJavaAPI5() throws IOException, ClassNotFoundException {
393
396
/***
394
397
* Verify that the compiler throws a FileNotFound exception when fed a list
395
398
* of schema files that do not exist.
396
- *
399
+ *
397
400
* @throws IOException
398
401
*/
399
402
@ Test
@@ -415,7 +418,7 @@ public void testJavaAPI6() throws IOException {
415
418
/**
416
419
* Tests a user submitted case where the XML appears to be serializing odd
417
420
* xml entities into the output.
418
- *
421
+ *
419
422
* @throws IOException
420
423
*/
421
424
@ Test
@@ -452,7 +455,7 @@ public void testJavaAPI7() throws IOException, ClassNotFoundException {
452
455
* that this test uses double newline as a terminator for the first element
453
456
* in the sequence rather than double newline as a separator for the
454
457
* sequence
455
- *
458
+ *
456
459
* @throws IOException
457
460
*/
458
461
@ Test
@@ -931,7 +934,6 @@ public void testJavaAPI20() throws IOException, ClassNotFoundException {
931
934
assertEquals ("42" , unparseBos .toString ());
932
935
}
933
936
934
-
935
937
@ Test
936
938
public void testJavaAPI21 () throws IOException , ClassNotFoundException {
937
939
// Test SAX parsing with errors
@@ -1213,4 +1215,81 @@ public void testJavaAPI26() throws IOException, ClassNotFoundException, External
1213
1215
assertTrue (DaffodilXMLEntityResolver .getXMLEntityResolver () != null );
1214
1216
assertTrue (DaffodilXMLEntityResolver .getLSResourceResolver () != null );
1215
1217
}
1218
+
1219
+ @ Test
1220
+ public void testJavaAPINullXMLTextEscapeStyle () throws IOException , ClassNotFoundException {
1221
+ ByteArrayOutputStream xmlBos = new ByteArrayOutputStream ();
1222
+ try {
1223
+ XMLTextInfosetOutputter outputter = new XMLTextInfosetOutputter (xmlBos , true , null );
1224
+ } catch (Exception e ) {
1225
+ String msg = e .getMessage ().toLowerCase ();
1226
+ assertTrue (msg .contains ("unrecognized" ));
1227
+ assertTrue (msg .contains ("null" ));
1228
+ assertTrue (msg .contains ("xmltextescapestyle" ));
1229
+ }
1230
+ }
1231
+
1232
+ @ Test
1233
+ public void testJavaAPICDATA1 () throws IOException , ClassNotFoundException {
1234
+ String expected = "NO_WHITESPACE_OR_SPECIAL_CHARS" ;
1235
+ String data = "NO_WHITESPACE_OR_SPECIAL_CHARS$" ;
1236
+ String schemaType = "string" ;
1237
+ doXMLTextEscapeStyleTest (expected , data , schemaType );
1238
+ }
1239
+
1240
+ @ Test
1241
+ public void testJavaAPICDATA2 () throws IOException , ClassNotFoundException {
1242
+ String expected = "<![CDATA[ 'some' stuff here  and ]]]]><![CDATA[> even]]>" ;
1243
+ String data = " 'some' stuff here  and ]]> even$" ;
1244
+ String schemaType = "string" ;
1245
+ doXMLTextEscapeStyleTest (expected , data , schemaType );
1246
+ }
1247
+
1248
+ @ Test
1249
+ public void testJavaAPICDATA3 () throws IOException , ClassNotFoundException {
1250
+ String expected = "6.892" ;
1251
+ String data = "6.892" ;
1252
+ String schemaType = "float" ;
1253
+ doXMLTextEscapeStyleTest (expected , data , schemaType );
1254
+ }
1255
+
1256
+ @ Test
1257
+ public void testJavaAPICDATA4 () throws IOException , ClassNotFoundException {
1258
+ String expected = "<![CDATA[this contains a CRLF\n line ending]]>" ;
1259
+ String data = "this contains a CRLF\r \n line ending$" ;
1260
+ String schemaType = "string" ;
1261
+ doXMLTextEscapeStyleTest (expected , data , schemaType );
1262
+ }
1263
+
1264
+ @ Test
1265
+ public void testJavaAPICDATA5 () throws IOException , ClassNotFoundException {
1266
+ String expected = "<![CDATA[abcd>]]>" ;
1267
+ String data = "abcd>$" ;
1268
+ String schemaType = "string" ;
1269
+ doXMLTextEscapeStyleTest (expected , data , schemaType );
1270
+ }
1271
+
1272
+ public void doXMLTextEscapeStyleTest (String expect , String data , String schemaType )
1273
+ throws IOException , ClassNotFoundException {
1274
+
1275
+ org .apache .daffodil .japi .Compiler c = Daffodil .compiler ();
1276
+ java .io .File schemaFile = getResource ("/test/japi/mySchemaCDATA.dfdl.xsd" );
1277
+ ProcessorFactory pf = c .compileFile (schemaFile , schemaType , null );
1278
+ DataProcessor dp = pf .onPath ("/" );
1279
+
1280
+ ByteArrayInputStream is = new ByteArrayInputStream (data .getBytes (StandardCharsets .UTF_8 ));
1281
+ InputSourceDataInputStream input = new InputSourceDataInputStream (is );
1282
+ ByteArrayOutputStream bosDP = new ByteArrayOutputStream ();
1283
+ XMLTextInfosetOutputter outputter = new XMLTextInfosetOutputter (bosDP , true , XMLTextEscapeStyle .CDATA );
1284
+ ParseResult res = dp .parse (input , outputter );
1285
+ boolean err = res .isError ();
1286
+
1287
+ String infosetDPString = bosDP .toString ();
1288
+ int start = infosetDPString .indexOf (".com\" >" ) + 6 ;
1289
+ int end = infosetDPString .indexOf ("</tns" );
1290
+ String value = infosetDPString .substring (start , end );
1291
+
1292
+ assertFalse (err );
1293
+ assertEquals (expect , value );
1294
+ }
1216
1295
}
0 commit comments