Skip to content

Commit 47a13f2

Browse files
Improved parser errors and warnings to include the resource the error was found in. Also improved Javadoc documentation.
1 parent 2e91abe commit 47a13f2

28 files changed

+585
-313
lines changed

core/src/main/java/gov/nist/secauto/metaschema/core/datatype/AbstractDataTypeAdapter.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.codehaus.stax2.evt.XMLEventFactory2;
2020

2121
import java.io.IOException;
22+
import java.net.URI;
2223

2324
import javax.xml.namespace.QName;
2425
import javax.xml.stream.XMLEventWriter;
@@ -91,15 +92,14 @@ public boolean isXmlMixed() {
9192
}
9293

9394
@Override
94-
public TYPE parse(XMLEventReader2 eventReader) throws IOException {
95+
public TYPE parse(XMLEventReader2 eventReader, URI resource) throws IOException {
9596
StringBuilder builder = new StringBuilder();
9697
XMLEvent nextEvent;
9798
try {
9899
while (!(nextEvent = eventReader.peek()).isEndElement()) {
99100
if (!nextEvent.isCharacters()) {
100-
throw new IOException(String.format("Invalid content '%s' at %s",
101-
XmlEventUtil.toString(nextEvent),
102-
XmlEventUtil.toString(nextEvent.getLocation())));
101+
throw new IOException(String.format("Invalid content %s",
102+
XmlEventUtil.toString(nextEvent, resource)));
103103
}
104104
Characters characters = nextEvent.asCharacters();
105105
builder.append(characters.getData());
@@ -115,7 +115,7 @@ public TYPE parse(XMLEventReader2 eventReader) throws IOException {
115115
throw new IOException(
116116
String.format("Malformed data '%s'%s. %s",
117117
value,
118-
XmlEventUtil.generateLocationMessage(nextEvent),
118+
XmlEventUtil.generateLocationMessage(nextEvent, resource),
119119
ex.getLocalizedMessage()),
120120
ex);
121121
}
@@ -129,12 +129,12 @@ public TYPE parse(XMLEventReader2 eventReader) throws IOException {
129129
* the string-based parsing method.
130130
*/
131131
@Override
132-
public TYPE parse(JsonParser parser) throws IOException {
132+
public TYPE parse(JsonParser parser, URI resource) throws IOException {
133133
String value = parser.getValueAsString();
134134
if (value == null) {
135135
throw new IOException(
136136
String.format("Unable to get null value as text%s",
137-
JsonUtil.generateLocationMessage(parser)));
137+
JsonUtil.generateLocationMessage(parser, resource)));
138138
}
139139
// skip over value
140140
parser.nextToken();
@@ -144,7 +144,7 @@ public TYPE parse(JsonParser parser) throws IOException {
144144
throw new IOException(
145145
String.format("Malformed data '%s'%s. %s",
146146
value,
147-
JsonUtil.generateLocationMessage(parser),
147+
JsonUtil.generateLocationMessage(parser, resource),
148148
ex.getLocalizedMessage()),
149149
ex);
150150
}
@@ -226,7 +226,7 @@ public ITEM_TYPE cast(IAnyAtomicItem item) {
226226
@NonNull
227227
protected ITEM_TYPE castInternal(@NonNull IAnyAtomicItem item) {
228228
// try string based casting as a fallback
229-
String itemString = null;
229+
String itemString;
230230
try {
231231
itemString = item.asString();
232232
TYPE value = parse(itemString);

core/src/main/java/gov/nist/secauto/metaschema/core/datatype/IDataTypeAdapter.java

Lines changed: 11 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919

2020
import java.io.IOException;
2121
import java.lang.reflect.Field;
22+
import java.net.URI;
2223
import java.util.List;
23-
import java.util.function.Supplier;
2424

2525
import javax.xml.namespace.QName;
2626
import javax.xml.stream.XMLEventWriter;
@@ -201,90 +201,30 @@ default boolean isAtomic() {
201201
*
202202
* @param eventReader
203203
* the XML parser used to read the parsed value
204+
* @param resource
205+
* the resource being parsed
204206
* @return the parsed value
205207
* @throws IOException
206208
* if a parsing error occurs
207209
*/
208210
// TODO: migrate code to XML parser implementation.
209211
@NonNull
210-
TYPE parse(@NonNull XMLEventReader2 eventReader) throws IOException;
212+
TYPE parse(@NonNull XMLEventReader2 eventReader, @NonNull URI resource) throws IOException;
211213

212214
/**
213215
* Parses a JSON property value.
214216
*
215217
* @param parser
216218
* the JSON parser used to read the parsed value
219+
* @param resource
220+
* the resource being parsed
217221
* @return the parsed value
218222
* @throws IOException
219223
* if a parsing error occurs
220224
*/
221225
// TODO: migrate code to JSON parser implementation.
222226
@NonNull
223-
TYPE parse(@NonNull JsonParser parser) throws IOException;
224-
225-
/**
226-
* Parses a provided string using {@link #parse(String)}.
227-
* <p>
228-
* This method may pre-parse the data and then return copies, since the data can
229-
* only be parsed once, but the supplier might be called multiple times.
230-
*
231-
* @param value
232-
* the string value to parse
233-
* @return a supplier that will provide new instances of the parsed data
234-
* @throws IOException
235-
* if an error occurs while parsing
236-
* @throws IllegalArgumentException
237-
* if the provided value is invalid based on the data type
238-
* @see #parse(String)
239-
*/
240-
@NonNull
241-
default Supplier<TYPE> parseAndSupply(@NonNull String value) throws IOException {
242-
TYPE retval = parse(value);
243-
return () -> copy(retval);
244-
}
245-
246-
/**
247-
* Parses a provided string using
248-
* {@link IDataTypeAdapter#parse(XMLEventReader2)}.
249-
* <p>
250-
* This method may pre-parse the data and then return copies, since the data can
251-
* only be parsed once, but the supplier might be called multiple times.
252-
*
253-
* @param eventReader
254-
* the XML parser used to read the parsed value
255-
* @return a supplier that will provide new instances of the parsed data
256-
* @throws IOException
257-
* if an error occurs while parsing
258-
* @see #parse(String)
259-
* @see #parse(XMLEventReader2)
260-
*/
261-
// TODO: migrate code to XML parser implementation.
262-
@NonNull
263-
default Supplier<TYPE> parseAndSupply(@NonNull XMLEventReader2 eventReader) throws IOException {
264-
TYPE retval = parse(eventReader);
265-
return () -> copy(retval);
266-
}
267-
268-
/**
269-
* Parses a provided string using {@link #parse(JsonParser)}.
270-
* <p>
271-
* This method may pre-parse the data and then return copies, since the data can
272-
* only be parsed once, but the supplier might be called multiple times.
273-
*
274-
* @param parser
275-
* the JSON parser used to read the parsed value
276-
* @return a supplier that will provide new instances of the parsed data
277-
* @throws IOException
278-
* if an error occurs while parsing
279-
* @see #parse(String)
280-
* @see #parse(JsonParser)
281-
*/
282-
// TODO: migrate code to JSON parser implementation.
283-
@NonNull
284-
default Supplier<TYPE> parseAndSupply(@NonNull JsonParser parser) throws IOException {
285-
TYPE retval = parse(parser);
286-
return () -> copy(retval);
287-
}
227+
TYPE parse(@NonNull JsonParser parser, @NonNull URI resource) throws IOException;
288228

289229
/**
290230
* Writes the provided Java class instance data as XML. The parent element
@@ -306,7 +246,10 @@ default Supplier<TYPE> parseAndSupply(@NonNull JsonParser parser) throws IOExcep
306246
* if an unexpected error occurred while writing to the output stream
307247
*/
308248
// TODO: migrate code to XML writer implementation.
309-
void writeXmlValue(@NonNull Object instance, @NonNull StartElement parent, @NonNull XMLEventFactory2 eventFactory,
249+
void writeXmlValue(
250+
@NonNull Object instance,
251+
@NonNull StartElement parent,
252+
@NonNull XMLEventFactory2 eventFactory,
310253
@NonNull XMLEventWriter eventWriter)
311254
throws IOException;
312255

core/src/main/java/gov/nist/secauto/metaschema/core/datatype/adapter/BooleanAdapter.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,18 @@
1919
import gov.nist.secauto.metaschema.core.util.ObjectUtils;
2020

2121
import java.io.IOException;
22+
import java.net.URI;
2223
import java.util.List;
2324

2425
import javax.xml.namespace.QName;
2526

2627
import edu.umd.cs.findbugs.annotations.NonNull;
2728

29+
/**
30+
* Support for the Metaschema <a href=
31+
* "https://pages.nist.gov/metaschema/specification/datatypes/#boolean">boolean</a>
32+
* data type.
33+
*/
2834
public class BooleanAdapter
2935
extends AbstractDataTypeAdapter<Boolean, IBooleanItem> {
3036
@NonNull
@@ -52,7 +58,7 @@ public Boolean parse(String value) {
5258
}
5359

5460
@Override
55-
public Boolean parse(JsonParser parser) throws IOException {
61+
public Boolean parse(JsonParser parser, URI resource) throws IOException {
5662
Boolean value = parser.getBooleanValue();
5763
// skip over value
5864
parser.nextToken();

core/src/main/java/gov/nist/secauto/metaschema/core/datatype/markup/MarkupLineAdapter.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,19 @@
1515
import org.codehaus.stax2.XMLEventReader2;
1616

1717
import java.io.IOException;
18+
import java.net.URI;
1819
import java.util.List;
1920

2021
import javax.xml.namespace.QName;
2122
import javax.xml.stream.XMLStreamException;
2223

2324
import edu.umd.cs.findbugs.annotations.NonNull;
2425

26+
/**
27+
* Support for the Metaschema <a href=
28+
* "https://pages.nist.gov/metaschema/specification/datatypes/#markup-line">markup-line</a>
29+
* data type.
30+
*/
2531
public class MarkupLineAdapter
2632
extends AbstractMarkupAdapter<MarkupLine> {
2733
@NonNull
@@ -47,16 +53,16 @@ public MarkupLine parse(String value) {
4753

4854
@SuppressWarnings("null")
4955
@Override
50-
public MarkupLine parse(XMLEventReader2 eventReader) throws IOException {
56+
public MarkupLine parse(XMLEventReader2 eventReader, URI resource) throws IOException {
5157
try {
52-
return XmlMarkupParser.instance().parseMarkupline(eventReader);
58+
return XmlMarkupParser.instance().parseMarkupline(eventReader, resource);
5359
} catch (XMLStreamException ex) {
5460
throw new IOException(ex);
5561
}
5662
}
5763

5864
@Override
59-
public MarkupLine parse(JsonParser parser) throws IOException {
65+
public MarkupLine parse(JsonParser parser, URI resource) throws IOException {
6066
@SuppressWarnings("null")
6167
MarkupLine retval = parse(parser.getValueAsString());
6268
// skip past value

core/src/main/java/gov/nist/secauto/metaschema/core/datatype/markup/MarkupMultilineAdapter.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,19 @@
1515
import org.codehaus.stax2.XMLEventReader2;
1616

1717
import java.io.IOException;
18+
import java.net.URI;
1819
import java.util.List;
1920

2021
import javax.xml.namespace.QName;
2122
import javax.xml.stream.XMLStreamException;
2223

2324
import edu.umd.cs.findbugs.annotations.NonNull;
2425

26+
/**
27+
* Support for the Metaschema <a href=
28+
* "https://pages.nist.gov/metaschema/specification/datatypes/#markup-line">markup-multiline</a>
29+
* data type.
30+
*/
2531
public class MarkupMultilineAdapter
2632
extends AbstractMarkupAdapter<MarkupMultiline> {
2733
@NonNull
@@ -52,16 +58,16 @@ public MarkupMultiline parse(String value) {
5258

5359
@SuppressWarnings("null")
5460
@Override
55-
public MarkupMultiline parse(XMLEventReader2 eventReader) throws IOException {
61+
public MarkupMultiline parse(XMLEventReader2 eventReader, URI resource) throws IOException {
5662
try {
57-
return XmlMarkupParser.instance().parseMarkupMultiline(eventReader);
63+
return XmlMarkupParser.instance().parseMarkupMultiline(eventReader, resource);
5864
} catch (XMLStreamException ex) {
5965
throw new IOException(ex);
6066
}
6167
}
6268

6369
@Override
64-
public MarkupMultiline parse(JsonParser parser) throws IOException {
70+
public MarkupMultiline parse(JsonParser parser, URI resource) throws IOException {
6571
@SuppressWarnings("null")
6672
MarkupMultiline retval = parse(parser.getValueAsString());
6773
// skip past value

0 commit comments

Comments
 (0)