Upgrade to Thymeleaf 3
This commit is contained in:
parent
82da79cd5f
commit
c4d302df8d
|
@ -10,7 +10,7 @@ package ca.uhn.fhir.narrative;
|
|||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
|
@ -21,57 +21,138 @@ package ca.uhn.fhir.narrative;
|
|||
*/
|
||||
import static org.apache.commons.lang3.StringUtils.isBlank;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.Reader;
|
||||
import java.io.StringReader;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.apache.commons.io.input.ReaderInputStream;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.hl7.fhir.instance.model.api.IBaseDatatype;
|
||||
import org.hl7.fhir.instance.model.api.IBaseResource;
|
||||
import org.hl7.fhir.instance.model.api.INarrative;
|
||||
import org.thymeleaf.Arguments;
|
||||
import org.thymeleaf.Configuration;
|
||||
import org.thymeleaf.IEngineConfiguration;
|
||||
import org.thymeleaf.TemplateEngine;
|
||||
import org.thymeleaf.TemplateProcessingParameters;
|
||||
import org.thymeleaf.cache.AlwaysValidCacheEntryValidity;
|
||||
import org.thymeleaf.cache.ICacheEntryValidity;
|
||||
import org.thymeleaf.context.Context;
|
||||
import org.thymeleaf.dom.Document;
|
||||
import org.thymeleaf.dom.Element;
|
||||
import org.thymeleaf.dom.Node;
|
||||
import org.thymeleaf.exceptions.TemplateInputException;
|
||||
import org.thymeleaf.messageresolver.StandardMessageResolver;
|
||||
import org.thymeleaf.context.ITemplateContext;
|
||||
import org.thymeleaf.engine.AttributeName;
|
||||
import org.thymeleaf.model.IModel;
|
||||
import org.thymeleaf.model.IProcessableElementTag;
|
||||
import org.thymeleaf.processor.IProcessor;
|
||||
import org.thymeleaf.processor.ProcessorResult;
|
||||
import org.thymeleaf.processor.attr.AbstractAttrProcessor;
|
||||
import org.thymeleaf.resourceresolver.IResourceResolver;
|
||||
import org.thymeleaf.processor.element.AbstractAttributeTagProcessor;
|
||||
import org.thymeleaf.processor.element.IElementTagStructureHandler;
|
||||
import org.thymeleaf.standard.StandardDialect;
|
||||
import org.thymeleaf.standard.expression.IStandardExpression;
|
||||
import org.thymeleaf.standard.expression.IStandardExpressionParser;
|
||||
import org.thymeleaf.standard.expression.StandardExpressions;
|
||||
import org.thymeleaf.templatemode.StandardTemplateModeHandlers;
|
||||
import org.thymeleaf.templateparser.xmlsax.XhtmlAndHtml5NonValidatingSAXTemplateParser;
|
||||
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;
|
||||
import org.thymeleaf.templateresolver.TemplateResolver;
|
||||
import org.thymeleaf.standard.processor.AbstractStandardExpressionAttributeTagProcessor;
|
||||
import org.thymeleaf.templatemode.TemplateMode;
|
||||
import org.thymeleaf.templateresolver.DefaultTemplateResolver;
|
||||
import org.thymeleaf.templateresource.ITemplateResource;
|
||||
import org.thymeleaf.templateresource.StringTemplateResource;
|
||||
|
||||
import ca.uhn.fhir.context.ConfigurationException;
|
||||
import ca.uhn.fhir.context.FhirContext;
|
||||
import ca.uhn.fhir.model.api.IDatatype;
|
||||
import ca.uhn.fhir.model.api.annotation.ResourceDef;
|
||||
import ca.uhn.fhir.narrative.BaseThymeleafNarrativeGenerator.NarrativeAttributeProcessor;
|
||||
import ca.uhn.fhir.parser.DataFormatException;
|
||||
import ca.uhn.fhir.rest.server.Constants;
|
||||
|
||||
public abstract class BaseThymeleafNarrativeGenerator implements INarrativeGenerator {
|
||||
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(BaseThymeleafNarrativeGenerator.class);
|
||||
private static final XhtmlAndHtml5NonValidatingSAXTemplateParser PARSER = new XhtmlAndHtml5NonValidatingSAXTemplateParser(1);
|
||||
|
||||
private Configuration myThymeleafConfig;
|
||||
public class NarrativeAttributeProcessor extends AbstractAttributeTagProcessor {
|
||||
|
||||
protected NarrativeAttributeProcessor(FhirContext theContext, String theDialectPrefix) {
|
||||
super(TemplateMode.XML, theDialectPrefix, null, false, "narrative", true, 0, true);
|
||||
myContext = theContext;
|
||||
}
|
||||
|
||||
private FhirContext myContext;
|
||||
|
||||
@Override
|
||||
protected void doProcess(ITemplateContext theContext, IProcessableElementTag theTag, AttributeName theAttributeName, String theAttributeValue, IElementTagStructureHandler theStructureHandler) {
|
||||
IEngineConfiguration configuration = theContext.getConfiguration();
|
||||
IStandardExpressionParser expressionParser = StandardExpressions.getExpressionParser(configuration);
|
||||
|
||||
final IStandardExpression expression = expressionParser.parseExpression(theContext, theAttributeValue);
|
||||
final Object value = expression.execute(theContext);
|
||||
|
||||
if (value == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
Context context = new Context();
|
||||
context.setVariable("fhirVersion", myContext.getVersion().getVersion().name());
|
||||
context.setVariable("resource", value);
|
||||
|
||||
String name = null;
|
||||
if (value != null) {
|
||||
Class<? extends Object> nextClass = value.getClass();
|
||||
do {
|
||||
name = myClassToName.get(nextClass);
|
||||
nextClass = nextClass.getSuperclass();
|
||||
} while (name == null && nextClass.equals(Object.class) == false);
|
||||
|
||||
if (name == null) {
|
||||
if (value instanceof IBaseResource) {
|
||||
name = myContext.getResourceDefinition((Class<? extends IBaseResource>) value).getName();
|
||||
} else if (value instanceof IDatatype) {
|
||||
name = value.getClass().getSimpleName();
|
||||
name = name.substring(0, name.length() - 2);
|
||||
} else if (value instanceof IBaseDatatype) {
|
||||
name = value.getClass().getSimpleName();
|
||||
if (name.endsWith("Type")) {
|
||||
name = name.substring(0, name.length() - 4);
|
||||
}
|
||||
} else {
|
||||
throw new DataFormatException("Don't know how to determine name for type: " + value.getClass());
|
||||
}
|
||||
name = name.toLowerCase();
|
||||
if (!myNameToNarrativeTemplate.containsKey(name)) {
|
||||
name = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (name == null) {
|
||||
if (myIgnoreMissingTemplates) {
|
||||
ourLog.debug("No narrative template available for type: {}", value.getClass());
|
||||
return;
|
||||
} else {
|
||||
throw new DataFormatException("No narrative template for class " + value.getClass());
|
||||
}
|
||||
}
|
||||
|
||||
String result = myProfileTemplateEngine.process(name, context);
|
||||
String trim = result.trim();
|
||||
|
||||
theStructureHandler.setBody(trim, true);
|
||||
|
||||
// if (!isBlank(trim + "AAA")) {
|
||||
// Document dom = getXhtmlDOMFor(new StringReader(trim));
|
||||
//
|
||||
// Element firstChild = (Element) dom.getFirstChild();
|
||||
// for (int i = 0; i < firstChild.getChildren().size(); i++) {
|
||||
// Node next = firstChild.getChildren().get(i);
|
||||
// if (i == 0 && firstChild.getChildren().size() == 1) {
|
||||
// if (next instanceof org.thymeleaf.dom.Text) {
|
||||
// org.thymeleaf.dom.Text nextText = (org.thymeleaf.dom.Text) next;
|
||||
// nextText.setContent(nextText.getContent().trim());
|
||||
// }
|
||||
// }
|
||||
// theElement.addChild(next);
|
||||
// }
|
||||
//
|
||||
// }
|
||||
//
|
||||
// return ProcessorResult.ok();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(BaseThymeleafNarrativeGenerator.class);
|
||||
// private static final XhtmlAndHtml5NonValidatingSAXTemplateParser PARSER = new XhtmlAndHtml5NonValidatingSAXTemplateParser(1);
|
||||
|
||||
private boolean myApplyDefaultDatatypeTemplates = true;
|
||||
private HashMap<Class<?>, String> myClassToName;
|
||||
private boolean myCleanWhitespace = true;
|
||||
|
@ -82,15 +163,13 @@ public abstract class BaseThymeleafNarrativeGenerator implements INarrativeGener
|
|||
private TemplateEngine myProfileTemplateEngine;
|
||||
|
||||
public BaseThymeleafNarrativeGenerator() {
|
||||
myThymeleafConfig = new Configuration();
|
||||
myThymeleafConfig.addTemplateResolver(new ClassLoaderTemplateResolver());
|
||||
myThymeleafConfig.addMessageResolver(new StandardMessageResolver());
|
||||
myThymeleafConfig.setTemplateModeHandlers(StandardTemplateModeHandlers.ALL_TEMPLATE_MODE_HANDLERS);
|
||||
myThymeleafConfig.initialize();
|
||||
// myThymeleafConfig = new Configuration();
|
||||
// myThymeleafConfig.addTemplateResolver(new ClassLoaderTemplateResolver());
|
||||
// myThymeleafConfig.addMessageResolver(new StandardMessageResolver());
|
||||
// myThymeleafConfig.setTemplateModeHandlers(StandardTemplateModeHandlers.ALL_TEMPLATE_MODE_HANDLERS);
|
||||
// myThymeleafConfig.initialize();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public void generateNarrative(FhirContext theContext, IBaseResource theResource, INarrative theNarrative) {
|
||||
if (!myInitialized) {
|
||||
|
@ -132,7 +211,7 @@ public abstract class BaseThymeleafNarrativeGenerator implements INarrativeGener
|
|||
result = cleanWhitespace(result);
|
||||
ourLog.trace("Post-whitespace cleaning: ", result);
|
||||
}
|
||||
|
||||
|
||||
if (isBlank(result)) {
|
||||
return;
|
||||
}
|
||||
|
@ -156,20 +235,18 @@ public abstract class BaseThymeleafNarrativeGenerator implements INarrativeGener
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
protected abstract List<String> getPropertyFile();
|
||||
|
||||
private Document getXhtmlDOMFor(final Reader source) {
|
||||
final Configuration configuration1 = myThymeleafConfig;
|
||||
try {
|
||||
return PARSER.parseTemplate(configuration1, "input", source);
|
||||
} catch (final Exception e) {
|
||||
throw new TemplateInputException("Exception during parsing of source", e);
|
||||
}
|
||||
}
|
||||
// private Document getXhtmlDOMFor(final Reader source) {
|
||||
// final Configuration configuration1 = myThymeleafConfig;
|
||||
// try {
|
||||
// return PARSER.parseTemplate(configuration1, "input", source);
|
||||
// } catch (final Exception e) {
|
||||
// throw new TemplateInputException("Exception during parsing of source", e);
|
||||
// }
|
||||
// }
|
||||
|
||||
private synchronized void initialize(FhirContext theContext) {
|
||||
private synchronized void initialize(final FhirContext theContext) {
|
||||
if (myInitialized) {
|
||||
return;
|
||||
}
|
||||
|
@ -195,15 +272,18 @@ public abstract class BaseThymeleafNarrativeGenerator implements INarrativeGener
|
|||
|
||||
{
|
||||
myProfileTemplateEngine = new TemplateEngine();
|
||||
TemplateResolver resolver = new TemplateResolver();
|
||||
resolver.setResourceResolver(new ProfileResourceResolver());
|
||||
ProfileResourceResolver resolver = new ProfileResourceResolver();
|
||||
myProfileTemplateEngine.setTemplateResolver(resolver);
|
||||
StandardDialect dialect = new StandardDialect();
|
||||
HashSet<IProcessor> additionalProcessors = new HashSet<IProcessor>();
|
||||
additionalProcessors.add(new NarrativeAttributeProcessor(theContext));
|
||||
dialect.setAdditionalProcessors(additionalProcessors);
|
||||
StandardDialect dialect = new StandardDialect() {
|
||||
@Override
|
||||
public Set<IProcessor> getProcessors(String theDialectPrefix) {
|
||||
Set<IProcessor> retVal = super.getProcessors(theDialectPrefix);
|
||||
retVal.add(new NarrativeAttributeProcessor(theContext, theDialectPrefix));
|
||||
return retVal;
|
||||
}
|
||||
|
||||
};
|
||||
myProfileTemplateEngine.setDialect(dialect);
|
||||
myProfileTemplateEngine.initialize();
|
||||
}
|
||||
|
||||
myInitialized = true;
|
||||
|
@ -214,7 +294,7 @@ public abstract class BaseThymeleafNarrativeGenerator implements INarrativeGener
|
|||
* before it is returned.
|
||||
* <p>
|
||||
* Note that in order to preserve formatting, not all whitespace is trimmed. Repeated whitespace characters (e.g.
|
||||
* "\n \n ") will be trimmed to a single space.
|
||||
* "\n \n ") will be trimmed to a single space.
|
||||
* </p>
|
||||
*/
|
||||
public boolean isCleanWhitespace() {
|
||||
|
@ -259,7 +339,7 @@ public abstract class BaseThymeleafNarrativeGenerator implements INarrativeGener
|
|||
}
|
||||
|
||||
if (StringUtils.isNotBlank(narrativeName)) {
|
||||
String narrative = IOUtils.toString(loadResource(narrativeName));
|
||||
String narrative = IOUtils.toString(loadResource(narrativeName), Constants.CHARSET_UTF8);
|
||||
myNameToNarrativeTemplate.put(name, narrative);
|
||||
}
|
||||
|
||||
|
@ -292,7 +372,7 @@ public abstract class BaseThymeleafNarrativeGenerator implements INarrativeGener
|
|||
String narrativePropName = name + ".narrative";
|
||||
String narrativeName = file.getProperty(narrativePropName);
|
||||
if (StringUtils.isNotBlank(narrativeName)) {
|
||||
String narrative = IOUtils.toString(loadResource(narrativeName));
|
||||
String narrative = IOUtils.toString(loadResource(narrativeName), Constants.CHARSET_UTF8);
|
||||
myNameToNarrativeTemplate.put(name, narrative);
|
||||
}
|
||||
continue;
|
||||
|
@ -332,7 +412,7 @@ public abstract class BaseThymeleafNarrativeGenerator implements INarrativeGener
|
|||
* before it is returned.
|
||||
* <p>
|
||||
* Note that in order to preserve formatting, not all whitespace is trimmed. Repeated whitespace characters (e.g.
|
||||
* "\n \n ") will be trimmed to a single space.
|
||||
* "\n \n ") will be trimmed to a single space.
|
||||
* </p>
|
||||
*/
|
||||
public void setCleanWhitespace(boolean theCleanWhitespace) {
|
||||
|
@ -398,7 +478,7 @@ public abstract class BaseThymeleafNarrativeGenerator implements INarrativeGener
|
|||
char char4 = Character.toLowerCase((i + 4 < theResult.length()) ? theResult.charAt(i + 4) : ' ');
|
||||
if (char1 == 'p' && char2 == 'r' && char3 == 'e') {
|
||||
inPre = true;
|
||||
} else if (char1 == '/' && char2 == 'p' && char3 == 'r'&&char4=='e') {
|
||||
} else if (char1 == '/' && char2 == 'p' && char3 == 'r' && char4 == 'e') {
|
||||
inPre = false;
|
||||
}
|
||||
}
|
||||
|
@ -416,105 +496,105 @@ public abstract class BaseThymeleafNarrativeGenerator implements INarrativeGener
|
|||
}
|
||||
return b.toString();
|
||||
}
|
||||
|
||||
public class NarrativeAttributeProcessor extends AbstractAttrProcessor {
|
||||
|
||||
private FhirContext myContext;
|
||||
|
||||
protected NarrativeAttributeProcessor(FhirContext theContext) {
|
||||
super("narrative");
|
||||
myContext = theContext;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getPrecedence() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
protected ProcessorResult processAttribute(Arguments theArguments, Element theElement, String theAttributeName) {
|
||||
final String attributeValue = theElement.getAttributeValue(theAttributeName);
|
||||
|
||||
final Configuration configuration = theArguments.getConfiguration();
|
||||
final IStandardExpressionParser expressionParser = StandardExpressions.getExpressionParser(configuration);
|
||||
|
||||
final IStandardExpression expression = expressionParser.parseExpression(configuration, theArguments, attributeValue);
|
||||
final Object value = expression.execute(configuration, theArguments);
|
||||
|
||||
theElement.removeAttribute(theAttributeName);
|
||||
theElement.clearChildren();
|
||||
|
||||
if (value == null) {
|
||||
return ProcessorResult.ok();
|
||||
}
|
||||
|
||||
Context context = new Context();
|
||||
context.setVariable("fhirVersion", myContext.getVersion().getVersion().name());
|
||||
context.setVariable("resource", value);
|
||||
|
||||
String name = null;
|
||||
if (value != null) {
|
||||
Class<? extends Object> nextClass = value.getClass();
|
||||
do {
|
||||
name = myClassToName.get(nextClass);
|
||||
nextClass = nextClass.getSuperclass();
|
||||
} while (name == null && nextClass.equals(Object.class) == false);
|
||||
|
||||
if (name == null) {
|
||||
if (value instanceof IBaseResource) {
|
||||
name = myContext.getResourceDefinition((Class<? extends IBaseResource>) value).getName();
|
||||
} else if (value instanceof IDatatype) {
|
||||
name = value.getClass().getSimpleName();
|
||||
name = name.substring(0, name.length() - 2);
|
||||
} else if (value instanceof IBaseDatatype) {
|
||||
name = value.getClass().getSimpleName();
|
||||
if (name.endsWith("Type")) {
|
||||
name = name.substring(0, name.length() - 4);
|
||||
}
|
||||
} else {
|
||||
throw new DataFormatException("Don't know how to determine name for type: " + value.getClass());
|
||||
}
|
||||
name = name.toLowerCase();
|
||||
if (!myNameToNarrativeTemplate.containsKey(name)) {
|
||||
name = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (name == null) {
|
||||
if (myIgnoreMissingTemplates) {
|
||||
ourLog.debug("No narrative template available for type: {}", value.getClass());
|
||||
return ProcessorResult.ok();
|
||||
} else {
|
||||
throw new DataFormatException("No narrative template for class " + value.getClass());
|
||||
}
|
||||
}
|
||||
|
||||
String result = myProfileTemplateEngine.process(name, context);
|
||||
String trim = result.trim();
|
||||
if (!isBlank(trim + "AAA")) {
|
||||
Document dom = getXhtmlDOMFor(new StringReader(trim));
|
||||
|
||||
Element firstChild = (Element) dom.getFirstChild();
|
||||
for (int i = 0; i < firstChild.getChildren().size(); i++) {
|
||||
Node next = firstChild.getChildren().get(i);
|
||||
if (i == 0 && firstChild.getChildren().size() == 1) {
|
||||
if (next instanceof org.thymeleaf.dom.Text) {
|
||||
org.thymeleaf.dom.Text nextText = (org.thymeleaf.dom.Text) next;
|
||||
nextText.setContent(nextText.getContent().trim());
|
||||
}
|
||||
}
|
||||
theElement.addChild(next);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
return ProcessorResult.ok();
|
||||
}
|
||||
|
||||
}
|
||||
// public class NarrativeAttributeProcessor extends AbstractAttributeTagProcessor {
|
||||
//
|
||||
// private FhirContext myContext;
|
||||
//
|
||||
// protected NarrativeAttributeProcessor(FhirContext theContext) {
|
||||
// super()
|
||||
// myContext = theContext;
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public int getPrecedence() {
|
||||
// return 0;
|
||||
// }
|
||||
//
|
||||
// @SuppressWarnings("unchecked")
|
||||
// @Override
|
||||
// protected ProcessorResult processAttribute(Arguments theArguments, Element theElement, String theAttributeName) {
|
||||
// final String attributeValue = theElement.getAttributeValue(theAttributeName);
|
||||
//
|
||||
// final Configuration configuration = theArguments.getConfiguration();
|
||||
// final IStandardExpressionParser expressionParser = StandardExpressions.getExpressionParser(configuration);
|
||||
//
|
||||
// final IStandardExpression expression = expressionParser.parseExpression(configuration, theArguments, attributeValue);
|
||||
// final Object value = expression.execute(configuration, theArguments);
|
||||
//
|
||||
// theElement.removeAttribute(theAttributeName);
|
||||
// theElement.clearChildren();
|
||||
//
|
||||
// if (value == null) {
|
||||
// return ProcessorResult.ok();
|
||||
// }
|
||||
//
|
||||
// Context context = new Context();
|
||||
// context.setVariable("fhirVersion", myContext.getVersion().getVersion().name());
|
||||
// context.setVariable("resource", value);
|
||||
//
|
||||
// String name = null;
|
||||
// if (value != null) {
|
||||
// Class<? extends Object> nextClass = value.getClass();
|
||||
// do {
|
||||
// name = myClassToName.get(nextClass);
|
||||
// nextClass = nextClass.getSuperclass();
|
||||
// } while (name == null && nextClass.equals(Object.class) == false);
|
||||
//
|
||||
// if (name == null) {
|
||||
// if (value instanceof IBaseResource) {
|
||||
// name = myContext.getResourceDefinition((Class<? extends IBaseResource>) value).getName();
|
||||
// } else if (value instanceof IDatatype) {
|
||||
// name = value.getClass().getSimpleName();
|
||||
// name = name.substring(0, name.length() - 2);
|
||||
// } else if (value instanceof IBaseDatatype) {
|
||||
// name = value.getClass().getSimpleName();
|
||||
// if (name.endsWith("Type")) {
|
||||
// name = name.substring(0, name.length() - 4);
|
||||
// }
|
||||
// } else {
|
||||
// throw new DataFormatException("Don't know how to determine name for type: " + value.getClass());
|
||||
// }
|
||||
// name = name.toLowerCase();
|
||||
// if (!myNameToNarrativeTemplate.containsKey(name)) {
|
||||
// name = null;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// if (name == null) {
|
||||
// if (myIgnoreMissingTemplates) {
|
||||
// ourLog.debug("No narrative template available for type: {}", value.getClass());
|
||||
// return ProcessorResult.ok();
|
||||
// } else {
|
||||
// throw new DataFormatException("No narrative template for class " + value.getClass());
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// String result = myProfileTemplateEngine.process(name, context);
|
||||
// String trim = result.trim();
|
||||
// if (!isBlank(trim + "AAA")) {
|
||||
// Document dom = getXhtmlDOMFor(new StringReader(trim));
|
||||
//
|
||||
// Element firstChild = (Element) dom.getFirstChild();
|
||||
// for (int i = 0; i < firstChild.getChildren().size(); i++) {
|
||||
// Node next = firstChild.getChildren().get(i);
|
||||
// if (i == 0 && firstChild.getChildren().size() == 1) {
|
||||
// if (next instanceof org.thymeleaf.dom.Text) {
|
||||
// org.thymeleaf.dom.Text nextText = (org.thymeleaf.dom.Text) next;
|
||||
// nextText.setContent(nextText.getContent().trim());
|
||||
// }
|
||||
// }
|
||||
// theElement.addChild(next);
|
||||
// }
|
||||
//
|
||||
// }
|
||||
//
|
||||
//
|
||||
// return ProcessorResult.ok();
|
||||
// }
|
||||
//
|
||||
// }
|
||||
|
||||
// public String generateString(Patient theValue) {
|
||||
//
|
||||
|
@ -529,21 +609,30 @@ public abstract class BaseThymeleafNarrativeGenerator implements INarrativeGener
|
|||
// return result;
|
||||
// }
|
||||
|
||||
private final class ProfileResourceResolver implements IResourceResolver {
|
||||
private final class ProfileResourceResolver extends DefaultTemplateResolver {
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return getClass().getCanonicalName();
|
||||
protected boolean computeResolvable(IEngineConfiguration theConfiguration, String theOwnerTemplate, String theTemplate, Map<String, Object> theTemplateResolutionAttributes) {
|
||||
String template = myNameToNarrativeTemplate.get(theTemplate);
|
||||
return template != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream getResourceAsStream(TemplateProcessingParameters theTemplateProcessingParameters, String theName) {
|
||||
String template = myNameToNarrativeTemplate.get(theName);
|
||||
if (template == null) {
|
||||
ourLog.info("No narative template for resource profile: {}", theName);
|
||||
return new ReaderInputStream(new StringReader(""));
|
||||
}
|
||||
return new ReaderInputStream(new StringReader(template));
|
||||
protected ITemplateResource computeTemplateResource(IEngineConfiguration theConfiguration, String theOwnerTemplate, String theTemplate, Map<String, Object> theTemplateResolutionAttributes) {
|
||||
String template = myNameToNarrativeTemplate.get(theTemplate);
|
||||
return new StringTemplateResource(template);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected TemplateMode computeTemplateMode(IEngineConfiguration theConfiguration, String theOwnerTemplate, String theTemplate, Map<String, Object> theTemplateResolutionAttributes) {
|
||||
return TemplateMode.XML;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ICacheEntryValidity computeValidity(IEngineConfiguration theConfiguration, String theOwnerTemplate, String theTemplate, Map<String, Object> theTemplateResolutionAttributes) {
|
||||
return AlwaysValidCacheEntryValidity.INSTANCE;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,8 +1,6 @@
|
|||
<div>
|
||||
<th:block th:each="line : ${resource.line}">
|
||||
<span th:text="${line} + ' '">123 Fake Street</span><br/>
|
||||
</th:block>
|
||||
<span th:if="${not resource.cityElement.empty}" th:text="${resource.city} + ' '">Toronto</span>
|
||||
<span th:if="${not resource.stateElement.empty}" th:text="${resource.state} + ' '">ON</span>
|
||||
<span th:if="${not resource.countryElement.empty}" th:text="${resource.country}+ ' '">Canada</span>
|
||||
</div>
|
||||
<th:block th:each="line : ${resource.line}">
|
||||
<span th:text="${line} + ' '">123 Fake Street</span><br/>
|
||||
</th:block>
|
||||
<span th:if="${not resource.cityElement.empty}" th:text="${resource.city} + ' '">Toronto</span>
|
||||
<span th:if="${not resource.stateElement.empty}" th:text="${resource.state} + ' '">ON</span>
|
||||
<span th:if="${not resource.countryElement.empty}" th:text="${resource.country}+ ' '">Canada</span>
|
||||
|
|
|
@ -1,3 +1 @@
|
|||
<div>
|
||||
<th:block th:if="${not resource.empty}" th:text="${resource.value}"/>
|
||||
</div>
|
||||
<th:block th:if="${not resource.empty}" th:text="${resource.value}"/>
|
||||
|
|
|
@ -1,13 +1,11 @@
|
|||
<div>
|
||||
<th:block th:if="${not resource.textElement.empty}" th:text="${resource.textElement.value}"/>
|
||||
<th:block th:if="${resource.textElement.empty}">
|
||||
<th:block th:if="${!resource.coding.empty}">
|
||||
<th:block th:if="${!resource.coding[0].displayElement.empty}" th:text="${!resource.coding[0].displayElement.value}"/>
|
||||
<th:block th:if="${resource.coding[0].displayElement.empty}">
|
||||
<th:block th:if="${!resource.coding[0].codeElement.empty}">
|
||||
<th:block th:text="${resource.coding[0].codeElement.value}"/>
|
||||
</th:block>
|
||||
<th:block th:if="${not resource.textElement.empty}" th:text="${resource.textElement.value}"/>
|
||||
<th:block th:if="${resource.textElement.empty}">
|
||||
<th:block th:if="${!resource.coding.empty}">
|
||||
<th:block th:if="${!resource.coding[0].displayElement.empty}" th:text="${!resource.coding[0].displayElement.value}"/>
|
||||
<th:block th:if="${resource.coding[0].displayElement.empty}">
|
||||
<th:block th:if="${!resource.coding[0].codeElement.empty}">
|
||||
<th:block th:text="${resource.coding[0].codeElement.value}"/>
|
||||
</th:block>
|
||||
</th:block>
|
||||
</th:block>
|
||||
</div>
|
||||
</th:block>
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
<div th:switch="${resource.precision.name()}">
|
||||
<th:block th:switch="${resource.precision.name()}">
|
||||
<th:block th:case="YEAR" th:text="${#dates.format(resource.value,'yyyy')}">22 March 2012</th:block>
|
||||
<th:block th:case="MONTH" th:text="${#dates.format(resource.value,'MMMM yyyy')}"></th:block>
|
||||
<th:block th:case="DAY" th:text="${#dates.format(resource.value,'dd MMMM yyyy')}"></th:block>
|
||||
<th:block th:case="SECOND" th:text="${#dates.format(resource.value,'dd MMMM yyyy HH:mm:ss')}"></th:block>
|
||||
<th:block th:case="MILLI" th:text="${#dates.format(resource.value,'dd MMMM yyyy HH:mm:ss')}"></th:block>
|
||||
</div>
|
||||
</th:block>
|
||||
|
|
|
@ -1,6 +1,4 @@
|
|||
<div>
|
||||
<th:block th:each="prefix : ${resource.prefix}" th:if="${!prefix.empty}" th:text="${prefix.value} + ' '">Dr</th:block>
|
||||
<th:block th:each="givenName : ${resource.given}" th:if="${!givenName.empty}" th:text="${givenName.value} + ' '">John</th:block>
|
||||
<b th:each="familyName : ${resource.family}" th:if="${!familyName.empty}" th:text="${#strings.toUpperCase(familyName.value)} + ' '">SMITH</b>
|
||||
<th:block th:each="suffix : ${resource.suffix}" th:if="${!suffix.empty}" th:text="${suffix.value} + ' '">Jr</th:block>
|
||||
</div>
|
||||
<th:block th:each="prefix : ${resource.prefix}" th:if="${!prefix.empty}" th:text="${prefix.value} + ' '">Dr</th:block>
|
||||
<th:block th:each="givenName : ${resource.given}" th:if="${!givenName.empty}" th:text="${givenName.value} + ' '">John</th:block>
|
||||
<b th:each="familyName : ${resource.family}" th:if="${!familyName.empty}" th:text="${#strings.toUpperCase(familyName.value)} + ' '">SMITH</b>
|
||||
<th:block th:each="suffix : ${resource.suffix}" th:if="${!suffix.empty}" th:text="${suffix.value} + ' '">Jr</th:block>
|
||||
|
|
|
@ -1,3 +1 @@
|
|||
<div>
|
||||
<th:block th:text="${resource.valueElement.valueAsString}"/>
|
||||
</div>
|
||||
<th:block th:text="${resource.valueElement.valueAsString}"/>
|
||||
|
|
|
@ -1,5 +1,3 @@
|
|||
<div>
|
||||
<th:block th:if="${!resource.startElement.empty} and ${!resource.endElement.empty}" th:text="${resource.startElement.value} + ' - ' + ${resource.endElement.value}"/>
|
||||
<th:block th:if="${!resource.startElement.empty} and ${resource.endElement.empty}" th:text="${resource.startElement.value} + ' - ?'"/>
|
||||
<th:block th:if="${resource.startElement.empty} and ${!resource.endElement.empty}" th:text="'? - ' + ${resource.endElement.value}"/>
|
||||
</div>
|
||||
<th:block th:if="${!resource.startElement.empty} and ${!resource.endElement.empty}" th:text="${resource.startElement.value} + ' - ' + ${resource.endElement.value}"/>
|
||||
<th:block th:if="${!resource.startElement.empty} and ${resource.endElement.empty}" th:text="${resource.startElement.value} + ' - ?'"/>
|
||||
<th:block th:if="${resource.startElement.empty} and ${!resource.endElement.empty}" th:text="'? - ' + ${resource.endElement.value}"/>
|
||||
|
|
|
@ -1,12 +1,10 @@
|
|||
<div>
|
||||
<th:block th:if="${not resource.comparatorElement.empty}" th:text="${resource.comparatorElement.value}"/>
|
||||
<th:block th:if="${not resource.valueElement.empty}" th:text="${resource.valueElement.valueAsString}"/>
|
||||
<th:block th:switch="${fhirVersion}">
|
||||
<th:block th:case="'DSTU1'">
|
||||
<th:block th:if="${not resource.unitsElement.empty}" th:text="${resource.unitsElement.value}"/>
|
||||
</th:block>
|
||||
<th:block th:case="*">
|
||||
<th:block th:if="${not resource.unitElement.empty}" th:text="${resource.unitElement.value}"/>
|
||||
</th:block>
|
||||
<th:block th:if="${not resource.comparatorElement.empty}" th:text="${resource.comparatorElement.value}"/>
|
||||
<th:block th:if="${not resource.valueElement.empty}" th:text="${resource.valueElement.valueAsString}"/>
|
||||
<th:block th:switch="${fhirVersion}">
|
||||
<th:block th:case="'DSTU1'">
|
||||
<th:block th:if="${not resource.unitsElement.empty}" th:text="${resource.unitsElement.value}"/>
|
||||
</th:block>
|
||||
</div>
|
||||
<th:block th:case="*">
|
||||
<th:block th:if="${not resource.unitElement.empty}" th:text="${resource.unitElement.value}"/>
|
||||
</th:block>
|
||||
</th:block>
|
||||
|
|
|
@ -1,11 +1,9 @@
|
|||
<div>
|
||||
<th:block th:if="${not resource.valueElement.empty}" th:text="${resource.valueElement.valueAsString}"/>
|
||||
<th:block th:switch="${fhirVersion}">
|
||||
<th:block th:case="'DSTU1'">
|
||||
<th:block th:if="${not resource.unitsElement.empty}" th:text="${resource.unitsElement.value}"/>
|
||||
</th:block>
|
||||
<th:block th:case="*">
|
||||
<th:block th:if="${not resource.unitElement.empty}" th:text="${resource.unitElement.value}"/>
|
||||
</th:block>
|
||||
<th:block th:if="${not resource.valueElement.empty}" th:text="${resource.valueElement.valueAsString}"/>
|
||||
<th:block th:switch="${fhirVersion}">
|
||||
<th:block th:case="'DSTU1'">
|
||||
<th:block th:if="${not resource.unitsElement.empty}" th:text="${resource.unitsElement.value}"/>
|
||||
</th:block>
|
||||
</div>
|
||||
<th:block th:case="*">
|
||||
<th:block th:if="${not resource.unitElement.empty}" th:text="${resource.unitElement.value}"/>
|
||||
</th:block>
|
||||
</th:block>
|
||||
|
|
|
@ -1,3 +1 @@
|
|||
<div>
|
||||
<th:block th:if="${not resource.value.empty}" th:text="${resource.value}"/>
|
||||
</div>
|
||||
<th:block th:if="${not resource.value.empty}" th:text="${resource.value}"/>
|
||||
|
|
|
@ -34,7 +34,7 @@ public class CustomThymeleafNarrativeGeneratorTest {
|
|||
String actual = narrative.getDiv().getValueAsString();
|
||||
ourLog.info(actual);
|
||||
|
||||
assertThat(actual, containsString("<h1>Name</h1><div class=\"nameElement\"> given <b>FAM1 </b></div><h1>Address</h1><div><span>line1 </span><br /><span>line2 </span><br /></div></div>"));
|
||||
assertThat(actual, containsString("<h1>Name</h1><div class=\"nameElement\">given <b>FAM1 </b></div><h1>Address</h1><div><span>line1 </span><br/><span>line2 </span><br/></div></div>"));
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -66,7 +66,7 @@ public class DefaultThymeleafNarrativeGeneratorTest {
|
|||
NarrativeDt narrative = new NarrativeDt();
|
||||
gen.generateNarrative(myCtx, value, narrative);
|
||||
String output = narrative.getDiv().getValueAsString();
|
||||
assertThat(output, StringContains.containsString("<div class=\"hapiHeaderText\"> joe john <b>BLOW </b></div>"));
|
||||
assertThat(output, StringContains.containsString("<div class=\"hapiHeaderText\">joe john <b>BLOW </b></div>"));
|
||||
|
||||
// Removed because label is gone in DSTU2
|
||||
// value.getIdentifierFirstRep().setLabel("FOO MRN 123");
|
||||
|
|
|
@ -42,7 +42,7 @@ public class CustomThymeleafNarrativeGeneratorDstu2Test {
|
|||
String actual = narrative.getDiv().getValueAsString();
|
||||
ourLog.info(actual);
|
||||
|
||||
assertThat(actual, containsString("<h1>Name</h1><div class=\"nameElement\"> given <b>FAM1 </b></div><h1>Address</h1><div><span>line1 </span><br /><span>line2 </span><br /></div></div>"));
|
||||
assertThat(actual, containsString("<h1>Name</h1><div class=\"nameElement\">given <b>FAM1 </b></div><h1>Address</h1><div><span>line1 </span><br/><span>line2 </span><br/></div></div>"));
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -73,7 +73,7 @@ public class DefaultThymeleafNarrativeGeneratorDstu2Test {
|
|||
myGen.generateNarrative(ourCtx, value, narrative);
|
||||
String output = narrative.getDiv().getValueAsString();
|
||||
ourLog.info(output);
|
||||
assertThat(output, StringContains.containsString("<div class=\"hapiHeaderText\"> joe john <b>BLOW </b></div>"));
|
||||
assertThat(output, StringContains.containsString("<div class=\"hapiHeaderText\">joe john <b>BLOW </b></div>"));
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -73,7 +73,7 @@ public class DefaultThymeleafNarrativeGeneratorDstu3Test {
|
|||
myGen.generateNarrative(ourCtx, value, narrative);
|
||||
String output = narrative.getDiv().getValueAsString();
|
||||
ourLog.info(output);
|
||||
assertThat(output, StringContains.containsString("<div class=\"hapiHeaderText\"> joe john <b>BLOW </b></div>"));
|
||||
assertThat(output, StringContains.containsString("<div class=\"hapiHeaderText\">joe john <b>BLOW </b></div>"));
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -1045,7 +1045,7 @@ public class JsonParserDstu3Test {
|
|||
String output = ourCtx.newJsonParser().encodeResourceToString(p);
|
||||
ourLog.info(output);
|
||||
|
||||
assertThat(output, containsString("\"text\":{\"status\":\"generated\",\"div\":\"<div xmlns=\\\"http://www.w3.org/1999/xhtml\\\"><div class=\\\"hapiHeaderText\\\"> John <b>SMITH </b></div>"));
|
||||
assertThat(output, containsString("\"text\":{\"status\":\"generated\",\"div\":\"<div xmlns=\\\"http://www.w3.org/1999/xhtml\\\"><div class=\\\"hapiHeaderText\\\">John <b>SMITH </b></div>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
@ -1784,7 +1784,7 @@ public class XmlParserDstu3Test {
|
|||
String output = ourCtx.newXmlParser().encodeResourceToString(p);
|
||||
ourLog.info(output);
|
||||
|
||||
assertThat(output, containsString("<text><status value=\"generated\"/><div xmlns=\"http://www.w3.org/1999/xhtml\"><div class=\"hapiHeaderText\"> John <b>SMITH </b>"));
|
||||
assertThat(output, containsString("<text><status value=\"generated\"/><div xmlns=\"http://www.w3.org/1999/xhtml\"><div class=\"hapiHeaderText\">John <b>SMITH </b>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
@ -7,8 +7,9 @@ import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
|||
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
|
||||
import org.thymeleaf.spring4.SpringTemplateEngine;
|
||||
import org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver;
|
||||
import org.thymeleaf.spring4.view.ThymeleafViewResolver;
|
||||
import org.thymeleaf.templateresolver.ServletContextTemplateResolver;
|
||||
import org.thymeleaf.templatemode.TemplateMode;
|
||||
|
||||
import ca.uhn.fhir.to.mvc.AnnotationMethodHandlerAdapterConfigurer;
|
||||
|
||||
|
@ -27,13 +28,13 @@ public class FhirTesterMvcConfig extends WebMvcConfigurerAdapter {
|
|||
}
|
||||
|
||||
@Bean
|
||||
public ServletContextTemplateResolver templateResolver() {
|
||||
ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver();
|
||||
templateResolver.setPrefix("/WEB-INF/templates/");
|
||||
templateResolver.setSuffix(".html");
|
||||
templateResolver.setTemplateMode("HTML5");
|
||||
templateResolver.setCharacterEncoding("UTF-8");
|
||||
return templateResolver;
|
||||
public SpringResourceTemplateResolver templateResolver() {
|
||||
SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
|
||||
resolver.setPrefix("/WEB-INF/templates/");
|
||||
resolver.setSuffix(".html");
|
||||
resolver.setTemplateMode(TemplateMode.HTML);
|
||||
resolver.setCharacterEncoding("UTF-8");
|
||||
return resolver;
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
|
2
pom.xml
2
pom.xml
|
@ -278,7 +278,7 @@
|
|||
<phloc_schematron_version>2.7.1</phloc_schematron_version>
|
||||
<phloc_commons_version>4.4.5</phloc_commons_version>
|
||||
<spring_version>4.3.1.RELEASE</spring_version>
|
||||
<thymeleaf-version>2.1.4.RELEASE</thymeleaf-version>
|
||||
<thymeleaf-version>3.0.1.RELEASE</thymeleaf-version>
|
||||
<ebay_cors_filter_version>1.0.1</ebay_cors_filter_version>
|
||||
<xmlunit_version>1.6</xmlunit_version>
|
||||
|
||||
|
|
|
@ -64,6 +64,7 @@
|
|||
<li>Spring (JPA, Web Tester): 4.3.0 -> 4.3.1</li>
|
||||
<!--<li>Hibernate ORM (JPA): 5.1.0 -> 5.2.1</li>-->
|
||||
<li>Hibernate Search (JPA): 5.5.2 -> 5.5.4</li>
|
||||
<li>Thymeleaf (Narrative Generator / Web Tester): 2.1.4 ->3.0.1</li>
|
||||
</ul>
|
||||
]]>
|
||||
</action>
|
||||
|
|
Loading…
Reference in New Issue