]> git.argeo.org Git - gpl/argeo-suite.git/blob - argeo/docbook/ui/DbkTextInterpreter.java
Prepare next development cycle
[gpl/argeo-suite.git] / argeo / docbook / ui / DbkTextInterpreter.java
1 package org.argeo.docbook.ui;
2
3 import java.io.IOException;
4 import java.io.StringReader;
5 import java.util.List;
6
7 import javax.jcr.Item;
8 import javax.jcr.Node;
9 import javax.jcr.Property;
10 import javax.jcr.RepositoryException;
11
12 import org.apache.commons.io.IOUtils;
13 import org.argeo.cms.text.TextInterpreter;
14 import org.argeo.jcr.Jcr;
15 import org.argeo.jcr.JcrException;
16 import org.argeo.jcr.JcrxType;
17
18 /** Based on HTML with a few Wiki-like shortcuts. */
19 public class DbkTextInterpreter implements TextInterpreter {
20
21 @Override
22 public void write(Item item, String content) {
23 try {
24 if (item instanceof Node) {
25 Node node = (Node) item;
26 if (node.isNodeType(DocBookTypes.PARA) || node.isNodeType(DocBookTypes.TITLE)) {
27 String raw = convertToStorage(node, content);
28 validateBeforeStoring(raw);
29 Node jcrText;
30 if (!node.hasNode(Jcr.JCR_XMLTEXT))
31 jcrText = node.addNode(Jcr.JCR_XMLTEXT, JcrxType.JCRX_XMLTEXT);
32 else
33 jcrText = node.getNode(Jcr.JCR_XMLTEXT);
34 jcrText.setProperty(Jcr.JCR_XMLCHARACTERS, raw);
35 } else {
36 throw new IllegalArgumentException("Don't know how to interpret " + node);
37 }
38 } else {// property
39 Property property = (Property) item;
40 property.setValue(content);
41 }
42 // item.getSession().save();
43 } catch (RepositoryException e) {
44 throw new JcrException("Cannot set content on " + item, e);
45 }
46 }
47
48 @Override
49 public String read(Item item) {
50 try {
51 String raw = raw(item);
52 return convertFromStorage(item, raw);
53 } catch (RepositoryException e) {
54 throw new JcrException("Cannot get " + item + " for edit", e);
55 }
56 }
57
58 @Override
59 public String raw(Item item) {
60 try {
61 item.getSession().refresh(true);
62 if (item instanceof Node) {
63 Node node = (Node) item;
64 if (node.isNodeType(DocBookTypes.PARA) || node.isNodeType(DocBookTypes.TITLE)) {
65 Node jcrText = node.getNode(Jcr.JCR_XMLTEXT);
66 String txt = jcrText.getProperty(Jcr.JCR_XMLCHARACTERS).getString();
67 // TODO make it more robust
68 // txt = txt.replace("\n", "").replace("\t", "");
69 txt = txt.replace("\t", " ");
70 return txt;
71 } else {
72 throw new IllegalArgumentException("Don't know how to interpret " + node);
73 }
74 } else {// property
75 Property property = (Property) item;
76 return property.getString();
77 }
78 } catch (RepositoryException e) {
79 throw new JcrException("Cannot get " + item + " content", e);
80 }
81 }
82
83 final static int BR_LENGTH = "<br/>".length();
84
85 public String readSimpleHtml(Item item) {
86 String raw = raw(item);
87 // raw = "<span style='text-align:justify'>" + raw + "</span>";
88 if (raw.length() == 0)
89 return raw;
90 try (StringReader reader = new StringReader(raw)) {
91 List<String> lines = IOUtils.readLines(reader);
92 if (lines.size() == 1)
93 return lines.get(0);
94 StringBuilder sb = new StringBuilder(raw.length() + lines.size() * BR_LENGTH);
95 for (int i = 0; i < lines.size(); i++) {
96 if (i != 0)
97 sb.append("<br/>");
98 sb.append(lines.get(i));
99 }
100 return sb.toString();
101 } catch (IOException e) {
102 throw new RuntimeException(e);
103 }
104 // String[] lines = raw.split("[\r\n]+");
105 // if (lines.length == 1)
106 // return lines[0];
107 // StringBuilder sb = new StringBuilder(raw.length() + lines.length * BR_LENGTH);
108 // for (int i = 0; i < lines.length; i++) {
109 // if (i != 0)
110 // sb.append("<br/>");
111 // sb.append(lines[i]);
112 // }
113 // return sb.toString();
114 }
115
116 // EXTENSIBILITY
117 /**
118 * To be overridden, in order to make sure that only valid strings are being
119 * stored.
120 */
121 protected void validateBeforeStoring(String raw) {
122 }
123
124 /** To be overridden, in order to support additional formatting. */
125 protected String convertToStorage(Item item, String content) throws RepositoryException {
126 return content;
127
128 }
129
130 /** To be overridden, in order to support additional formatting. */
131 protected String convertFromStorage(Item item, String content) throws RepositoryException {
132 return content;
133 }
134 }