org.magicwerk.brownies.html.XmlWriter Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2010 by Thomas Mauch
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* $Id$
*/
package org.magicwerk.brownies.html;
import org.jdom2.CDATA;
import org.jdom2.DocType;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.Text;
import org.magicwerk.brownies.jdom.JdomTools;
/**
* Helper class for writing an XML file.
*
* @author Thomas Mauch
* @version $Id$
*/
public class XmlWriter {
private Document doc;
private Element root;
private Element curr;
public XmlWriter() {
}
public XmlWriter(Element curr) {
this.curr = curr;
this.root = JdomTools.getRoot(curr);
this.doc = curr.getDocument();
}
/**
* Return document containing the root element.
* The document is created on request the first time and then cached.
*
* @return document containing the root element
*/
public Document getDocument() {
if (doc == null) {
doc = new Document(root);
}
return doc;
}
/**
* Set document type.
*
* @param docType document type
*/
public void setDocType(DocType docType) {
getDocument().setDocType(docType);
}
/**
* Return root element.
*
* @return root element
*/
public Element getRoot() {
return root;
}
public Element getCurrent() {
return curr;
}
/**
* Adds an XML element with given name and make is the current element.
* It must be closed using close().
*
* @param elemName name of XML element to add
* @return this XmlWriter
*/
public XmlWriter open(String elemName) {
Element elem = new Element(elemName);
return open(elem);
}
/**
* Adds the given XML element at the current position and makes
* it the current element. It must be closed using close().
*
* @param elem XML element to add
* @return this XmlWriter
*/
public XmlWriter open(Element elem) {
if (root == null) {
root = elem;
curr = elem;
if (doc != null) {
doc.setRootElement(root);
}
} else {
curr.addContent(elem);
curr = elem;
}
return this;
}
/**
* Closes the current tag.
* The passed name is just used to verify the correctness of the xml structure.
*
* @param elemName name of XML element
* @throws IllegalArgumentException if the specified element is not the current
*/
public void close(String elemName) {
if (!elemName.equals(curr.getName())) {
throw new IllegalArgumentException("Element " + elemName + " cannot be closed as it is not the current");
}
close();
}
/**
* Closes an XML element which was created with open() before
*/
public void close() {
curr = curr.getParentElement();
}
/**
* Add element to current XML tag.
*
* @param elemName name of XML element
* @return this XmlWriter
*/
public XmlWriter add(String elemName) {
open(elemName);
close();
return this;
}
/**
* Creates an element with given name and text and adds it to
* the current XML tag.
*
* @param elemName name of XML element
* @param text text to add to XML element
* @return this XmlWriter
*/
public XmlWriter add(String elemName, String text) {
open(elemName);
addText(text);
close();
return this;
}
/**
* Add element to current XML tag.
*
* @param elem XML element to add
* @return this XmlWriter
*/
public XmlWriter add(Element elem) {
curr.addContent(elem);
return this;
}
/**
* Set attribute for current XML tag.
*
* @param name name of attribute
* @param value value of attribute
* @return this XmlWriter
*/
public XmlWriter setAttribute(String name, String value) {
curr.setAttribute(name, value);
return this;
}
/**
* Add text to current XML tag.
*
* @param text text to add
* @return this XmlWriter
*/
public XmlWriter addText(String text) {
curr.addContent(new Text(text));
return this;
}
/**
* Add CDATA text to current XML tag.
*
* @param text text to add as CDATA
* @return this XmlWriter
*/
public XmlWriter addCdata(String text) {
curr.addContent(new CDATA(text));
return this;
}
}