]> git.argeo.org Git - gpl/argeo-suite.git/blob - publishing/org.argeo.publishing.ui/src/org/argeo/cms/text/IdentityTextInterpreter.java
Improve terms framework.
[gpl/argeo-suite.git] / publishing / org.argeo.publishing.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
10 /** Based on HTML with a few Wiki-like shortcuts. */
11 public class IdentityTextInterpreter implements TextInterpreter, CmsNames {
12
13 @Override
14 public void write(Item item, String content) {
15 try {
16 if (item instanceof Node) {
17 Node node = (Node) item;
18 if (node.isNodeType(CmsTypes.CMS_STYLED)) {
19 String raw = convertToStorage(node, content);
20 validateBeforeStoring(raw);
21 node.setProperty(CMS_CONTENT, raw);
22 } else {
23 throw new CmsException("Don't know how to interpret " + node);
24 }
25 } else {// property
26 Property property = (Property) item;
27 property.setValue(content);
28 }
29 // item.getSession().save();
30 } catch (RepositoryException e) {
31 throw new CmsException("Cannot set content on " + item, e);
32 }
33 }
34
35 @Override
36 public String readSimpleHtml(Item item) {
37 return raw(item);
38 }
39
40 @Override
41 public String read(Item item) {
42 try {
43 String raw = raw(item);
44 return convertFromStorage(item, raw);
45 } catch (RepositoryException e) {
46 throw new CmsException("Cannot get " + item + " for edit", e);
47 }
48 }
49
50 @Override
51 public String raw(Item item) {
52 try {
53 item.getSession().refresh(true);
54 if (item instanceof Node) {
55 Node node = (Node) item;
56 if (node.isNodeType(CmsTypes.CMS_STYLED)) {
57 // WORKAROUND FOR BROKEN PARARAPHS
58 if (!node.hasProperty(CMS_CONTENT)) {
59 node.setProperty(CMS_CONTENT, "");
60 node.getSession().save();
61 }
62
63 return node.getProperty(CMS_CONTENT).getString();
64 } else {
65 throw new CmsException("Don't know how to interpret " + node);
66 }
67 } else {// property
68 Property property = (Property) item;
69 return property.getString();
70 }
71 } catch (RepositoryException e) {
72 throw new CmsException("Cannot get " + item + " content", e);
73 }
74 }
75
76 // EXTENSIBILITY
77 /**
78 * To be overridden, in order to make sure that only valid strings are being
79 * stored.
80 */
81 protected void validateBeforeStoring(String raw) {
82 }
83
84 /** To be overridden, in order to support additional formatting. */
85 protected String convertToStorage(Item item, String content) 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) throws RepositoryException {
92 return content;
93 }
94 }