X-Git-Url: https://git.argeo.org/?p=gpl%2Fargeo-suite.git;a=blobdiff_plain;f=publishing%2Forg.argeo.publishing.ui%2Fsrc%2Forg%2Fargeo%2Fdocbook%2Fui%2FDbkTextInterpreter.java;fp=publishing%2Forg.argeo.publishing.ui%2Fsrc%2Forg%2Fargeo%2Fdocbook%2Fui%2FDbkTextInterpreter.java;h=f13826bd693f1d66aff097f78e7923db1b79aa7c;hp=0000000000000000000000000000000000000000;hb=147ada7da5bf6292569f17a53a77fca04c97f707;hpb=5431f941fd6161e89f495a330c2ecddaf4f6bfb1 diff --git a/publishing/org.argeo.publishing.ui/src/org/argeo/docbook/ui/DbkTextInterpreter.java b/publishing/org.argeo.publishing.ui/src/org/argeo/docbook/ui/DbkTextInterpreter.java new file mode 100644 index 0000000..f13826b --- /dev/null +++ b/publishing/org.argeo.publishing.ui/src/org/argeo/docbook/ui/DbkTextInterpreter.java @@ -0,0 +1,93 @@ +package org.argeo.docbook.ui; + +import javax.jcr.Item; +import javax.jcr.Node; +import javax.jcr.Property; +import javax.jcr.RepositoryException; + +import org.argeo.cms.CmsException; +import org.argeo.cms.text.TextInterpreter; + +/** Based on HTML with a few Wiki-like shortcuts. */ +public class DbkTextInterpreter implements TextInterpreter { + + @Override + public void write(Item item, String content) { + try { + if (item instanceof Node) { + Node node = (Node) item; + if (node.isNodeType(DocBookTypes.PARA)) { + String raw = convertToStorage(node, content); + validateBeforeStoring(raw); + Node jcrText; + if (!node.hasNode(DocBookNames.JCR_XMLTEXT)) + jcrText = node.addNode(DocBookNames.JCR_XMLTEXT, DocBookTypes.XMLTEXT); + else + jcrText = node.getNode(DocBookNames.JCR_XMLTEXT); + jcrText.setProperty(DocBookNames.JCR_XMLCHARACTERS, raw); + } else { + throw new CmsException("Don't know how to interpret " + node); + } + } else {// property + Property property = (Property) item; + property.setValue(content); + } + // item.getSession().save(); + } catch (RepositoryException e) { + throw new CmsException("Cannot set content on " + item, e); + } + } + + @Override + public String read(Item item) { + try { + String raw = raw(item); + return convertFromStorage(item, raw); + } catch (RepositoryException e) { + throw new CmsException("Cannot get " + item + " for edit", e); + } + } + + @Override + public String raw(Item item) { + try { + item.getSession().refresh(true); + if (item instanceof Node) { + Node node = (Node) item; + if (node.isNodeType(DocBookTypes.PARA)) { + Node jcrText = node.getNode(DocBookNames.JCR_XMLTEXT); + String txt = jcrText.getProperty(DocBookNames.JCR_XMLCHARACTERS).getString(); + // TODO make it more robust + txt = txt.replace("\n", "").replace("\t", ""); + return txt; + } else { + throw new CmsException("Don't know how to interpret " + node); + } + } else {// property + Property property = (Property) item; + return property.getString(); + } + } catch (RepositoryException e) { + throw new CmsException("Cannot get " + item + " content", e); + } + } + + // EXTENSIBILITY + /** + * To be overridden, in order to make sure that only valid strings are being + * stored. + */ + protected void validateBeforeStoring(String raw) { + } + + /** To be overridden, in order to support additional formatting. */ + protected String convertToStorage(Item item, String content) throws RepositoryException { + return content; + + } + + /** To be overridden, in order to support additional formatting. */ + protected String convertFromStorage(Item item, String content) throws RepositoryException { + return content; + } +}