]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms.ui/src/org/argeo/cms/text/IdentityTextInterpreter.java
Add JGit to client.
[lgpl/argeo-commons.git] / org.argeo.cms.ui / src / org / argeo / cms / text / IdentityTextInterpreter.java
1 package org.argeo.cms.text;
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.CmsNames;
10 import org.argeo.cms.CmsTypes;
11
12 /** Based on HTML with a few Wiki-like shortcuts. */
13 public class IdentityTextInterpreter implements TextInterpreter, CmsNames {
14
15 @Override
16 public void write(Item item, String content) {
17 try {
18 if (item instanceof Node) {
19 Node node = (Node) item;
20 if (node.isNodeType(CmsTypes.CMS_STYLED)) {
21 String raw = convertToStorage(node, content);
22 validateBeforeStoring(raw);
23 node.setProperty(CMS_CONTENT, raw);
24 } else {
25 throw new CmsException("Don't know how to interpret "
26 + node);
27 }
28 } else {// property
29 Property property = (Property) item;
30 property.setValue(content);
31 }
32 // item.getSession().save();
33 } catch (RepositoryException e) {
34 throw new CmsException("Cannot set content on " + item, e);
35 }
36 }
37
38 @Override
39 public String read(Item item) {
40 try {
41 String raw = raw(item);
42 return convertFromStorage(item, raw);
43 } catch (RepositoryException e) {
44 throw new CmsException("Cannot get " + item + " for edit", e);
45 }
46 }
47
48 @Override
49 public String raw(Item item) {
50 try {
51 item.getSession().refresh(true);
52 if (item instanceof Node) {
53 Node node = (Node) item;
54 if (node.isNodeType(CmsTypes.CMS_STYLED)) {
55 // WORKAROUND FOR BROKEN PARARAPHS
56 if (!node.hasProperty(CMS_CONTENT)) {
57 node.setProperty(CMS_CONTENT, "");
58 node.getSession().save();
59 }
60
61 return node.getProperty(CMS_CONTENT).getString();
62 } else {
63 throw new CmsException("Don't know how to interpret "
64 + 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)
85 throws RepositoryException {
86 return content;
87
88 }
89
90 /** To be overridden, in order to support additional formatting. */
91 protected String convertFromStorage(Item item, String content)
92 throws RepositoryException {
93 return content;
94 }
95 }