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