more XHTML fluent routines

This commit is contained in:
Grahame Grieve 2024-02-25 19:21:14 +11:00
parent e63a2ad16f
commit 13342f09a3
4 changed files with 125 additions and 53 deletions

View File

@ -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("\\.");

View File

@ -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);
}

View File

@ -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);
}
@ -786,7 +781,13 @@ public class XhtmlNode extends XhtmlFluent implements IBaseXhtml {
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);
}
}

View File

@ -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<XhtmlNode>, java.io.Serializable {
private static final long serialVersionUID = 1L;
@ -31,15 +33,13 @@ public class XhtmlNodeList extends XhtmlFluent implements List<XhtmlNode>, 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<XhtmlNode>, 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<XhtmlNode>, java.
protected void addChildren(XhtmlNodeList childNodes) {
this.addAll(childNodes);
}
protected int indexOfNode(XhtmlNode node) {
return indexOf(node);
}
}