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.outputter;
35  
36  import java.io.IOException;
37  import java.io.Writer;
38  import java.util.ArrayList;
39  import java.util.List;
40  
41  import org.jdom.Attribute;
42  import org.jdom.Element;
43  import org.jdom.Namespace;
44  import org.jdom.output.Format;
45  import org.jdom.output.XMLOutputter;
46  
47  /**
48   * {@link org.jdom.output.XMLOutputter} which print every attribute to a separate line and exposes some private method from the parent class.
49   */
50  public class DocumentViewOutputter extends XMLOutputter {
51  
52      private String separatorChars = "\n";
53      private String apostropheChars = "\"";
54      private String asignmentChars = "=";
55  
56      private int currentLevel;
57  
58      public DocumentViewOutputter() {
59          final Format format = Format.getPrettyFormat();
60          format.setTextMode(Format.TextMode.NORMALIZE);
61          format.setOmitDeclaration(true);
62          setFormat(format);
63      }
64  
65      @Override
66      protected void printElement(Writer out, Element element, int level, NamespaceStack namespaces) throws IOException {
67          currentLevel = level;
68          super.printElement(out, element, level, namespaces);
69      }
70  
71      @Override
72      protected void printAttributes(Writer out, List attributes, Element parent, NamespaceStack namespaces) throws IOException {
73          attributes = sortAttributes(attributes);
74          for (int i = 0; i < attributes.size(); i++) {
75              Attribute attribute = (Attribute) attributes.get(i);
76              Namespace ns = attribute.getNamespace();
77              if (ns != Namespace.NO_NAMESPACE && ns != Namespace.XML_NAMESPACE) { //TODO which print?
78                  printNamespace(out, ns, namespaces);
79              }
80              attributeSeparator(out);
81              if (separatorChars.contains("\n")) {
82                  indent(out, currentLevel + 1);
83              }
84              printQualifiedName(out, attribute);
85              out.write(asignmentChars);
86              out.write(apostropheChars);
87              out.write(escapeAttributeEntities(attribute.getValue()));
88              out.write(apostropheChars);
89          }
90      }
91  
92      protected void attributeSeparator(Writer out) throws IOException {
93          out.write(separatorChars);
94      }
95  
96      protected void indent(Writer out, int level) throws IOException {
97          if (getFormat().getIndent() == null || getFormat().getIndent().equals("")) {
98              return;
99          }
100         for (int i = 0; i < level; i++) {
101             out.write(getFormat().getIndent());
102         }
103     }
104 
105     protected void newline(Writer out) throws IOException {
106         if (getFormat().getIndent() != null) {
107             out.write(getFormat().getLineSeparator());
108         }
109     }
110 
111     protected void printQualifiedName(Writer out, Attribute a) throws IOException {
112         String prefix = a.getNamespace().getPrefix();
113         if ((prefix != null) && (!prefix.equals(""))) {
114             out.write(prefix);
115             out.write(':');
116         }
117         printName(out, a);
118     }
119 
120     protected void printName(Writer out, Attribute a) throws IOException {
121         out.write(a.getName());
122     }
123 
124     private void printNamespace(Writer out, Namespace ns, NamespaceStack namespaces) throws IOException {
125         if (namespaces == null) {
126             return;
127         }
128         String prefix = ns.getPrefix();
129         String uri = ns.getURI();
130 
131         // Already printed namespace decl?
132         if (uri.equals(namespaces.getURI(prefix))) {
133             return;
134         }
135 
136         out.write(" xmlns");
137         if (!prefix.equals("")) {
138             out.write(":");
139             out.write(prefix);
140         }
141         out.write("=\"");
142         out.write(escapeAttributeEntities(uri));
143         out.write("\"");
144         namespaces.push(ns);
145     }
146 
147     protected List<Attribute> sortAttributes(List attributes) throws IOException {
148         List<Attribute> sortedAttributes = new ArrayList<>(attributes);
149         sortedAttributes.sort((o1, o2) -> o1.getQualifiedName().compareTo(o2.getQualifiedName()));
150         return sortedAttributes;
151     }
152 
153     protected int getCurrentLevel() {
154         return currentLevel;
155     }
156 
157     protected void setCurrentLevel(int currentLevel) {
158         this.currentLevel = currentLevel;
159     }
160 
161     public String getSeparatorChars() {
162         return separatorChars;
163     }
164 
165     public void setSeparatorChars(String separatorChars) {
166         this.separatorChars = separatorChars;
167     }
168 
169     public String getApostropheChars() {
170         return apostropheChars;
171     }
172 
173     public void setApostropheChars(String apostropheChars) {
174         this.apostropheChars = apostropheChars;
175     }
176 
177     public String getAsignmentChars() {
178         return asignmentChars;
179     }
180 
181     public void setAsignmentChars(String asignmentChars) {
182         this.asignmentChars = asignmentChars;
183     }
184 }