]>
git.argeo.org Git - gpl/argeo-suite.git/blob - publishing/org.argeo.publishing.ui/src/org/argeo/docbook/ui/DbkTextInterpreter.java
1 package org
.argeo
.docbook
.ui
;
3 import static org
.argeo
.docbook
.DbkUtils
.isDbk
;
4 import static org
.argeo
.docbook
.DbkType
.para
;
5 import static org
.argeo
.docbook
.DbkType
.title
;
7 import java
.io
.IOException
;
8 import java
.io
.StringReader
;
11 import javax
.jcr
.Item
;
12 import javax
.jcr
.Node
;
13 import javax
.jcr
.Property
;
14 import javax
.jcr
.RepositoryException
;
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
;
22 /** Based on HTML with a few Wiki-like shortcuts. */
23 public class DbkTextInterpreter
implements TextInterpreter
{
26 public void write(Item item
, String content
) {
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
);
34 if (!node
.hasNode(Jcr
.JCR_XMLTEXT
))
35 jcrText
= node
.addNode(Jcr
.JCR_XMLTEXT
, JcrxType
.JCRX_XMLTEXT
);
37 jcrText
= node
.getNode(Jcr
.JCR_XMLTEXT
);
38 jcrText
.setProperty(Jcr
.JCR_XMLCHARACTERS
, raw
);
40 throw new IllegalArgumentException("Don't know how to interpret " + node
);
43 Property property
= (Property
) item
;
44 property
.setValue(content
);
46 // item.getSession().save();
47 } catch (RepositoryException e
) {
48 throw new JcrException("Cannot set content on " + item
, e
);
53 public String
read(Item item
) {
55 String raw
= raw(item
);
56 return convertFromStorage(item
, raw
);
57 } catch (RepositoryException e
) {
58 throw new JcrException("Cannot get " + item
+ " for edit", e
);
63 public String
raw(Item item
) {
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", " ");
76 throw new IllegalArgumentException("Don't know how to interpret " + node
);
79 Property property
= (Property
) item
;
80 return property
.getString();
82 } catch (RepositoryException e
) {
83 throw new JcrException("Cannot get " + item
+ " content", e
);
87 final static int BR_LENGTH
= "<br/>".length();
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("&", "&");
95 if (raw
.indexOf('<') >= 0) {
96 raw
= raw
.replace("<", "<");
98 if (raw
.indexOf('>') >= 0) {
99 raw
= raw
.replace(">", ">");
101 if (raw
.indexOf('\"') >= 0) {
102 raw
= raw
.replace("\"", """);
104 if (raw
.indexOf('\'') >= 0) {
105 raw
= raw
.replace("\'", "'");
107 // raw = "<span style='text-align:justify'>" + raw + "</span>";
108 if (raw
.length() == 0)
110 try (StringReader reader
= new StringReader(raw
)) {
111 List
<String
> lines
= IOUtils
.readLines(reader
);
112 if (lines
.size() == 1)
114 StringBuilder sb
= new StringBuilder(raw
.length() + lines
.size() * BR_LENGTH
);
115 for (int i
= 0; i
< lines
.size(); i
++) {
118 sb
.append(lines
.get(i
));
120 return sb
.toString();
121 } catch (IOException e
) {
122 throw new RuntimeException(e
);
124 // String[] lines = raw.split("[\r\n]+");
125 // if (lines.length == 1)
127 // StringBuilder sb = new StringBuilder(raw.length() + lines.length * BR_LENGTH);
128 // for (int i = 0; i < lines.length; i++) {
130 // sb.append("<br/>");
131 // sb.append(lines[i]);
133 // return sb.toString();
138 * To be overridden, in order to make sure that only valid strings are being
141 protected void validateBeforeStoring(String raw
) {
144 /** To be overridden, in order to support additional formatting. */
145 protected String
convertToStorage(Item item
, String content
) throws RepositoryException
{
150 /** To be overridden, in order to support additional formatting. */
151 protected String
convertFromStorage(Item item
, String content
) throws RepositoryException
{