]>
git.argeo.org Git - gpl/argeo-suite.git/blob - argeo/docbook/ui/DbkTextInterpreter.java
1 package org
.argeo
.docbook
.ui
;
3 import java
.io
.IOException
;
4 import java
.io
.StringReader
;
9 import javax
.jcr
.Property
;
10 import javax
.jcr
.RepositoryException
;
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
;
18 /** Based on HTML with a few Wiki-like shortcuts. */
19 public class DbkTextInterpreter
implements TextInterpreter
{
22 public void write(Item item
, String content
) {
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
);
30 if (!node
.hasNode(Jcr
.JCR_XMLTEXT
))
31 jcrText
= node
.addNode(Jcr
.JCR_XMLTEXT
, JcrxType
.JCRX_XMLTEXT
);
33 jcrText
= node
.getNode(Jcr
.JCR_XMLTEXT
);
34 jcrText
.setProperty(Jcr
.JCR_XMLCHARACTERS
, raw
);
36 throw new IllegalArgumentException("Don't know how to interpret " + node
);
39 Property property
= (Property
) item
;
40 property
.setValue(content
);
42 // item.getSession().save();
43 } catch (RepositoryException e
) {
44 throw new JcrException("Cannot set content on " + item
, e
);
49 public String
read(Item item
) {
51 String raw
= raw(item
);
52 return convertFromStorage(item
, raw
);
53 } catch (RepositoryException e
) {
54 throw new JcrException("Cannot get " + item
+ " for edit", e
);
59 public String
raw(Item item
) {
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", " ");
72 throw new IllegalArgumentException("Don't know how to interpret " + node
);
75 Property property
= (Property
) item
;
76 return property
.getString();
78 } catch (RepositoryException e
) {
79 throw new JcrException("Cannot get " + item
+ " content", e
);
83 final static int BR_LENGTH
= "<br/>".length();
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)
90 try (StringReader reader
= new StringReader(raw
)) {
91 List
<String
> lines
= IOUtils
.readLines(reader
);
92 if (lines
.size() == 1)
94 StringBuilder sb
= new StringBuilder(raw
.length() + lines
.size() * BR_LENGTH
);
95 for (int i
= 0; i
< lines
.size(); i
++) {
98 sb
.append(lines
.get(i
));
100 return sb
.toString();
101 } catch (IOException e
) {
102 throw new RuntimeException(e
);
104 // String[] lines = raw.split("[\r\n]+");
105 // if (lines.length == 1)
107 // StringBuilder sb = new StringBuilder(raw.length() + lines.length * BR_LENGTH);
108 // for (int i = 0; i < lines.length; i++) {
110 // sb.append("<br/>");
111 // sb.append(lines[i]);
113 // return sb.toString();
118 * To be overridden, in order to make sure that only valid strings are being
121 protected void validateBeforeStoring(String raw
) {
124 /** To be overridden, in order to support additional formatting. */
125 protected String
convertToStorage(Item item
, String content
) throws RepositoryException
{
130 /** To be overridden, in order to support additional formatting. */
131 protected String
convertFromStorage(Item item
, String content
) throws RepositoryException
{