]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms.lib.json/src/org/argeo/cms/acr/json/AcrJsonUtils.java
Remove direct reference to Tomcat JNI
[lgpl/argeo-commons.git] / org.argeo.cms.lib.json / src / org / argeo / cms / acr / json / AcrJsonUtils.java
1 package org.argeo.cms.acr.json;
2
3 import javax.xml.namespace.QName;
4
5 import org.argeo.api.acr.Content;
6 import org.argeo.api.acr.DName;
7 import org.argeo.api.acr.NamespaceUtils;
8 import org.argeo.api.acr.QNamed;
9
10 import jakarta.json.stream.JsonGenerator;
11
12 /** Utilities around ACR and the JSON format. */
13 public class AcrJsonUtils {
14 public static void writeAttr(JsonGenerator g, Content content, String attr) {
15 writeAttr(g, content, NamespaceUtils.parsePrefixedName(attr));
16 }
17
18 public static void writeAttr(JsonGenerator g, Content content, QNamed attr) {
19 writeAttr(g, content, attr.qName());
20 }
21
22 public static void writeAttr(JsonGenerator g, Content content, QName attr) {
23 // String value = content.attr(attr);
24 Object value = content.get(attr);
25 if (value != null) {
26 // TODO specify NamespaceContext
27 String key = NamespaceUtils.toPrefixedName(attr);
28 if (value instanceof Double v)
29 g.write(key, v);
30 else if (value instanceof Long v)
31 g.write(key, v);
32 else if (value instanceof Integer v)
33 g.write(key, v);
34 else if (value instanceof Boolean v)
35 g.write(key, v);
36 else
37 g.write(key, value.toString());
38 }
39 }
40
41 /** singleton */
42 private AcrJsonUtils() {
43 }
44
45 // private final QName JCR_CREATED = NamespaceUtils.parsePrefixedName("jcr:created");
46 //
47 // private final QName JCR_LAST_MODIFIED = NamespaceUtils.parsePrefixedName("jcr:lastModified");
48
49 public static void writeTimeProperties(JsonGenerator g, Content content) {
50 String creationDate = content.attr(DName.creationdate);
51 // if (creationDate == null)
52 // creationDate = content.attr(JCR_CREATED);
53 if (creationDate != null)
54 g.write(DName.creationdate.get(), creationDate);
55 String lastModified = content.attr(DName.getlastmodified);
56 // if (lastModified == null)
57 // lastModified = content.attr(JCR_LAST_MODIFIED);
58 if (lastModified != null)
59 g.write(DName.getlastmodified.get(), lastModified);
60 }
61 }