]>
git.argeo.org Git - gpl/argeo-suite.git/blob - docbook/ui/DbkTextInterpreter.java
1 package org
.argeo
.docbook
.ui
;
5 import javax
.jcr
.Property
;
6 import javax
.jcr
.RepositoryException
;
8 import org
.argeo
.cms
.CmsException
;
9 import org
.argeo
.cms
.text
.TextInterpreter
;
11 /** Based on HTML with a few Wiki-like shortcuts. */
12 public class DbkTextInterpreter
implements TextInterpreter
{
15 public void write(Item item
, String content
) {
17 if (item
instanceof Node
) {
18 Node node
= (Node
) item
;
19 if (node
.isNodeType(DocBookTypes
.PARA
)) {
20 String raw
= convertToStorage(node
, content
);
21 validateBeforeStoring(raw
);
23 if (!node
.hasNode(DocBookNames
.JCR_XMLTEXT
))
24 jcrText
= node
.addNode(DocBookNames
.JCR_XMLTEXT
, DocBookTypes
.XMLTEXT
);
26 jcrText
= node
.getNode(DocBookNames
.JCR_XMLTEXT
);
27 jcrText
.setProperty(DocBookNames
.JCR_XMLCHARACTERS
, raw
);
29 throw new CmsException("Don't know how to interpret " + node
);
32 Property property
= (Property
) item
;
33 property
.setValue(content
);
35 // item.getSession().save();
36 } catch (RepositoryException e
) {
37 throw new CmsException("Cannot set content on " + item
, e
);
42 public String
read(Item item
) {
44 String raw
= raw(item
);
45 return convertFromStorage(item
, raw
);
46 } catch (RepositoryException e
) {
47 throw new CmsException("Cannot get " + item
+ " for edit", e
);
52 public String
raw(Item item
) {
54 item
.getSession().refresh(true);
55 if (item
instanceof Node
) {
56 Node node
= (Node
) item
;
57 if (node
.isNodeType(DocBookTypes
.PARA
)) {
58 Node jcrText
= node
.getNode(DocBookNames
.JCR_XMLTEXT
);
59 String txt
= jcrText
.getProperty(DocBookNames
.JCR_XMLCHARACTERS
).getString();
60 // TODO make it more robust
61 txt
= txt
.replace("\n", "").replace("\t", "");
64 throw new CmsException("Don't know how to interpret " + node
);
67 Property property
= (Property
) item
;
68 return property
.getString();
70 } catch (RepositoryException e
) {
71 throw new CmsException("Cannot get " + item
+ " content", e
);
77 * To be overridden, in order to make sure that only valid strings are being
80 protected void validateBeforeStoring(String raw
) {
83 /** To be overridden, in order to support additional formatting. */
84 protected String
convertToStorage(Item item
, String content
) throws RepositoryException
{
89 /** To be overridden, in order to support additional formatting. */
90 protected String
convertFromStorage(Item item
, String content
) throws RepositoryException
{