1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.importexport.exporter;
35
36 import info.magnolia.jcr.util.NodeTypes;
37
38 import javax.jcr.Node;
39 import javax.jcr.NodeIterator;
40 import javax.jcr.PropertyType;
41 import javax.jcr.RepositoryException;
42 import javax.jcr.Session;
43 import javax.jcr.Value;
44
45 import org.apache.commons.lang3.StringUtils;
46 import org.apache.jackrabbit.JcrConstants;
47 import org.apache.jackrabbit.commons.xml.DocumentViewExporter;
48 import org.apache.jackrabbit.util.ISO9075;
49 import org.apache.jackrabbit.value.ValueHelper;
50 import org.xml.sax.ContentHandler;
51 import org.xml.sax.SAXException;
52
53
54
55
56 public class YamlExporter extends DocumentViewExporter {
57
58 public YamlExporter(Session session, ContentHandler handler, boolean recurse, boolean binary) {
59 super(session, handler, recurse, binary);
60 }
61
62 @Override
63 protected void exportNode(String uri, String local, Node node) throws RepositoryException, SAXException {
64 if (node.getDepth() == 0) {
65 final NodeIterator iterator = node.getNodes();
66 while (iterator.hasNext()) {
67 final Node child = iterator.nextNode();
68 super.exportNode(StringUtils.EMPTY, serializeKey(uri, child.getName()), child);
69 }
70 } else {
71 super.exportNode(uri, serializeKey(uri, local), node);
72 }
73 }
74
75 @Override
76 protected void exportProperty(String uri, String local, Value value) throws RepositoryException {
77
78 if (JcrConstants.JCR_PRIMARYTYPE.equals(getXMLName(uri, local)) && NodeTypes.ContentNode.NAME.equals(value.getString())) {
79 return;
80 }
81 final String name = ISO9075.encode(serializeKey(uri, local));
82 final String attribute = serializeValue(value, value.getType());
83 final String propertyType = getPropertyTypePrefix(value);
84 super.addAttribute(uri, name, propertyType + attribute);
85 }
86
87 @Override
88 protected void exportProperty(String uri, String local, int type, Value[] values) {
89 try {
90 final StringBuilder attribute = new StringBuilder("");
91 for (Value value : values) {
92 attribute.append(getPropertyTypePrefix(value));
93 attribute.append(serializeValue(value, type));
94 attribute.append(",");
95 }
96 final String name = StringUtils.isEmpty(uri) ? ISO9075.encode(serializeKey(uri, local)) : local;
97 final String attributeString = "[" + (values.length == 0 ? "" : attribute.substring(0, attribute.length() - 1)) + "]";
98 super.addAttribute(uri, name, attributeString);
99 } catch (RepositoryException e) {
100 throw new RuntimeException(e);
101 }
102 }
103
104 private String serializeValue(Value value, int type) throws RepositoryException {
105 String stringValue = ValueHelper.serialize(value, false);
106 switch (type) {
107 case PropertyType.STRING:
108 case PropertyType.URI:
109 case PropertyType.NAME:
110 case PropertyType.PATH:
111 stringValue = "'" + stringValue.replace("'", "''") + "'";
112 }
113 return stringValue;
114 }
115
116 private String getPropertyTypePrefix(Value value) throws RepositoryException {
117 String type = "";
118
119 switch (value.getType()) {
120 case PropertyType.BINARY:
121 case PropertyType.PATH:
122 case PropertyType.REFERENCE:
123 case PropertyType.WEAKREFERENCE:
124 case PropertyType.URI:
125 type = "!" + StringUtils.lowerCase(PropertyType.nameFromValue(value.getType())) + " ";
126 }
127 return type;
128 }
129
130 private String serializeKey(String uri, String local) throws RepositoryException {
131 return StringUtils.isEmpty(uri) ? "'" + local.replace("'", "''") + "'" : local;
132 }
133 }