Introduce Argeo Publishing.
[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 javax.jcr.Item;
4 import javax.jcr.Node;
5 import javax.jcr.Property;
6 import javax.jcr.RepositoryException;
7
8 import org.argeo.cms.CmsException;
9 import org.argeo.cms.text.TextInterpreter;
10
11 /** Based on HTML with a few Wiki-like shortcuts. */
12 public class DbkTextInterpreter implements TextInterpreter {
13
14         @Override
15         public void write(Item item, String content) {
16                 try {
17                         if (item instanceof Node) {
18                                 Node node = (Node) item;
19                                 if (node.isNodeType(DocBookTypes.PARA)) {
20                                         String raw = convertToStorage(node, content);
21                                         validateBeforeStoring(raw);
22                                         Node jcrText;
23                                         if (!node.hasNode(DocBookNames.JCR_XMLTEXT))
24                                                 jcrText = node.addNode(DocBookNames.JCR_XMLTEXT, DocBookTypes.XMLTEXT);
25                                         else
26                                                 jcrText = node.getNode(DocBookNames.JCR_XMLTEXT);
27                                         jcrText.setProperty(DocBookNames.JCR_XMLCHARACTERS, raw);
28                                 } else {
29                                         throw new CmsException("Don't know how to interpret " + node);
30                                 }
31                         } else {// property
32                                 Property property = (Property) item;
33                                 property.setValue(content);
34                         }
35                         // item.getSession().save();
36                 } catch (RepositoryException e) {
37                         throw new CmsException("Cannot set content on " + item, e);
38                 }
39         }
40
41         @Override
42         public String read(Item item) {
43                 try {
44                         String raw = raw(item);
45                         return convertFromStorage(item, raw);
46                 } catch (RepositoryException e) {
47                         throw new CmsException("Cannot get " + item + " for edit", e);
48                 }
49         }
50
51         @Override
52         public String raw(Item item) {
53                 try {
54                         item.getSession().refresh(true);
55                         if (item instanceof Node) {
56                                 Node node = (Node) item;
57                                 if (node.isNodeType(DocBookTypes.PARA)) {
58                                         Node jcrText = node.getNode(DocBookNames.JCR_XMLTEXT);
59                                         String txt = jcrText.getProperty(DocBookNames.JCR_XMLCHARACTERS).getString();
60                                         // TODO make it more robust
61                                         txt = txt.replace("\n", "").replace("\t", "");
62                                         return txt;
63                                 } else {
64                                         throw new CmsException("Don't know how to interpret " + node);
65                                 }
66                         } else {// property
67                                 Property property = (Property) item;
68                                 return property.getString();
69                         }
70                 } catch (RepositoryException e) {
71                         throw new CmsException("Cannot get " + item + " content", e);
72                 }
73         }
74
75         // EXTENSIBILITY
76         /**
77          * To be overridden, in order to make sure that only valid strings are being
78          * stored.
79          */
80         protected void validateBeforeStoring(String raw) {
81         }
82
83         /** To be overridden, in order to support additional formatting. */
84         protected String convertToStorage(Item item, String content) throws RepositoryException {
85                 return content;
86
87         }
88
89         /** To be overridden, in order to support additional formatting. */
90         protected String convertFromStorage(Item item, String content) throws RepositoryException {
91                 return content;
92         }
93 }