1 package org
.argeo
.app
.swt
.docbook
;
3 import static org
.argeo
.app
.docbook
.DbkAcrUtils
.isDbk
;
4 import static org
.argeo
.app
.docbook
.DbkType
.para
;
5 import static org
.argeo
.app
.docbook
.DbkType
.title
;
7 import java
.io
.IOException
;
8 import java
.io
.StringReader
;
9 import java
.io
.StringWriter
;
10 import java
.util
.List
;
12 import javax
.xml
.parsers
.DocumentBuilderFactory
;
13 import javax
.xml
.transform
.Result
;
14 import javax
.xml
.transform
.Source
;
15 import javax
.xml
.transform
.TransformerException
;
16 import javax
.xml
.transform
.TransformerFactory
;
17 import javax
.xml
.transform
.stream
.StreamResult
;
19 import org
.apache
.commons
.io
.IOUtils
;
20 import org
.argeo
.api
.acr
.Content
;
21 import org
.argeo
.app
.docbook
.DbkType
;
23 /** Based on HTML with a few Wiki-like shortcuts. */
24 public class DbkTextInterpreter
implements TextInterpreter
{
26 private TransformerFactory transformerFactory
= TransformerFactory
.newDefaultInstance();
28 private String linkCssClass
= DbkType
.link
.name();
31 public void write(Content node
, String content
) {
32 if (isDbk(node
, para
) || isDbk(node
, title
)) {
33 String raw
= convertToStorage(node
, content
);
34 validateBeforeStoring(raw
);
36 String jcrUuid
= null;// node.getIdentifier();
37 // if (node.hasProperty(Property.JCR_UUID))
38 // jcrUuid = node.getProperty(Property.JCR_UUID).getString();
40 // // TODO use time based
41 // jcrUuid = UUID.randomUUID().toString();
42 // node.setProperty(Property.JCR_UUID, jcrUuid);
43 // node.getSession().save();
46 StringBuilder namespaces
= new StringBuilder();
47 namespaces
.append(" xmlns:dbk=\"http://docbook.org/ns/docbook\"");
48 namespaces
.append(" xmlns:jcr=\"http://www.jcp.org/jcr/1.0\"");
49 namespaces
.append(" xmlns:xlink=\"http://www.w3.org/1999/xlink\"");
50 raw
= "<" + node
.getName() + " jcr:uuid=\"" + jcrUuid
+ "\"" + namespaces
+ ">" + raw
+ "</"
51 + node
.getName() + ">";
52 // System.out.println(raw);
53 // try (InputStream in = new ByteArrayInputStream(raw.getBytes(StandardCharsets.UTF_8))) {
54 // node.getSession().importXML(node.getParent().getPath(), in,
55 // ImportUUIDBehavior.IMPORT_UUID_COLLISION_REPLACE_EXISTING);
56 // // node.getSession().save();
57 // } catch (IOException e) {
58 // throw new IllegalArgumentException("Cannot parse raw content of " + node, e);
62 // DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
64 // try (Reader in = new StringReader(raw)) {
65 // document = documentBuilder.parse(new InputSource(in));
67 // NodeList nl = document.getChildNodes();
68 // for (int i = 0; i < nl.getLength(); i++) {
69 // org.w3c.dom.Node n = nl.item(i);
70 // if (node instanceof Text) {
74 // } catch (ParserConfigurationException | SAXException | IOException e) {
75 // throw new IllegalArgumentException("Cannot parse raw content of " + node, e);
79 // if (!node.hasNode(Jcr.JCR_XMLTEXT))
80 // jcrText = node.addNode(Jcr.JCR_XMLTEXT, JcrxType.JCRX_XMLTEXT);
82 // jcrText = node.getNode(Jcr.JCR_XMLTEXT);
83 // jcrText.setProperty(Jcr.JCR_XMLCHARACTERS, raw);
85 throw new IllegalArgumentException("Don't know how to interpret " + node
);
90 public String
read(Content item
) {
91 String raw
= raw(item
);
92 return convertFromStorage(item
, raw
);
96 public String
raw(Content node
) {
97 if (isDbk(node
, para
) || isDbk(node
, title
)) {
98 Source source
= node
.adapt(Source
.class);
100 StringWriter stringWriter
= new StringWriter();
101 Result result
= new StreamResult(stringWriter
);
104 transformerFactory
.newTransformer().transform(source
, result
);
105 return stringWriter
.toString();
106 } catch (TransformerException e
) {
107 throw new RuntimeException("Could not convert " + node
+ " to XML", e
);
110 // StringBuilder sb = new StringBuilder();
111 // readXml(node, sb);
112 // NodeIterator nit = node.getNodes();
113 // while (nit.hasNext()) {
114 // Node child = nit.nextNode();
115 // if (child.getName().equals(Jcr.JCR_XMLTEXT)) {
116 // Node jcrText = node.getNode(Jcr.JCR_XMLTEXT);
117 // String txt = jcrText.getProperty(Jcr.JCR_XMLCHARACTERS).getString();
118 // // TODO make it more robust
119 // // txt = txt.replace("\n", "").replace("\t", "");
120 // txt = txt.replace("\t", " ");
123 // try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
124 // child.getSession().exportDocumentView(child.getPath(), out, true, false);
125 // sb.append(new String(out.toByteArray(), StandardCharsets.UTF_8));
126 // } catch (IOException e) {
127 // throw new IllegalStateException("Cannot export " + child, e);
131 // return sb.toString();
133 throw new IllegalArgumentException("Don't know how to interpret " + node
);
137 // private void readXml(Content node, StringBuilder sb){
139 // NodeIterator nit = node.getNodes();
140 // while (nit.hasNext()) {
141 // Node child = nit.nextNode();
142 // if (child.getName().equals(Jcr.JCR_XMLTEXT)) {
143 // String txt = child.getProperty(Jcr.JCR_XMLCHARACTERS).getString();
144 // // TODO make it more robust
145 // // txt = txt.replace("\n", "").replace("\t", "");
146 // txt = txt.replace("\t", " ");
149 // sb.append('<').append(child.getName());
150 // PropertyIterator pit = child.getProperties();
151 // properties: while (pit.hasNext()) {
152 // Property p = pit.nextProperty();
153 // if (p.getName().startsWith("jcr:"))
154 // continue properties;
155 // sb.append(' ').append(p.getName()).append("=\"").append(p.getString()).append('\"');
158 // readXml(child, sb);
159 //// try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
160 //// child.getSession().exportDocumentView(child.getPath(), out, true, false);
161 //// sb.append(new String(out.toByteArray(), StandardCharsets.UTF_8));
162 //// } catch (IOException e) {
163 //// throw new IllegalStateException("Cannot export " + child, e);
165 // sb.append("</").append(child.getName()).append('>');
170 private void readAsSimpleHtml(Content node
, StringBuilder sb
) {
171 // NodeIterator nit = node.getNodes();
172 // while (nit.hasNext()) {
173 // Node child = nit.nextNode();
174 // if (child.getName().equals(Jcr.JCR_XMLTEXT)) {
175 // String txt = child.getProperty(Jcr.JCR_XMLCHARACTERS).getString();
176 // // TODO make it more robust
177 // // txt = txt.replace("\n", "").replace("\t", "");
178 // txt = txt.replace("\t", " ");
179 // String html = textToSimpleHtml(txt);
181 // } else if (child.getName().equals(DbkType.link.get())) {
182 // if (child.hasProperty(DbkAttr.XLINK_HREF)) {
183 // String href = child.getProperty(DbkAttr.XLINK_HREF).getString();
184 // // TODO deal with other forbidden XML characters?
185 // href = href.replace("&", "&");
186 // sb.append("<a class='" + linkCssClass + "' href='").append(href).append("'>");
187 // readAsSimpleHtml(child, sb);
188 // sb.append("</a>");
196 private String
textToSimpleHtml(String raw
) {
197 // FIXME the saved data should be corrected instead.
198 if (raw
.indexOf('&') >= 0) {
199 raw
= raw
.replace("&", "&");
201 if (raw
.indexOf('<') >= 0) {
202 raw
= raw
.replace("<", "<");
204 if (raw
.indexOf('>') >= 0) {
205 raw
= raw
.replace(">", ">");
207 if (raw
.indexOf('\"') >= 0) {
208 raw
= raw
.replace("\"", """);
210 if (raw
.indexOf('\'') >= 0) {
211 raw
= raw
.replace("\'", "'");
213 // raw = "<span style='text-align:justify'>" + raw + "</span>";
214 if (raw
.length() == 0)
216 try (StringReader reader
= new StringReader(raw
)) {
217 List
<String
> lines
= IOUtils
.readLines(reader
);
218 if (lines
.size() == 1)
220 StringBuilder sb
= new StringBuilder(raw
.length() + lines
.size() * BR_LENGTH
);
221 for (int i
= 0; i
< lines
.size(); i
++) {
224 sb
.append(lines
.get(i
));
226 return sb
.toString();
227 } catch (IOException e
) {
228 throw new RuntimeException(e
);
232 final static int BR_LENGTH
= "<br/>".length();
234 public String
readSimpleHtml(Content item
) {
235 StringBuilder sb
= new StringBuilder();
236 // sb.append("<div style='text-align: justify;'>");
237 readAsSimpleHtml(item
, sb
);
238 // sb.append("</div>");
239 // System.out.println(sb);
240 return sb
.toString();
245 * To be overridden, in order to make sure that only valid strings are being
248 protected void validateBeforeStoring(String raw
) {
251 /** To be overridden, in order to support additional formatting. */
252 protected String
convertToStorage(Content item
, String content
) {
257 /** To be overridden, in order to support additional formatting. */
258 protected String
convertFromStorage(Content item
, String content
) {