Refactor "focus" ValidatedFragment usage

This commit is contained in:
dotasek 2023-09-21 13:59:41 -04:00
parent ba8a4797e5
commit c79fa08473
6 changed files with 77 additions and 75 deletions

View File

@ -50,9 +50,9 @@ public class FmlParser extends ParserBase {
ByteArrayInputStream stream = new ByteArrayInputStream(content);
String text = TextFile.streamToString(stream);
List<ValidatedFragment> result = new ArrayList<>();
ValidatedFragment ctxt = new ValidatedFragment("focus", "fml", content);
ctxt.setElement(parse(ctxt.getErrors(), text));
result.add(ctxt);
ValidatedFragment focusFragment = new ValidatedFragment(ValidatedFragment.FOCUS_NAME, "fml", content);
focusFragment.setElement(parse(focusFragment.getErrors(), text));
result.add(focusFragment);
return result;
}

View File

@ -122,7 +122,7 @@ public class JsonParser extends ParserBase {
public List<ValidatedFragment> parse(InputStream inStream) throws IOException, FHIRException {
// long start = System.currentTimeMillis();
byte[] content = TextFile.streamToBytes(inStream);
ValidatedFragment ctxt = new ValidatedFragment("focus", "json", content);
ValidatedFragment focusFragment = new ValidatedFragment(ValidatedFragment.FOCUS_NAME, "json", content);
ByteArrayInputStream stream = new ByteArrayInputStream(content);
@ -134,17 +134,17 @@ public class JsonParser extends ParserBase {
try {
obj = org.hl7.fhir.utilities.json.parser.JsonParser.parseObject(source, true, true);
} catch (Exception e) {
logError(ctxt.getErrors(), ValidationMessage.NO_RULE_DATE, -1, -1,context.formatMessage(I18nConstants.DOCUMENT), IssueType.INVALID, context.formatMessage(I18nConstants.ERROR_PARSING_JSON_, e.getMessage()), IssueSeverity.FATAL);
logError(focusFragment.getErrors(), ValidationMessage.NO_RULE_DATE, -1, -1,context.formatMessage(I18nConstants.DOCUMENT), IssueType.INVALID, context.formatMessage(I18nConstants.ERROR_PARSING_JSON_, e.getMessage()), IssueSeverity.FATAL);
}
} else {
obj = org.hl7.fhir.utilities.json.parser.JsonParser.parseObject(source, true, true);
}
if (obj != null) {
ctxt.setElement(parse(ctxt.getErrors(), obj));
focusFragment.setElement(parse(focusFragment.getErrors(), obj));
}
List<ValidatedFragment> res = new ArrayList<>();
res.add(ctxt);
res.add(focusFragment);
// long t =System.currentTimeMillis()-start;
// System.out.println("json parser: "+(t)+"ms, "+(content.length/1024)+"kb "+(t == 0 ? "" : " @ "+(content.length / t)+"kb/s"));

View File

@ -2,34 +2,34 @@ package org.hl7.fhir.r5.elementmodel;
import java.io.ByteArrayInputStream;
/*
Copyright (c) 2011+, HL7, Inc.
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of HL7 nor the names of its contributors may be used to
endorse or promote products derived from this software without specific
prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
/*
Copyright (c) 2011+, HL7, Inc.
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of HL7 nor the names of its contributors may be used to
endorse or promote products derived from this software without specific
prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
@ -82,7 +82,7 @@ public class TurtleParser extends ParserBase {
@Override
public List<ValidatedFragment> parse(InputStream inStream) throws IOException, FHIRException {
byte[] content = TextFile.streamToBytes(inStream);
ValidatedFragment ctxt = new ValidatedFragment("focus", "ttl", content);
ValidatedFragment focusFragment = new ValidatedFragment(ValidatedFragment.FOCUS_NAME, "ttl", content);
ByteArrayInputStream stream = new ByteArrayInputStream(content);
Turtle src = new Turtle();
@ -90,16 +90,16 @@ public class TurtleParser extends ParserBase {
try {
src.parse(TextFile.streamToString(stream));
} catch (Exception e) {
logError(ctxt.getErrors(), ValidationMessage.NO_RULE_DATE, -1, -1, "(document)", IssueType.INVALID, context.formatMessage(I18nConstants.ERROR_PARSING_TURTLE_, e.getMessage()), IssueSeverity.FATAL);
logError(focusFragment.getErrors(), ValidationMessage.NO_RULE_DATE, -1, -1, "(document)", IssueType.INVALID, context.formatMessage(I18nConstants.ERROR_PARSING_TURTLE_, e.getMessage()), IssueSeverity.FATAL);
return null;
}
ctxt.setElement(parse(ctxt.getErrors(), src));
focusFragment.setElement(parse(focusFragment.getErrors(), src));
} else {
src.parse(TextFile.streamToString(stream));
ctxt.setElement(parse(ctxt.getErrors(), src));
focusFragment.setElement(parse(focusFragment.getErrors(), src));
}
List<ValidatedFragment> res = new ArrayList<>();
res.add(ctxt);
res.add(focusFragment);
return res;
}

View File

@ -10,6 +10,8 @@ public class ValidatedFragment {
private String extension;
private Element element;
private byte[] content;
public final static String FOCUS_NAME = "focus";
private List<ValidationMessage> errors = new ArrayList<>();
public ValidatedFragment(String name, String extension, Element element, byte[] content) {

View File

@ -1,33 +1,33 @@
package org.hl7.fhir.r5.elementmodel;
/*
Copyright (c) 2011+, HL7, Inc.
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of HL7 nor the names of its contributors may be used to
endorse or promote products derived from this software without specific
prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
/*
Copyright (c) 2011+, HL7, Inc.
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of HL7 nor the names of its contributors may be used to
endorse or promote products derived from this software without specific
prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
@ -465,7 +465,7 @@ public class VerticalBarParser extends ParserBase {
while (!reader.isFinished()) // && (getOptions().getSegmentLimit() == 0 || getOptions().getSegmentLimit() > message.getSegments().size()))
readSegment(message, reader);
List<ValidatedFragment> res = new ArrayList<>();
res.add(new ValidatedFragment("focus", "hl7", message, content));
res.add(new ValidatedFragment(ValidatedFragment.FOCUS_NAME, "hl7", message, content));
return res;
}

View File

@ -114,7 +114,7 @@ public class XmlParser extends ParserBase {
public List<ValidatedFragment> parse(InputStream inStream) throws FHIRFormatError, DefinitionException, FHIRException, IOException {
byte[] content = TextFile.streamToBytes(inStream);
ValidatedFragment context = new ValidatedFragment("focus", "xml", content);
ValidatedFragment focusFragment = new ValidatedFragment(ValidatedFragment.FOCUS_NAME, "xml", content);
ByteArrayInputStream stream = new ByteArrayInputStream(content);
Document doc = null;
@ -134,7 +134,7 @@ public class XmlParser extends ParserBase {
// if we can, we'll inspect the header/encoding ourselves
stream.mark(1024);
version = checkHeader(context.getErrors(), stream);
version = checkHeader(focusFragment.getErrors(), stream);
stream.reset();
// use a slower parser that keeps location data
@ -168,17 +168,17 @@ public class XmlParser extends ParserBase {
if (e.getMessage().contains("lineNumber:") && e.getMessage().contains("columnNumber:")) {
int line = Utilities.parseInt(extractVal(e.getMessage(), "lineNumber"), 0);
int col = Utilities.parseInt(extractVal(e.getMessage(), "columnNumber"), 0);
logError(context.getErrors(), ValidationMessage.NO_RULE_DATE, line, col, "(xml)", IssueType.INVALID, e.getMessage().substring(e.getMessage().lastIndexOf(";")+1).trim(), IssueSeverity.FATAL);
logError(focusFragment.getErrors(), ValidationMessage.NO_RULE_DATE, line, col, "(xml)", IssueType.INVALID, e.getMessage().substring(e.getMessage().lastIndexOf(";")+1).trim(), IssueSeverity.FATAL);
} else {
logError(context.getErrors(), ValidationMessage.NO_RULE_DATE, 0, 0, "(xml)", IssueType.INVALID, e.getMessage(), IssueSeverity.FATAL);
logError(focusFragment.getErrors(), ValidationMessage.NO_RULE_DATE, 0, 0, "(xml)", IssueType.INVALID, e.getMessage(), IssueSeverity.FATAL);
}
doc = null;
}
if (doc != null) {
context.setElement(parse(context.getErrors(), doc));
focusFragment.setElement(parse(focusFragment.getErrors(), doc));
}
List<ValidatedFragment> res = new ArrayList<>();
res.add(context);
res.add(focusFragment);
return res;
}