Fix code highlighting

This commit is contained in:
jamesagnew 2014-06-26 09:06:10 -04:00
parent 3bc1d35698
commit 97e17f9149
4 changed files with 199 additions and 45 deletions

View File

@ -34,6 +34,8 @@ import java.util.TimeZone;
import javax.xml.bind.DatatypeConverter;
import org.apache.commons.lang3.Validate;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.commons.lang3.time.FastDateFormat;
import ca.uhn.fhir.model.api.BasePrimitive;
@ -44,17 +46,17 @@ public abstract class BaseDateTimeDt extends BasePrimitive<Date> {
private static final FastDateFormat ourYearFormat = FastDateFormat.getInstance("yyyy");
private static final FastDateFormat ourYearMonthDayFormat = FastDateFormat.getInstance("yyyy-MM-dd");
private static final FastDateFormat ourYearMonthFormat = FastDateFormat.getInstance("yyyy-MM");
private static final FastDateFormat ourYearMonthDayNoDashesFormat = FastDateFormat.getInstance("yyyyMMdd");
private static final FastDateFormat ourYearMonthDayTimeFormat = FastDateFormat.getInstance("yyyy-MM-dd'T'HH:mm:ss");
private static final FastDateFormat ourYearMonthDayTimeZoneFormat = FastDateFormat.getInstance("yyyy-MM-dd'T'HH:mm:ssZZ");
private static final FastDateFormat ourYearMonthDayTimeMilliZoneFormat = FastDateFormat.getInstance("yyyy-MM-dd'T'HH:mm:ss.SSSZZ");
private static final FastDateFormat ourYearMonthDayTimeMilliFormat = FastDateFormat.getInstance("yyyy-MM-dd'T'HH:mm:ss.SSS");
private static final FastDateFormat ourYearMonthDayTimeMilliZoneFormat = FastDateFormat.getInstance("yyyy-MM-dd'T'HH:mm:ss.SSSZZ");
private static final FastDateFormat ourYearMonthDayTimeZoneFormat = FastDateFormat.getInstance("yyyy-MM-dd'T'HH:mm:ssZZ");
private static final FastDateFormat ourYearMonthFormat = FastDateFormat.getInstance("yyyy-MM");
private TemporalPrecisionEnum myPrecision = TemporalPrecisionEnum.SECOND;
private Date myValue;
private TimeZone myTimeZone;
private boolean myTimeZoneZulu = false;
private Date myValue;
/**
* Gets the precision for this datatype using field values from
@ -67,6 +69,10 @@ public abstract class BaseDateTimeDt extends BasePrimitive<Date> {
return myPrecision;
}
public TimeZone getTimeZone() {
return myTimeZone;
}
@Override
public Date getValue() {
return myValue;
@ -113,6 +119,20 @@ public abstract class BaseDateTimeDt extends BasePrimitive<Date> {
}
}
public boolean isTimeZoneZulu() {
return myTimeZoneZulu;
}
/**
* Returns <code>true</code> if this object represents a date that is today's date
*
* @throws NullPointerException if {@link #getValue()} returns <code>null</code>
*/
public boolean isToday() {
Validate.notNull(myValue, getClass().getSimpleName() + " contains null value");
return DateUtils.isSameDay(new Date(), myValue);
}
/**
* Sets the precision for this datatype using field values from
* {@link Calendar}. Valid values are:
@ -132,6 +152,14 @@ public abstract class BaseDateTimeDt extends BasePrimitive<Date> {
myPrecision = thePrecision;
}
public void setTimeZone(TimeZone theTimeZone) {
myTimeZone = theTimeZone;
}
public void setTimeZoneZulu(boolean theTimeZoneZulu) {
myTimeZoneZulu = theTimeZoneZulu;
}
@Override
public void setValue(Date theValue) throws DataFormatException {
myValue = theValue;
@ -214,22 +242,6 @@ public abstract class BaseDateTimeDt extends BasePrimitive<Date> {
}
}
public TimeZone getTimeZone() {
return myTimeZone;
}
public void setTimeZone(TimeZone theTimeZone) {
myTimeZone = theTimeZone;
}
public boolean isTimeZoneZulu() {
return myTimeZoneZulu;
}
public void setTimeZoneZulu(boolean theTimeZoneZulu) {
myTimeZoneZulu = theTimeZoneZulu;
}
private void clearTimeZone() {
myTimeZone = null;
myTimeZoneZulu = false;
@ -240,5 +252,5 @@ public abstract class BaseDateTimeDt extends BasePrimitive<Date> {
* is allowed by this type
*/
abstract boolean isPrecisionAllowed(TemporalPrecisionEnum thePrecision);
}

View File

@ -25,6 +25,7 @@ import static org.apache.commons.lang3.StringUtils.*;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
@ -138,7 +139,7 @@ public class RestfulTesterServlet extends HttpServlet {
myStaticResources.put("fonts/glyphicons-halflings-regular.svg", "application/octet-stream");
myStaticResources.put("fonts/glyphicons-halflings-regular.ttf", "application/octet-stream");
myStaticResources.put("fonts/glyphicons-halflings-regular.woff", "application/octet-stream");
myStaticResources.put("fa/css/font-awesome.css", "text/css");
myStaticResources.put("fa/css/font-awesome.min.css", "text/css");
myStaticResources.put("fa/fonts/fontawesome-webfont.eot", "application/octet-stream");
@ -171,8 +172,8 @@ public class RestfulTesterServlet extends HttpServlet {
myStaticResources.put("fa/scss/_spinning.scss", "text/css");
myStaticResources.put("fa/scss/_stacked.scss", "text/css");
myStaticResources.put("fa/scss/_variables.scss", "text/css");
myStaticResources.put("fa/scss/font-awesome.scss" , "text/css");
myStaticResources.put("fa/scss/font-awesome.scss", "text/css");
myCtx = new FhirContext();
}
@ -289,9 +290,12 @@ public class RestfulTesterServlet extends HttpServlet {
theContext.getVariables().put("errorMsg", "Invalid page URL: " + url);
return;
}
returnsResource = ResultType.TAGLIST;
outcomeDescription = "Tag List";
url = url.replace("&amp;", "&");
client.loadPage().url(url).execute();
returnsResource = ResultType.BUNDLE;
outcomeDescription = "Bundle Page";
} else if ("delete".equals(method)) {
RuntimeResourceDefinition def = getResourceType(theReq);
@ -551,11 +555,11 @@ public class RestfulTesterServlet extends HttpServlet {
theContext.setVariable("bundle", bundle);
theContext.setVariable("resultStatus", resultStatus);
theContext.setVariable("requestUrl", requestUrl);
String requestBodyText = StringEscapeUtils.escapeHtml4(requestBody);
theContext.setVariable("requestBody", requestBody);
String requestBodyText = format(requestBody, ctEnum);
theContext.setVariable("requestBody", requestBodyText);
theContext.setVariable("requestSyntaxHighlighterClass", requestSyntaxHighlighterClass);
String resultBodyText = StringEscapeUtils.escapeHtml4(resultBody);
theContext.setVariable("resultBody", resultBody);
String resultBodyText = format(resultBody, ctEnum);
theContext.setVariable("resultBody", resultBodyText);
theContext.setVariable("resultBodyIsLong", resultBodyText.length() > 1000);
theContext.setVariable("resultSyntaxHighlighterClass", resultSyntaxHighlighterClass);
theContext.setVariable("requestHeaders", requestHeaders);
@ -569,14 +573,125 @@ public class RestfulTesterServlet extends HttpServlet {
}
}
private String format(String theResultBody, EncodingEnum theEncodingEnum) {
String str = StringEscapeUtils.escapeHtml4(theResultBody);
if (str == null || theEncodingEnum == null) {
return str;
}
StringBuilder b = new StringBuilder();
if (theEncodingEnum == EncodingEnum.JSON) {
boolean inValue = false;
boolean inQuote = false;
for (int i = 0; i < str.length(); i++) {
char prevChar = (i > 0) ? str.charAt(i-1): ' ';
char nextChar = str.charAt(i);
char nextChar2 = (i + 1) < str.length() ? str.charAt(i + 1) : ' ';
char nextChar3 = (i + 2) < str.length() ? str.charAt(i + 2) : ' ';
char nextChar4 = (i + 3) < str.length() ? str.charAt(i + 3) : ' ';
char nextChar5 = (i + 4) < str.length() ? str.charAt(i + 4) : ' ';
char nextChar6 = (i + 5) < str.length() ? str.charAt(i + 5) : ' ';
if (inQuote) {
b.append(nextChar);
if (prevChar != '\\' && nextChar == '&' && nextChar2 == 'q' && nextChar3 == 'u' && nextChar4 == 'o' && nextChar5 == 't' && nextChar6 == ';') {
b.append("quot;</span>");
i += 5;
inQuote = false;
}else if (nextChar == '\\' && nextChar2 == '"') {
b.append("quot;</span>");
i += 5;
inQuote = false;
}
} else {
if (nextChar == ':') {
inValue = true;
b.append(nextChar);
}else if (nextChar == '[' || nextChar == '[') {
b.append("<span class='hlControl'>");
b.append(nextChar);
b.append("</span>");
inValue = false;
}else if (nextChar == '}' || nextChar == '}' || nextChar == ',') {
b.append("<span class='hlControl'>");
b.append(nextChar);
b.append("</span>");
inValue = false;
} else if (nextChar == '&' && nextChar2 == 'q' && nextChar3 == 'u' && nextChar4 == 'o' && nextChar5 == 't' && nextChar6 == ';') {
if (inValue) {
b.append("<span class='hlQuot'>&quot;");
}else {
b.append("<span class='hlTagName'>&quot;");
}
inQuote = true;
i += 5;
}else if (nextChar == ':') {
b.append("<span class='hlControl'>");
b.append(nextChar);
b.append("</span>");
inValue = true;
} else {
b.append(nextChar);
}
}
}
} else {
boolean inQuote = false;
boolean inTag = false;
for (int i = 0; i < str.length(); i++) {
char nextChar = str.charAt(i);
char nextChar2 = (i + 1) < str.length() ? str.charAt(i + 1) : ' ';
char nextChar3 = (i + 2) < str.length() ? str.charAt(i + 2) : ' ';
char nextChar4 = (i + 3) < str.length() ? str.charAt(i + 3) : ' ';
char nextChar5 = (i + 4) < str.length() ? str.charAt(i + 4) : ' ';
char nextChar6 = (i + 5) < str.length() ? str.charAt(i + 5) : ' ';
if (inQuote) {
b.append(nextChar);
if (nextChar == '&' && nextChar2 == 'q' && nextChar3 == 'u' && nextChar4 == 'o' && nextChar5 == 't' && nextChar6 == ';') {
b.append("quot;</span>");
i += 5;
inQuote = false;
}
} else if (inTag) {
if (nextChar == '&' && nextChar2 == 'g' && nextChar3 == 't' && nextChar4 == ';') {
b.append("</span><span class='hlControl'>&gt;</span>");
inTag = false;
i += 3;
} else if (nextChar == ' ') {
b.append("</span><span class='hlAttr'>");
b.append(nextChar);
} else if (nextChar == '&' && nextChar2 == 'q' && nextChar3 == 'u' && nextChar4 == 'o' && nextChar5 == 't' && nextChar6 == ';') {
b.append("<span class='hlQuot'>&quot;");
inQuote = true;
i += 5;
} else {
b.append(nextChar);
}
} else {
if (nextChar == '&' && nextChar2 == 'l' && nextChar3 == 't' && nextChar4 == ';') {
b.append("<span class='hlControl'>&lt;</span><span class='hlTagName'>");
inTag = true;
i += 3;
} else {
b.append(nextChar);
}
}
}
}
return b.toString();
}
private EncodingEnum getRequestEncoding(HttpServletRequest theReq) {
EncodingEnum encoding;
if ("xml".equals(theReq.getParameter("encoding"))) {
encoding = EncodingEnum.XML;
} else if ("json".equals(theReq.getParameter("encoding"))) {
encoding=(EncodingEnum.JSON);
}else {
encoding=null;
encoding = (EncodingEnum.JSON);
} else {
encoding = null;
}
return encoding;
}
@ -637,8 +752,7 @@ public class RestfulTesterServlet extends HttpServlet {
});
}
}
ctx.setVariable("resourceCounts", resourceCounts);
ctx.setVariable("conf", conformance);
ctx.setVariable("base", myServerBase);
@ -663,14 +777,14 @@ public class RestfulTesterServlet extends HttpServlet {
includes.add(nextPath);
}
ctx.setVariable("includes", includes);
if (isNotBlank(theReq.getParameter("update-id"))) {
String updateId = theReq.getParameter("update-id");
String updateVid = defaultIfEmpty(theReq.getParameter("update-vid"),null);
String updateVid = defaultIfEmpty(theReq.getParameter("update-vid"), null);
IResource updateResource = client.read(def.getImplementingClass(), new IdDt(resourceName, updateId, updateVid));
EncodingEnum encoding = getRequestEncoding(theReq);
if (encoding==null) {
encoding=EncodingEnum.XML;
if (encoding == null) {
encoding = EncodingEnum.XML;
}
String updateResourceString = encoding.newParser(myCtx).setPrettyPrint(true).encodeResourceToString(updateResource);
ctx.setVariable("updateResource", updateResourceString);

View File

@ -907,7 +907,7 @@
<td>Request Body</td>
<td valign="top" style="margin: 0px; padding: 0px;">
<pre class="requestBodyPre resultBodyPlaceholder" id="requestBodyPlaceholder">...loading...</pre>
<pre th:text="${requestBody}" th:class="${requestSyntaxHighlighterClass} + ' resultBodyPre pre'" id="requestBodyActual" style="display: none;">{}</pre>
<pre th:utext="${requestBody}" th:class="${requestSyntaxHighlighterClass} + ' resultBodyPre pre'" id="requestBodyActual" style="display: none;">{}</pre>
</td>
</tr>
</tbody>
@ -953,7 +953,7 @@
<h4 class="panel-title">
<a data-toggle="collapse" data-parent="#accordion" href="#collapseOne">
<i id="collapseOneIcon" class="fa fa-minus-square-o"></i>
<span th:text="'Bundle contains ' + ${bundle.entries.size} + ' entries'"/>
<span th:text="'Bundle contains ' + ${bundle.totalResults.value} + ' entries'"/>
</a>
<!-- Prev/Next Page Buttons -->
@ -968,7 +968,7 @@
$('#page-prev-btn').click(function() {
var btn = $(this);
btn.button('loading');
btn.append($('<input />', { type: 'hidden', name: 'pageUrl', value: '<th:block th:text="${bundle.linkPrevious}"/>' }));
btn.append($('<input />', { type: 'hidden', name: 'page-url', value: '<th:block th:text="${bundle.linkPrevious}"/>' }));
});
</script>
@ -983,7 +983,7 @@
$('#page-next-btn').click(function() {
var btn = $(this);
btn.button('loading');
btn.append($('<input />', { type: 'hidden', name: 'pageUrl', value: '<th:block th:text="${bundle.linkNext}"/>' }));
btn.append($('<input />', { type: 'hidden', name: 'page-url', value: '<th:block th:text="${bundle.linkNext}"/>' }));
});
</script>
@ -992,6 +992,14 @@
<div id="collapseOne" class="panel-collapse collapse in">
<div class="panel-body" style="padding-bottom: 0px;">
<table class="table table-condensed" style="padding-bottom: 0px; margin-bottom: 0px;">
<thead>
<tr>
<th></th>
<th>Title</th>
<th>Updated</th>
</tr>
</thead>
<tbody>
<tr th:each="entry : ${bundle.entries}">
<td>
<button class="btn btn-primary btn-xs" th:onclick="'readFromEntriesTable(this, \'' + ${entry.resource.id.resourceType} + '\', \'' + ${entry.resource.id.idPart} + '\', \'' + ${entry.resource.id.versionIdPart} + '\');'" type="submit" name="action" value="read">Read</button>
@ -999,7 +1007,11 @@
</td>
<td th:text="${entry.title}">
</td>
<td th:if="${entry.updated.value} == null"></td>
<td th:if="${entry.updated.value} != null and ${entry.updated.today} == true" th:text="${#dates.format(entry.updated.value, 'HH:mm:ss')}"></td>
<td th:if="${entry.updated.value} != null and ${entry.updated.today} == false" th:text="${#dates.format(entry.updated.value, 'yyyy-MM-dd')}"></td>
</tr>
</tbody>
</table>
<script type="text/javascript">
function readFromEntriesTable(source, type, id, vid) {
@ -1060,7 +1072,7 @@
<td valign="top" style="margin: 0px; padding: 0px; font-weight: normal;">
<pre class="resultBodyPre resultBodyPlaceholder" id="resultBodyPlaceholder">...loading...</pre>
<div id="resultBodyActual" class="resultBodyActual" style="display: none;">
<pre id="resultBodyActualPre" th:text="${resultBody}" th:class="(${resultBodyIsLong} ? '' : ${resultSyntaxHighlighterClass}) + ' resultBodyPre pre'">{}</pre>
<pre id="resultBodyActualPre" th:utext="${resultBody}" th:class="(${resultBodyIsLong} ? '' : ${resultSyntaxHighlighterClass}) + ' resultBodyPre pre'">{}</pre>
</div>
</td>
</tr>

View File

@ -32,6 +32,22 @@ SPAN.headerName {
font-size: 0.8em;
}
.hlQuot {
color: #88F;
}
.hlAttr {
color: #888;
}
.hlTagName {
color: #006699;
}
.hlControl {
color: #660000;
}
.hlText {
color: #000000;
}
SPAN.headerValue {
color: #70A070;
font-family: monospace;