]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/src/org/argeo/cms/IdentityTextInterpreter.java
c66cd866def539e9cd22486b8648dc45ea79f615
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / IdentityTextInterpreter.java
1 package org.argeo.cms;
2
3 import javax.jcr.Item;
4 import javax.jcr.Node;
5 import javax.jcr.Property;
6 import javax.jcr.RepositoryException;
7
8 /** Based on HTML with a few Wiki-like shortcuts. */
9 public class IdentityTextInterpreter implements TextInterpreter, CmsNames {
10
11 @Override
12 public void write(Item item, String content) {
13 try {
14 if (item instanceof Node) {
15 Node node = (Node) item;
16 if (node.isNodeType(CmsTypes.CMS_STYLED)) {
17 String raw = convertToStorage(node, content);
18 node.setProperty(CMS_CONTENT, raw);
19 } else {
20 throw new CmsException("Don't know how to interpret "
21 + node);
22 }
23 } else {// property
24 Property property = (Property) item;
25 property.setValue(content);
26 }
27 item.getSession().save();
28 } catch (RepositoryException e) {
29 throw new CmsException("Cannot set content on " + item, e);
30 }
31 }
32
33 @Override
34 public String read(Item item) {
35 try {
36 String raw = raw(item);
37 return convertFromStorage(item, raw);
38 } catch (RepositoryException e) {
39 throw new CmsException("Cannot get " + item + " for edit", e);
40 }
41 }
42
43 @Override
44 public String raw(Item item) {
45 try {
46 if (item instanceof Node) {
47 Node node = (Node) item;
48 if (node.isNodeType(CmsTypes.CMS_STYLED)) {
49 // WORKAROUND FOR BROKEN PARARAPHS
50 if (!node.hasProperty(CMS_CONTENT)) {
51 node.setProperty(CMS_CONTENT, "");
52 node.getSession().save();
53 }
54
55 return node.getProperty(CMS_CONTENT).getString();
56 } else {
57 throw new CmsException("Don't know how to interpret "
58 + node);
59 }
60 } else {// property
61 Property property = (Property) item;
62 return property.getString();
63 }
64 } catch (RepositoryException e) {
65 throw new CmsException("Cannot get " + item + " content", e);
66 }
67 }
68
69 protected String convertToStorage(Item item, String content)
70 throws RepositoryException {
71 return content;
72
73 }
74
75 protected String convertFromStorage(Item item, String content)
76 throws RepositoryException {
77 return content;
78 }
79 }