]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/src/org/argeo/cms/text/IdentityTextInterpreter.java
- Introduce CMS specific TextInterpreter
[lgpl/argeo-commons.git] / org.argeo.cms / 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 if (item instanceof Node) {
52 Node node = (Node) item;
53 if (node.isNodeType(CmsTypes.CMS_STYLED)) {
54 // WORKAROUND FOR BROKEN PARARAPHS
55 if (!node.hasProperty(CMS_CONTENT)) {
56 node.setProperty(CMS_CONTENT, "");
57 node.getSession().save();
58 }
59
60 return node.getProperty(CMS_CONTENT).getString();
61 } else {
62 throw new CmsException("Don't know how to interpret "
63 + node);
64 }
65 } else {// property
66 Property property = (Property) item;
67 return property.getString();
68 }
69 } catch (RepositoryException e) {
70 throw new CmsException("Cannot get " + item + " content", e);
71 }
72 }
73
74 // EXTENSIBILITY
75 /**
76 * To be overridden, in order to make sure that only valid strings are being
77 * stored.
78 */
79 protected void validateBeforeStoring(String raw) {
80 }
81
82 /** To be overridden, in order to support additional formatting. */
83 protected String convertToStorage(Item item, String content)
84 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)
91 throws RepositoryException {
92 return content;
93 }
94 }