Skip to content

Commit 11351a3

Browse files
Fixing splitValues, small fix JussSerializer#from, finishing refactor of
VarParser and Converter + scope inheritance removed!
1 parent 269d20f commit 11351a3

File tree

6 files changed

+82
-58
lines changed

6 files changed

+82
-58
lines changed

SerialX-core/src/main/java/org/ugp/serialx/Serializer.java

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,8 @@
2828
import java.util.Map;
2929
import java.util.function.Function;
3030

31-
import org.ugp.serialx.converters.BooleanConverter;
32-
import org.ugp.serialx.converters.CharacterConverter;
3331
import org.ugp.serialx.converters.DataParser;
3432
import org.ugp.serialx.converters.DataParser.ParserRegistry;
35-
import org.ugp.serialx.converters.NullConverter;
36-
import org.ugp.serialx.converters.NumberConverter;
37-
import org.ugp.serialx.converters.SerializableBase64Converter;
3833
import org.ugp.serialx.converters.StringConverter;
3934
import org.ugp.serialx.protocols.SerializationProtocol;
4035
import org.ugp.serialx.protocols.SerializationProtocol.ProtocolRegistry;
@@ -56,7 +51,7 @@ public abstract class Serializer extends Scope
5651
*
5752
* @since 1.3.2
5853
*/
59-
public static final ParserRegistry COMMON_PARSERS = new ParserRegistry(new StringConverter(), /* TODO: new ProtocolConverter() */ new NumberConverter(), new BooleanConverter(), new CharacterConverter(), new NullConverter(), new SerializableBase64Converter());
54+
public static final ParserRegistry COMMON_PARSERS = DataParser.REGISTRY.clone();
6055

6156
protected ParserRegistry parsers;
6257
protected ProtocolRegistry protocols;
@@ -1064,29 +1059,28 @@ <th>Obtained serializer content (return)</th>
10641059
</tr>
10651060
</table>
10661061
*
1067-
* @throws Exception if calling of some {@link PropertyDescriptor}s write method fails (should not happen often) or when something went wrong during deserialization!
1062+
* @throws Exception if calling of some {@link PropertyDescriptor}s write method fails (should not happen often) or when something went very wrong during deserialization!
10681063
* @throws IntrospectionException when there were no PropertyDescriptor found for obj class!
1064+
* @throws RuntimeException in case of something went wrong during deserialization process and {@link LogProvider#isReThrowException()} is set to true!
10691065
*
10701066
* @since 1.3.5
10711067
*/
10721068
public static Serializer from(Serializer newInstance, Object fromObj, String... fieldNamesToUse) throws Exception, IntrospectionException
10731069
{
10741070
if (fromObj instanceof CharSequence)
10751071
{
1076-
if (indexOfNotInObj((CharSequence) fromObj, "http") == 0)
1077-
try
1078-
{
1079-
return newInstance.LoadFrom(new URL(fromObj.toString()).openStream());
1080-
}
1081-
catch (IOException e)
1082-
{}
1083-
10841072
try
10851073
{
1086-
return newInstance.LoadFrom(new File(fromObj.toString()));
1074+
String fromStr;
1075+
if (indexOfNotInObj(fromStr = fromObj.toString(), "http") == 0)
1076+
return newInstance.LoadFrom(new URL(fromStr).openStream());
1077+
return newInstance.LoadFrom(new File(fromStr));
10871078
}
10881079
catch (Exception e)
1089-
{}
1080+
{
1081+
if (e instanceof RuntimeException)
1082+
throw e;
1083+
}
10901084

10911085
return newInstance.LoadFrom((CharSequence) fromObj);
10921086
}

SerialX-core/src/main/java/org/ugp/serialx/Utils.java

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -468,42 +468,65 @@ public static StringBuilder multilpy(CharSequence str, int times)
468468
* @param s | String to split and check some syntax.
469469
* @param splitter | Chars where string will be split!
470470
*
471-
* @return String splitted after splitters. If there is more than one splitter in row, it will be taken as one whole!
471+
* @return String splitted after splitters. More than one splitter in row will be take as 1. Each resulting token will be {@link String#trim() trim}<code>med</code>!
472472
*
473473
* @since 1.0.0
474474
*/
475475
public static String[] splitValues(String s, char... splitter)
476476
{
477-
return splitValues(s, 0, true, splitter);
477+
return splitValues(s, 0, 2, splitter);
478478
}
479479

480480
/**
481481
* @param s | String to split and check some syntax.
482482
* @param limit | If 0 or less = no limit, 1 = no splitting, more than 1 = count of results!
483-
* @param oneOrMore | If true, string will be splitted after one or more splitters in row, if false splitting will occur only after single splitter (this is similar to "+" in regex)!
483+
* @param splittingStrategy | <b>If 0</b>, splitting will occur after each splitter!
484+
* <b>If 1</b>, string will be splitted after only one splitter, more than one splitters in row will be ignored!
485+
* <b>If 2</b>, splitting will occur after any number of splitters, n number of splitters in row will be treated as 1!
484486
* @param splitter | Chars where string will be split!
485487
*
486-
* @return String splitted after splitters according to arguments. If there is more than one splitter in row, it will be taken as one whole!
488+
* @return String splitted after splitters according to arguments. Each resulting token will be {@link String#trim() trim}<code>med</code>!
487489
*
488490
* @since 1.3.0
489491
*/
490-
public static String[] splitValues(String s, int limit, boolean splitAfterSingleCharOnly, char... splitter)
492+
public static String[] splitValues(String s, int limit, int splittingStrategy, char... splitter)
491493
{
492-
return splitValues(s, limit, splitAfterSingleCharOnly, new char[0], splitter);
494+
return splitValues(s, 0, limit, splittingStrategy, new char[0], splitter);
493495
}
494496

495497
/**
496498
* @param s | String to split and check some syntax.
497499
* @param limit | If 0 or less = no limit, 1 = no splitting, more than 1 = count of results!
498-
* @param oneOrMore | If true, string will be splitted after one or more splitters in row, if false splitting will occur only after single splitter (this is similar to "+" in regex)!
500+
* @param splittingStrategy | <b>If 0</b>, splitting will occur after each splitter!
501+
* <b>If 1</b>, string will be splitted after only one splitter, more than one splitters in row will be ignored!
502+
* <b>If 2</b>, splitting will occur after any number of splitters, n number of splitters in row will be treated as 1!
499503
* @param splitBreaks | When some of these characters is encountered, splitting is terminated for the rest of the string!
500504
* @param splitter | Chars where string will be split!
501505
*
502-
* @return String splitted after splitters according to arguments. If there is more than one splitter in row, it will be taken as one whole!
506+
* @return String splitted after splitters according to arguments. Each resulting token will be {@link String#trim() trim}<code>med</code>!
503507
*
504508
* @since 1.3.5
505509
*/
506-
public static String[] splitValues(String s, int limit, boolean oneOrMore, char[] splitBreaks, char... splitter) //TODO: This bs is terribly broken! Idk what I was doing back then but its not doing what I would assume at all...
510+
public static String[] splitValues(String s, int limit, int splittingStrategy, char[] splitBreaks, char... splitter)
511+
{
512+
return splitValues(s, 0, limit, splittingStrategy, splitBreaks, splitter);
513+
}
514+
515+
/**
516+
* @param s | String to split and check some syntax.
517+
* @param i | Index of character to start at. Note that everything before this index will be ignored by other options...
518+
* @param limit | If 0 or less = no limit, 1 = no splitting, more than 1 = count of results!
519+
* @param splittingStrategy | <b>If 0</b>, splitting will occur after each splitter!
520+
* <b>If 1</b>, string will be splitted after only one splitter, more than one splitters in row will be ignored!
521+
* <b>If 2</b>, splitting will occur after any number of splitters, n number of splitters in row will be treated as 1!
522+
* @param splitBreaks | When some of these characters is encountered, splitting is terminated for the rest of the string!
523+
* @param splitter | Chars where string will be split!
524+
*
525+
* @return String splitted after splitters according to arguments. Each resulting token will be {@link String#trim() trim}<code>med</code>!
526+
*
527+
* @since 1.3.7
528+
*/
529+
public static String[] splitValues(String s, int i, int limit, int splittingStrategy, char[] splitBreaks, char... splitter)
507530
{
508531
if (splitter.length <= 0 || limit == 1)
509532
return new String[] {s};
@@ -512,9 +535,9 @@ public static String[] splitValues(String s, int limit, boolean oneOrMore, char[
512535
// return splitValues(" "+s, limit, oneOrMore, splitBreaks, splitter);
513536

514537
List<String> result = new ArrayList<>();
515-
516-
int brackets = 0, quote = 0, lastIndex = 0;
517-
for (int i = 0, count = 1, len = s.length(), oldCh = splitter[0]; i < len && (limit <= 0 || count < limit); i++)
538+
539+
int brackets = 0, quote = 0, lastIndex = 0, len = s.length();
540+
for (int count = 1, oldCh = 0; i < len && (limit <= 0 || count < limit); i++)
518541
{
519542
char ch = s.charAt(i);
520543
if (ch == '"')
@@ -528,10 +551,17 @@ public static String[] splitValues(String s, int limit, boolean oneOrMore, char[
528551
break;
529552
}
530553

531-
if (brackets == 0 && oldCh != ch && isOneOf(ch, splitter) && (oneOrMore || (i >= len-1 || !isOneOf(s.charAt(i+1), splitter))))
554+
if (brackets == 0 && isOneOf(ch, splitter) &&
555+
(splittingStrategy != 1 || ch != oldCh && (i >= len-1 || !isOneOf(s.charAt(i+1), splitter))))
532556
{
533-
result.add(s.substring(lastIndex == 0 ? 0 : lastIndex + 1, lastIndex = i).trim());
534-
count++;
557+
String tok = s.substring(lastIndex, i).trim();
558+
if (splittingStrategy < 2 || result.isEmpty() || !tok.isEmpty())
559+
{
560+
result.add(tok);
561+
lastIndex = i + 1;
562+
563+
count++;
564+
}
535565
}
536566
else if ((ch | ' ') == '{')
537567
brackets++;
@@ -545,14 +575,14 @@ else if ((ch | ' ') == '}')
545575
}
546576
oldCh = ch;
547577
}
548-
578+
549579
if (brackets > 0)
550580
throw new IllegalArgumentException("Unclosed brackets in: " + s);
551581
else if (quote % 2 != 0)
552582
throw new IllegalArgumentException("Unclosed or missing quotes in: " + s);
553583
else
554584
{
555-
result.add(s.substring(lastIndex == 0 ? 0 : lastIndex + 1, s.length()).trim());
585+
result.add(s.substring(lastIndex, len).trim());
556586
}
557587

558588
return result.toArray(new String[0]);

SerialX-core/src/main/java/org/ugp/serialx/converters/VariableParser.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
import org.ugp.serialx.Serializer;
1111

1212
/**
13-
* This parser is capable of reading variables from {@link GenericScope} by using "$"!
14-
* {@link VariableConverter#parse(String, Object...)} required one additional Scope argument in args... at index 0!<br>
15-
* It also manages access member operator also known as separator <code>"."</code>.
13+
* This parser is capable of reading variables from {@link GenericScope} by using <code>$</code>!<br>
14+
* {@link VariableParser#parse(String, Object...)} requires one additional Scope argument in args... at index 0!<br>
15+
* It also manages access member operator also known as separator <code>"."</code>.<br>
1616
* Its case sensitive!<br>
1717
* Exact outputs of this converter are based on inserted scope!
1818
*
@@ -71,9 +71,10 @@ else if (newModif = str.endsWith("::new"))
7171
str = str.substring(0, str.length()-5);
7272

7373
Object obj = null;
74-
if (str.indexOf('.') > -1)
74+
int op0Index;
75+
if ((op0Index = str.indexOf('.')) > -1)
7576
{
76-
String[] path = splitValues(str, '.');
77+
String[] path = splitValues(str, op0Index, 0, 0, new char[0], '.');
7778
int iLast = path.length-1;
7879

7980
backlook: do

SerialX-juss/src/main/java/org/ugp/serialx/juss/JussSerializer.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -497,10 +497,6 @@ else if (ch == '}' || ch == ']')
497497

498498
if (parent == null)
499499
getImports().removeImportsOf(this);
500-
// else
501-
// for (Map.Entry<?, ?> ent : parent.varEntrySet())
502-
// if (variables().get(ent.getKey()) == ent.getValue())
503-
// variables().remove(ent.getKey());//TODO: Prevent neccesity of scope parent inheritance.
504500
return (S) this;
505501
}
506502

SerialX-juss/src/main/java/org/ugp/serialx/juss/converters/ObjectConverter.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,9 @@ public Object parse(ParserRegistry myHomeRegistry, String str, Object... compile
111111
scope = getPreferredSerializer();
112112
}
113113

114-
//TODO: Prevent neccesity of scope parent inheritance.
115114
compilerArgs = compilerArgs.clone();
116115
compilerArgs[0] = false; //No extra formating...
117-
return str.isEmpty() ? scope : ((Serializer) scope/*.inheritParent()*/).LoadFrom(new StringReader(str), compilerArgs);
116+
return str.isEmpty() ? scope : scope.LoadFrom(new StringReader(str), compilerArgs);
118117
}
119118
}
120119
return CONTINUE;

SerialX-juss/src/main/java/org/ugp/serialx/juss/converters/VariableConverter.java

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
import org.ugp.serialx.converters.VariableParser;
1515

1616
/**
17-
* This converter is capable of converting {@link Map.Entry} and reading variables from {@link GenericScope} by using "$"!
18-
* {@link VariableConverter#parse(String, Object...)} required one additional Scope argument in args... at index 0!<br>
17+
* This converter is capable of converting {@link Map.Entry} and reading variables from {@link GenericScope} by using <code>$</code>!<br>
18+
* {@link VariableConverter#parse(String, Object...)} requires one additional Scope argument in args... at index 0!<br>
1919
* It manages assign operator <code>=</code> as well as access member operator also known as separator <code>"."</code>.<br>
2020
* Its case sensitive!<br>
2121
* Exact outputs of this converter are based on inserted scope!
@@ -59,13 +59,17 @@ public Object parse(ParserRegistry myHomeRegistry, String arg, Object... args)
5959
if (args.length > 0 && arg.length() > 0 && args[0] instanceof GenericScope)
6060
{
6161
GenericScope<?, Object> scope = (GenericScope<?, Object>) args[0];
62-
if (isVarAssignment(arg))
62+
int op0Index;
63+
if ((op0Index = isVarAssignment(arg)) > -1)
6364
{
6465
boolean getValueModif = arg.charAt(0) == '$', genericVar = args.length > 4 && args[4] == GenericScope.class;
6566
if (getValueModif)
67+
{
6668
arg = arg.substring(1);
69+
op0Index--;
70+
}
6771

68-
String vars[] = splitValues(arg, 0, false, new char[] {'?'}, '=', ':'), valStr;
72+
String vars[] = splitValues(arg, op0Index, 0, 1, new char[] {'?'}, '=', ':'), valStr;
6973

7074
Object val = null;
7175
int iVal = vars.length-1;
@@ -74,14 +78,14 @@ public Object parse(ParserRegistry myHomeRegistry, String arg, Object... args)
7478
val = myHomeRegistry.parse(valStr, args);
7579
}
7680

77-
eachVar: for (int i = 0; i < iVal; i++) // Support for assigning multiple vars to the same value...
81+
eachVar: for (int i = 0; i < iVal; i++) // Support for assigning multiple vars to the same value... Yea this is not the prettiest code but it does the job and mainly it does it fast so shut up!
7882
{
7983
String var = vars[i];
80-
if (!genericVar && contains(var, ' '))
84+
if (!genericVar && contains(var, ' '))
8185
LogProvider.instance.logErr("Variable name \"" + var + "\" is invalid, blank characters are not allowed!", null);
82-
else if (var.indexOf('.') > -1)
86+
else if ((op0Index = var.indexOf('.')) > -1)
8387
{
84-
String[] path = splitValues(var, '.');
88+
String[] path = splitValues(var, op0Index, 0, 0, new char[0], '.');
8589
int iLast = path.length-1, j = 0;
8690

8791
backlook: do
@@ -188,11 +192,11 @@ public void setJsonStyle(boolean jsonStyle)
188192
/**
189193
* @param s | CharSequence to search!
190194
*
191-
* @return true if inserted expression is variable assignment expression such as <code>variable = 4</code> otherwise false!
195+
* @return Index of first assignment operator ('=' or ':') if inserted expression is variable assignment expression such as <code>variable = 4</code>, otherwise -1!
192196
*
193197
* @since 1.3.0
194198
*/
195-
public static boolean isVarAssignment(CharSequence s)
199+
public static int isVarAssignment(CharSequence s)
196200
{
197201
for (int i = 0, brackets = 0, quote = 0, len = s.length(), oldCh = -1, chNext; i < len; i++)
198202
{
@@ -203,9 +207,9 @@ public static boolean isVarAssignment(CharSequence s)
203207
if (quote % 2 == 0)
204208
{
205209
if (ch == '?')
206-
return false;
210+
return -1;
207211
else if (brackets == 0 && (ch == '=' || ch == ':') && !(oldCh == '=' || oldCh == ':' || oldCh == '!' || oldCh == '>'|| oldCh == '<') && (i >= len-1 || !((chNext = s.charAt(i+1)) == '=' || chNext == ':' || chNext == '!' || chNext == '>'|| chNext == '<')))
208-
return true;
212+
return i;
209213
else if ((ch | ' ') == '{')
210214
brackets++;
211215
else if ((ch | ' ') == '}')
@@ -214,7 +218,7 @@ else if ((ch | ' ') == '}')
214218
}
215219
oldCh = ch;
216220
}
217-
return false;
221+
return -1;
218222
}
219223

220224
// public static <K, V> V getValueOf(GenericScope<K, V> scope, K... pathToScope)

0 commit comments

Comments
 (0)