From 975653e07dc8577ef81d39c79829ff9b0c96c382 Mon Sep 17 00:00:00 2001 From: Lloyd McKenzie Date: Thu, 29 Aug 2024 21:17:20 -0600 Subject: [PATCH 1/4] Added support for tracking whether elements are ellipsed and for rendering XHTML versions of XML and JSON content with ellipsed elements --- .../org/hl7/fhir/r5/elementmodel/Element.java | 10 + .../hl7/fhir/r5/elementmodel/JsonParser.java | 77 ++- .../hl7/fhir/r5/elementmodel/XmlParser.java | 187 ++++--- .../org/hl7/fhir/r5/formats/JsonCreator.java | 91 ++-- .../fhir/r5/formats/JsonCreatorCanonical.java | 514 +++++++++--------- .../fhir/r5/formats/JsonCreatorDirect.java | 439 +++++++-------- .../hl7/fhir/r5/formats/JsonCreatorGson.java | 207 +++---- 7 files changed, 830 insertions(+), 695 deletions(-) diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/elementmodel/Element.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/elementmodel/Element.java index c6254b682..1aaecc1c9 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/elementmodel/Element.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/elementmodel/Element.java @@ -162,6 +162,7 @@ public class Element extends Base implements NamedItem { private FhirFormat format; private Object nativeObject; private List sliceDefinitions; + private boolean ellipsed; public Element(String name) { super(); @@ -1429,6 +1430,8 @@ public class Element extends Base implements NamedItem { public Base copy() { Element element = new Element(this); this.copyValues(element); + if (this.isEllipsed()) + element.setEllipsed(true); return element; } @@ -1638,4 +1641,11 @@ public class Element extends Base implements NamedItem { return FhirPublication.fromCode(property.getStructure().getVersion()); } + public void setEllipsed(boolean ellipsed) { + this.ellipsed = ellipsed; + } + + public boolean isEllipsed() { + return this.ellipsed; + } } \ No newline at end of file diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/elementmodel/JsonParser.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/elementmodel/JsonParser.java index a11c0c9f1..f8f77e387 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/elementmodel/JsonParser.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/elementmodel/JsonParser.java @@ -62,6 +62,7 @@ import org.hl7.fhir.r5.formats.JsonCreatorDirect; import org.hl7.fhir.r5.model.ElementDefinition.TypeRefComponent; import org.hl7.fhir.r5.model.ElementDefinition; import org.hl7.fhir.r5.model.StructureDefinition; +import org.hl7.fhir.r5.utils.ToolingExtensions; import org.hl7.fhir.utilities.CommaSeparatedStringBuilder; import org.hl7.fhir.utilities.StringPair; import org.hl7.fhir.utilities.TextFile; @@ -85,6 +86,8 @@ public class JsonParser extends ParserBase { private JsonCreator json; private boolean allowComments; + private boolean ellipseElements; + private boolean suppressResourceType; private Element baseElement; private boolean markedXhtml; @@ -782,7 +785,8 @@ public class JsonParser extends ParserBase { } checkComposeComments(e); json.beginObject(); - prop("resourceType", e.getType(), null); + if (!isSuppressResourceType()) + prop("resourceType", e.getType(), null); Set done = new HashSet(); for (Element child : e.getChildren()) { compose(e.getName(), e, done, child); @@ -807,7 +811,8 @@ public class JsonParser extends ParserBase { checkComposeComments(e); json.beginObject(); - prop("resourceType", e.getType(), linkResolver == null ? null : linkResolver.resolveProperty(e.getProperty())); + if (!isSuppressResourceType()) + prop("resourceType", e.getType(), linkResolver == null ? null : linkResolver.resolveProperty(e.getProperty())); Set done = new HashSet(); for (Element child : e.getChildren()) { compose(e.getName(), e, done, child); @@ -821,15 +826,50 @@ public class JsonParser extends ParserBase { if (wantCompose(path, child)) { boolean isList = child.hasElementProperty() ? child.getElementProperty().isList() : child.getProperty().isList(); if (!isList) {// for specials, ignore the cardinality of the stated type - compose(path, child); + if (child.isEllipsed() && ellipseElements) + json.ellipse(); + else + compose(path, child); } else if (!done.contains(child.getName())) { done.add(child.getName()); List list = e.getChildrenByName(child.getName()); - composeList(path, list); + if (child.getProperty().getDefinition().hasExtension(ToolingExtensions.EXT_JSON_PROP_KEY)) + composeKeyList(path, list); + else + composeList(path, list); } } } + private void composeKeyList(String path, List list) throws IOException { + String keyName = list.get(0).getProperty().getDefinition().getExtensionString(ToolingExtensions.EXT_JSON_PROP_KEY); + json.name(list.get(0).getName()); + json.beginObject(); + for (Element e: list) { + Element key = null; + Element value = null; + for (Element child: e.getChildren()) { + if (child.getName().equals(keyName)) + key = child; + else + value = child; + } + if (value.isPrimitive()) + primitiveValue(key.getValue(), value); + else { + json.name(key.getValue()); + checkComposeComments(e); + json.beginObject(); + Set done = new HashSet(); + for (Element child : value.getChildren()) { + compose(value.getName(), value, done, child); + } + json.endObject(); + compose(path + "." + key.getValue(), value); + } + } + json.endObject(); + } private void composeList(String path, List list) throws IOException { // there will be at least one element @@ -847,7 +887,9 @@ public class JsonParser extends ParserBase { if (prim) { openArray(name, linkResolver == null ? null : linkResolver.resolveProperty(list.get(0).getProperty())); for (Element item : list) { - if (item.hasValue()) { + if (item.isEllipsed()) + json.ellipse(); + else if (item.hasValue()) { if (linkResolver != null && item.getProperty().isReference()) { String ref = linkResolver.resolveReference(getReferenceForElement(item)); if (ref != null) { @@ -866,7 +908,9 @@ public class JsonParser extends ParserBase { openArray(name, linkResolver == null ? null : linkResolver.resolveProperty(list.get(0).getProperty())); int i = 0; for (Element item : list) { - if (item.hasChildren()) { + if (item.isEllipsed()) + json.ellipse(); + else if (item.hasChildren()) { open(null,null); if (item.getProperty().isResource()) { prop("resourceType", item.getType(), linkResolver == null ? null : linkResolver.resolveType(item.getType())); @@ -933,9 +977,10 @@ public class JsonParser extends ParserBase { json.externalLink(ref); } } + Set done = new HashSet(); for (Element child : element.getChildren()) { - compose(path+"."+element.getName(), element, done, child); + compose(path + "." + element.getName(), element, done, child); } close(); } @@ -951,5 +996,23 @@ public class JsonParser extends ParserBase { return this; } + public boolean isEllipseElements() { + return ellipseElements; + } + + public JsonParser setEllipseElements(boolean ellipseElements) { + this.ellipseElements = ellipseElements; + return this; + } + + public boolean isSuppressResourceType() { + return suppressResourceType; + } + + public JsonParser setSuppressResourceType(boolean suppressResourceType) { + this.suppressResourceType = suppressResourceType; + return this; + } + } \ No newline at end of file diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/elementmodel/XmlParser.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/elementmodel/XmlParser.java index adf06a789..ce65e2a0c 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/elementmodel/XmlParser.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/elementmodel/XmlParser.java @@ -94,6 +94,7 @@ import org.xml.sax.XMLReader; public class XmlParser extends ParserBase { private boolean allowXsiLocation; private String version; + private boolean ellipseElements; public XmlParser(IWorkerContext context) { super(context); @@ -805,92 +806,125 @@ public class XmlParser extends ParserBase { } private void composeElement(IXMLWriter xml, Element element, String elementName, boolean root) throws IOException, FHIRException { - if (showDecorations) { - @SuppressWarnings("unchecked") - List decorations = (List) element.getUserData("fhir.decorations"); - if (decorations != null) - for (ElementDecoration d : decorations) - xml.decorate(d); - } - for (String s : element.getComments()) { - xml.comment(s, true); + if (!(isEllipseElements() && element.isEllipsed())) { + if (showDecorations) { + @SuppressWarnings("unchecked") + List decorations = (List) element.getUserData("fhir.decorations"); + if (decorations != null) + for (ElementDecoration d : decorations) + xml.decorate(d); + } + for (String s : element.getComments()) { + xml.comment(s, true); + } } if (isText(element.getProperty())) { - if (linkResolver != null) - xml.link(linkResolver.resolveProperty(element.getProperty())); - xml.enter(element.getProperty().getXmlNamespace(),elementName); - if (linkResolver != null && element.getProperty().isReference()) { - String ref = linkResolver.resolveReference(getReferenceForElement(element)); - if (ref != null) { - xml.externalLink(ref); + if (isEllipseElements() && element.isEllipsed()) + xml.ellipse(); + else { + if (linkResolver != null) + xml.link(linkResolver.resolveProperty(element.getProperty())); + xml.enter(element.getProperty().getXmlNamespace(),elementName); + if (linkResolver != null && element.getProperty().isReference()) { + String ref = linkResolver.resolveReference(getReferenceForElement(element)); + if (ref != null) { + xml.externalLink(ref); + } } + xml.text(element.getValue()); + xml.exit(element.getProperty().getXmlNamespace(),elementName); } - xml.text(element.getValue()); - xml.exit(element.getProperty().getXmlNamespace(),elementName); } else if (!element.hasChildren() && !element.hasValue()) { - if (element.getExplicitType() != null) - xml.attribute("xsi:type", element.getExplicitType()); - xml.element(elementName); + if (isEllipseElements() && element.isEllipsed()) + xml.ellipse(); + else { + if (element.getExplicitType() != null) + xml.attribute("xsi:type", element.getExplicitType()); + xml.element(elementName); + } } else if (element.isPrimitive() || (element.hasType() && isPrimitive(element.getType()))) { if (element.getType().equals("xhtml")) { - String rawXhtml = element.getValue(); - if (isCdaText(element.getProperty())) { - new CDANarrativeFormat().convert(xml, new XhtmlParser().parseFragment(rawXhtml)); - } else { - xml.escapedText(rawXhtml); - if (!markedXhtml) { - xml.anchor("end-xhtml"); - markedXhtml = true; + if (isEllipseElements() && element.isEllipsed()) + xml.ellipse(); + else { + String rawXhtml = element.getValue(); + if (isCdaText(element.getProperty())) { + new CDANarrativeFormat().convert(xml, new XhtmlParser().parseFragment(rawXhtml)); + } else { + xml.escapedText(rawXhtml); + if (!markedXhtml) { + xml.anchor("end-xhtml"); + markedXhtml = true; + } } } } else if (isText(element.getProperty())) { - if (linkResolver != null) - xml.link(linkResolver.resolveProperty(element.getProperty())); - xml.text(element.getValue()); - } else { - setXsiTypeIfIsTypeAttr(xml, element); - if (element.hasValue()) { + if (isEllipseElements() && element.isEllipsed()) + xml.ellipse(); + else { if (linkResolver != null) - xml.link(linkResolver.resolveType(element.getType())); - xml.attribute("value", element.getValue()); + xml.link(linkResolver.resolveProperty(element.getProperty())); + xml.text(element.getValue()); } - if (linkResolver != null) - xml.link(linkResolver.resolveProperty(element.getProperty())); - if (element.hasChildren()) { - xml.enter(element.getProperty().getXmlNamespace(), elementName); - if (linkResolver != null && element.getProperty().isReference()) { - String ref = linkResolver.resolveReference(getReferenceForElement(element)); - if (ref != null) { - xml.externalLink(ref); - } + } else { + if (isEllipseElements() && element.isEllipsed()) + xml.attributeEllipse(); + else { + setXsiTypeIfIsTypeAttr(xml, element); + if (element.hasValue()) { + if (linkResolver != null) + xml.link(linkResolver.resolveType(element.getType())); + xml.attribute("value", element.getValue()); } - for (Element child : element.getChildren()) - composeElement(xml, child, child.getName(), false); - xml.exit(element.getProperty().getXmlNamespace(),elementName); - } else - xml.element(elementName); + if (linkResolver != null) + xml.link(linkResolver.resolveProperty(element.getProperty())); + if (element.hasChildren()) { + xml.enter(element.getProperty().getXmlNamespace(), elementName); + if (linkResolver != null && element.getProperty().isReference()) { + String ref = linkResolver.resolveReference(getReferenceForElement(element)); + if (ref != null) { + xml.externalLink(ref); + } + } + for (Element child : element.getChildren()) + composeElement(xml, child, child.getName(), false); + xml.exit(element.getProperty().getXmlNamespace(),elementName); + } else + xml.element(elementName); + } } } else { - setXsiTypeIfIsTypeAttr(xml, element); - Set handled = new HashSet<>(); + if (isEllipseElements() && element.isEllipsed()) + xml.ellipse(); + else { + setXsiTypeIfIsTypeAttr(xml, element); + Set handled = new HashSet<>(); for (Element child : element.getChildren()) { if (!handled.contains(child.getName()) && isAttr(child.getProperty()) && wantCompose(element.getPath(), child)) { handled.add(child.getName()); - String av = child.getValue(); - if (child.getProperty().isList()) { - for (Element c2 : element.getChildren()) { - if (c2 != child && c2.getName().equals(child.getName())) { - av = av + " "+c2.getValue(); + if (isEllipseElements() && child.isEllipsed()) + xml.attributeEllipse(); + else { + String av = child.getValue(); + if (child.getProperty().isList()) { + for (Element c2 : element.getChildren()) { + if (c2 != child && c2.getName().equals(child.getName())) { + if (c2.isEllipsed()) + av = av + " ..."; + else + av = av + " " + c2.getValue(); + } } - } + } + if (linkResolver != null) + xml.link(linkResolver.resolveType(child.getType())); + if (ToolingExtensions.hasExtension(child.getProperty().getDefinition(), ToolingExtensions.EXT_DATE_FORMAT)) + av = convertForDateFormatToExternal(ToolingExtensions.readStringExtension(child.getProperty().getDefinition(), ToolingExtensions.EXT_DATE_FORMAT), av); + xml.attribute(child.getProperty().getXmlNamespace(), child.getProperty().getXmlName(), av); } - if (linkResolver != null) - xml.link(linkResolver.resolveType(child.getType())); - if (ToolingExtensions.hasExtension(child.getProperty().getDefinition(), ToolingExtensions.EXT_DATE_FORMAT)) - av = convertForDateFormatToExternal(ToolingExtensions.readStringExtension(child.getProperty().getDefinition(), ToolingExtensions.EXT_DATE_FORMAT), av); - xml.attribute(child.getProperty().getXmlNamespace(),child.getProperty().getXmlName(), av); } } + } if (!element.getProperty().getDefinition().hasExtension(ToolingExtensions.EXT_ID_CHOICE_GROUP)) { if (linkResolver != null) xml.link(linkResolver.resolveProperty(element.getProperty())); @@ -914,12 +948,16 @@ public class XmlParser extends ParserBase { } for (Element child : element.getChildren()) { if (wantCompose(element.getPath(), child)) { - if (isText(child.getProperty())) { - if (linkResolver != null) - xml.link(linkResolver.resolveProperty(element.getProperty())); - xml.text(child.getValue()); - } else if (!isAttr(child.getProperty())) { - composeElement(xml, child, child.getName(), false); + if (isEllipseElements() && child.isEllipsed()) + xml.ellipse(); + else { + if (isText(child.getProperty())) { + if (linkResolver != null) + xml.link(linkResolver.resolveProperty(element.getProperty())); + xml.text(child.getValue()); + } else if (!isAttr(child.getProperty())) { + composeElement(xml, child, child.getName(), false); + } } } } @@ -1034,4 +1072,13 @@ public class XmlParser extends ParserBase { // do nothing } } + + public boolean isEllipseElements() { + return ellipseElements; + } + + public void setEllipseElements(boolean ellipseElements) { + this.ellipseElements = ellipseElements; + } + } \ No newline at end of file diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/formats/JsonCreator.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/formats/JsonCreator.java index 0d4b0cf27..58f44d505 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/formats/JsonCreator.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/formats/JsonCreator.java @@ -1,5 +1,5 @@ -package org.hl7.fhir.r5.formats; - +package org.hl7.fhir.r5.formats; + /* Copyright (c) 2011+, HL7, Inc. All rights reserved. @@ -28,47 +28,48 @@ package org.hl7.fhir.r5.formats; POSSIBILITY OF SUCH DAMAGE. */ - - - -import java.io.IOException; -import java.math.BigDecimal; - -/** - * Facade to GSON writer, or something that imposes property ordering first - * - * @author Grahame - * - */ -public interface JsonCreator { - - void comment(String comment); - - void beginObject() throws IOException; - - void endObject() throws IOException; - - void nullValue() throws IOException; - - void name(String name) throws IOException; - - void value(String value) throws IOException; - - void value(Boolean value) throws IOException; - - void value(BigDecimal value) throws IOException; - void valueNum(String value) throws IOException; // allow full control of representation - - void value(Integer value) throws IOException; - - void beginArray() throws IOException; - - void endArray() throws IOException; - - void finish() throws IOException; - - // only used by an creator that actually produces xhtml - void link(String href); - void anchor(String string); - void externalLink(String string); + + + +import java.io.IOException; +import java.math.BigDecimal; + +/** + * Facade to GSON writer, or something that imposes property ordering first + * + * @author Grahame + * + */ +public interface JsonCreator { + + void comment(String comment); + + void beginObject() throws IOException; + + void endObject() throws IOException; + + void nullValue() throws IOException; + + void name(String name) throws IOException; + + void value(String value) throws IOException; + + void value(Boolean value) throws IOException; + + void value(BigDecimal value) throws IOException; + void valueNum(String value) throws IOException; // allow full control of representation + + void value(Integer value) throws IOException; + + void beginArray() throws IOException; + + void endArray() throws IOException; + + void finish() throws IOException; + + // only used by an creator that actually produces xhtml + void link(String href); + void anchor(String string); + void externalLink(String string); + void ellipse(); } \ No newline at end of file diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/formats/JsonCreatorCanonical.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/formats/JsonCreatorCanonical.java index 66acfeff6..badb9a4a0 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/formats/JsonCreatorCanonical.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/formats/JsonCreatorCanonical.java @@ -1,5 +1,5 @@ -package org.hl7.fhir.r5.formats; - +package org.hl7.fhir.r5.formats; + /* Copyright (c) 2011+, HL7, Inc. All rights reserved. @@ -28,257 +28,261 @@ package org.hl7.fhir.r5.formats; POSSIBILITY OF SUCH DAMAGE. */ - - - -import java.io.IOException; -import java.io.OutputStreamWriter; -import java.math.BigDecimal; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Stack; - -public class JsonCreatorCanonical implements JsonCreator { - - public class JsonCanValue { - String name; - private JsonCanValue(String name) { - this.name = name; - } - } - - private class JsonCanNumberValue extends JsonCanValue { - private BigDecimal value; - private JsonCanNumberValue(String name, BigDecimal value) { - super(name); - this.value = value; - } - } - - private class JsonCanPresentedNumberValue extends JsonCanValue { - private String value; - private JsonCanPresentedNumberValue(String name, String value) { - super(name); - this.value = value; - } - } - - private class JsonCanIntegerValue extends JsonCanValue { - private Integer value; - private JsonCanIntegerValue(String name, Integer value) { - super(name); - this.value = value; - } - } - - private class JsonCanBooleanValue extends JsonCanValue { - private Boolean value; - private JsonCanBooleanValue(String name, Boolean value) { - super(name); - this.value = value; - } - } - - private class JsonCanStringValue extends JsonCanValue { - private String value; - private JsonCanStringValue(String name, String value) { - super(name); - this.value = value; - } - } - - private class JsonCanNullValue extends JsonCanValue { - private JsonCanNullValue(String name) { - super(name); - } - } - - public class JsonCanObject extends JsonCanValue { - - boolean array; - List children = new ArrayList(); - - public JsonCanObject(String name, boolean array) { - super(name); - this.array = array; - } - - public void addProp(JsonCanValue obj) { - children.add(obj); - } - } - - Stack stack; - JsonCanObject root; - JsonCreatorDirect jj; - String name; - - public JsonCreatorCanonical(OutputStreamWriter osw) { - stack = new Stack(); - jj = new JsonCreatorDirect(osw, false, false); - name = null; - } - - private String takeName() { - String res = name; - name = null; - return res; - } - - @Override - public void beginObject() throws IOException { - JsonCanObject obj = new JsonCanObject(takeName(), false); - if (stack.isEmpty()) - root = obj; - else - stack.peek().addProp(obj); - stack.push(obj); - } - - @Override - public void endObject() throws IOException { - stack.pop(); - } - - @Override - public void nullValue() throws IOException { - stack.peek().addProp(new JsonCanNullValue(takeName())); - } - - @Override - public void name(String name) throws IOException { - this.name = name; - } - - @Override - public void value(String value) throws IOException { - stack.peek().addProp(new JsonCanStringValue(takeName(), value)); - } - - @Override - public void value(Boolean value) throws IOException { - stack.peek().addProp(new JsonCanBooleanValue(takeName(), value)); - } - - @Override - public void value(BigDecimal value) throws IOException { - stack.peek().addProp(new JsonCanNumberValue(takeName(), value)); - } - @Override - public void valueNum(String value) throws IOException { - stack.peek().addProp(new JsonCanPresentedNumberValue(takeName(), value)); - } - - - @Override - public void value(Integer value) throws IOException { - stack.peek().addProp(new JsonCanIntegerValue(takeName(), value)); - } - - @Override - public void beginArray() throws IOException { - JsonCanObject obj = new JsonCanObject(takeName(), true); - if (!stack.isEmpty()) - stack.peek().addProp(obj); - stack.push(obj); - - } - - @Override - public void endArray() throws IOException { - stack.pop(); - } - - @Override - public void finish() throws IOException { - writeObject(root); - } - - private void writeObject(JsonCanObject obj) throws IOException { - jj.beginObject(); - List names = new ArrayList(); - for (JsonCanValue v : obj.children) - names.add(v.name); - Collections.sort(names); - for (String n : names) { - jj.name(n); - JsonCanValue v = getPropForName(n, obj.children); - if (v instanceof JsonCanNumberValue) - jj.value(((JsonCanNumberValue) v).value); - else if (v instanceof JsonCanPresentedNumberValue) - jj.valueNum(((JsonCanPresentedNumberValue) v).value); - else if (v instanceof JsonCanIntegerValue) - jj.value(((JsonCanIntegerValue) v).value); - else if (v instanceof JsonCanBooleanValue) - jj.value(((JsonCanBooleanValue) v).value); - else if (v instanceof JsonCanStringValue) - jj.value(((JsonCanStringValue) v).value); - else if (v instanceof JsonCanNullValue) - jj.nullValue(); - else if (v instanceof JsonCanObject) { - JsonCanObject o = (JsonCanObject) v; - if (o.array) - writeArray(o); - else - writeObject(o); - } else - throw new Error("not possible"); - } - jj.endObject(); - } - - private JsonCanValue getPropForName(String name, List children) { - for (JsonCanValue child : children) - if (child.name.equals(name)) - return child; - return null; - } - - private void writeArray(JsonCanObject arr) throws IOException { - jj.beginArray(); - for (JsonCanValue v : arr.children) { - if (v instanceof JsonCanNumberValue) - jj.value(((JsonCanNumberValue) v).value); - else if (v instanceof JsonCanIntegerValue) - jj.value(((JsonCanIntegerValue) v).value); - else if (v instanceof JsonCanBooleanValue) - jj.value(((JsonCanBooleanValue) v).value); - else if (v instanceof JsonCanStringValue) - jj.value(((JsonCanStringValue) v).value); - else if (v instanceof JsonCanNullValue) - jj.nullValue(); - else if (v instanceof JsonCanObject) { - JsonCanObject o = (JsonCanObject) v; - if (o.array) - writeArray(o); - else - writeObject(o); - } else - throw new Error("not possible"); - } - jj.endArray(); - } - - @Override - public void comment(String content) { - // canonical JSON ignores comments - } - - @Override - public void link(String href) { - // not used - } - - @Override - public void anchor(String name) { - // not used - } - - @Override - public void externalLink(String string) { - // not used - } - - + + + +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.math.BigDecimal; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Stack; + +public class JsonCreatorCanonical implements JsonCreator { + + public class JsonCanValue { + String name; + private JsonCanValue(String name) { + this.name = name; + } + } + + private class JsonCanNumberValue extends JsonCanValue { + private BigDecimal value; + private JsonCanNumberValue(String name, BigDecimal value) { + super(name); + this.value = value; + } + } + + private class JsonCanPresentedNumberValue extends JsonCanValue { + private String value; + private JsonCanPresentedNumberValue(String name, String value) { + super(name); + this.value = value; + } + } + + private class JsonCanIntegerValue extends JsonCanValue { + private Integer value; + private JsonCanIntegerValue(String name, Integer value) { + super(name); + this.value = value; + } + } + + private class JsonCanBooleanValue extends JsonCanValue { + private Boolean value; + private JsonCanBooleanValue(String name, Boolean value) { + super(name); + this.value = value; + } + } + + private class JsonCanStringValue extends JsonCanValue { + private String value; + private JsonCanStringValue(String name, String value) { + super(name); + this.value = value; + } + } + + private class JsonCanNullValue extends JsonCanValue { + private JsonCanNullValue(String name) { + super(name); + } + } + + public class JsonCanObject extends JsonCanValue { + + boolean array; + List children = new ArrayList(); + + public JsonCanObject(String name, boolean array) { + super(name); + this.array = array; + } + + public void addProp(JsonCanValue obj) { + children.add(obj); + } + } + + Stack stack; + JsonCanObject root; + JsonCreatorDirect jj; + String name; + + public JsonCreatorCanonical(OutputStreamWriter osw) { + stack = new Stack(); + jj = new JsonCreatorDirect(osw, false, false); + name = null; + } + + private String takeName() { + String res = name; + name = null; + return res; + } + + @Override + public void beginObject() throws IOException { + JsonCanObject obj = new JsonCanObject(takeName(), false); + if (stack.isEmpty()) + root = obj; + else + stack.peek().addProp(obj); + stack.push(obj); + } + + @Override + public void endObject() throws IOException { + stack.pop(); + } + + @Override + public void nullValue() throws IOException { + stack.peek().addProp(new JsonCanNullValue(takeName())); + } + + @Override + public void name(String name) throws IOException { + this.name = name; + } + + @Override + public void value(String value) throws IOException { + stack.peek().addProp(new JsonCanStringValue(takeName(), value)); + } + + @Override + public void value(Boolean value) throws IOException { + stack.peek().addProp(new JsonCanBooleanValue(takeName(), value)); + } + + @Override + public void value(BigDecimal value) throws IOException { + stack.peek().addProp(new JsonCanNumberValue(takeName(), value)); + } + @Override + public void valueNum(String value) throws IOException { + stack.peek().addProp(new JsonCanPresentedNumberValue(takeName(), value)); + } + + + @Override + public void value(Integer value) throws IOException { + stack.peek().addProp(new JsonCanIntegerValue(takeName(), value)); + } + + @Override + public void beginArray() throws IOException { + JsonCanObject obj = new JsonCanObject(takeName(), true); + if (!stack.isEmpty()) + stack.peek().addProp(obj); + stack.push(obj); + + } + + @Override + public void endArray() throws IOException { + stack.pop(); + } + + @Override + public void finish() throws IOException { + writeObject(root); + } + + private void writeObject(JsonCanObject obj) throws IOException { + jj.beginObject(); + List names = new ArrayList(); + for (JsonCanValue v : obj.children) + names.add(v.name); + Collections.sort(names); + for (String n : names) { + jj.name(n); + JsonCanValue v = getPropForName(n, obj.children); + if (v instanceof JsonCanNumberValue) + jj.value(((JsonCanNumberValue) v).value); + else if (v instanceof JsonCanPresentedNumberValue) + jj.valueNum(((JsonCanPresentedNumberValue) v).value); + else if (v instanceof JsonCanIntegerValue) + jj.value(((JsonCanIntegerValue) v).value); + else if (v instanceof JsonCanBooleanValue) + jj.value(((JsonCanBooleanValue) v).value); + else if (v instanceof JsonCanStringValue) + jj.value(((JsonCanStringValue) v).value); + else if (v instanceof JsonCanNullValue) + jj.nullValue(); + else if (v instanceof JsonCanObject) { + JsonCanObject o = (JsonCanObject) v; + if (o.array) + writeArray(o); + else + writeObject(o); + } else + throw new Error("not possible"); + } + jj.endObject(); + } + + private JsonCanValue getPropForName(String name, List children) { + for (JsonCanValue child : children) + if (child.name.equals(name)) + return child; + return null; + } + + private void writeArray(JsonCanObject arr) throws IOException { + jj.beginArray(); + for (JsonCanValue v : arr.children) { + if (v instanceof JsonCanNumberValue) + jj.value(((JsonCanNumberValue) v).value); + else if (v instanceof JsonCanIntegerValue) + jj.value(((JsonCanIntegerValue) v).value); + else if (v instanceof JsonCanBooleanValue) + jj.value(((JsonCanBooleanValue) v).value); + else if (v instanceof JsonCanStringValue) + jj.value(((JsonCanStringValue) v).value); + else if (v instanceof JsonCanNullValue) + jj.nullValue(); + else if (v instanceof JsonCanObject) { + JsonCanObject o = (JsonCanObject) v; + if (o.array) + writeArray(o); + else + writeObject(o); + } else + throw new Error("not possible"); + } + jj.endArray(); + } + + @Override + public void comment(String content) { + // canonical JSON ignores comments + } + + @Override + public void link(String href) { + // not used + } + + @Override + public void anchor(String name) { + // not used + } + + @Override + public void externalLink(String string) { + // not used + } + + @Override + public void ellipse() { + // not used + } + } \ No newline at end of file diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/formats/JsonCreatorDirect.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/formats/JsonCreatorDirect.java index 019f505e3..5132099f3 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/formats/JsonCreatorDirect.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/formats/JsonCreatorDirect.java @@ -1,5 +1,5 @@ -package org.hl7.fhir.r5.formats; - +package org.hl7.fhir.r5.formats; + /* Copyright (c) 2011+, HL7, Inc. All rights reserved. @@ -28,219 +28,224 @@ package org.hl7.fhir.r5.formats; POSSIBILITY OF SUCH DAMAGE. */ - - - -import java.io.IOException; -import java.io.Writer; -import java.math.BigDecimal; -import java.util.ArrayList; -import java.util.List; - -import org.hl7.fhir.utilities.Utilities; - -/** - * A little implementation of a json write to replace Gson .... because Gson screws up decimal values, and *we care* - * - * @author Grahame Grieve - * - */ -public class JsonCreatorDirect implements JsonCreator { - - private Writer writer; - private boolean pretty; - private boolean comments; - private boolean named; - private List valued = new ArrayList(); - private int indent; - private List commentList = new ArrayList<>(); - - public JsonCreatorDirect(Writer writer, boolean pretty, boolean comments) { - super(); - this.writer = writer; - this.pretty = pretty; - this.comments = pretty && comments; - } - - @Override - public void comment(String content) { - if (comments) { - commentList.add(content); - } - } - - @Override - public void beginObject() throws IOException { - checkState(); - writer.write("{"); - stepIn(); - if (!valued.isEmpty()) { - valued.set(0, true); - } - valued.add(0, false); - } - - private void commitComments() throws IOException { - if (comments) { - for (String s : commentList) { - writer.write("// "); - writer.write(s); - writer.write("\r\n"); - for (int i = 0; i < indent; i++) { - writer.write(" "); - } - } - commentList.clear(); - } - } - - - public void stepIn() throws IOException { - if (pretty) { - indent++; - writer.write("\r\n"); - for (int i = 0; i < indent; i++) { - writer.write(" "); - } - } - } - - public void stepOut() throws IOException { - if (pretty) { - indent--; - writer.write("\r\n"); - for (int i = 0; i < indent; i++) { - writer.write(" "); - } - } - } - - private void checkState() throws IOException { - commitComments(); - if (named) { - if (pretty) - writer.write(" : "); - else - writer.write(":"); - named = false; - } - if (!valued.isEmpty() && valued.get(0)) { - writer.write(","); - if (pretty) { - writer.write("\r\n"); - for (int i = 0; i < indent; i++) { - writer.write(" "); - } - } - valued.set(0, false); - } - } - - @Override - public void endObject() throws IOException { - stepOut(); - writer.write("}"); - valued.remove(0); - } - - @Override - public void nullValue() throws IOException { - checkState(); - writer.write("null"); - valued.set(0, true); - } - - @Override - public void name(String name) throws IOException { - checkState(); - writer.write("\""+name+"\""); - named = true; - } - - @Override - public void value(String value) throws IOException { - checkState(); - writer.write("\""+Utilities.escapeJson(value)+"\""); - valued.set(0, true); - } - - @Override - public void value(Boolean value) throws IOException { - checkState(); - if (value == null) - writer.write("null"); - else if (value.booleanValue()) - writer.write("true"); - else - writer.write("false"); - valued.set(0, true); - } - - @Override - public void value(BigDecimal value) throws IOException { - checkState(); - if (value == null) - writer.write("null"); - else - writer.write(value.toString()); - valued.set(0, true); - } - - @Override - public void valueNum(String value) throws IOException { - checkState(); - if (value == null) - writer.write("null"); - else - writer.write(value); - valued.set(0, true); - } - - @Override - public void value(Integer value) throws IOException { - checkState(); - if (value == null) - writer.write("null"); - else - writer.write(value.toString()); - valued.set(0, true); - } - - @Override - public void beginArray() throws IOException { - checkState(); - writer.write("["); - if (!valued.isEmpty()) { - valued.set(0, true); - } - valued.add(0, false); - } - - @Override - public void endArray() throws IOException { - writer.write("]"); - valued.remove(0); - } - - @Override - public void finish() throws IOException { - writer.flush(); - } - - @Override - public void link(String href) { - // not used - - } - - @Override - public void anchor(String name) { - // not used - } - - - @Override - public void externalLink(String string) { - // not used - } + + + +import java.io.IOException; +import java.io.Writer; +import java.math.BigDecimal; +import java.util.ArrayList; +import java.util.List; + +import org.hl7.fhir.utilities.Utilities; + +/** + * A little implementation of a json write to replace Gson .... because Gson screws up decimal values, and *we care* + * + * @author Grahame Grieve + * + */ +public class JsonCreatorDirect implements JsonCreator { + + private Writer writer; + private boolean pretty; + private boolean comments; + private boolean named; + private List valued = new ArrayList(); + private int indent; + private List commentList = new ArrayList<>(); + + public JsonCreatorDirect(Writer writer, boolean pretty, boolean comments) { + super(); + this.writer = writer; + this.pretty = pretty; + this.comments = pretty && comments; + } + + @Override + public void comment(String content) { + if (comments) { + commentList.add(content); + } + } + + @Override + public void beginObject() throws IOException { + checkState(); + writer.write("{"); + stepIn(); + if (!valued.isEmpty()) { + valued.set(0, true); + } + valued.add(0, false); + } + + private void commitComments() throws IOException { + if (comments) { + for (String s : commentList) { + writer.write("// "); + writer.write(s); + writer.write("\r\n"); + for (int i = 0; i < indent; i++) { + writer.write(" "); + } + } + commentList.clear(); + } + } + + + public void stepIn() throws IOException { + if (pretty) { + indent++; + writer.write("\r\n"); + for (int i = 0; i < indent; i++) { + writer.write(" "); + } + } + } + + public void stepOut() throws IOException { + if (pretty) { + indent--; + writer.write("\r\n"); + for (int i = 0; i < indent; i++) { + writer.write(" "); + } + } + } + + private void checkState() throws IOException { + commitComments(); + if (named) { + if (pretty) + writer.write(" : "); + else + writer.write(":"); + named = false; + } + if (!valued.isEmpty() && valued.get(0)) { + writer.write(","); + if (pretty) { + writer.write("\r\n"); + for (int i = 0; i < indent; i++) { + writer.write(" "); + } + } + valued.set(0, false); + } + } + + @Override + public void endObject() throws IOException { + stepOut(); + writer.write("}"); + valued.remove(0); + } + + @Override + public void nullValue() throws IOException { + checkState(); + writer.write("null"); + valued.set(0, true); + } + + @Override + public void name(String name) throws IOException { + checkState(); + writer.write("\""+name+"\""); + named = true; + } + + @Override + public void value(String value) throws IOException { + checkState(); + writer.write("\""+Utilities.escapeJson(value)+"\""); + valued.set(0, true); + } + + @Override + public void value(Boolean value) throws IOException { + checkState(); + if (value == null) + writer.write("null"); + else if (value.booleanValue()) + writer.write("true"); + else + writer.write("false"); + valued.set(0, true); + } + + @Override + public void value(BigDecimal value) throws IOException { + checkState(); + if (value == null) + writer.write("null"); + else + writer.write(value.toString()); + valued.set(0, true); + } + + @Override + public void valueNum(String value) throws IOException { + checkState(); + if (value == null) + writer.write("null"); + else + writer.write(value); + valued.set(0, true); + } + + @Override + public void value(Integer value) throws IOException { + checkState(); + if (value == null) + writer.write("null"); + else + writer.write(value.toString()); + valued.set(0, true); + } + + @Override + public void beginArray() throws IOException { + checkState(); + writer.write("["); + if (!valued.isEmpty()) { + valued.set(0, true); + } + valued.add(0, false); + } + + @Override + public void endArray() throws IOException { + writer.write("]"); + valued.remove(0); + } + + @Override + public void finish() throws IOException { + writer.flush(); + } + + @Override + public void link(String href) { + // not used + + } + + @Override + public void anchor(String name) { + // not used + } + + + @Override + public void externalLink(String string) { + // not used + } + + @Override + public void ellipse() { + // not used + } } \ No newline at end of file diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/formats/JsonCreatorGson.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/formats/JsonCreatorGson.java index d93669b2a..ded1fcac7 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/formats/JsonCreatorGson.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/formats/JsonCreatorGson.java @@ -1,5 +1,5 @@ -package org.hl7.fhir.r5.formats; - +package org.hl7.fhir.r5.formats; + /* Copyright (c) 2011+, HL7, Inc. All rights reserved. @@ -28,103 +28,108 @@ package org.hl7.fhir.r5.formats; POSSIBILITY OF SUCH DAMAGE. */ - - - -import java.io.IOException; -import java.io.OutputStreamWriter; -import java.math.BigDecimal; - -import com.google.gson.stream.JsonWriter; - -public class JsonCreatorGson implements JsonCreator { - - JsonWriter gson; - - public JsonCreatorGson(OutputStreamWriter osw) { - gson = new JsonWriter(osw); - } - - @Override - public void beginObject() throws IOException { - gson.beginObject(); - } - - @Override - public void endObject() throws IOException { - gson.endObject(); - } - - @Override - public void nullValue() throws IOException { - gson.nullValue(); - } - - @Override - public void name(String name) throws IOException { - gson.name(name); - } - - @Override - public void value(String value) throws IOException { - gson.value(value); - } - - @Override - public void value(Boolean value) throws IOException { - gson.value(value); - } - - @Override - public void value(BigDecimal value) throws IOException { - gson.value(value); - } - - @Override - public void value(Integer value) throws IOException { - gson.value(value); - } - - @Override - public void beginArray() throws IOException { - gson.beginArray(); - } - - @Override - public void endArray() throws IOException { - gson.endArray(); - } - - @Override - public void finish() { - // nothing to do here - - } - - @Override - public void link(String href) { - // not used - } - - @Override - public void valueNum(String value) throws IOException { - value(new BigDecimal(value)); - } - - @Override - public void anchor(String name) { - // not used - } - - @Override - public void comment(String content) { - // gson (dense json) ignores comments - } - - - @Override - public void externalLink(String string) { - // not used - } - + + + +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.math.BigDecimal; + +import com.google.gson.stream.JsonWriter; + +public class JsonCreatorGson implements JsonCreator { + + JsonWriter gson; + + public JsonCreatorGson(OutputStreamWriter osw) { + gson = new JsonWriter(osw); + } + + @Override + public void beginObject() throws IOException { + gson.beginObject(); + } + + @Override + public void endObject() throws IOException { + gson.endObject(); + } + + @Override + public void nullValue() throws IOException { + gson.nullValue(); + } + + @Override + public void name(String name) throws IOException { + gson.name(name); + } + + @Override + public void value(String value) throws IOException { + gson.value(value); + } + + @Override + public void value(Boolean value) throws IOException { + gson.value(value); + } + + @Override + public void value(BigDecimal value) throws IOException { + gson.value(value); + } + + @Override + public void value(Integer value) throws IOException { + gson.value(value); + } + + @Override + public void beginArray() throws IOException { + gson.beginArray(); + } + + @Override + public void endArray() throws IOException { + gson.endArray(); + } + + @Override + public void finish() { + // nothing to do here + + } + + @Override + public void link(String href) { + // not used + } + + @Override + public void valueNum(String value) throws IOException { + value(new BigDecimal(value)); + } + + @Override + public void anchor(String name) { + // not used + } + + @Override + public void comment(String content) { + // gson (dense json) ignores comments + } + + + @Override + public void externalLink(String string) { + // not used + } + + @Override + public void ellipse() { + // not used + } + } \ No newline at end of file From 966798cc2f6efca73c4179fc06697cad24767960 Mon Sep 17 00:00:00 2001 From: Lloyd McKenzie Date: Wed, 4 Sep 2024 22:25:07 -0600 Subject: [PATCH 2/4] Missed commits --- .../hl7/fhir/utilities/xml/IXMLWriter.java | 160 +- .../org/hl7/fhir/utilities/xml/XMLWriter.java | 1783 +++++++++-------- 2 files changed, 978 insertions(+), 965 deletions(-) diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/xml/IXMLWriter.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/xml/IXMLWriter.java index 80459e7ad..0a2a61ec3 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/xml/IXMLWriter.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/xml/IXMLWriter.java @@ -1,5 +1,5 @@ -package org.hl7.fhir.utilities.xml; - +package org.hl7.fhir.utilities.xml; + /* Copyright (c) 2011+, HL7, Inc. All rights reserved. @@ -28,80 +28,84 @@ package org.hl7.fhir.utilities.xml; POSSIBILITY OF SUCH DAMAGE. */ - - - -import java.io.IOException; - -import org.hl7.fhir.utilities.ElementDecoration; - - -/** - * Generalize - * @author dennisn - */ -public interface IXMLWriter { - - public abstract void start() throws IOException; - public abstract void end() throws IOException; - - public abstract void attribute(String namespace, String name, String value, boolean onlyIfNotEmpty) throws IOException; - public abstract void attribute(String namespace, String name, String value) throws IOException; - public abstract void attribute(String name, String value, boolean onlyIfNotEmpty) throws IOException; - public abstract void attribute(String name, String value) throws IOException; - public abstract void attributeNoLines(String name, String value) throws IOException; - - public abstract boolean namespaceDefined(String namespace); - public abstract boolean abbreviationDefined(String abbreviation); - public abstract String getDefaultNamespace(); - public abstract void namespace(String namespace) throws IOException; - public abstract void setDefaultNamespace(String namespace) throws IOException; - public abstract void namespace(String namespace, String abbreviation) throws IOException; - - public abstract void comment(String comment, boolean doPretty) throws IOException; - public abstract void decorate(ElementDecoration decoration) throws IOException; - public abstract void setSchemaLocation(String ns, String loc) throws IOException; - - public abstract void enter(String name) throws IOException; - public abstract void enter(String namespace, String name) throws IOException; - public abstract void enter(String namespace, String name, String comment) throws IOException; - - public abstract void exit() throws IOException; - public abstract void exit(String name) throws IOException; - public abstract void exit(String namespace, String name) throws IOException; - public abstract void exitToLevel(int count) throws IOException; - - - public abstract void element(String namespace, String name, String content, boolean onlyIfNotEmpty) throws IOException; - public abstract void element(String namespace, String name, String content, String comment) throws IOException; - public abstract void element(String namespace, String name, String content) throws IOException; - public abstract void element(String name, String content, boolean onlyIfNotEmpty) throws IOException; - public abstract void element(String name, String content) throws IOException; - public abstract void element(String name) throws IOException; - - public abstract void text(String content) throws IOException; - public abstract void text(String content, boolean dontEscape) throws IOException; - - public abstract void cData(String text) throws IOException; - - public abstract void writeBytes(byte[] bytes) throws IOException; - - public abstract boolean isPretty() throws IOException; - public abstract void setPretty(boolean pretty) throws IOException; - - /** - * Start comment inserts a - * so the comment doesn't close prematurely. - * @throws IOException - */ - public abstract void startCommentBlock() throws IOException; - public abstract void endCommentBlock() throws IOException; - public abstract void escapedText(String content) throws IOException; - - // this is only implemented by an implementation that is producing an xhtml representation, and is able to render elements as hyperlinks - public abstract void link(String href); - public abstract void anchor(String name); - public abstract void externalLink(String ref) throws IOException; + + + +import java.io.IOException; + +import org.hl7.fhir.utilities.ElementDecoration; + + +/** + * Generalize + * @author dennisn + */ +public interface IXMLWriter { + + public abstract void start() throws IOException; + public abstract void end() throws IOException; + + public abstract void attribute(String namespace, String name, String value, boolean onlyIfNotEmpty) throws IOException; + public abstract void attribute(String namespace, String name, String value) throws IOException; + public abstract void attribute(String name, String value, boolean onlyIfNotEmpty) throws IOException; + public abstract void attribute(String name, String value) throws IOException; + public abstract void attributeNoLines(String name, String value) throws IOException; + + public abstract boolean namespaceDefined(String namespace); + public abstract boolean abbreviationDefined(String abbreviation); + public abstract String getDefaultNamespace(); + public abstract void namespace(String namespace) throws IOException; + public abstract void setDefaultNamespace(String namespace) throws IOException; + public abstract void namespace(String namespace, String abbreviation) throws IOException; + + public abstract void comment(String comment, boolean doPretty) throws IOException; + public abstract void decorate(ElementDecoration decoration) throws IOException; + public abstract void setSchemaLocation(String ns, String loc) throws IOException; + + public abstract void enter(String name) throws IOException; + public abstract void enter(String namespace, String name) throws IOException; + public abstract void enter(String namespace, String name, String comment) throws IOException; + + public abstract void exit() throws IOException; + public abstract void exit(String name) throws IOException; + public abstract void exit(String namespace, String name) throws IOException; + public abstract void exitToLevel(int count) throws IOException; + + + public abstract void element(String namespace, String name, String content, boolean onlyIfNotEmpty) throws IOException; + public abstract void element(String namespace, String name, String content, String comment) throws IOException; + public abstract void element(String namespace, String name, String content) throws IOException; + public abstract void element(String name, String content, boolean onlyIfNotEmpty) throws IOException; + public abstract void element(String name, String content) throws IOException; + public abstract void element(String name) throws IOException; + + public abstract void text(String content) throws IOException; + public abstract void text(String content, boolean dontEscape) throws IOException; + + public abstract void cData(String text) throws IOException; + + public abstract void writeBytes(byte[] bytes) throws IOException; + + public abstract boolean isPretty() throws IOException; + public abstract void setPretty(boolean pretty) throws IOException; + + /** + * Start comment inserts a + * so the comment doesn't close prematurely. + * @throws IOException + */ + public abstract void startCommentBlock() throws IOException; + public abstract void endCommentBlock() throws IOException; + public abstract void escapedText(String content) throws IOException; + + // this is only implemented by an implementation that is producing an xhtml representation, and is able to render elements as hyperlinks + public abstract void link(String href); + public abstract void anchor(String name); + public abstract void externalLink(String ref) throws IOException; + + // this is only implemented by an implementation that is producing an xhtml representation and handles ellipsing elements + public abstract void ellipse() throws IOException; + public abstract void attributeEllipse(); } \ No newline at end of file diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/xml/XMLWriter.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/xml/XMLWriter.java index a8e40fee0..777083e99 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/xml/XMLWriter.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/xml/XMLWriter.java @@ -1,5 +1,5 @@ -package org.hl7.fhir.utilities.xml; - +package org.hl7.fhir.utilities.xml; + /* Copyright (c) 2011+, HL7, Inc. All rights reserved. @@ -28,889 +28,898 @@ package org.hl7.fhir.utilities.xml; POSSIBILITY OF SUCH DAMAGE. */ - - - -import java.io.IOException; -import java.io.OutputStream; -import java.io.OutputStreamWriter; -import java.io.UnsupportedEncodingException; - -import org.hl7.fhir.utilities.ElementDecoration; - -/** - * XML Writer class. - */ -public class XMLWriter extends OutputStreamWriter implements IXMLWriter { - - private boolean xmlHeader = true; - private String charset; - private boolean prettyBase; - private boolean prettyHeader; - private boolean pendingClose; - private boolean pendingOpen; - private String pendingComment; - private int lineType = LINE_UNIX; - private OutputStream stream; - private boolean started = false; - private String[] specialAttributeNames = new String[] {"id", "name" }; - private boolean sortAttributes; - private int attributeLineWrap; - private boolean xml1_1; - - public final static int LINE_UNIX = 0; - public final static int LINE_WINDOWS = 1; - - public XMLWriter(OutputStream stream, String charset, boolean xml1_1) throws UnsupportedEncodingException { - super(stream, charset); - this.stream = stream; - this.charset = charset; - this.xml1_1 = xml1_1; - } - public XMLWriter(OutputStream stream, String charset) throws UnsupportedEncodingException { - super(stream, charset); - this.stream = stream; - this.charset = charset; - this.xml1_1 = false; - } - - protected boolean condition(boolean bTest, String message) throws IOException { - if (!bTest) - throw new IOException(message); - return bTest; - } - - // -- writing context ------------------------------------------------ - - - - /** - * Returns the encoding. - * - * @param charset - * @return encoding - * @throws IOException - */ - public static String getXMLCharsetName(String charset) throws IOException { - if (charset == null || charset.equals("")) - return "UTF-8"; - else if (charset.equals("US-ASCII")) - return "UTF-8"; - else if (XMLUtil.charSetImpliesAscii(charset)) - return "ISO-8859-1"; - else if (charset.equals("UTF-8")) - return "UTF-8"; - else if (charset.equals("UTF-16") || charset.equals("UTF-16BE") || charset.equals("UTF-16LE")) - return "UTF-16"; - else - throw new IOException("Unknown charset encoding "+charset); - } - - /* (non-Javadoc) - * @see org.eclipse.ohf.utilities.xml.IXMLWriter#start() - */ - @Override - public void start() throws IOException { - condition(!started, "attempt to start after starting"); - levels.clear(); - attributes = null; - try { - if (xmlHeader) { - write(""); - if (prettyBase || prettyHeader) - write(lineType == LINE_UNIX ? "\n" : "\r\n"); - } - } catch (UnsupportedEncodingException e) { - // TODO Auto-generated catch block - throw new IOException(e.getMessage()); - } - started = true; - } - - private void checkStarted () throws IOException { - condition(started, "not started"); - } - - private void checkInElement() throws IOException { - condition(levels.size() > 0, "not in an element"); - } - - // -- attributes ---------------------------------------------------- - - private String[][] attributes; - - private void addAttribute(String name, String value) throws IOException { - addAttribute(name, value, false); - } - - private void addAttribute(String name, String value, boolean noLines) throws IOException { - if (!XMLUtil.isNMToken(name)) - throw new IOException("XML name "+name+" is not valid for value '"+value+"'"); - - newLevelIfRequired(); - value = XMLUtil.escapeXML(value, charset, noLines); - - if (attributes == null) - attributes = new String[][] {{name, value}}; - else { - String[][] newattr = new String[attributes.length+1][]; - for (int i = 0; i < attributes.length; i++) { - condition(!attributes[i][0].equals(name), "attempt to define attribute with name "+name+" more than once for value '"+value+"'"); - newattr[i] = attributes[i]; - } - attributes = newattr; - attributes[attributes.length-1] = new String[] {name, value}; - } - } - - protected String getAttribute(String name) { - if (attributes != null) { - for (int i = 0; i < attributes.length; i++) { - if (attributes[i][0].equals(name)) { - return attributes[i][1]; - } - } - } - return null; - } - - protected void setAttribute(String name, String value) throws IOException { - newLevelIfRequired(); - if (attributes == null) - addAttribute(name, value, false); - else { - for (int i = 0; i < attributes.length; i++) { - if (attributes[i][0].equals(name)) { - attributes[i][1] = XMLUtil.escapeXML(value, charset, false); - return; - } - } - addAttribute(name, value); - } - } - - protected void commitAttributes() throws IOException { - - } - - - private boolean nameIsSpecial(String name) { - for (int i = 0; i < specialAttributeNames.length; i++) { - String n = specialAttributeNames[i]; - if (n.equalsIgnoreCase(name)) - return true; - } - return false; - } - - private void writeAttributes(int col) throws IOException { - commitAttributes(); - if (attributes != null && sortAttributes) - sortAttributes(); - int c = col; - c = writeAttributeSet(true, c, col); - writeAttributeSet(false, c, col); - attributes = null; - } - - - private void sortAttributes() { - // bubble sort - look, it's easy - for (int i = 0; i < attributes.length - 1; i++) { - for (int j = 0; j < attributes.length - 1; j++) { - if (String.CASE_INSENSITIVE_ORDER.compare(attributes[j][0], attributes[j+1][0]) < 0) { - String[] t = attributes[j]; - attributes[j] = attributes[j+1]; - attributes[j+1] = t; - } - } - } - - } - - - private int writeAttributeSet(boolean special, int col, int wrap) throws IOException { - // first pass: name, id - if (attributes != null) { - for (int i=0; i < attributes.length; i++) { - String[] element = attributes[i]; - if (nameIsSpecial(element[0]) == special) { - col = col + element[0].length()+element[1].length() + 4; - if (isPretty() && attributeLineWrap > 0 && col > attributeLineWrap && col > wrap) { - write(lineType == LINE_UNIX ? "\n" : "\r\n"); - for (int j = 0; j < wrap; j++) - write(" "); - col = wrap; - } - write(' '); - write(element[0]); - write("=\""); - if (element[1] != null) - write(xmlEscape(element[1])); - write("\""); - } - } - } - return col; - } - - protected String xmlEscape(String s) { - StringBuilder b = new StringBuilder(); - for (char c : s.toCharArray()) { - if (c < ' ') { - b.append("&#x"); - b.append(Integer.toHexString(c).toUpperCase()); - b.append(";"); - } else - b.append(c); - } - return b.toString(); - } - - /* (non-Javadoc) - * @see org.eclipse.ohf.utilities.xml.IXMLWriter#attribute(java.lang.String, java.lang.String, java.lang.String, boolean) - */ - @Override - public void attribute(String namespace, String name, String value, boolean onlyIfNotEmpty) throws IOException { - if (!onlyIfNotEmpty || value != null && !value.equals("")) - attribute(namespace, name, value); - } - - /* (non-Javadoc) - * @see org.eclipse.ohf.utilities.xml.IXMLWriter#attribute(java.lang.String, java.lang.String, java.lang.String) - */ - @Override - public void attribute(String namespace, String name, String value) throws IOException { - - checkStarted(); - if (namespace == null || namespace.equals("")) - addAttribute(name, value); - else - addAttribute(getNSAbbreviation(namespace)+name, value); - } - - /* (non-Javadoc) - * @see org.eclipse.ohf.utilities.xml.IXMLWriter#attribute(java.lang.String, java.lang.String, boolean) - */ - @Override - public void attribute(String name, String value, boolean onlyIfNotEmpty) throws IOException { - if (!onlyIfNotEmpty || value != null && !value.equals("")) - attribute(name, value); - } - - /* (non-Javadoc) - * @see org.eclipse.ohf.utilities.xml.IXMLWriter#attribute(java.lang.String, java.lang.String) - */ - @Override - public void attribute(String name, String value) throws IOException { - checkStarted(); - addAttribute(name, value); - } - - /* (non-Javadoc) - * @see org.eclipse.ohf.utilities.xml.IXMLWriter#attributeNoLines(java.lang.String, java.lang.String) - */ - @Override - public void attributeNoLines(String name, String value) throws IOException { - checkStarted(); - addAttribute(name, value, true); - } - - // -- levels ------------------------------------------------- - - private XMLWriterStateStack levels = new XMLWriterStateStack(); - - private void newLevelIfRequired() throws IOException { - if (!pendingOpen) { - if (!levels.empty()) - levels.current().seeChild(); - XMLWriterState level = new XMLWriterState(); - level.setPretty(isPretty()); - levels.push(level); - pendingOpen = true; - } - } - - // -- namespaces --------------------------------------------- - - - private void defineNamespace(String namespace, String abbrev) throws IOException { - checkStarted(); - if (namespace != null && !namespace.equals("")) { - if ("".equals(abbrev)) - abbrev = null; - - newLevelIfRequired(); - - levels.current().addNamespaceDefn(namespace, abbrev); - if (abbrev == null) - addAttribute("xmlns", namespace); - else - addAttribute("xmlns:"+abbrev, namespace); - } - } - - /* (non-Javadoc) - * @see org.eclipse.ohf.utilities.xml.IXMLWriter#findByNamespace(java.lang.String) - */ - public XMLNamespace findByNamespace(String namespace) { - for (int i = levels.size() - 1; i >= 0; i--) { - XMLNamespace ns = levels.item(i).getDefnByNamespace(namespace); - if (ns != null) - return ns; - } - return null; - } - - /* (non-Javadoc) - * @see org.eclipse.ohf.utilities.xml.IXMLWriter#namespaceDefined(java.lang.String) - */ - @Override - public boolean namespaceDefined(String namespace) { - return namespace == null || namespace.equals("") || findByNamespace(namespace) != null; - } - - /* (non-Javadoc) - * @see org.eclipse.ohf.utilities.xml.IXMLWriter#findByAbbreviation(java.lang.String) - */ - public XMLNamespace findByAbbreviation(String abbreviation) { - for (int i = levels.size() - 1; i >= 0; i--) { - XMLNamespace ns = levels.item(i).getDefnByAbbreviation(abbreviation); - if (ns != null) - return ns; - } - return null; - } - - /* (non-Javadoc) - * @see org.eclipse.ohf.utilities.xml.IXMLWriter#abbreviationDefined(java.lang.String) - */ - @Override - public boolean abbreviationDefined(String abbreviation) { - return findByAbbreviation(abbreviation) != null; - } - - protected XMLNamespace findDefaultNamespace() { - for (int i = levels.size() - 1; i >= 0; i--) { - XMLNamespace ns = levels.item(i).getDefaultNamespace(); - if (ns != null) - return ns; - } - return null; - } - - /* (non-Javadoc) - * @see org.eclipse.ohf.utilities.xml.IXMLWriter#getDefaultNamespace() - */ - @Override - public String getDefaultNamespace() { - XMLNamespace ns = findDefaultNamespace(); - if (ns == null) - return null; - else - return ns.getNamespace(); - } - - /* (non-Javadoc) - * @see org.eclipse.ohf.utilities.xml.IXMLWriter#namespace(java.lang.String) - */ - @Override - public void namespace(String namespace) throws IOException { - if (!namespaceDefined(namespace)) { - int index = 0; - while (abbreviationDefined("ns"+Integer.toString(index))) - index++; - defineNamespace(namespace, "ns"+Integer.toString(index)); - } - } - - /* (non-Javadoc) - * @see org.eclipse.ohf.utilities.xml.IXMLWriter#defaultNamespace(java.lang.String) - * - * Replace defaultNamespace() - */ - @Override - public void setDefaultNamespace(String namespace) throws IOException { - if ((namespace == null && getDefaultNamespace() != null) || - (namespace != null && !namespace.equals(getDefaultNamespace()))) - defineNamespace(namespace, ""); - } - - /* (non-Javadoc) - * @see org.eclipse.ohf.utilities.xml.IXMLWriter#namespace(java.lang.String, java.lang.String) - */ - @Override - public void namespace(String namespace, String abbreviation) throws IOException { - XMLNamespace ns = findByAbbreviation(abbreviation); - if (ns == null || !ns.getNamespace().equals(namespace)) - defineNamespace(namespace, abbreviation); - } - - - private String getNSAbbreviation(String namespace) throws IOException { - if ("http://www.w3.org/XML/1998/namespace".equals(namespace)) - return "xml:"; - - if (namespace == null || "".equals(namespace) || "noNamespace".equals(namespace)) - return ""; - - XMLNamespace ns = findByNamespace(namespace); - if (ns == null) - throw new IOException("Namespace "+namespace+" is not defined"); - else if (ns.getAbbreviation() == null) - return ""; - else - return ns.getAbbreviation()+":"; - } - - // -- public API ----------------------------------------------------------- - - /* (non-Javadoc) - * @see org.eclipse.ohf.utilities.xml.IXMLWriter#comment(java.lang.String, boolean) - */ - @Override - public void comment(String comment, boolean doPretty) throws IOException { - checkStarted(); - if (pendingClose) { - write('>'); - writePendingComment(); - pendingClose = false; - } - if (doPretty) { - writePretty(); - } - if (levels.inComment()) - write(" "); - if (doPretty && !isPretty()) - writePretty(); - } - - - private void writePendingComment() throws IOException { - if (pendingComment != null) { - if (isPretty()) - write(" "); - if (levels.inComment()) - write(""); - } - } - - /* (non-Javadoc) - * @see org.eclipse.ohf.utilities.xml.IXMLWriter#open(java.lang.String, java.lang.String) - */ - @Override - public void enter(String namespace, String name) throws IOException { - enter(namespace, name, null); - } - - - /* (non-Javadoc) - * @see org.eclipse.ohf.utilities.xml.IXMLWriter#open(java.lang.String, java.lang.String, java.lang.String) - */ - @Override - public void enter(String namespace, String name, String comment) throws IOException { - if (name == null) - throw new Error("name == null"); - if (!XMLUtil.isNMToken(name)) - throw new IOException("XML name "+name+" is not valid"); - checkStarted(); - if (pendingClose) { - write('>'); - writePendingComment(); - pendingClose = false; - } - - if (name == null) { - throw new IOException("name is null"); - } - newLevelIfRequired(); - levels.current().setName(name); - levels.current().setNamespace(namespace); - int col = writePretty(); - write('<'); - if (namespace == null) { - write(name); - col = col + name.length()+1; - } else { - String n = getNSAbbreviation(namespace)+name; - write(n); - col = col + n.length()+1; - } - writeAttributes(col); - pendingOpen = false; - pendingClose = true; - pendingComment = comment; - } - - - /* (non-Javadoc) - * @see org.eclipse.ohf.utilities.xml.IXMLWriter#close(java.lang.String) - */ - @Override - public void exit(String name) throws IOException { - checkStarted(); - if (levels.empty()) - throw new IOException("Unable to close null|"+name+", nothing to close"); - if (levels.current().getNamespace() != null || !levels.current().getName().equals(name)) - throw new IOException("Unable to close null|"+name+", found "+levels.current().getNamespace()+"|"+levels.current().getName()); - exit(); - } - - /* (non-Javadoc) - * @see org.eclipse.ohf.utilities.xml.IXMLWriter#close(java.lang.String, java.lang.String) - */ - @Override - public void exit(String namespace, String name) throws IOException { - checkStarted(); - if (levels.empty()) - throw new IOException("Unable to close "+namespace+"|"+name+", nothing to close"); - if (levels == null) - throw new Error("levels = null"); - if (levels.current() == null) - throw new Error("levels.current() = null"); - if (levels.current().getName() == null) - throw new Error("levels.current().getName() = null"); - if (levels.current().getNamespace() == null) - throw new Error("levels.current().getNamespace() = null"); - if (name == null) - throw new Error("name = null"); - if (namespace == null) - throw new Error("namespace = null"); - if (!levels.current().getNamespace().equals(namespace) || !levels.current().getName().equals(name)) - throw new IOException("Unable to close "+namespace+"|"+name+", found "+levels.current().getNamespace()+"|"+levels.current().getName()); - exit(); - } - - /* (non-Javadoc) - * @see org.eclipse.ohf.utilities.xml.IXMLWriter#closeToLevel(int) - */ - @Override - public void exitToLevel(int count) throws IOException { - checkStarted(); - while (levels.size() > count) - exit(); - } - - - /* (non-Javadoc) - * @see org.eclipse.ohf.utilities.xml.IXMLWriter#close() - */ - @Override - public void close() throws IOException { - checkStarted(); - if (!levels.empty()) - throw new IOException("Called close before exiting all opened elements"); - super.close(); - } - - @Override - public void end() throws IOException { - checkStarted(); - if (!levels.empty()) - throw new IOException("Called end() before exiting all opened elements"); - flush(); - } - @Override - public void exit() throws IOException { - checkStarted(); - if (levels.empty()) { - throw new IOException("Called exit one too many times"); - } else { - if (pendingClose) { - write("/>"); - writePendingComment(); - pendingClose = false; - } else { - if (levels.current().hasChildren()) - writePretty(); - write("'); - } - levels.pop(); - } - } - - /* (non-Javadoc) - * @see org.eclipse.ohf.utilities.xml.IXMLWriter#open(java.lang.String) - */ - @Override - public void enter(String name) throws IOException { - enter(null, name); - } - - - /* (non-Javadoc) - * @see org.eclipse.ohf.utilities.xml.IXMLWriter#element(java.lang.String, java.lang.String, java.lang.String, boolean) - */ - @Override - public void element(String namespace, String name, String content, boolean onlyIfNotEmpty) throws IOException { - if (!onlyIfNotEmpty || content != null && !content.equals("")) - element(namespace, name, content); - } - - /* (non-Javadoc) - * @see org.eclipse.ohf.utilities.xml.IXMLWriter#element(java.lang.String, java.lang.String, java.lang.String, java.lang.String) - */ - @Override - public void element(String namespace, String name, String content, String comment) throws IOException { - if (!XMLUtil.isNMToken(name)) - throw new IOException("XML name "+name+" is not valid"); - enter(namespace, name, comment); - text(content); - exit(); - } - - /* (non-Javadoc) - * @see org.eclipse.ohf.utilities.xml.IXMLWriter#element(java.lang.String, java.lang.String, java.lang.String) - */ - @Override - public void element(String namespace, String name, String content) throws IOException { - if (!XMLUtil.isNMToken(name)) - throw new IOException("XML name "+name+" is not valid"); - enter(namespace, name); - text(content); - exit(); - } - - /* (non-Javadoc) - * @see org.eclipse.ohf.utilities.xml.IXMLWriter#element(java.lang.String, java.lang.String, boolean) - */ - @Override - public void element(String name, String content, boolean onlyIfNotEmpty) throws IOException { - if (!onlyIfNotEmpty || content != null && !content.equals("")) - element(null, name, content); - } - - /* (non-Javadoc) - * @see org.eclipse.ohf.utilities.xml.IXMLWriter#element(java.lang.String, java.lang.String) - */ - @Override - public void element(String name, String content) throws IOException { - element(null, name, content); - } - - @Override - public void element(String name) throws IOException { - element(null, name, null); - } - - /* (non-Javadoc) - * @see org.eclipse.ohf.utilities.xml.IXMLWriter#text(java.lang.String) - */ - @Override - public void text(String content) throws IOException { - text(content, false); - } - - /* (non-Javadoc) - * @see org.eclipse.ohf.utilities.xml.IXMLWriter#text(java.lang.String, boolean) - * - * Replace escapeText() - */ - @Override - public void text(String content, boolean dontEscape) throws IOException { - checkInElement(); - if (content != null) { - if (pendingClose) { - write(">"); - writePendingComment(); - pendingClose = false; - } - if (dontEscape) - write(content); - else - write(XMLUtil.escapeXML(content, charset, false)); - } - } - - /* (non-Javadoc) - * @see org.eclipse.ohf.utilities.xml.IXMLWriter#cData(java.lang.String) - */ - @Override - public void cData(String text) throws IOException { - text(""); - } - - /* (non-Javadoc) - * @see org.eclipse.ohf.utilities.xml.IXMLWriter#writeBytes(byte[]) - */ - @Override - public void writeBytes(byte[] bytes) throws IOException { - checkInElement(); - if (pendingClose) { - write(">"); - writePendingComment(); - pendingClose = false; - } - flush(); - stream.write(bytes); - } - - - /* (non-Javadoc) - * @see org.eclipse.ohf.utilities.xml.IXMLWriter#isPretty() - */ - @Override - public boolean isPretty() throws IOException { - return (levels == null || levels.empty()) ? prettyBase : levels.current().isPretty(); - } - - /* (non-Javadoc) - * @see org.eclipse.ohf.utilities.xml.IXMLWriter#setPretty(boolean) - */ - @Override - public void setPretty(boolean pretty) throws IOException { - if (levels == null || levels.empty()) - this.prettyBase = pretty; - else - levels.current().setPretty(pretty); - } - - /* (non-Javadoc) - * @see org.eclipse.ohf.utilities.xml.IXMLWriter#startCommentBlock() - */ - @Override - public void startCommentBlock() throws IOException { - if (levels.inComment()) - throw new IOException("cannot nest comments"); - levels.current().setInComment(true); - if (isPretty()) - writePretty(); - write(""); - if (isPretty()) - writePretty(); - levels.current().setInComment(false); - } - - public boolean isSortAttributes() { - return sortAttributes; - } - - public void setSortAttributes(boolean sortAttributes) { - this.sortAttributes = sortAttributes; - } - - - public boolean isPrettyHeader() { - return prettyHeader; - } - - public void setPrettyHeader(boolean pretty) { - this.prettyHeader = pretty; - } - - public int writePretty() throws IOException { - return writePretty(true); - } - - public int writePretty(boolean eoln) throws IOException { - if (isPretty()) { - if (eoln) - write(lineType == LINE_UNIX ? "\n" : "\r\n"); - for (int i = 0; i < levels.size() - 1; i++) - write(" "); - return (levels.size() - 1) * 2; - } else - return 0; - } - - public int getLineType() { - return lineType; - } - - public void setLineType(int lineType) { - this.lineType = lineType; - } - - public boolean isXmlHeader() { - return xmlHeader; - } - - public void setXmlHeader(boolean xmlHeader) { - this.xmlHeader = xmlHeader; - } - - public String[] getSpecialAttributeNames() { - return specialAttributeNames; - } - - public void setSpecialAttributeNames(String[] specialAttributeNames) { - this.specialAttributeNames = specialAttributeNames; - } - - public int getAttributeLineWrap() { - return attributeLineWrap; - } - - public void setAttributeLineWrap(int attributeLineWrap) { - this.attributeLineWrap = attributeLineWrap; - } - - @Override - public void escapedText(String content) throws IOException { - text(""); - int i = content.length(); - if (isPretty()) - while (i > 0 && (content.charAt(i-1) == '\r' || content.charAt(i-1) == '\n')) - i--; - write(content.substring(0, i)); - } - - public void processingInstruction(String value) throws IOException { - write(""); - if (isPrettyHeader()) - write("\r\n"); - } - - @Override - public void link(String href) { - // ignore this - - } - - @Override - public void anchor(String name) { - // ignore this - } - - @Override - public void decorate(ElementDecoration element) throws IOException { - // nothing... - } - @Override - public void setSchemaLocation(String ns, String loc) throws IOException { - namespace("http://www.w3.org/2001/XMLSchema-instance", "xsi"); - attribute("http://www.w3.org/2001/XMLSchema-instance", "schemaLocation", ns+" "+loc); - - } - @Override - public void externalLink(String ref) { - // ignore this - } - - + + + +import java.io.IOException; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.io.UnsupportedEncodingException; + +import org.hl7.fhir.utilities.ElementDecoration; + +/** + * XML Writer class. + */ +public class XMLWriter extends OutputStreamWriter implements IXMLWriter { + + private boolean xmlHeader = true; + private String charset; + private boolean prettyBase; + private boolean prettyHeader; + private boolean pendingClose; + private boolean pendingOpen; + private String pendingComment; + private int lineType = LINE_UNIX; + private OutputStream stream; + private boolean started = false; + private String[] specialAttributeNames = new String[] {"id", "name" }; + private boolean sortAttributes; + private int attributeLineWrap; + private boolean xml1_1; + + public final static int LINE_UNIX = 0; + public final static int LINE_WINDOWS = 1; + + public XMLWriter(OutputStream stream, String charset, boolean xml1_1) throws UnsupportedEncodingException { + super(stream, charset); + this.stream = stream; + this.charset = charset; + this.xml1_1 = xml1_1; + } + public XMLWriter(OutputStream stream, String charset) throws UnsupportedEncodingException { + super(stream, charset); + this.stream = stream; + this.charset = charset; + this.xml1_1 = false; + } + + protected boolean condition(boolean bTest, String message) throws IOException { + if (!bTest) + throw new IOException(message); + return bTest; + } + + // -- writing context ------------------------------------------------ + + + + /** + * Returns the encoding. + * + * @param charset + * @return encoding + * @throws IOException + */ + public static String getXMLCharsetName(String charset) throws IOException { + if (charset == null || charset.equals("")) + return "UTF-8"; + else if (charset.equals("US-ASCII")) + return "UTF-8"; + else if (XMLUtil.charSetImpliesAscii(charset)) + return "ISO-8859-1"; + else if (charset.equals("UTF-8")) + return "UTF-8"; + else if (charset.equals("UTF-16") || charset.equals("UTF-16BE") || charset.equals("UTF-16LE")) + return "UTF-16"; + else + throw new IOException("Unknown charset encoding "+charset); + } + + /* (non-Javadoc) + * @see org.eclipse.ohf.utilities.xml.IXMLWriter#start() + */ + @Override + public void start() throws IOException { + condition(!started, "attempt to start after starting"); + levels.clear(); + attributes = null; + try { + if (xmlHeader) { + write(""); + if (prettyBase || prettyHeader) + write(lineType == LINE_UNIX ? "\n" : "\r\n"); + } + } catch (UnsupportedEncodingException e) { + // TODO Auto-generated catch block + throw new IOException(e.getMessage()); + } + started = true; + } + + private void checkStarted () throws IOException { + condition(started, "not started"); + } + + private void checkInElement() throws IOException { + condition(levels.size() > 0, "not in an element"); + } + + // -- attributes ---------------------------------------------------- + + private String[][] attributes; + + private void addAttribute(String name, String value) throws IOException { + addAttribute(name, value, false); + } + + private void addAttribute(String name, String value, boolean noLines) throws IOException { + if (!XMLUtil.isNMToken(name)) + throw new IOException("XML name "+name+" is not valid for value '"+value+"'"); + + newLevelIfRequired(); + value = XMLUtil.escapeXML(value, charset, noLines); + + if (attributes == null) + attributes = new String[][] {{name, value}}; + else { + String[][] newattr = new String[attributes.length+1][]; + for (int i = 0; i < attributes.length; i++) { + condition(!attributes[i][0].equals(name), "attempt to define attribute with name "+name+" more than once for value '"+value+"'"); + newattr[i] = attributes[i]; + } + attributes = newattr; + attributes[attributes.length-1] = new String[] {name, value}; + } + } + + protected String getAttribute(String name) { + if (attributes != null) { + for (int i = 0; i < attributes.length; i++) { + if (attributes[i][0].equals(name)) { + return attributes[i][1]; + } + } + } + return null; + } + + protected void setAttribute(String name, String value) throws IOException { + newLevelIfRequired(); + if (attributes == null) + addAttribute(name, value, false); + else { + for (int i = 0; i < attributes.length; i++) { + if (attributes[i][0].equals(name)) { + attributes[i][1] = XMLUtil.escapeXML(value, charset, false); + return; + } + } + addAttribute(name, value); + } + } + + protected void commitAttributes() throws IOException { + + } + + + private boolean nameIsSpecial(String name) { + for (int i = 0; i < specialAttributeNames.length; i++) { + String n = specialAttributeNames[i]; + if (n.equalsIgnoreCase(name)) + return true; + } + return false; + } + + private void writeAttributes(int col) throws IOException { + commitAttributes(); + if (attributes != null && sortAttributes) + sortAttributes(); + int c = col; + c = writeAttributeSet(true, c, col); + writeAttributeSet(false, c, col); + attributes = null; + } + + + private void sortAttributes() { + // bubble sort - look, it's easy + for (int i = 0; i < attributes.length - 1; i++) { + for (int j = 0; j < attributes.length - 1; j++) { + if (String.CASE_INSENSITIVE_ORDER.compare(attributes[j][0], attributes[j+1][0]) < 0) { + String[] t = attributes[j]; + attributes[j] = attributes[j+1]; + attributes[j+1] = t; + } + } + } + + } + + + private int writeAttributeSet(boolean special, int col, int wrap) throws IOException { + // first pass: name, id + if (attributes != null) { + for (int i=0; i < attributes.length; i++) { + String[] element = attributes[i]; + if (nameIsSpecial(element[0]) == special) { + col = col + element[0].length()+element[1].length() + 4; + if (isPretty() && attributeLineWrap > 0 && col > attributeLineWrap && col > wrap) { + write(lineType == LINE_UNIX ? "\n" : "\r\n"); + for (int j = 0; j < wrap; j++) + write(" "); + col = wrap; + } + write(' '); + write(element[0]); + write("=\""); + if (element[1] != null) + write(xmlEscape(element[1])); + write("\""); + } + } + } + return col; + } + + protected String xmlEscape(String s) { + StringBuilder b = new StringBuilder(); + for (char c : s.toCharArray()) { + if (c < ' ') { + b.append("&#x"); + b.append(Integer.toHexString(c).toUpperCase()); + b.append(";"); + } else + b.append(c); + } + return b.toString(); + } + + /* (non-Javadoc) + * @see org.eclipse.ohf.utilities.xml.IXMLWriter#attribute(java.lang.String, java.lang.String, java.lang.String, boolean) + */ + @Override + public void attribute(String namespace, String name, String value, boolean onlyIfNotEmpty) throws IOException { + if (!onlyIfNotEmpty || value != null && !value.equals("")) + attribute(namespace, name, value); + } + + /* (non-Javadoc) + * @see org.eclipse.ohf.utilities.xml.IXMLWriter#attribute(java.lang.String, java.lang.String, java.lang.String) + */ + @Override + public void attribute(String namespace, String name, String value) throws IOException { + + checkStarted(); + if (namespace == null || namespace.equals("")) + addAttribute(name, value); + else + addAttribute(getNSAbbreviation(namespace)+name, value); + } + + /* (non-Javadoc) + * @see org.eclipse.ohf.utilities.xml.IXMLWriter#attribute(java.lang.String, java.lang.String, boolean) + */ + @Override + public void attribute(String name, String value, boolean onlyIfNotEmpty) throws IOException { + if (!onlyIfNotEmpty || value != null && !value.equals("")) + attribute(name, value); + } + + /* (non-Javadoc) + * @see org.eclipse.ohf.utilities.xml.IXMLWriter#attribute(java.lang.String, java.lang.String) + */ + @Override + public void attribute(String name, String value) throws IOException { + checkStarted(); + addAttribute(name, value); + } + + /* (non-Javadoc) + * @see org.eclipse.ohf.utilities.xml.IXMLWriter#attributeNoLines(java.lang.String, java.lang.String) + */ + @Override + public void attributeNoLines(String name, String value) throws IOException { + checkStarted(); + addAttribute(name, value, true); + } + + // -- levels ------------------------------------------------- + + private XMLWriterStateStack levels = new XMLWriterStateStack(); + + private void newLevelIfRequired() throws IOException { + if (!pendingOpen) { + if (!levels.empty()) + levels.current().seeChild(); + XMLWriterState level = new XMLWriterState(); + level.setPretty(isPretty()); + levels.push(level); + pendingOpen = true; + } + } + + // -- namespaces --------------------------------------------- + + + private void defineNamespace(String namespace, String abbrev) throws IOException { + checkStarted(); + if (namespace != null && !namespace.equals("")) { + if ("".equals(abbrev)) + abbrev = null; + + newLevelIfRequired(); + + levels.current().addNamespaceDefn(namespace, abbrev); + if (abbrev == null) + addAttribute("xmlns", namespace); + else + addAttribute("xmlns:"+abbrev, namespace); + } + } + + /* (non-Javadoc) + * @see org.eclipse.ohf.utilities.xml.IXMLWriter#findByNamespace(java.lang.String) + */ + public XMLNamespace findByNamespace(String namespace) { + for (int i = levels.size() - 1; i >= 0; i--) { + XMLNamespace ns = levels.item(i).getDefnByNamespace(namespace); + if (ns != null) + return ns; + } + return null; + } + + /* (non-Javadoc) + * @see org.eclipse.ohf.utilities.xml.IXMLWriter#namespaceDefined(java.lang.String) + */ + @Override + public boolean namespaceDefined(String namespace) { + return namespace == null || namespace.equals("") || findByNamespace(namespace) != null; + } + + /* (non-Javadoc) + * @see org.eclipse.ohf.utilities.xml.IXMLWriter#findByAbbreviation(java.lang.String) + */ + public XMLNamespace findByAbbreviation(String abbreviation) { + for (int i = levels.size() - 1; i >= 0; i--) { + XMLNamespace ns = levels.item(i).getDefnByAbbreviation(abbreviation); + if (ns != null) + return ns; + } + return null; + } + + /* (non-Javadoc) + * @see org.eclipse.ohf.utilities.xml.IXMLWriter#abbreviationDefined(java.lang.String) + */ + @Override + public boolean abbreviationDefined(String abbreviation) { + return findByAbbreviation(abbreviation) != null; + } + + protected XMLNamespace findDefaultNamespace() { + for (int i = levels.size() - 1; i >= 0; i--) { + XMLNamespace ns = levels.item(i).getDefaultNamespace(); + if (ns != null) + return ns; + } + return null; + } + + /* (non-Javadoc) + * @see org.eclipse.ohf.utilities.xml.IXMLWriter#getDefaultNamespace() + */ + @Override + public String getDefaultNamespace() { + XMLNamespace ns = findDefaultNamespace(); + if (ns == null) + return null; + else + return ns.getNamespace(); + } + + /* (non-Javadoc) + * @see org.eclipse.ohf.utilities.xml.IXMLWriter#namespace(java.lang.String) + */ + @Override + public void namespace(String namespace) throws IOException { + if (!namespaceDefined(namespace)) { + int index = 0; + while (abbreviationDefined("ns"+Integer.toString(index))) + index++; + defineNamespace(namespace, "ns"+Integer.toString(index)); + } + } + + /* (non-Javadoc) + * @see org.eclipse.ohf.utilities.xml.IXMLWriter#defaultNamespace(java.lang.String) + * + * Replace defaultNamespace() + */ + @Override + public void setDefaultNamespace(String namespace) throws IOException { + if ((namespace == null && getDefaultNamespace() != null) || + (namespace != null && !namespace.equals(getDefaultNamespace()))) + defineNamespace(namespace, ""); + } + + /* (non-Javadoc) + * @see org.eclipse.ohf.utilities.xml.IXMLWriter#namespace(java.lang.String, java.lang.String) + */ + @Override + public void namespace(String namespace, String abbreviation) throws IOException { + XMLNamespace ns = findByAbbreviation(abbreviation); + if (ns == null || !ns.getNamespace().equals(namespace)) + defineNamespace(namespace, abbreviation); + } + + + private String getNSAbbreviation(String namespace) throws IOException { + if ("http://www.w3.org/XML/1998/namespace".equals(namespace)) + return "xml:"; + + if (namespace == null || "".equals(namespace) || "noNamespace".equals(namespace)) + return ""; + + XMLNamespace ns = findByNamespace(namespace); + if (ns == null) + throw new IOException("Namespace "+namespace+" is not defined"); + else if (ns.getAbbreviation() == null) + return ""; + else + return ns.getAbbreviation()+":"; + } + + // -- public API ----------------------------------------------------------- + + /* (non-Javadoc) + * @see org.eclipse.ohf.utilities.xml.IXMLWriter#comment(java.lang.String, boolean) + */ + @Override + public void comment(String comment, boolean doPretty) throws IOException { + checkStarted(); + if (pendingClose) { + write('>'); + writePendingComment(); + pendingClose = false; + } + if (doPretty) { + writePretty(); + } + if (levels.inComment()) + write(" "); + if (doPretty && !isPretty()) + writePretty(); + } + + + private void writePendingComment() throws IOException { + if (pendingComment != null) { + if (isPretty()) + write(" "); + if (levels.inComment()) + write(""); + } + } + + /* (non-Javadoc) + * @see org.eclipse.ohf.utilities.xml.IXMLWriter#open(java.lang.String, java.lang.String) + */ + @Override + public void enter(String namespace, String name) throws IOException { + enter(namespace, name, null); + } + + + /* (non-Javadoc) + * @see org.eclipse.ohf.utilities.xml.IXMLWriter#open(java.lang.String, java.lang.String, java.lang.String) + */ + @Override + public void enter(String namespace, String name, String comment) throws IOException { + if (name == null) + throw new Error("name == null"); + if (!XMLUtil.isNMToken(name)) + throw new IOException("XML name "+name+" is not valid"); + checkStarted(); + if (pendingClose) { + write('>'); + writePendingComment(); + pendingClose = false; + } + + if (name == null) { + throw new IOException("name is null"); + } + newLevelIfRequired(); + levels.current().setName(name); + levels.current().setNamespace(namespace); + int col = writePretty(); + write('<'); + if (namespace == null) { + write(name); + col = col + name.length()+1; + } else { + String n = getNSAbbreviation(namespace)+name; + write(n); + col = col + n.length()+1; + } + writeAttributes(col); + pendingOpen = false; + pendingClose = true; + pendingComment = comment; + } + + + /* (non-Javadoc) + * @see org.eclipse.ohf.utilities.xml.IXMLWriter#close(java.lang.String) + */ + @Override + public void exit(String name) throws IOException { + checkStarted(); + if (levels.empty()) + throw new IOException("Unable to close null|"+name+", nothing to close"); + if (levels.current().getNamespace() != null || !levels.current().getName().equals(name)) + throw new IOException("Unable to close null|"+name+", found "+levels.current().getNamespace()+"|"+levels.current().getName()); + exit(); + } + + /* (non-Javadoc) + * @see org.eclipse.ohf.utilities.xml.IXMLWriter#close(java.lang.String, java.lang.String) + */ + @Override + public void exit(String namespace, String name) throws IOException { + checkStarted(); + if (levels.empty()) + throw new IOException("Unable to close "+namespace+"|"+name+", nothing to close"); + if (levels == null) + throw new Error("levels = null"); + if (levels.current() == null) + throw new Error("levels.current() = null"); + if (levels.current().getName() == null) + throw new Error("levels.current().getName() = null"); + if (levels.current().getNamespace() == null) + throw new Error("levels.current().getNamespace() = null"); + if (name == null) + throw new Error("name = null"); + if (namespace == null) + throw new Error("namespace = null"); + if (!levels.current().getNamespace().equals(namespace) || !levels.current().getName().equals(name)) + throw new IOException("Unable to close "+namespace+"|"+name+", found "+levels.current().getNamespace()+"|"+levels.current().getName()); + exit(); + } + + /* (non-Javadoc) + * @see org.eclipse.ohf.utilities.xml.IXMLWriter#closeToLevel(int) + */ + @Override + public void exitToLevel(int count) throws IOException { + checkStarted(); + while (levels.size() > count) + exit(); + } + + + /* (non-Javadoc) + * @see org.eclipse.ohf.utilities.xml.IXMLWriter#close() + */ + @Override + public void close() throws IOException { + checkStarted(); + if (!levels.empty()) + throw new IOException("Called close before exiting all opened elements"); + super.close(); + } + + @Override + public void end() throws IOException { + checkStarted(); + if (!levels.empty()) + throw new IOException("Called end() before exiting all opened elements"); + flush(); + } + @Override + public void exit() throws IOException { + checkStarted(); + if (levels.empty()) { + throw new IOException("Called exit one too many times"); + } else { + if (pendingClose) { + write("/>"); + writePendingComment(); + pendingClose = false; + } else { + if (levels.current().hasChildren()) + writePretty(); + write("'); + } + levels.pop(); + } + } + + /* (non-Javadoc) + * @see org.eclipse.ohf.utilities.xml.IXMLWriter#open(java.lang.String) + */ + @Override + public void enter(String name) throws IOException { + enter(null, name); + } + + + /* (non-Javadoc) + * @see org.eclipse.ohf.utilities.xml.IXMLWriter#element(java.lang.String, java.lang.String, java.lang.String, boolean) + */ + @Override + public void element(String namespace, String name, String content, boolean onlyIfNotEmpty) throws IOException { + if (!onlyIfNotEmpty || content != null && !content.equals("")) + element(namespace, name, content); + } + + /* (non-Javadoc) + * @see org.eclipse.ohf.utilities.xml.IXMLWriter#element(java.lang.String, java.lang.String, java.lang.String, java.lang.String) + */ + @Override + public void element(String namespace, String name, String content, String comment) throws IOException { + if (!XMLUtil.isNMToken(name)) + throw new IOException("XML name "+name+" is not valid"); + enter(namespace, name, comment); + text(content); + exit(); + } + + /* (non-Javadoc) + * @see org.eclipse.ohf.utilities.xml.IXMLWriter#element(java.lang.String, java.lang.String, java.lang.String) + */ + @Override + public void element(String namespace, String name, String content) throws IOException { + if (!XMLUtil.isNMToken(name)) + throw new IOException("XML name "+name+" is not valid"); + enter(namespace, name); + text(content); + exit(); + } + + /* (non-Javadoc) + * @see org.eclipse.ohf.utilities.xml.IXMLWriter#element(java.lang.String, java.lang.String, boolean) + */ + @Override + public void element(String name, String content, boolean onlyIfNotEmpty) throws IOException { + if (!onlyIfNotEmpty || content != null && !content.equals("")) + element(null, name, content); + } + + /* (non-Javadoc) + * @see org.eclipse.ohf.utilities.xml.IXMLWriter#element(java.lang.String, java.lang.String) + */ + @Override + public void element(String name, String content) throws IOException { + element(null, name, content); + } + + @Override + public void element(String name) throws IOException { + element(null, name, null); + } + + /* (non-Javadoc) + * @see org.eclipse.ohf.utilities.xml.IXMLWriter#text(java.lang.String) + */ + @Override + public void text(String content) throws IOException { + text(content, false); + } + + /* (non-Javadoc) + * @see org.eclipse.ohf.utilities.xml.IXMLWriter#text(java.lang.String, boolean) + * + * Replace escapeText() + */ + @Override + public void text(String content, boolean dontEscape) throws IOException { + checkInElement(); + if (content != null) { + if (pendingClose) { + write(">"); + writePendingComment(); + pendingClose = false; + } + if (dontEscape) + write(content); + else + write(XMLUtil.escapeXML(content, charset, false)); + } + } + + /* (non-Javadoc) + * @see org.eclipse.ohf.utilities.xml.IXMLWriter#cData(java.lang.String) + */ + @Override + public void cData(String text) throws IOException { + text(""); + } + + /* (non-Javadoc) + * @see org.eclipse.ohf.utilities.xml.IXMLWriter#writeBytes(byte[]) + */ + @Override + public void writeBytes(byte[] bytes) throws IOException { + checkInElement(); + if (pendingClose) { + write(">"); + writePendingComment(); + pendingClose = false; + } + flush(); + stream.write(bytes); + } + + + /* (non-Javadoc) + * @see org.eclipse.ohf.utilities.xml.IXMLWriter#isPretty() + */ + @Override + public boolean isPretty() throws IOException { + return (levels == null || levels.empty()) ? prettyBase : levels.current().isPretty(); + } + + /* (non-Javadoc) + * @see org.eclipse.ohf.utilities.xml.IXMLWriter#setPretty(boolean) + */ + @Override + public void setPretty(boolean pretty) throws IOException { + if (levels == null || levels.empty()) + this.prettyBase = pretty; + else + levels.current().setPretty(pretty); + } + + /* (non-Javadoc) + * @see org.eclipse.ohf.utilities.xml.IXMLWriter#startCommentBlock() + */ + @Override + public void startCommentBlock() throws IOException { + if (levels.inComment()) + throw new IOException("cannot nest comments"); + levels.current().setInComment(true); + if (isPretty()) + writePretty(); + write(""); + if (isPretty()) + writePretty(); + levels.current().setInComment(false); + } + + public boolean isSortAttributes() { + return sortAttributes; + } + + public void setSortAttributes(boolean sortAttributes) { + this.sortAttributes = sortAttributes; + } + + + public boolean isPrettyHeader() { + return prettyHeader; + } + + public void setPrettyHeader(boolean pretty) { + this.prettyHeader = pretty; + } + + public int writePretty() throws IOException { + return writePretty(true); + } + + public int writePretty(boolean eoln) throws IOException { + if (isPretty()) { + if (eoln) + write(lineType == LINE_UNIX ? "\n" : "\r\n"); + for (int i = 0; i < levels.size() - 1; i++) + write(" "); + return (levels.size() - 1) * 2; + } else + return 0; + } + + public int getLineType() { + return lineType; + } + + public void setLineType(int lineType) { + this.lineType = lineType; + } + + public boolean isXmlHeader() { + return xmlHeader; + } + + public void setXmlHeader(boolean xmlHeader) { + this.xmlHeader = xmlHeader; + } + + public String[] getSpecialAttributeNames() { + return specialAttributeNames; + } + + public void setSpecialAttributeNames(String[] specialAttributeNames) { + this.specialAttributeNames = specialAttributeNames; + } + + public int getAttributeLineWrap() { + return attributeLineWrap; + } + + public void setAttributeLineWrap(int attributeLineWrap) { + this.attributeLineWrap = attributeLineWrap; + } + + @Override + public void escapedText(String content) throws IOException { + text(""); + int i = content.length(); + if (isPretty()) + while (i > 0 && (content.charAt(i-1) == '\r' || content.charAt(i-1) == '\n')) + i--; + write(content.substring(0, i)); + } + + public void processingInstruction(String value) throws IOException { + write(""); + if (isPrettyHeader()) + write("\r\n"); + } + + @Override + public void link(String href) { + // ignore this + + } + + @Override + public void anchor(String name) { + // ignore this + } + + @Override + public void decorate(ElementDecoration element) throws IOException { + // nothing... + } + @Override + public void setSchemaLocation(String ns, String loc) throws IOException { + namespace("http://www.w3.org/2001/XMLSchema-instance", "xsi"); + attribute("http://www.w3.org/2001/XMLSchema-instance", "schemaLocation", ns+" "+loc); + + } + @Override + public void externalLink(String ref) { + // ignore this + } + + @Override + public void attributeEllipse() { + // ignore this + } + + @Override + public void ellipse() throws IOException { + // ignore this + } + } \ No newline at end of file From fff805e5b73731394f22697f30625b07425618bc Mon Sep 17 00:00:00 2001 From: Lloyd McKenzie Date: Tue, 10 Sep 2024 08:07:01 -0600 Subject: [PATCH 3/4] Made changes as recommended by Grahame and Elliot --- .../org/hl7/fhir/r5/elementmodel/Element.java | 14 +++--- .../hl7/fhir/r5/elementmodel/JsonParser.java | 32 ++++++------- .../hl7/fhir/r5/elementmodel/XmlParser.java | 46 +++++++++---------- .../org/hl7/fhir/r5/formats/JsonCreator.java | 3 +- .../fhir/r5/formats/JsonCreatorCanonical.java | 5 +- .../fhir/r5/formats/JsonCreatorDirect.java | 5 +- .../hl7/fhir/r5/formats/JsonCreatorGson.java | 5 +- .../hl7/fhir/utilities/xml/IXMLWriter.java | 5 +- .../org/hl7/fhir/utilities/xml/XMLWriter.java | 7 ++- 9 files changed, 68 insertions(+), 54 deletions(-) diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/elementmodel/Element.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/elementmodel/Element.java index 1aaecc1c9..71643ba9e 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/elementmodel/Element.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/elementmodel/Element.java @@ -162,7 +162,7 @@ public class Element extends Base implements NamedItem { private FhirFormat format; private Object nativeObject; private List sliceDefinitions; - private boolean ellipsed; + private boolean ellided; public Element(String name) { super(); @@ -1430,8 +1430,8 @@ public class Element extends Base implements NamedItem { public Base copy() { Element element = new Element(this); this.copyValues(element); - if (this.isEllipsed()) - element.setEllipsed(true); + if (this.isEllided()) + element.setEllided(true); return element; } @@ -1641,11 +1641,11 @@ public class Element extends Base implements NamedItem { return FhirPublication.fromCode(property.getStructure().getVersion()); } - public void setEllipsed(boolean ellipsed) { - this.ellipsed = ellipsed; + public void setEllided(boolean ellided) { + this.ellided = ellided; } - public boolean isEllipsed() { - return this.ellipsed; + public boolean isEllided() { + return this.ellided; } } \ No newline at end of file diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/elementmodel/JsonParser.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/elementmodel/JsonParser.java index f8f77e387..20b27c8cd 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/elementmodel/JsonParser.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/elementmodel/JsonParser.java @@ -86,8 +86,8 @@ public class JsonParser extends ParserBase { private JsonCreator json; private boolean allowComments; - private boolean ellipseElements; - private boolean suppressResourceType; + private boolean ellideElements; +// private boolean suppressResourceType; private Element baseElement; private boolean markedXhtml; @@ -785,7 +785,7 @@ public class JsonParser extends ParserBase { } checkComposeComments(e); json.beginObject(); - if (!isSuppressResourceType()) +// if (!isSuppressResourceType()) prop("resourceType", e.getType(), null); Set done = new HashSet(); for (Element child : e.getChildren()) { @@ -811,7 +811,7 @@ public class JsonParser extends ParserBase { checkComposeComments(e); json.beginObject(); - if (!isSuppressResourceType()) +// if (!isSuppressResourceType()) prop("resourceType", e.getType(), linkResolver == null ? null : linkResolver.resolveProperty(e.getProperty())); Set done = new HashSet(); for (Element child : e.getChildren()) { @@ -826,8 +826,8 @@ public class JsonParser extends ParserBase { if (wantCompose(path, child)) { boolean isList = child.hasElementProperty() ? child.getElementProperty().isList() : child.getProperty().isList(); if (!isList) {// for specials, ignore the cardinality of the stated type - if (child.isEllipsed() && ellipseElements) - json.ellipse(); + if (child.isEllided() && isEllideElements() && json.canEllide()) + json.ellide(); else compose(path, child); } else if (!done.contains(child.getName())) { @@ -887,8 +887,8 @@ public class JsonParser extends ParserBase { if (prim) { openArray(name, linkResolver == null ? null : linkResolver.resolveProperty(list.get(0).getProperty())); for (Element item : list) { - if (item.isEllipsed()) - json.ellipse(); + if (item.isEllided() && json.canEllide()) + json.ellide(); else if (item.hasValue()) { if (linkResolver != null && item.getProperty().isReference()) { String ref = linkResolver.resolveReference(getReferenceForElement(item)); @@ -908,8 +908,8 @@ public class JsonParser extends ParserBase { openArray(name, linkResolver == null ? null : linkResolver.resolveProperty(list.get(0).getProperty())); int i = 0; for (Element item : list) { - if (item.isEllipsed()) - json.ellipse(); + if (item.isEllided() && json.canEllide()) + json.ellide(); else if (item.hasChildren()) { open(null,null); if (item.getProperty().isResource()) { @@ -996,15 +996,15 @@ public class JsonParser extends ParserBase { return this; } - public boolean isEllipseElements() { - return ellipseElements; + public boolean isEllideElements() { + return ellideElements; } - public JsonParser setEllipseElements(boolean ellipseElements) { - this.ellipseElements = ellipseElements; + public JsonParser setEllideElements(boolean ellideElements) { + this.ellideElements = ellideElements; return this; } - +/* public boolean isSuppressResourceType() { return suppressResourceType; } @@ -1013,6 +1013,6 @@ public class JsonParser extends ParserBase { this.suppressResourceType = suppressResourceType; return this; } - +*/ } \ No newline at end of file diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/elementmodel/XmlParser.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/elementmodel/XmlParser.java index ce65e2a0c..e2ef427be 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/elementmodel/XmlParser.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/elementmodel/XmlParser.java @@ -94,7 +94,7 @@ import org.xml.sax.XMLReader; public class XmlParser extends ParserBase { private boolean allowXsiLocation; private String version; - private boolean ellipseElements; + private boolean ellideElements; public XmlParser(IWorkerContext context) { super(context); @@ -806,7 +806,7 @@ public class XmlParser extends ParserBase { } private void composeElement(IXMLWriter xml, Element element, String elementName, boolean root) throws IOException, FHIRException { - if (!(isEllipseElements() && element.isEllipsed())) { + if (!(isEllideElements() && element.isEllided())) { if (showDecorations) { @SuppressWarnings("unchecked") List decorations = (List) element.getUserData("fhir.decorations"); @@ -819,8 +819,8 @@ public class XmlParser extends ParserBase { } } if (isText(element.getProperty())) { - if (isEllipseElements() && element.isEllipsed()) - xml.ellipse(); + if (isEllideElements() && element.isEllided() && xml.canEllide()) + xml.ellide(); else { if (linkResolver != null) xml.link(linkResolver.resolveProperty(element.getProperty())); @@ -835,8 +835,8 @@ public class XmlParser extends ParserBase { xml.exit(element.getProperty().getXmlNamespace(),elementName); } } else if (!element.hasChildren() && !element.hasValue()) { - if (isEllipseElements() && element.isEllipsed()) - xml.ellipse(); + if (isEllideElements() && element.isEllided() && xml.canEllide()) + xml.ellide(); else { if (element.getExplicitType() != null) xml.attribute("xsi:type", element.getExplicitType()); @@ -844,8 +844,8 @@ public class XmlParser extends ParserBase { } } else if (element.isPrimitive() || (element.hasType() && isPrimitive(element.getType()))) { if (element.getType().equals("xhtml")) { - if (isEllipseElements() && element.isEllipsed()) - xml.ellipse(); + if (isEllideElements() && element.isEllided() && xml.canEllide()) + xml.ellide(); else { String rawXhtml = element.getValue(); if (isCdaText(element.getProperty())) { @@ -859,16 +859,16 @@ public class XmlParser extends ParserBase { } } } else if (isText(element.getProperty())) { - if (isEllipseElements() && element.isEllipsed()) - xml.ellipse(); + if (isEllideElements() && element.isEllided() && xml.canEllide()) + xml.ellide(); else { if (linkResolver != null) xml.link(linkResolver.resolveProperty(element.getProperty())); xml.text(element.getValue()); } } else { - if (isEllipseElements() && element.isEllipsed()) - xml.attributeEllipse(); + if (isEllideElements() && element.isEllided()) + xml.attributeEllide(); else { setXsiTypeIfIsTypeAttr(xml, element); if (element.hasValue()) { @@ -894,22 +894,22 @@ public class XmlParser extends ParserBase { } } } else { - if (isEllipseElements() && element.isEllipsed()) - xml.ellipse(); + if (isEllideElements() && element.isEllided() && xml.canEllide()) + xml.ellide(); else { setXsiTypeIfIsTypeAttr(xml, element); Set handled = new HashSet<>(); for (Element child : element.getChildren()) { if (!handled.contains(child.getName()) && isAttr(child.getProperty()) && wantCompose(element.getPath(), child)) { handled.add(child.getName()); - if (isEllipseElements() && child.isEllipsed()) - xml.attributeEllipse(); + if (isEllideElements() && child.isEllided()) + xml.attributeEllide(); else { String av = child.getValue(); if (child.getProperty().isList()) { for (Element c2 : element.getChildren()) { if (c2 != child && c2.getName().equals(child.getName())) { - if (c2.isEllipsed()) + if (c2.isEllided()) av = av + " ..."; else av = av + " " + c2.getValue(); @@ -948,8 +948,8 @@ public class XmlParser extends ParserBase { } for (Element child : element.getChildren()) { if (wantCompose(element.getPath(), child)) { - if (isEllipseElements() && child.isEllipsed()) - xml.ellipse(); + if (isEllideElements() && child.isEllided() && xml.canEllide()) + xml.ellide(); else { if (isText(child.getProperty())) { if (linkResolver != null) @@ -1073,12 +1073,12 @@ public class XmlParser extends ParserBase { } } - public boolean isEllipseElements() { - return ellipseElements; + public boolean isEllideElements() { + return ellideElements; } - public void setEllipseElements(boolean ellipseElements) { - this.ellipseElements = ellipseElements; + public void setEllideElements(boolean ellideElements) { + this.ellideElements = ellideElements; } } \ No newline at end of file diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/formats/JsonCreator.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/formats/JsonCreator.java index 58f44d505..c5edc1180 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/formats/JsonCreator.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/formats/JsonCreator.java @@ -71,5 +71,6 @@ public interface JsonCreator { void link(String href); void anchor(String string); void externalLink(String string); - void ellipse(); + void ellide(); + boolean canEllide(); } \ No newline at end of file diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/formats/JsonCreatorCanonical.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/formats/JsonCreatorCanonical.java index badb9a4a0..43b16362b 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/formats/JsonCreatorCanonical.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/formats/JsonCreatorCanonical.java @@ -281,7 +281,10 @@ public class JsonCreatorCanonical implements JsonCreator { } @Override - public void ellipse() { + public boolean canEllide() { return false; } + + @Override + public void ellide() { // not used } diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/formats/JsonCreatorDirect.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/formats/JsonCreatorDirect.java index 5132099f3..9148d8636 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/formats/JsonCreatorDirect.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/formats/JsonCreatorDirect.java @@ -245,7 +245,10 @@ public class JsonCreatorDirect implements JsonCreator { } @Override - public void ellipse() { + public boolean canEllide() { return false; } + + @Override + public void ellide() { // not used } } \ No newline at end of file diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/formats/JsonCreatorGson.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/formats/JsonCreatorGson.java index ded1fcac7..7d7a701ae 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/formats/JsonCreatorGson.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/formats/JsonCreatorGson.java @@ -128,8 +128,11 @@ public class JsonCreatorGson implements JsonCreator { } @Override - public void ellipse() { + public void ellide() { // not used } + @Override + public boolean canEllide() { return false;} + } \ No newline at end of file diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/xml/IXMLWriter.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/xml/IXMLWriter.java index 0a2a61ec3..cb0276f9d 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/xml/IXMLWriter.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/xml/IXMLWriter.java @@ -106,6 +106,7 @@ public interface IXMLWriter { public abstract void externalLink(String ref) throws IOException; // this is only implemented by an implementation that is producing an xhtml representation and handles ellipsing elements - public abstract void ellipse() throws IOException; - public abstract void attributeEllipse(); + public abstract boolean canEllide(); + public abstract void ellide() throws IOException; + public abstract void attributeEllide(); } \ No newline at end of file diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/xml/XMLWriter.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/xml/XMLWriter.java index 777083e99..aa70a26dd 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/xml/XMLWriter.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/xml/XMLWriter.java @@ -913,12 +913,15 @@ public class XMLWriter extends OutputStreamWriter implements IXMLWriter { } @Override - public void attributeEllipse() { + public boolean canEllide() { return false; } + + @Override + public void attributeEllide() { // ignore this } @Override - public void ellipse() throws IOException { + public void ellide() throws IOException { // ignore this } From 729c2da4c05327365bb62c42f9188f6b47a8c8a7 Mon Sep 17 00:00:00 2001 From: Lloyd McKenzie Date: Tue, 10 Sep 2024 08:23:26 -0600 Subject: [PATCH 4/4] Fixed elide spelling --- .../org/hl7/fhir/r5/elementmodel/Element.java | 14 +++--- .../hl7/fhir/r5/elementmodel/JsonParser.java | 22 ++++----- .../hl7/fhir/r5/elementmodel/XmlParser.java | 46 +++++++++---------- .../org/hl7/fhir/r5/formats/JsonCreator.java | 4 +- .../fhir/r5/formats/JsonCreatorCanonical.java | 4 +- .../fhir/r5/formats/JsonCreatorDirect.java | 4 +- .../hl7/fhir/r5/formats/JsonCreatorGson.java | 4 +- .../hl7/fhir/utilities/xml/IXMLWriter.java | 6 +-- .../org/hl7/fhir/utilities/xml/XMLWriter.java | 6 +-- 9 files changed, 55 insertions(+), 55 deletions(-) diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/elementmodel/Element.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/elementmodel/Element.java index 71643ba9e..fffa8164a 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/elementmodel/Element.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/elementmodel/Element.java @@ -162,7 +162,7 @@ public class Element extends Base implements NamedItem { private FhirFormat format; private Object nativeObject; private List sliceDefinitions; - private boolean ellided; + private boolean elided; public Element(String name) { super(); @@ -1430,8 +1430,8 @@ public class Element extends Base implements NamedItem { public Base copy() { Element element = new Element(this); this.copyValues(element); - if (this.isEllided()) - element.setEllided(true); + if (this.isElided()) + element.setElided(true); return element; } @@ -1641,11 +1641,11 @@ public class Element extends Base implements NamedItem { return FhirPublication.fromCode(property.getStructure().getVersion()); } - public void setEllided(boolean ellided) { - this.ellided = ellided; + public void setElided(boolean elided) { + this.elided = elided; } - public boolean isEllided() { - return this.ellided; + public boolean isElided() { + return this.elided; } } \ No newline at end of file diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/elementmodel/JsonParser.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/elementmodel/JsonParser.java index 20b27c8cd..fa64c7ada 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/elementmodel/JsonParser.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/elementmodel/JsonParser.java @@ -86,7 +86,7 @@ public class JsonParser extends ParserBase { private JsonCreator json; private boolean allowComments; - private boolean ellideElements; + private boolean elideElements; // private boolean suppressResourceType; private Element baseElement; @@ -826,8 +826,8 @@ public class JsonParser extends ParserBase { if (wantCompose(path, child)) { boolean isList = child.hasElementProperty() ? child.getElementProperty().isList() : child.getProperty().isList(); if (!isList) {// for specials, ignore the cardinality of the stated type - if (child.isEllided() && isEllideElements() && json.canEllide()) - json.ellide(); + if (child.isElided() && isElideElements() && json.canElide()) + json.elide(); else compose(path, child); } else if (!done.contains(child.getName())) { @@ -887,8 +887,8 @@ public class JsonParser extends ParserBase { if (prim) { openArray(name, linkResolver == null ? null : linkResolver.resolveProperty(list.get(0).getProperty())); for (Element item : list) { - if (item.isEllided() && json.canEllide()) - json.ellide(); + if (item.isElided() && json.canElide()) + json.elide(); else if (item.hasValue()) { if (linkResolver != null && item.getProperty().isReference()) { String ref = linkResolver.resolveReference(getReferenceForElement(item)); @@ -908,8 +908,8 @@ public class JsonParser extends ParserBase { openArray(name, linkResolver == null ? null : linkResolver.resolveProperty(list.get(0).getProperty())); int i = 0; for (Element item : list) { - if (item.isEllided() && json.canEllide()) - json.ellide(); + if (item.isElided() && json.canElide()) + json.elide(); else if (item.hasChildren()) { open(null,null); if (item.getProperty().isResource()) { @@ -996,12 +996,12 @@ public class JsonParser extends ParserBase { return this; } - public boolean isEllideElements() { - return ellideElements; + public boolean isElideElements() { + return elideElements; } - public JsonParser setEllideElements(boolean ellideElements) { - this.ellideElements = ellideElements; + public JsonParser setElideElements(boolean elideElements) { + this.elideElements = elideElements; return this; } /* diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/elementmodel/XmlParser.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/elementmodel/XmlParser.java index e2ef427be..4669b6095 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/elementmodel/XmlParser.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/elementmodel/XmlParser.java @@ -94,7 +94,7 @@ import org.xml.sax.XMLReader; public class XmlParser extends ParserBase { private boolean allowXsiLocation; private String version; - private boolean ellideElements; + private boolean elideElements; public XmlParser(IWorkerContext context) { super(context); @@ -806,7 +806,7 @@ public class XmlParser extends ParserBase { } private void composeElement(IXMLWriter xml, Element element, String elementName, boolean root) throws IOException, FHIRException { - if (!(isEllideElements() && element.isEllided())) { + if (!(isElideElements() && element.isElided())) { if (showDecorations) { @SuppressWarnings("unchecked") List decorations = (List) element.getUserData("fhir.decorations"); @@ -819,8 +819,8 @@ public class XmlParser extends ParserBase { } } if (isText(element.getProperty())) { - if (isEllideElements() && element.isEllided() && xml.canEllide()) - xml.ellide(); + if (isElideElements() && element.isElided() && xml.canElide()) + xml.elide(); else { if (linkResolver != null) xml.link(linkResolver.resolveProperty(element.getProperty())); @@ -835,8 +835,8 @@ public class XmlParser extends ParserBase { xml.exit(element.getProperty().getXmlNamespace(),elementName); } } else if (!element.hasChildren() && !element.hasValue()) { - if (isEllideElements() && element.isEllided() && xml.canEllide()) - xml.ellide(); + if (isElideElements() && element.isElided() && xml.canElide()) + xml.elide(); else { if (element.getExplicitType() != null) xml.attribute("xsi:type", element.getExplicitType()); @@ -844,8 +844,8 @@ public class XmlParser extends ParserBase { } } else if (element.isPrimitive() || (element.hasType() && isPrimitive(element.getType()))) { if (element.getType().equals("xhtml")) { - if (isEllideElements() && element.isEllided() && xml.canEllide()) - xml.ellide(); + if (isElideElements() && element.isElided() && xml.canElide()) + xml.elide(); else { String rawXhtml = element.getValue(); if (isCdaText(element.getProperty())) { @@ -859,16 +859,16 @@ public class XmlParser extends ParserBase { } } } else if (isText(element.getProperty())) { - if (isEllideElements() && element.isEllided() && xml.canEllide()) - xml.ellide(); + if (isElideElements() && element.isElided() && xml.canElide()) + xml.elide(); else { if (linkResolver != null) xml.link(linkResolver.resolveProperty(element.getProperty())); xml.text(element.getValue()); } } else { - if (isEllideElements() && element.isEllided()) - xml.attributeEllide(); + if (isElideElements() && element.isElided()) + xml.attributeElide(); else { setXsiTypeIfIsTypeAttr(xml, element); if (element.hasValue()) { @@ -894,22 +894,22 @@ public class XmlParser extends ParserBase { } } } else { - if (isEllideElements() && element.isEllided() && xml.canEllide()) - xml.ellide(); + if (isElideElements() && element.isElided() && xml.canElide()) + xml.elide(); else { setXsiTypeIfIsTypeAttr(xml, element); Set handled = new HashSet<>(); for (Element child : element.getChildren()) { if (!handled.contains(child.getName()) && isAttr(child.getProperty()) && wantCompose(element.getPath(), child)) { handled.add(child.getName()); - if (isEllideElements() && child.isEllided()) - xml.attributeEllide(); + if (isElideElements() && child.isElided()) + xml.attributeElide(); else { String av = child.getValue(); if (child.getProperty().isList()) { for (Element c2 : element.getChildren()) { if (c2 != child && c2.getName().equals(child.getName())) { - if (c2.isEllided()) + if (c2.isElided()) av = av + " ..."; else av = av + " " + c2.getValue(); @@ -948,8 +948,8 @@ public class XmlParser extends ParserBase { } for (Element child : element.getChildren()) { if (wantCompose(element.getPath(), child)) { - if (isEllideElements() && child.isEllided() && xml.canEllide()) - xml.ellide(); + if (isElideElements() && child.isElided() && xml.canElide()) + xml.elide(); else { if (isText(child.getProperty())) { if (linkResolver != null) @@ -1073,12 +1073,12 @@ public class XmlParser extends ParserBase { } } - public boolean isEllideElements() { - return ellideElements; + public boolean isElideElements() { + return elideElements; } - public void setEllideElements(boolean ellideElements) { - this.ellideElements = ellideElements; + public void setElideElements(boolean elideElements) { + this.elideElements = elideElements; } } \ No newline at end of file diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/formats/JsonCreator.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/formats/JsonCreator.java index c5edc1180..84c46ce0c 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/formats/JsonCreator.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/formats/JsonCreator.java @@ -71,6 +71,6 @@ public interface JsonCreator { void link(String href); void anchor(String string); void externalLink(String string); - void ellide(); - boolean canEllide(); + void elide(); + boolean canElide(); } \ No newline at end of file diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/formats/JsonCreatorCanonical.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/formats/JsonCreatorCanonical.java index 43b16362b..426e9cb0e 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/formats/JsonCreatorCanonical.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/formats/JsonCreatorCanonical.java @@ -281,10 +281,10 @@ public class JsonCreatorCanonical implements JsonCreator { } @Override - public boolean canEllide() { return false; } + public boolean canElide() { return false; } @Override - public void ellide() { + public void elide() { // not used } diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/formats/JsonCreatorDirect.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/formats/JsonCreatorDirect.java index 9148d8636..41ac714c9 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/formats/JsonCreatorDirect.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/formats/JsonCreatorDirect.java @@ -245,10 +245,10 @@ public class JsonCreatorDirect implements JsonCreator { } @Override - public boolean canEllide() { return false; } + public boolean canElide() { return false; } @Override - public void ellide() { + public void elide() { // not used } } \ No newline at end of file diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/formats/JsonCreatorGson.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/formats/JsonCreatorGson.java index 7d7a701ae..14c76b0cd 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/formats/JsonCreatorGson.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/formats/JsonCreatorGson.java @@ -128,11 +128,11 @@ public class JsonCreatorGson implements JsonCreator { } @Override - public void ellide() { + public void elide() { // not used } @Override - public boolean canEllide() { return false;} + public boolean canElide() { return false;} } \ No newline at end of file diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/xml/IXMLWriter.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/xml/IXMLWriter.java index cb0276f9d..826ac97c0 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/xml/IXMLWriter.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/xml/IXMLWriter.java @@ -106,7 +106,7 @@ public interface IXMLWriter { public abstract void externalLink(String ref) throws IOException; // this is only implemented by an implementation that is producing an xhtml representation and handles ellipsing elements - public abstract boolean canEllide(); - public abstract void ellide() throws IOException; - public abstract void attributeEllide(); + public abstract boolean canElide(); + public abstract void elide() throws IOException; + public abstract void attributeElide(); } \ No newline at end of file diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/xml/XMLWriter.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/xml/XMLWriter.java index aa70a26dd..cedf9ed71 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/xml/XMLWriter.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/xml/XMLWriter.java @@ -913,15 +913,15 @@ public class XMLWriter extends OutputStreamWriter implements IXMLWriter { } @Override - public boolean canEllide() { return false; } + public boolean canElide() { return false; } @Override - public void attributeEllide() { + public void attributeElide() { // ignore this } @Override - public void ellide() throws IOException { + public void elide() throws IOException { // ignore this }