View Javadoc
1   /**
2    * This file Copyright (c) 2017 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
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   * {@link org.apache.jackrabbit.commons.xml.Exporter} for YAML format.
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) { //don't include root node
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); //omit jcr: prefix for all child nodes
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          //mgnl:contentNode is the default nodeType, no need to export it, this saves a lot of lines e.g. in the configuration workspace export
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 }