001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.xml.simple;
016    
017    import com.liferay.portal.kernel.util.HtmlUtil;
018    import com.liferay.portal.kernel.util.StringBundler;
019    import com.liferay.portal.kernel.util.StringPool;
020    
021    import java.util.LinkedList;
022    
023    /**
024     * @author Shuyang Zhou
025     * @author Brian Wing Shun Chan
026     */
027    public class Element {
028    
029            public Element(Element parentElement, String name) {
030                    this(parentElement, name, null);
031            }
032    
033            public Element(Element parentElement, String name, String text) {
034                    if (parentElement._elementClosed) {
035                            throw new IllegalArgumentException("Parent element is closed");
036                    }
037    
038                    _parentElement = parentElement;
039                    _name = name;
040                    _text = _formatText(text);
041                    _elementStack = parentElement._elementStack;
042                    _stringBundler = parentElement._stringBundler;
043    
044                    _appendChildElement();
045            }
046    
047            public Element(String name) {
048                    this(name, null, true);
049            }
050    
051            public Element(String name, boolean addHeader) {
052                    this(name, null, addHeader);
053            }
054    
055            public Element(String name, String text) {
056                    this(name, text, true);
057            }
058    
059            public Element(String name, String text, boolean addHeader) {
060                    _name = name;
061                    _text = _formatText(text);
062                    _elementStack = new LinkedList<Element>();
063                    _stringBundler = new StringBundler();
064    
065                    if (addHeader) {
066                            _stringBundler.append(_XML_HEADER);
067                    }
068    
069                    _openElement(this);
070            }
071    
072            public void addAttribute(String name, String value) {
073                    if (_openTagClosed) {
074                            throw new IllegalStateException("Element is closed");
075                    }
076    
077                    _stringBundler.append(StringPool.SPACE);
078                    _stringBundler.append(name);
079                    _stringBundler.append(_EQUAL_QUOTE);
080                    _stringBundler.append(value);
081                    _stringBundler.append(StringPool.QUOTE);
082            }
083    
084            public Element addElement(String name) {
085                    return addElement(name, null);
086            }
087    
088            public Element addElement(String name, boolean text) {
089                    return addElement(name, String.valueOf(text));
090            }
091    
092            public Element addElement(String name, double text) {
093                    return addElement(name, String.valueOf(text));
094            }
095    
096            public Element addElement(String name, float text) {
097                    return addElement(name, String.valueOf(text));
098            }
099    
100            public Element addElement(String name, int text) {
101                    return addElement(name, String.valueOf(text));
102            }
103    
104            public Element addElement(String name, long text) {
105                    return addElement(name, String.valueOf(text));
106            }
107    
108            public Element addElement(String name, Object text) {
109                    return addElement(name, String.valueOf(text));
110            }
111    
112            public Element addElement(String name, short text) {
113                    return addElement(name, String.valueOf(text));
114            }
115    
116            public Element addElement(String name, String text) {
117                    return new Element(this, name, text);
118            }
119    
120            public String getName() {
121                    return _name;
122            }
123    
124            public Element getParentElement() {
125                    return _parentElement;
126            }
127    
128            public String getText() {
129                    return _text;
130            }
131    
132            public boolean isRootElement() {
133                    if (_parentElement == null) {
134                            return true;
135                    }
136                    else {
137                            return false;
138                    }
139            }
140    
141            public String toXMLString() {
142                    if (_parentElement != null) {
143                            throw new IllegalStateException(
144                                    "XML string can only generated from a root element");
145                    }
146    
147                    if (_xmlString == null) {
148                            _flushPendingOpenElements();
149    
150                            _xmlString = _stringBundler.toString();
151                    }
152    
153                    return _xmlString;
154            }
155    
156            private void _appendChildElement() {
157                    Element topElement = _elementStack.getLast();
158    
159                    while ((topElement != _parentElement) && (topElement != null)) {
160    
161                            // Close previous sibling elements
162    
163                            _closeElement(topElement);
164    
165                            _elementStack.removeLast();
166    
167                            topElement = _elementStack.getLast();
168                    }
169    
170                    if (topElement == _parentElement) {
171    
172                            // Append current element to its parent
173    
174                            _closeOpenTag(topElement);
175    
176                            _openElement(this);
177                    }
178                    else {
179                            throw new IllegalArgumentException(
180                                    "The parent element does not exist");
181                    }
182            }
183    
184            private void _closeElement(Element element) {
185                    _closeOpenTag(element);
186    
187                    _stringBundler.append(_CLOSE_PRE);
188                    _stringBundler.append(element._name);
189                    _stringBundler.append(_CLOSE_POST);
190    
191                    element._elementClosed = true;
192            }
193    
194            private void _closeOpenTag(Element element) {
195                    if (element._openTagClosed == false) {
196                            _stringBundler.append(_OPEN_POST);
197    
198                            if (element._text != null) {
199                                    _stringBundler.append(element._text);
200                            }
201    
202                            element._openTagClosed = true;
203                    }
204            }
205    
206            private void _flushPendingOpenElements() {
207                    while (_elementStack.size() > 0) {
208                            _closeElement(_elementStack.removeLast());
209                    }
210            }
211    
212            private String _formatText(String text) {
213                    return HtmlUtil.escape(text);
214            }
215    
216            private void _openElement(Element element) {
217                    _stringBundler.append(_OPEN_PRE).append(element._name);
218    
219                    _elementStack.addLast(element);
220            }
221    
222            private static final String _CLOSE_POST = ">";
223    
224            private static final String _CLOSE_PRE = "</";
225    
226            private static final String _EQUAL_QUOTE = "=\"";
227    
228            private static final String _OPEN_POST = ">";
229    
230            private static final String _OPEN_PRE = "<";
231    
232            private static final String _XML_HEADER =
233                    "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
234    
235            private boolean _elementClosed;
236            private LinkedList<Element> _elementStack;
237            private String _name;
238            private boolean _openTagClosed;
239            private Element _parentElement;
240            private StringBundler _stringBundler;
241            private String _text;
242            private String _xmlString;
243    
244    }