Skip to content

Commit 5dddd1d

Browse files
committed
update: Use JsonUrlOptions in JsonUrl static's and ParseResultFacade
The static methods in org.jsonurl.JsonUrl should follow the same pattern and accept an instance of the JsonUrl options interface. Additionally, while it's not visible to code outside the package, ParseResultFacade has been updated to use them as well.
1 parent b0497cb commit 5dddd1d

File tree

6 files changed

+105
-196
lines changed

6 files changed

+105
-196
lines changed

module/jsonurl-core/src/main/java/org/jsonurl/JsonUrl.java

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -306,18 +306,17 @@ static String literalToJavaString(
306306
CharSequence text,
307307
int start,
308308
int stop,
309-
boolean isEmptyUnquotedStringOK,
310-
boolean isImpliedStringLiteral) {
309+
JsonUrlOptions options) {
311310

312311
if (stop <= start) {
313-
if (isEmptyUnquotedStringOK) {
312+
if (JsonUrlOptions.isEmptyUnquotedKeyAllowed(options)) {
314313
return EMPTY_STRING;
315314
}
316315

317316
throw new SyntaxException(MSG_EXPECT_LITERAL, start);
318317
}
319318

320-
if (isImpliedStringLiteral) {
319+
if (JsonUrlOptions.isImpliedStringLiterals(options)) {
321320
return string(buf, text, start, stop, false);
322321
}
323322

@@ -373,18 +372,17 @@ static <V> V literal(
373372
int start,
374373
int stop,
375374
ValueFactory<V,?,?,?,?,?,?,?,?,?> factory,
376-
boolean isEmptyUnquotedStringOK,
377-
boolean isImpliedStringLiteral) {
375+
JsonUrlOptions options) {
378376

379377
if (stop <= start) {
380-
if (isEmptyUnquotedStringOK) {
378+
if (JsonUrlOptions.isEmptyUnquotedValueAllowed(options)) {
381379
return factory.getString("");
382380
}
383381

384382
throw new SyntaxException(MSG_EXPECT_LITERAL, start);
385383
}
386384

387-
if (isImpliedStringLiteral) {
385+
if (JsonUrlOptions.isImpliedStringLiterals(options)) {
388386
return factory.getString(
389387
string(buf, text, start, stop, false));
390388
}
@@ -745,17 +743,15 @@ public static int parseLiteralLength(CharSequence text) {
745743
* @param start start position in text
746744
* @param length number of characters to parse
747745
* @param factory a valid ValueFactory
748-
* @param isEmptyUnquotedStringOK if true, allow empty unquoted strings
749-
* @param isImpliedStringLiteral if true, assume all literals are strings
746+
* @param options parse options
750747
* @return an object for the parsed literal
751748
*/
752749
public static <V> V parseLiteral(
753750
CharSequence text,
754751
int start,
755752
int length,
756753
ValueFactory<V,?,?,?,?,?,?,?,?,?> factory,
757-
boolean isEmptyUnquotedStringOK,
758-
boolean isImpliedStringLiteral) {
754+
JsonUrlOptions options) {
759755

760756
//
761757
// Note: not checking length == 0 here. That case is handled properly
@@ -764,8 +760,9 @@ public static <V> V parseLiteral(
764760

765761
int stop = start + length;
766762

767-
final SyntaxException.Message errmsg = isEmptyUnquotedStringOK
768-
? null : MSG_EXPECT_LITERAL;
763+
final SyntaxException.Message errmsg =
764+
JsonUrlOptions.isEmptyUnquotedValueAllowed(options)
765+
? null : MSG_EXPECT_LITERAL;
769766

770767
parseLiteralLength(text, start, stop, errmsg);
771768

@@ -776,18 +773,17 @@ public static <V> V parseLiteral(
776773
start,
777774
stop,
778775
factory,
779-
isEmptyUnquotedStringOK,
780-
isImpliedStringLiteral);
776+
options);
781777
}
782778

783779
/**
784780
* Parse a single literal value.
785781
*
786782
* <p>This simply calls
787-
* {@link #parseLiteral(CharSequence, int, int, ValueFactory, boolean, boolean)
783+
* {@link #parseLiteral(CharSequence, int, int, ValueFactory, JsonUrlOptions)
788784
* parseLiteral(s, start, length, JavaValueFactory.PRIMITIVE, false, false)}.
789785
*
790-
* @see #parseLiteral(CharSequence, int, int, ValueFactory, boolean, boolean)
786+
* @see #parseLiteral(CharSequence, int, int, ValueFactory, JsonUrlOptions)
791787
* @see JavaValueFactory#PRIMITIVE
792788
*/
793789
public static Object parseLiteral(
@@ -800,18 +796,17 @@ public static Object parseLiteral(
800796
start,
801797
length,
802798
JavaValueFactory.PRIMITIVE,
803-
false,
804-
false);
799+
null);
805800
}
806801

807802
/**
808803
* Parse a single literal value.
809804
*
810805
* <p>This simply calls
811-
* {@link #parseLiteral(CharSequence, int, int, ValueFactory, boolean, boolean)
806+
* {@link #parseLiteral(CharSequence, int, int, ValueFactory, JsonUrlOptions)
812807
* parseLiteral(s, 0, s.length(), JavaValueFactory.PRIMITIVE, false, false)}.
813808
*
814-
* @see #parseLiteral(CharSequence, int, int, ValueFactory, boolean, boolean)
809+
* @see #parseLiteral(CharSequence, int, int, ValueFactory, JsonUrlOptions)
815810
* @see JavaValueFactory#PRIMITIVE
816811
*/
817812
public static Object parseLiteral(CharSequence text) {
@@ -820,22 +815,21 @@ public static Object parseLiteral(CharSequence text) {
820815
0,
821816
text.length(),
822817
JavaValueFactory.PRIMITIVE,
823-
false,
824-
false);
818+
null);
825819
}
826820

827821
/**
828822
* Parse a single literal value.
829823
*
830824
* <p>This simply calls
831-
* {@link #parseLiteral(CharSequence, int, int, ValueFactory, boolean, boolean)
825+
* {@link #parseLiteral(CharSequence, int, int, ValueFactory, JsonUrlOptions)
832826
* parseLiteral(s, 0, s.length(), factory, false, false)}.
833-
* @see #parseLiteral(CharSequence, int, int, ValueFactory, boolean, boolean)
827+
* @see #parseLiteral(CharSequence, int, int, ValueFactory, JsonUrlOptions)
834828
*/
835829
public static <V> V parseLiteral(
836830
CharSequence text,
837831
ValueFactory<V,?,?,?,?,?,?,?,?,?> factory) {
838-
return parseLiteral(text, 0, text.length(), factory, false, false);
832+
return parseLiteral(text, 0, text.length(), factory, null);
839833
}
840834

841835
/**
@@ -854,18 +848,21 @@ public static <T extends Appendable> boolean appendLiteral(// NOPMD - Cyclomatic
854848
int start,
855849
int end,
856850
boolean isKey,
857-
boolean isEmptyUnquotedStringOK,
858-
boolean isImpliedStringLiteral) throws IOException {
851+
JsonUrlOptions options) throws IOException {
859852

860853
if (end <= start) {
861854
//
862855
// empty string
863856
//
864-
if (isEmptyUnquotedStringOK) {
857+
boolean emptyOK = isKey
858+
? JsonUrlOptions.isEmptyUnquotedKeyAllowed(options)
859+
: JsonUrlOptions.isEmptyUnquotedValueAllowed(options);
860+
861+
if (emptyOK) {
865862
return false;
866863
}
867864

868-
if (isImpliedStringLiteral) {
865+
if (JsonUrlOptions.isImpliedStringLiterals(options)) {
869866
throw new IOException("implied strings: unexpected empty string");
870867
}
871868

@@ -876,7 +873,7 @@ public static <T extends Appendable> boolean appendLiteral(// NOPMD - Cyclomatic
876873
return true;
877874
}
878875

879-
if (isImpliedStringLiteral) {
876+
if (JsonUrlOptions.isImpliedStringLiterals(options)) {
880877
Encode.encode(dest, text, start, end, false, true);
881878
return true;
882879
}

module/jsonurl-core/src/main/java/org/jsonurl/JsonUrlTextAppender.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -185,17 +185,13 @@ public JsonUrlTextAppender<A,R> add(
185185
return addNull();
186186
}
187187

188-
boolean emptyOK = isKey
189-
? isEmptyUnquotedKeyAllowed() : isEmptyUnquotedValueAllowed();
190-
191188
JsonUrl.appendLiteral(
192189
out,
193190
text,
194191
start,
195192
end,
196193
isKey,
197-
emptyOK,
198-
isImpliedStringLiterals());
194+
this);
199195

200196
return this;
201197
}

module/jsonurl-core/src/main/java/org/jsonurl/ParseResultFacade.java

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,7 @@ interface ParseResultFacade<R> {
6868
/**
6969
* Add the given literal.
7070
*/
71-
void addLiteral(
72-
CharSequence text,
73-
int start,
74-
int stop,
75-
boolean isEmptyUnquotedStringOK,
76-
boolean isImpliedStringLiteral);
71+
void addLiteral(CharSequence text, int start, int stop);
7772

7873
/**
7974
* Add a single element array.
@@ -86,22 +81,12 @@ void addLiteral(
8681
* endArray();
8782
* </pre>
8883
*/
89-
void addSingleElementArray(
90-
CharSequence text,
91-
int start,
92-
int stop,
93-
boolean isEmptyUnquotedStringOK,
94-
boolean isImpliedStringLiteral);
84+
void addSingleElementArray(CharSequence text, int start, int stop);
9585

9686
/**
9787
* Add object key.
9888
*/
99-
void addObjectKey(
100-
CharSequence text,
101-
int start,
102-
int stop,
103-
boolean isEmptyUnquotedStringOK,
104-
boolean isImpliedStringLiteral);
89+
void addObjectKey(CharSequence text, int start, int stop);
10590

10691
/**
10792
* Add an array element.
@@ -121,12 +106,7 @@ void addObjectKey(
121106
/**
122107
* Get a top-level literal result.
123108
*/
124-
R getResult(
125-
CharSequence text,
126-
int start,
127-
int stop,
128-
boolean isEmptyUnquotedStringOK,
129-
boolean isImpliedStringLiteral);
109+
R getResult(CharSequence text, int start, int stop);
130110

131111
/**
132112
* Test if the given result is valid.
@@ -153,6 +133,5 @@ R getResult(
153133
ParseResultFacade<R> addMissingValue(
154134
CharSequence text,
155135
int start,
156-
int stop,
157-
boolean isImpliedStringLiteral);
136+
int stop);
158137
}

0 commit comments

Comments
 (0)