From 13342f09a39ba8a383ebcf9704cbc53a47dc6f76 Mon Sep 17 00:00:00 2001 From: Grahame Grieve Date: Sun, 25 Feb 2024 19:21:14 +1100 Subject: [PATCH] more XHTML fluent routines --- .../hl7/fhir/utilities/VersionUtilities.java | 9 +- .../hl7/fhir/utilities/xhtml/XhtmlFluent.java | 12 +- .../hl7/fhir/utilities/xhtml/XhtmlNode.java | 118 ++++++++++++------ .../fhir/utilities/xhtml/XhtmlNodeList.java | 39 ++++-- 4 files changed, 125 insertions(+), 53 deletions(-) diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/VersionUtilities.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/VersionUtilities.java index 30558136f..07f9ee5e1 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/VersionUtilities.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/VersionUtilities.java @@ -284,8 +284,15 @@ public class VersionUtilities { } public static String getMajMin(String version) { - if (version == null) + if (version == null) { return null; + } + if (version.startsWith("http://hl7.org/fhir/")) { + version = version.substring(20); + if (version.contains("/")) { + version = version.substring(0, version.indexOf("/")); + } + } if (Utilities.charCount(version, '.') == 1) { String[] p = version.split("\\."); diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/xhtml/XhtmlFluent.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/xhtml/XhtmlFluent.java index 52411a1d3..d3f278ff7 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/xhtml/XhtmlFluent.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/xhtml/XhtmlFluent.java @@ -11,8 +11,10 @@ import org.hl7.fhir.utilities.Utilities; public abstract class XhtmlFluent { protected abstract XhtmlNode addTag(String string); + protected abstract XhtmlNode addTag(int index, String string); protected abstract XhtmlNode addText(String cnt); protected abstract void addChildren(XhtmlNodeList childNodes); + protected abstract int indexOfNode(XhtmlNode node); public XhtmlNode h1() { return addTag("h1"); @@ -61,6 +63,14 @@ public abstract class XhtmlFluent { return addTag("tr"); } + public XhtmlNode tr(XhtmlNode tr) { + return addTag(indexOfNode(tr)+1, "tr"); + } + + public XhtmlNode th(int index) { + return addTag(index, "th"); + } + public XhtmlNode th() { return addTag("th"); } @@ -182,7 +192,7 @@ public abstract class XhtmlFluent { public XhtmlNode img(String src, String alt) { if (alt == null) { - return addTag("img").attribute("src", src); + return addTag("img").attribute("src", src).attribute("alt", "."); } else { return addTag("img").attribute("src", src).attribute("alt", alt); } diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/xhtml/XhtmlNode.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/xhtml/XhtmlNode.java index 3d704a985..daa53b551 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/xhtml/XhtmlNode.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/xhtml/XhtmlNode.java @@ -203,19 +203,16 @@ public class XhtmlNode extends XhtmlFluent implements IBaseXhtml { } } - public XhtmlNode addTag(String name) - { - + private XhtmlNode makeTag(String name) { if (!(nodeType == NodeType.Element || nodeType == NodeType.Document)) { throw new Error("Wrong node type - node is "+nodeType.toString()+" ('"+getName()+"/"+getContent()+"')"); } - -// if (inPara && name.equals("p")) { -// throw new FHIRException("nested Para"); -// } -// if (inLink && name.equals("a")) { -// throw new FHIRException("Nested Link"); -// } +// if (inPara && name.equals("p")) { +// throw new FHIRException("nested Para"); +//} +//if (inLink && name.equals("a")) { +// throw new FHIRException("Nested Link"); +//} XhtmlNode node = new XhtmlNode(NodeType.Element); node.setName(name); if (getChildNodes().isInPara() || name.equals("p")) { @@ -224,35 +221,26 @@ public class XhtmlNode extends XhtmlFluent implements IBaseXhtml { if (getChildNodes().isInLink() || name.equals("a")) { node.getChildNodes().setInLink(true); } - getChildNodes().add(node); if (Utilities.existsInList(name, "b", "big", "i", "small", "tt", "abbr", "acronym", "cite", "code", "dfn", "em", "kbd", "strong", "samp", "var", "a", "bdo", "br", "img", "map", "object", "q", "script", "span", "sub", "sup", " button", "input", "label", "select", "textarea")) { node.notPretty(); - } + } + return node; + } + + public XhtmlNode addTag(String name) { + XhtmlNode node = makeTag(name); + getChildNodes().add(node); return node; } - - - public XhtmlNode addTag(int index, String name) - { - - if (!(nodeType == NodeType.Element || nodeType == NodeType.Document)) - throw new Error("Wrong node type. is "+nodeType.toString()); - XhtmlNode node = new XhtmlNode(NodeType.Element); - if (getChildNodes().isInPara() || name.equals("p")) { - node.getChildNodes().setInPara(true); - } - if (getChildNodes().isInLink() || name.equals("a")) { - node.getChildNodes().setInLink(true); - } - node.setName(name); + public XhtmlNode addTag(int index, String name) { + XhtmlNode node = makeTag(name); getChildNodes().add(index, node); return node; } - public XhtmlNode addComment(String content) - { + public XhtmlNode addComment(String content) { if (!(nodeType == NodeType.Element || nodeType == NodeType.Document)) throw new Error("Wrong node type"); XhtmlNode node = new XhtmlNode(NodeType.Comment); @@ -261,8 +249,7 @@ public class XhtmlNode extends XhtmlFluent implements IBaseXhtml { return node; } - public XhtmlNode addDocType(String content) - { + public XhtmlNode addDocType(String content) { if (!(nodeType == NodeType.Document)) throw new Error("Wrong node type"); XhtmlNode node = new XhtmlNode(NodeType.DocType); @@ -271,8 +258,7 @@ public class XhtmlNode extends XhtmlFluent implements IBaseXhtml { return node; } - public XhtmlNode addInstruction(String content) - { + public XhtmlNode addInstruction(String content) { if (!(nodeType == NodeType.Document)) throw new Error("Wrong node type"); XhtmlNode node = new XhtmlNode(NodeType.Instruction); @@ -280,8 +266,8 @@ public class XhtmlNode extends XhtmlFluent implements IBaseXhtml { getChildNodes().add(node); return node; } - public XhtmlNode addText(String content) - { + + public XhtmlNode addText(String content) { if (!(nodeType == NodeType.Element || nodeType == NodeType.Document)) throw new Error("Wrong node type"); if (content != null) { @@ -293,8 +279,7 @@ public class XhtmlNode extends XhtmlFluent implements IBaseXhtml { return null; } - public XhtmlNode addText(int index, String content) - { + public XhtmlNode addText(int index, String content) { if (!(nodeType == NodeType.Element || nodeType == NodeType.Document)) throw new Error("Wrong node type"); if (content == null) @@ -306,8 +291,7 @@ public class XhtmlNode extends XhtmlFluent implements IBaseXhtml { return node; } - public boolean allChildrenAreText() - { + public boolean allChildrenAreText() { boolean res = true; if (hasChildren()) { for (XhtmlNode n : childNodes) @@ -376,6 +360,17 @@ public class XhtmlNode extends XhtmlFluent implements IBaseXhtml { return this; } + public XhtmlNode attributeNN(String name, String value) { + if (!(nodeType == NodeType.Element || nodeType == NodeType.Document)) + throw new Error("Wrong node type"); + if (name == null) + throw new Error("name is null"); + if (value != null) { + getAttributes().put(name, value); + } + return this; + } + public boolean hasAttribute(String name) { return hasAttributes() && getAttributes().containsKey(name); } @@ -781,12 +776,18 @@ public class XhtmlNode extends XhtmlFluent implements IBaseXhtml { public XhtmlNode colspan(String n) { return setAttribute("colspan", n); } - + public XhtmlNode colspan(int n) { return setAttribute("colspan", Integer.toString(n)); } - + public XhtmlNode rowspan(int n) { + if (n > 1) { + return setAttribute("rowspan", Integer.toString(n)); + } else { + return this; + } + } @Override protected void addChildren(XhtmlNodeList childNodes) { @@ -928,6 +929,15 @@ public class XhtmlNode extends XhtmlFluent implements IBaseXhtml { } + public XhtmlNode td(int index) { + XhtmlNode x = addTag(index, "td"); + XhtmlNode t = (XhtmlNode) getUserData("cells"); + if (t != null) { + x.copyAllContent(t); + } + return x; + } + public XhtmlNode td() { XhtmlNode x = addTag("td"); XhtmlNode t = (XhtmlNode) getUserData("cells"); @@ -937,6 +947,27 @@ public class XhtmlNode extends XhtmlFluent implements IBaseXhtml { return x; } + public XhtmlNode td(int index, String width) { + XhtmlNode x = addTag(index, "td"); + x.attribute("width", width); + XhtmlNode t = (XhtmlNode) getUserData("cells"); + if (t != null) { + x.copyAllContent(t); + } + return x; + } + + public XhtmlNode tdW(int width) { + XhtmlNode x = addTag("td"); + x.attribute("width", Integer.toString(width)); + XhtmlNode t = (XhtmlNode) getUserData("cells"); + if (t != null) { + x.copyAllContent(t); + } + return x; + } + + // differs from tx because it returns the owner node, not the created text public XhtmlNode txN(String cnt) { @@ -999,4 +1030,9 @@ public class XhtmlNode extends XhtmlFluent implements IBaseXhtml { } + protected int indexOfNode(XhtmlNode node) { + return getChildNodes().indexOf(node); + } + + } \ No newline at end of file diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/xhtml/XhtmlNodeList.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/xhtml/XhtmlNodeList.java index 30ba2f1c3..fbf802f9e 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/xhtml/XhtmlNodeList.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/xhtml/XhtmlNodeList.java @@ -6,6 +6,8 @@ import java.util.Iterator; import java.util.List; import java.util.ListIterator; +import org.hl7.fhir.utilities.Utilities; + public class XhtmlNodeList extends XhtmlFluent implements List, java.io.Serializable { private static final long serialVersionUID = 1L; @@ -31,15 +33,13 @@ public class XhtmlNodeList extends XhtmlFluent implements List, java. } - public XhtmlNode addTag(String name) - { - -// if (inPara && name.equals("p")) { -// throw new FHIRException("nested Para"); -// } -// if (inLink && name.equals("a")) { -// throw new FHIRException("Nested Link"); -// } + private XhtmlNode makeTag(String name) { +// if (inPara && name.equals("p")) { +// throw new FHIRException("nested Para"); +//} +//if (inLink && name.equals("a")) { +// throw new FHIRException("Nested Link"); +//} XhtmlNode node = new XhtmlNode(NodeType.Element); node.setName(name); if (isInPara() || name.equals("p")) { @@ -48,10 +48,24 @@ public class XhtmlNodeList extends XhtmlFluent implements List, java. if (isInLink() || name.equals("a")) { node.getChildNodes().setInLink(true); } + if (Utilities.existsInList(name, "b", "big", "i", "small", "tt", "abbr", "acronym", "cite", "code", "dfn", "em", "kbd", "strong", "samp", "var", "a", "bdo", "br", "img", "map", "object", "q", "script", "span", "sub", "sup", " button", "input", "label", "select", "textarea")) { + node.notPretty(); + } + return node; + } + + public XhtmlNode addTag(String name) { + XhtmlNode node = makeTag(name); add(node); return node; } - + + public XhtmlNode addTag(int index, String name) { + XhtmlNode node = makeTag(name); + add(index, node); + return node; + } + public XhtmlNode addText(String content) { if (content != null) { XhtmlNode node = new XhtmlNode(NodeType.Text); @@ -183,4 +197,9 @@ public class XhtmlNodeList extends XhtmlFluent implements List, java. protected void addChildren(XhtmlNodeList childNodes) { this.addAll(childNodes); } + + protected int indexOfNode(XhtmlNode node) { + return indexOf(node); + } + } \ No newline at end of file