View Javadoc
1   /**
2    *    Copyright 2006-2016 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
15   */
16  package org.mybatis.generator.api.dom.xml;
17  
18  import java.util.ArrayList;
19  import java.util.Collections;
20  import java.util.List;
21  
22  import org.mybatis.generator.api.dom.OutputUtilities;
23  
24  /**
25   * The Class XmlElement.
26   *
27   * @author Jeff Butler
28   */
29  public class XmlElement extends Element {
30      
31      /** The attributes. */
32      private List<Attribute> attributes;
33  
34      /** The elements. */
35      private List<Element> elements;
36  
37      /** The name. */
38      private String name;
39  
40      /**
41       * Instantiates a new xml element.
42       *
43       * @param name
44       *            the name
45       */
46      public XmlElement(String name) {
47          super();
48          attributes = new ArrayList<Attribute>();
49          elements = new ArrayList<Element>();
50          this.name = name;
51      }
52      
53      /**
54       * Copy constructor. Not a truly deep copy, but close enough for most purposes.
55       *
56       * @param original
57       *            the original
58       */
59      public XmlElement(XmlElement original) {
60          super();
61          attributes = new ArrayList<Attribute>();
62          attributes.addAll(original.attributes);
63          elements = new ArrayList<Element>();
64          elements.addAll(original.elements);
65          this.name = original.name;
66      }
67  
68      /**
69       * Gets the attributes.
70       *
71       * @return Returns the attributes.
72       */
73      public List<Attribute> getAttributes() {
74          return attributes;
75      }
76  
77      /**
78       * Adds the attribute.
79       *
80       * @param attribute
81       *            the attribute
82       */
83      public void addAttribute(Attribute attribute) {
84          attributes.add(attribute);
85      }
86  
87      /**
88       * Gets the elements.
89       *
90       * @return Returns the elements.
91       */
92      public List<Element> getElements() {
93          return elements;
94      }
95  
96      /**
97       * Adds the element.
98       *
99       * @param element
100      *            the element
101      */
102     public void addElement(Element element) {
103         elements.add(element);
104     }
105 
106     /**
107      * Adds the element.
108      *
109      * @param index
110      *            the index
111      * @param element
112      *            the element
113      */
114     public void addElement(int index, Element element) {
115         elements.add(index, element);
116     }
117 
118     /**
119      * Gets the name.
120      *
121      * @return Returns the name.
122      */
123     public String getName() {
124         return name;
125     }
126 
127     /* (non-Javadoc)
128      * @see org.mybatis.generator.api.dom.xml.Element#getFormattedContent(int)
129      */
130     @Override
131     public String getFormattedContent(int indentLevel) {
132         StringBuilder sb = new StringBuilder();
133 
134         OutputUtilities.xmlIndent(sb, indentLevel);
135         sb.append('<');
136         sb.append(name);
137 
138         Collections.sort(attributes);
139         for (Attribute att : attributes) {
140             sb.append(' ');
141             sb.append(att.getFormattedContent());
142         }
143 
144         if (elements.size() > 0) {
145             sb.append(">"); //$NON-NLS-1$
146             for (Element element : elements) {
147                 OutputUtilities.newLine(sb);
148                 sb.append(element.getFormattedContent(indentLevel + 1));
149             }
150             OutputUtilities.newLine(sb);
151             OutputUtilities.xmlIndent(sb, indentLevel);
152             sb.append("</"); //$NON-NLS-1$
153             sb.append(name);
154             sb.append('>');
155 
156         } else {
157             sb.append(" />"); //$NON-NLS-1$
158         }
159 
160         return sb.toString();
161     }
162 
163     /**
164      * Sets the name.
165      *
166      * @param name
167      *            the new name
168      */
169     public void setName(String name) {
170         this.name = name;
171     }
172 }