Skip to content

Commit 9a57347

Browse files
committed
Perf
1 parent 27832fb commit 9a57347

File tree

5 files changed

+52
-15
lines changed

5 files changed

+52
-15
lines changed

src/main/java/com/networknt/schema/AbsoluteIri.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
import java.util.Objects;
1919

20+
import com.networknt.schema.utils.Strings;
21+
2022
/**
2123
* The absolute IRI is an IRI without the fragment.
2224
* <p>
@@ -131,7 +133,7 @@ public static String resolve(String parent, String iri) {
131133
}
132134
base = parent(base, scheme);
133135

134-
String[] iriParts = iri.split("/");
136+
String[] iriParts = Strings.split(iri, '/');
135137
for (int x = 0; x < iriParts.length; x++) {
136138
if ("..".equals(iriParts[x])) {
137139
base = parent(base, scheme);
@@ -149,9 +151,6 @@ public static String resolve(String parent, String iri) {
149151
}
150152
}
151153
}
152-
if (iri.endsWith("/")) {
153-
base = base + "/";
154-
}
155154
return base;
156155
}
157156
}

src/main/java/com/networknt/schema/SchemaLocation.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import com.networknt.schema.path.NodePath;
2424
import com.networknt.schema.path.PathType;
25+
import com.networknt.schema.utils.Strings;
2526

2627
/**
2728
* The schema location is the canonical IRI of the schema object plus a JSON
@@ -219,7 +220,8 @@ public static NodePath of(String fragmentString) {
219220
fragmentString = fragmentString.substring(1);
220221
}
221222
NodePath fragment = JSON_POINTER;
222-
String[] fragmentParts = fragmentString.split("/");
223+
// String[] fragmentParts = fragmentString.split("/");
224+
String[] fragmentParts = Strings.split(fragmentString, '/');
223225

224226
boolean jsonPointer = false;
225227
if (fragmentString.startsWith("/")) {
@@ -270,10 +272,6 @@ public static NodePath of(String fragmentString) {
270272
fragment = fragment.append(fragmentPartString);
271273
}
272274
}
273-
if (index == -1 && fragmentString.endsWith("/")) {
274-
// Trailing / in fragment
275-
fragment = fragment.append("");
276-
}
277275
return fragment;
278276
}
279277

src/main/java/com/networknt/schema/keyword/RecursiveRefValidator.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ static Schema getSchema(Schema parentSchema, ExecutionContext executionContext)
5454
Schema check = null;
5555
String base = null;
5656
String baseCheck = null;
57-
if (refSchema != null)
57+
if (refSchema != null) {
5858
base = current.getSchemaLocation().getAbsoluteIri() != null ? current.getSchemaLocation().getAbsoluteIri().toString() : "";
5959
if (current.isRecursiveAnchor()) {
6060
// Check dynamic scope
@@ -71,9 +71,6 @@ static Schema getSchema(Schema parentSchema, ExecutionContext executionContext)
7171
}
7272
}
7373
}
74-
if (refSchema != null) {
75-
System.out.println(refSchema);
76-
// refSchema = refSchema.fromRef(parentSchema, evaluationPath);
7774
}
7875
return refSchema;
7976
}

src/main/java/com/networknt/schema/keyword/RefValidator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,14 +157,14 @@ private static Schema getSchema(Schema parent,
157157
String refValue,
158158
String refValueOriginal
159159
) {
160-
// This should be processing json pointer fragments only
161-
NodePath fragment = SchemaLocation.Fragment.of(refValue);
162160
String schemaReference = resolve(parent, refValueOriginal);
163161
// ConcurrentHashMap computeIfAbsent does not allow calls that result in a
164162
// recursive update to the map.
165163
// The getSubSchema potentially recurses to call back to getJsonSchema again
166164
Schema result = schemaContext.getSchemaReferences().get(schemaReference);
167165
if (result == null) {
166+
// This should be processing json pointer fragments only
167+
NodePath fragment = SchemaLocation.Fragment.of(refValue);
168168
synchronized (schemaContext.getSchemaRegistry()) { // acquire lock on shared factory object to prevent deadlock
169169
result = schemaContext.getSchemaReferences().get(schemaReference);
170170
if (result == null) {

src/main/java/com/networknt/schema/utils/Strings.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616

1717
package com.networknt.schema.utils;
1818

19+
import java.util.ArrayList;
20+
import java.util.List;
21+
1922
/**
2023
* Utility methods for working with Strings.
2124
*/
@@ -126,4 +129,44 @@ public static boolean isNumeric(String string) {
126129
public static boolean isBlank(String string) {
127130
return null == string || string.trim().isEmpty();
128131
}
132+
133+
/**
134+
* Split text. Unlike the JDK String split using regex trailing delimiters are
135+
* preserved.
136+
*
137+
* @param text the text to split
138+
* @param delimiter the delimiter
139+
* @return the fragments
140+
*/
141+
public static String[] split(String text, char delimiter) {
142+
if (text == null) {
143+
return new String[0];
144+
}
145+
if (text.isEmpty()) {
146+
return new String[]{""};
147+
}
148+
149+
List<String> segments = new ArrayList<>();
150+
int start = 0;
151+
int end;
152+
153+
while (start <= text.length()) {
154+
end = text.indexOf(delimiter, start);
155+
156+
if (end == -1) {
157+
end = text.length();
158+
}
159+
160+
String segment = text.substring(start, end);
161+
segments.add(segment);
162+
163+
if (end == text.length()) {
164+
break;
165+
}
166+
167+
start = end + 1;
168+
}
169+
170+
return segments.toArray(new String[segments.size()]);
171+
}
129172
}

0 commit comments

Comments
 (0)