+ private void readXml(Node node, StringBuilder sb) throws RepositoryException {
+ NodeIterator nit = node.getNodes();
+ while (nit.hasNext()) {
+ Node child = nit.nextNode();
+ if (child.getName().equals(Jcr.JCR_XMLTEXT)) {
+ String txt = child.getProperty(Jcr.JCR_XMLCHARACTERS).getString();
+ // TODO make it more robust
+ // txt = txt.replace("\n", "").replace("\t", "");
+ txt = txt.replace("\t", " ");
+ sb.append(txt);
+ } else {
+ sb.append('<').append(child.getName());
+ PropertyIterator pit = child.getProperties();
+ properties: while (pit.hasNext()) {
+ Property p = pit.nextProperty();
+ if (p.getName().startsWith("jcr:"))
+ continue properties;
+ sb.append(' ').append(p.getName()).append("=\"").append(p.getString()).append('\"');
+ }
+ sb.append('>');
+ readXml(child, sb);
+// try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
+// child.getSession().exportDocumentView(child.getPath(), out, true, false);
+// sb.append(new String(out.toByteArray(), StandardCharsets.UTF_8));
+// } catch (IOException e) {
+// throw new IllegalStateException("Cannot export " + child, e);
+// }
+ sb.append("</").append(child.getName()).append('>');
+ }
+ }
+ }
+
+ private void readAsSimpleHtml(Node node, StringBuilder sb) throws RepositoryException {
+ NodeIterator nit = node.getNodes();
+ while (nit.hasNext()) {
+ Node child = nit.nextNode();
+ if (child.getName().equals(Jcr.JCR_XMLTEXT)) {
+ String txt = child.getProperty(Jcr.JCR_XMLCHARACTERS).getString();
+ // TODO make it more robust
+ // txt = txt.replace("\n", "").replace("\t", "");
+ txt = txt.replace("\t", " ");
+ String html = textToSimpleHtml(txt);
+ sb.append(html);
+ } else if (child.getName().equals(DbkType.link.get())) {
+ if (child.hasProperty(DbkAttr.XLINK_HREF)) {
+ String href = child.getProperty(DbkAttr.XLINK_HREF).getString();
+ // TODO deal with other forbidden XML characters?
+ href = href.replace("&", "&");
+ sb.append("<a class='" + linkCssClass + "' href='").append(href).append("'>");
+ readAsSimpleHtml(child, sb);
+ sb.append("</a>");
+ }
+ } else {
+ // ignore
+ }
+ }
+ }
+
+ private String textToSimpleHtml(String raw) {
+ // FIXME the saved data should be corrected instead.
+ if (raw.indexOf('&') >= 0) {
+ raw = raw.replace("&", "&");
+ }
+ if (raw.indexOf('<') >= 0) {
+ raw = raw.replace("<", "<");
+ }
+ if (raw.indexOf('>') >= 0) {
+ raw = raw.replace(">", ">");
+ }
+ if (raw.indexOf('\"') >= 0) {
+ raw = raw.replace("\"", """);
+ }
+ if (raw.indexOf('\'') >= 0) {
+ raw = raw.replace("\'", "'");
+ }
+// raw = "<span style='text-align:justify'>" + raw + "</span>";
+ if (raw.length() == 0)
+ return raw;
+ try (StringReader reader = new StringReader(raw)) {
+ List<String> lines = IOUtils.readLines(reader);
+ if (lines.size() == 1)
+ return lines.get(0);
+ StringBuilder sb = new StringBuilder(raw.length() + lines.size() * BR_LENGTH);
+ for (int i = 0; i < lines.size(); i++) {
+ if (i != 0)
+ sb.append("<br/>");
+ sb.append(lines.get(i));
+ }
+ return sb.toString();
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ final static int BR_LENGTH = "<br/>".length();
+
+ public String readSimpleHtml(Item item) {
+ try {
+ StringBuilder sb = new StringBuilder();
+// sb.append("<div style='text-align: justify;'>");
+ readAsSimpleHtml((Node) item, sb);
+// sb.append("</div>");
+// System.out.println(sb);
+ return sb.toString();
+ } catch (RepositoryException e) {
+ throw new JcrException("Cannot convert " + item + " to simple HTML", e);
+ }
+ }
+