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 static javax.jcr.PropertyType.nameFromValue;
37 import static org.apache.commons.lang3.StringUtils.lowerCase;
38 import static org.apache.jackrabbit.value.ValueHelper.serialize;
39
40 import info.magnolia.jcr.util.NodeTypes;
41
42 import javax.jcr.Node;
43 import javax.jcr.PropertyType;
44 import javax.jcr.RepositoryException;
45 import javax.jcr.Session;
46 import javax.jcr.Value;
47
48 import org.apache.jackrabbit.JcrConstants;
49 import org.apache.jackrabbit.commons.xml.DocumentViewExporter;
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 super.exportNodes(node);
66 } else {
67 super.exportNode(uri, local, node);
68 }
69 }
70
71 @Override
72 protected void exportProperty(String uri, String local, Value value) throws RepositoryException {
73
74 if (JcrConstants.JCR_PRIMARYTYPE.equals(getXMLName(uri, local)) && NodeTypes.ContentNode.NAME.equals(value.getString())) {
75 return;
76 }
77 String typeTag = getPropertyTypeTag(value);
78 String attValue = serialize(value, false);
79 super.addAttribute(uri, local, typeTag + attValue);
80 }
81
82 @Override
83 protected void exportProperty(String uri, String local, int type, Value[] values) {
84 try {
85 if (values.length == 0) {
86 super.addAttribute(uri, local, "[]!");
87 } else {
88 String typeTag = getPropertyTypeTag(values[0]);
89 for (int i = 0; i < values.length; i++) {
90 super.addAttribute(uri, local, "[" + i + "]" + typeTag + serialize(values[i], false));
91 }
92 }
93 } catch (RepositoryException e) {
94 throw new RuntimeException(e);
95 }
96 }
97
98 private String getPropertyTypeTag(Value value) {
99 switch (value.getType()) {
100 case PropertyType.BINARY:
101 throw new UnsupportedOperationException("command.error.binary.export.not.supported");
102 case PropertyType.PATH:
103 case PropertyType.REFERENCE:
104 case PropertyType.WEAKREFERENCE:
105 case PropertyType.URI:
106 return lowerCase(nameFromValue(value.getType())) + "!";
107 default:
108 return "!";
109 }
110 }
111
112 }
113 ;