23
23
import static org .jsonurl .CharUtil .IS_QUOTE ;
24
24
import static org .jsonurl .CharUtil .IS_STRUCTCHAR ;
25
25
import static org .jsonurl .CharUtil .hexDecode ;
26
- import static org .jsonurl .SyntaxException .ERR_MSG_BADCHAR ;
27
- import static org .jsonurl .SyntaxException .ERR_MSG_BADPCTENC ;
28
- import static org .jsonurl .SyntaxException .ERR_MSG_BADQSTR ;
29
- import static org .jsonurl .SyntaxException .ERR_MSG_BADUTF8 ;
30
- import static org .jsonurl .SyntaxException .ERR_MSG_EXPECT_LITERAL ;
31
- import static org .jsonurl .SyntaxException .ERR_MSG_NOTEXT ;
26
+ import static org .jsonurl .SyntaxException .Message . MSG_BAD_CHAR ;
27
+ import static org .jsonurl .SyntaxException .Message . MSG_BAD_PCT_ENC ;
28
+ import static org .jsonurl .SyntaxException .Message . MSG_BAD_QSTR ;
29
+ import static org .jsonurl .SyntaxException .Message . MSG_BAD_UTF8 ;
30
+ import static org .jsonurl .SyntaxException .Message . MSG_EXPECT_LITERAL ;
31
+ import static org .jsonurl .SyntaxException .Message . MSG_NO_TEXT ;
32
32
33
33
import java .io .IOException ;
34
34
import java .nio .charset .MalformedInputException ;
@@ -61,7 +61,7 @@ static final NumberBuilder newNumberBuilder(
61
61
//
62
62
// use the factory as a BigMathProvider
63
63
//
64
- ret .setMathContextProvider ((BigMathProvider )factory );
64
+ ret .setBigMathProvider ((BigMathProvider )factory );
65
65
}
66
66
67
67
return ret ;
@@ -73,14 +73,14 @@ private static final int percentDecode(
73
73
int len ) {
74
74
75
75
if (off + 2 > len ) {
76
- throw new SyntaxException (ERR_MSG_BADPCTENC , off );
76
+ throw new SyntaxException (MSG_BAD_PCT_ENC , off );
77
77
}
78
78
79
79
int c1 = hexDecode (s .charAt (off ));
80
80
int c2 = hexDecode (s .charAt (off + 1 ));
81
81
82
82
if (c1 < 0 || c2 < 0 ) {
83
- throw new SyntaxException (ERR_MSG_BADPCTENC , off );
83
+ throw new SyntaxException (MSG_BAD_PCT_ENC , off );
84
84
}
85
85
86
86
return ((c1 << 4 ) | c2 );
@@ -150,7 +150,8 @@ private static final String string(
150
150
CharSequence s ,
151
151
int start ,
152
152
int stop ,
153
- boolean quoted ) {
153
+ boolean quoted ,
154
+ boolean isEmptyUnquotedStringOK ) {
154
155
155
156
boolean needEndQuote = quoted ;
156
157
@@ -229,10 +230,16 @@ private static final String string(
229
230
}
230
231
}
231
232
if (needEndQuote ) {
232
- throw new SyntaxException (ERR_MSG_BADQSTR , stop );
233
+ throw new SyntaxException (MSG_BAD_QSTR , stop );
233
234
}
234
235
if (more > 0 ) {
235
- throw new SyntaxException (ERR_MSG_BADUTF8 , stop );
236
+ throw new SyntaxException (MSG_BAD_UTF8 , stop );
237
+ }
238
+
239
+ if (!quoted && !isEmptyUnquotedStringOK && buf .length () == 0 ) {
240
+ throw new SyntaxException (
241
+ SyntaxException .Message .MSG_EXPECT_LITERAL ,
242
+ start );
236
243
}
237
244
238
245
return buf .toString ();
@@ -251,21 +258,23 @@ private static final String string(
251
258
* @param start the start index
252
259
* @param stop the stop index
253
260
*/
254
- @ SuppressWarnings ("PMD" )
255
261
static final String literalToJavaString (
256
262
StringBuilder buf ,
257
263
NumberBuilder num ,
258
264
CharSequence s ,
259
265
int start ,
260
- int stop ) {
266
+ int stop ,
267
+ boolean isEmptyUnquotedStringOK ) {
261
268
262
- String ret ;
269
+
263
270
264
271
if (s .charAt (start ) == '\'' ) {
265
- return string (buf , s , start + 1 , stop , true );
272
+ return string (buf , s , start + 1 , stop , true , isEmptyUnquotedStringOK );
266
273
}
267
274
268
- if ((ret = getTrueFalseNull (s , start , stop )) != null ) {
275
+ String ret = getTrueFalseNull (s , start , stop );
276
+
277
+ if (ret != null ) {
269
278
return ret ;
270
279
}
271
280
@@ -282,10 +291,10 @@ static final String literalToJavaString(
282
291
}
283
292
284
293
if (num .parse (s , start , stop )) {
285
- return num .toString ();
294
+ return s . subSequence ( start , stop ) .toString ();
286
295
}
287
296
288
- return string (buf , s , start , stop , false );
297
+ return string (buf , s , start , stop , false , isEmptyUnquotedStringOK );
289
298
}
290
299
291
300
/**
@@ -309,10 +318,12 @@ static final <V> V literal(
309
318
CharSequence s ,
310
319
int start ,
311
320
int stop ,
312
- ValueFactory <V ,?,?,?,?,?,?,?,?,?> factory ) {
321
+ ValueFactory <V ,?,?,?,?,?,?,?,?,?> factory ,
322
+ boolean isEmptyUnquotedStringOK ) {
313
323
314
324
if (s .charAt (start ) == '\'' ) {
315
- return factory .getString (string (buf , s , start + 1 , stop , true ));
325
+ return factory .getString (string (
326
+ buf , s , start + 1 , stop , true , isEmptyUnquotedStringOK ));
316
327
}
317
328
318
329
V ret = factory .getTrueFalseNull (s , start , stop );
@@ -332,7 +343,8 @@ static final <V> V literal(
332
343
return factory .getNumber (num );
333
344
}
334
345
335
- return factory .getString (string (buf , s , start , stop , false ));
346
+ return factory .getString (string (
347
+ buf , s , start , stop , false , isEmptyUnquotedStringOK ));
336
348
}
337
349
}
338
350
@@ -497,7 +509,7 @@ private JsonUrl() {
497
509
* Determine the length of a literal value.
498
510
*
499
511
* <p>This simply calls
500
- * {@link #parseLiteralLength(CharSequence, int, int, String )
512
+ * {@link #parseLiteralLength(CharSequence, int, int, org.jsonurl.SyntaxException.Message )
501
513
* parseLiteralLength(s, start, stop, null)}.
502
514
*
503
515
* @param s text to be parsed
@@ -522,7 +534,8 @@ public static final int parseLiteralLength(
522
534
* <p>This method will stop when the first structural character
523
535
* is found or when ``stop'' is reached, whichever comes first.
524
536
* Note, because it is static no limits are enforced as is with
525
- * {@link org.jsonurl.Parser#parse(CharSequence, int, int) parse}.
537
+ * {@link org.jsonurl.Parser#parse(CharSequence, ValueFactory)
538
+ * parse}.
526
539
*
527
540
* @param s text to be parsed
528
541
* @param start start position in text
@@ -535,7 +548,7 @@ public static final int parseLiteralLength(
535
548
CharSequence s ,
536
549
int start ,
537
550
int stop ,
538
- String errmsg ) {
551
+ SyntaxException . Message errmsg ) {
539
552
540
553
int ret = 0 ;
541
554
@@ -561,9 +574,9 @@ public static final int parseLiteralLength(
561
574
break ;
562
575
}
563
576
}
564
- throw new SyntaxException (ERR_MSG_BADCHAR , start );
577
+ throw new SyntaxException (MSG_BAD_CHAR , start );
565
578
}
566
- throw new SyntaxException (ERR_MSG_BADQSTR , start );
579
+ throw new SyntaxException (MSG_BAD_QSTR , start );
567
580
}
568
581
569
582
for (; start < stop ; start ++) {
@@ -580,7 +593,7 @@ public static final int parseLiteralLength(
580
593
break ;
581
594
}
582
595
}
583
- throw new SyntaxException (ERR_MSG_BADCHAR , start );
596
+ throw new SyntaxException (MSG_BAD_CHAR , start );
584
597
}
585
598
586
599
if (start != stop && errmsg != null ) {
@@ -591,7 +604,7 @@ public static final int parseLiteralLength(
591
604
592
605
/**
593
606
* Determine the length of a literal value.
594
- * @see #parseLiteralLength(CharSequence, int, int, String )
607
+ * @see #parseLiteralLength(CharSequence, int, int, org.jsonurl.SyntaxException.Message )
595
608
*/
596
609
public static final int parseLiteralLength (CharSequence s ) {
597
610
return parseLiteralLength (s , 0 , s .length (), null );
@@ -608,8 +621,8 @@ public static final int parseLiteralLength(CharSequence s) {
608
621
* <li>string
609
622
* </ul>
610
623
* No limits are enforced (as is with
611
- * {@link org.jsonurl.Parser#parse(CharSequence, int, int) parse} )
612
- * because this method is static.
624
+ * {@link org.jsonurl.Parser#parse(CharSequence, ValueFactory )
625
+ * parse}) because this method is static.
613
626
*
614
627
* <p>Note, the third argument is a length not a position. It indicates
615
628
* the number of characters to be parsed.
@@ -623,55 +636,62 @@ public static final <V> V parseLiteral(
623
636
CharSequence s ,
624
637
int start ,
625
638
int length ,
626
- ValueFactory <V ,?,?,?,?,?,?,?,?,?> factory ) {
639
+ ValueFactory <V ,?,?,?,?,?,?,?,?,?> factory ,
640
+ boolean isEmptyUnquotedStringOK ) {
627
641
628
642
if (length == 0 ) {
629
- throw new SyntaxException (ERR_MSG_NOTEXT );
643
+ throw new SyntaxException (MSG_NO_TEXT );
630
644
}
631
645
632
646
int stop = start + length ;
633
- parseLiteralLength (s , start , stop , ERR_MSG_EXPECT_LITERAL );
647
+ parseLiteralLength (s , start , stop , MSG_EXPECT_LITERAL );
634
648
635
- return Parse .literal (null , null , s , start , stop , factory );
649
+ return Parse .literal (null , null , s , start , stop , factory , isEmptyUnquotedStringOK );
636
650
}
637
651
638
652
/**
639
653
* Parse a single literal value.
640
654
*
641
655
* <p>This simply calls
642
- * {@link #parseLiteral(CharSequence, int, int, ValueFactory)
643
- * parseLiteral(s, start, length, JavaValueFactory.PRIMITIVE)}.
644
- * @see JsonUrl#parseLiteral(CharSequence, int, int, ValueFactory)
656
+ * {@link #parseLiteral(CharSequence, int, int, ValueFactory, boolean)
657
+ * parseLiteral(s, start, length, JavaValueFactory.PRIMITIVE, false)}.
658
+ *
659
+ * @see JsonUrl#parseLiteral(CharSequence, int, int, ValueFactory, boolean)
645
660
* @see JavaValueFactory#PRIMITIVE
646
661
*/
647
662
public static final Object parseLiteral (
648
663
CharSequence s ,
649
664
int start ,
650
665
int length ) {
651
- return parseLiteral (s , start , length , JavaValueFactory .PRIMITIVE );
666
+ return parseLiteral (s , start , length , JavaValueFactory .PRIMITIVE , false );
652
667
}
653
668
654
669
/**
655
670
* Parse a single literal value.
656
671
*
657
672
* <p>This simply calls
658
- * {@link #parseLiteral(CharSequence, int, int, ValueFactory)
659
- * parseLiteral(s, 0, s.length(), JavaValueFactory.PRIMITIVE)}.
660
- * @see JsonUrl#parseLiteral(CharSequence, int, int, ValueFactory)
673
+ * {@link #parseLiteral(CharSequence, int, int, ValueFactory, boolean)
674
+ * parseLiteral(s, 0, s.length(), JavaValueFactory.PRIMITIVE, false)}.
675
+ *
676
+ * @see JsonUrl#parseLiteral(CharSequence, int, int, ValueFactory, boolean)
677
+ * @see JavaValueFactory#PRIMITIVE
661
678
*/
662
679
public static final Object parseLiteral (CharSequence s ) {
663
- return parseLiteral (s , 0 , s .length (), JavaValueFactory .PRIMITIVE );
680
+ return parseLiteral (s , 0 , s .length (), JavaValueFactory .PRIMITIVE , false );
664
681
}
665
682
666
683
/**
667
684
* Parse a single literal value.
668
685
*
669
- * @see JsonUrl#parseLiteral(CharSequence, int, int, ValueFactory)
686
+ * <p>This simply calls
687
+ * {@link #parseLiteral(CharSequence, int, int, ValueFactory, boolean)
688
+ * parseLiteral(s, 0, s.length(), factory, false)}.
689
+ * @see JsonUrl#parseLiteral(CharSequence, int, int, ValueFactory, boolean)
670
690
*/
671
691
public static final <V > V parseLiteral (
672
692
CharSequence s ,
673
693
ValueFactory <V ,?,?,?,?,?,?,?,?,?> factory ) {
674
- return parseLiteral (s , 0 , s .length (), factory );
694
+ return parseLiteral (s , 0 , s .length (), factory , false );
675
695
}
676
696
677
697
/**
0 commit comments