View Javadoc
1   /**
2    * This file Copyright (c) 2017-2018 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 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   * {@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              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          //mgnl:contentNode is the default nodeType, no need to export it, this saves a lot of lines e.g. in the configuration workspace export
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 ;