+ logger.error("error while filtering non-HTML text: " + t.toString());
+
+ throw new RuntimeException(t.toString());
+ }
+ }
+ public String filterHTMLText(String aText) {
+ try {
+ StringWriter out = new StringWriter();
+ Tidy tidy = new Tidy();
+ ByteArrayInputStream in = new ByteArrayInputStream(aText.getBytes("UTF8"));
+ tidy.setMakeClean(true);
+ tidy.setCharEncoding(Configuration.UTF8);
+ tidy.setErrout(logger.asPrintWriter(LoggerWrapper.DEBUG_MESSAGE));
+ print(tidy.parseDOM(in, null), out);
+
+ return out.toString();
+ }
+ catch (IOException e) {
+ return e.getMessage();
+ }
+ }
+
+ private boolean checkAttr(String attrName) {
+ if (attrName.equals("onLoad") || attrName.equals("onClick") || attrName.equals("onFocus") || attrName.equals("onBlur") || attrName.equals("onMouseOver") || attrName.equals("onMouseOut") || attrName.equals("style") || attrName.equals("STYLE") || attrName.equals("height") || attrName.equals("width") || attrName.equals("HEIGHT") || attrName.equals("WIDTH"))
+ return false;
+ return true;
+
+ }
+
+ private boolean checkNode(String nodeName) {
+ List languages = StringRoutines.splitString(MirGlobal.config().getString("Localizer.HTML.Whitelist"), ",");
+ Iterator i = languages.iterator();
+ while (i.hasNext()) {
+ if (nodeName.equals(i.next()))
+ return true;
+ }
+ return false;
+ }
+
+ private void print(Node node, StringWriter out) throws IOException {
+ if (node == null) {
+ return;
+ }
+ int type = node.getNodeType();
+ boolean canOutput = checkNode(node.getNodeName());
+
+ switch (type) {
+
+ case Node.DOCUMENT_NODE:
+
+ print(((Document) node).getDocumentElement(), out);
+ out.flush();
+ break;
+
+ case Node.ELEMENT_NODE:
+ if (canOutput) {
+ out.write('<');
+
+ out.write(node.getNodeName());
+ NamedNodeMap attrs = node.getAttributes();
+
+ for (int i = 0; i < attrs.getLength(); i++) {
+ String attrName = attrs.item(i).getNodeName();
+ if (checkAttr(attrName)) {
+ out.write(' ');
+ out.write(attrs.item(i).getNodeName());
+ out.write("=\"");
+
+ out.write(attrs.item(i).getNodeValue());
+ out.write('"');
+ }
+ }
+
+ if (node.getChildNodes()==null || node.getChildNodes().getLength()==0) {
+ out.write("/");
+ }
+ out.write('>');
+ }
+ NodeList children = node.getChildNodes();
+ if (children != null) {
+ int len = children.getLength();
+ for (int i = 0; i < len; i++) {
+ print(children.item(i), out);
+ }
+ }
+ break;
+
+ case Node.TEXT_NODE:
+ out.write(node.getNodeValue());
+ break;
+
+ }
+
+ if (type == Node.ELEMENT_NODE && canOutput && node.getChildNodes()!=null && node.getChildNodes().getLength()>0) {
+ out.write("</");
+ out.write(node.getNodeName());
+ out.write('>');
+ }
+
+ out.flush();
+ }
+
+ public static class Utility extends ReflectionGeneratorFunctionsAdapter {
+ public Utility () {
+ super(new MirBasicUtilityFunctions());
+ }
+
+ public Object getDatetime() {
+ return new GeneratorDateTimeFunctions.DateTimeFunctions(
+ MirPropertiesConfiguration.instance().getString("Mir.DefaultTimezone"));
+ }
+
+ public Object getCompressWhitespace() {
+ return new freemarker.template.utility.CompressWhitespace();