1 package org.argeo.connect.ui.widgets;
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 import org.argeo.docbook.jcr.DocBookNames;
11 import org.argeo.docbook.jcr.DocBookTypes;
12
13
14 public class DbkTextInterpreter implements TextInterpreter {
15
16 @Override
17 public void write(Item item, String content) {
18 try {
19 if (item instanceof Node) {
20 Node node = (Node) item;
21 if (node.isNodeType(DocBookTypes.PARA)) {
22 String raw = convertToStorage(node, content);
23 validateBeforeStoring(raw);
24 Node jcrText;
25 if (!node.hasNode(DocBookNames.JCR_XMLTEXT))
26 jcrText = node.addNode(DocBookNames.JCR_XMLTEXT, DocBookTypes.XMLTEXT);
27 else
28 jcrText = node.getNode(DocBookNames.JCR_XMLTEXT);
29 jcrText.setProperty(DocBookNames.JCR_XMLCHARACTERS, raw);
30 } else {
31 throw new CmsException("Don't know how to interpret " + node);
32 }
33 } else {
34 Property property = (Property) item;
35 property.setValue(content);
36 }
37
38 } catch (RepositoryException e) {
39 throw new CmsException("Cannot set content on " + item, e);
40 }
41 }
42
43 @Override
44 public String read(Item item) {
45 try {
46 String raw = raw(item);
47 return convertFromStorage(item, raw);
48 } catch (RepositoryException e) {
49 throw new CmsException("Cannot get " + item + " for edit", e);
50 }
51 }
52
53 @Override
54 public String raw(Item item) {
55 try {
56 item.getSession().refresh(true);
57 if (item instanceof Node) {
58 Node node = (Node) item;
59 if (node.isNodeType(DocBookTypes.PARA)) {
60
61
62
63
64
65 Node jcrText = node.getNode(DocBookNames.JCR_XMLTEXT);
66 return jcrText.getProperty(DocBookNames.JCR_XMLCHARACTERS).getString();
67 } else {
68 throw new CmsException("Don't know how to interpret " + node);
69 }
70 } else {
71 Property property = (Property) item;
72 return property.getString();
73 }
74 } catch (RepositoryException e) {
75 throw new CmsException("Cannot get " + item + " content", e);
76 }
77 }
78
79
80
81
82
83
84 protected void validateBeforeStoring(String raw) {
85 }
86
87
88 protected String convertToStorage(Item item, String content) throws RepositoryException {
89 return content;
90
91 }
92
93
94 protected String convertFromStorage(Item item, String content) throws RepositoryException {
95 return content;
96 }
97 }