use of org.hl7.fhir.utilities.xhtml.XhtmlParser in project org.hl7.fhir.core by hapifhir.
the class DataRenderer method addMarkdown.
// -- 2. Markdown support -------------------------------------------------------
protected void addMarkdown(XhtmlNode x, String text) throws FHIRFormatError, IOException, DefinitionException {
if (text != null) {
// 1. custom FHIR extensions
while (text.contains("[[[")) {
String left = text.substring(0, text.indexOf("[[["));
String link = text.substring(text.indexOf("[[[") + 3, text.indexOf("]]]"));
String right = text.substring(text.indexOf("]]]") + 3);
String url = link;
String[] parts = link.split("\\#");
StructureDefinition p = getContext().getWorker().fetchResource(StructureDefinition.class, parts[0]);
if (p == null)
p = getContext().getWorker().fetchTypeDefinition(parts[0]);
if (p == null)
p = getContext().getWorker().fetchResource(StructureDefinition.class, link);
if (p != null) {
url = p.getUserString("path");
if (url == null)
url = p.getUserString("filename");
} else
throw new DefinitionException("Unable to resolve markdown link " + link);
text = left + "[" + link + "](" + url + ")" + right;
}
// 2. markdown
String s = getContext().getMarkdown().process(Utilities.escapeXml(text), "narrative generator");
XhtmlParser p = new XhtmlParser();
XhtmlNode m;
try {
m = p.parse("<div>" + s + "</div>", "div");
} catch (org.hl7.fhir.exceptions.FHIRFormatError e) {
throw new FHIRFormatError(e.getMessage(), e);
}
x.getChildNodes().addAll(m.getChildNodes());
}
}
use of org.hl7.fhir.utilities.xhtml.XhtmlParser in project org.hl7.fhir.core by hapifhir.
the class LiquidRenderer method render.
@Override
public boolean render(XhtmlNode x, Resource r) throws FHIRFormatError, DefinitionException, IOException, FHIRException, EOperationOutcome {
LiquidEngine engine = new LiquidEngine(context.getWorker(), context.getServices());
XhtmlNode xn;
try {
engine.setIncludeResolver(new LiquidRendererIncludeResolver(context));
LiquidDocument doc = engine.parse(liquidTemplate, "template");
String html = engine.evaluate(doc, r, rcontext);
xn = new XhtmlParser().parseFragment(html);
if (!x.getName().equals("div"))
throw new FHIRException("Error in template: Root element is not 'div'");
} catch (FHIRException | IOException e) {
xn = new XhtmlNode(NodeType.Element, "div");
xn.para().b().style("color: maroon").tx("Exception generating Narrative: " + e.getMessage());
}
x.getChildNodes().addAll(xn.getChildNodes());
return true;
}
use of org.hl7.fhir.utilities.xhtml.XhtmlParser in project org.hl7.fhir.core by hapifhir.
the class XhtmlNode method innerHTML.
public void innerHTML(String html) throws IOException {
if (html != null) {
XhtmlParser p = new XhtmlParser();
XhtmlNode m;
try {
m = p.parse("<div>" + html + "</div>", "div");
} catch (org.hl7.fhir.exceptions.FHIRFormatError e) {
throw new FHIRFormatError(e.getMessage(), e);
}
getChildNodes().addAll(m.getChildNodes());
}
}
use of org.hl7.fhir.utilities.xhtml.XhtmlParser in project kindling by HL7.
the class PageProcessor method checkFormat.
private void checkFormat(String filename, String res, ResourceDefn r) throws FHIRException {
XhtmlNode doc;
try {
doc = new XhtmlParser().parse("<div>" + res + "</div>", null).getFirstElement();
if (doc.getFirstElement() == null || !doc.getFirstElement().getName().equals("div"))
log("file \"" + filename + "\": root element should be 'div'", LogMessageType.Error);
else if (doc.getFirstElement() == null) {
log("file \"" + filename + "\": there is no 'Scope and Usage'", LogMessageType.Error);
} else {
XhtmlNode scope = null;
XhtmlNode context = null;
for (XhtmlNode x : doc.getChildNodes()) {
if (x.getNodeType() == NodeType.Element) {
if (!x.getName().equals("div")) {
log("file \"" + filename + "\": all child elements of the root div should be 'div's too (found '" + x.getName() + "')", LogMessageType.Error);
return;
} else if (x.getChildNodes().isEmpty()) {
log("file \"" + filename + "\": div/div[" + Integer.toString(doc.getChildNodes().indexOf(x)) + "] must have at least an h2", LogMessageType.Error);
return;
} else if (!isFirstChildElementH2(x)) {
log("file \"" + filename + "\": div/div[" + Integer.toString(doc.getChildNodes().indexOf(x)) + "] must start with an h2", LogMessageType.Error);
return;
} else {
XhtmlNode fn = getH2Element(x);
String s = fn.allText();
if (!((s.equals("Scope and Usage")) || (s.equals("Boundaries and Relationships")) || (s.equals("Background and Context")))) {
log("file \"" + filename + "\": div/div[" + Integer.toString(doc.getChildNodes().indexOf(x)) + "]/h2 must be either 'Scope and Usage', 'Boundaries and Relationships', or 'Background and Context'", LogMessageType.Error);
return;
} else {
if (scope == null) {
if (s.equals("Scope and Usage")) {
scope = x;
if (r != null)
r.setRequirements(new XhtmlComposer(XhtmlComposer.HTML).composePlainText(x));
} else {
log("file \"" + filename + "\": 'Scope and Usage' must come first", LogMessageType.Error);
return;
}
if (s.equals("Boundaries and Relationships")) {
if (context != null) {
log("file \"" + filename + "\": 'Boundaries and Relationships' must come first before 'Background and Context'", LogMessageType.Error);
return;
}
}
if (s.equals("Background and Context"))
context = x;
}
}
boolean found = false;
for (XhtmlNode n : x.getChildNodes()) {
if (!found)
found = n == fn;
else {
if ("h1".equals(n.getName()) || "h2".equals(n.getName())) {
log("file \"" + filename + "\": content of a <div> inner section cannot contain h1 or h2 headings", LogMessageType.Error);
return;
}
}
}
}
}
}
}
List<String> allowed = Arrays.asList("div", "h2", "h3", "h4", "h5", "i", "b", "code", "pre", "blockquote", "p", "a", "img", "table", "thead", "tbody", "tr", "th", "td", "ol", "ul", "li", "br", "span", "em", "strong");
iterateAllChildNodes(doc, allowed);
} catch (Exception e) {
throw new FHIRException("Error processing " + filename + ": " + e.getMessage(), e);
}
}
use of org.hl7.fhir.utilities.xhtml.XhtmlParser in project kindling by HL7.
the class SourceParser method loadMappingSpaces.
private void loadMappingSpaces() throws Exception {
FileInputStream is = null;
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
is = new FileInputStream(Utilities.path(srcDir, "mappingSpaces.xml"));
Document doc = builder.parse(is);
Element e = XMLUtil.getFirstChild(doc.getDocumentElement());
while (e != null) {
MappingSpace m = new MappingSpace(XMLUtil.getNamedChild(e, "columnName").getTextContent(), XMLUtil.getNamedChild(e, "title").getTextContent(), XMLUtil.getNamedChild(e, "id").getTextContent(), Integer.parseInt(XMLUtil.getNamedChild(e, "sort").getTextContent()), isTrue(XMLUtil.getNamedChild(e, "publish")), isTrue(XMLUtil.getNamedChild(e, "sparse")), isTrue(XMLUtil.getNamedChild(e, "pattern")), XMLUtil.getNamedChild(e, "link") != null ? XMLUtil.getNamedChild(e, "link").getTextContent() : XMLUtil.getNamedChild(e, "url").getTextContent());
definitions.getMapTypes().put(XMLUtil.getNamedChild(e, "url").getTextContent(), m);
Element p = XMLUtil.getNamedChild(e, "preamble");
if (p != null)
m.setPreamble(new XhtmlParser().parseHtmlNode(p).setName("div"));
e = XMLUtil.getNextSibling(e);
}
} catch (Exception e) {
throw new Exception("Error processing mappingSpaces.xml: " + e.getMessage(), e);
} finally {
IOUtils.closeQuietly(is);
}
}
Aggregations