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