public class ServiceBuilder {
public static final String AUTHOR = "Brian Wing Shun Chan";
public static String getContent(String fileName) throws Exception {
Document document = _getContentDocument(fileName);
Element rootElement = document.getRootElement();
Element authorElement = null;
Element namespaceElement = null;
Map<String, Element> entityElements = new TreeMap<String, Element>();
Map<String, Element> exceptionElements = new TreeMap<String, Element>();
for (Element element : rootElement.elements()) {
String elementName = element.getName();
if (elementName.equals("author")) {
element.detach();
if (authorElement != null) {
throw new IllegalArgumentException(
"There can only be one author element");
}
authorElement = element;
}
else if (elementName.equals("namespace")) {
element.detach();
if (namespaceElement != null) {
throw new IllegalArgumentException(
"There can only be one namespace element");
}
namespaceElement = element;
}
else if (elementName.equals("entity")) {
element.detach();
String name = element.attributeValue("name");
entityElements.put(StringUtil.toLowerCase(name), element);
}
else if (elementName.equals("exceptions")) {
element.detach();
for (Element exceptionElement : element.elements("exception")) {
exceptionElement.detach();
exceptionElements.put(
exceptionElement.getText(), exceptionElement);
}
}
}
if (authorElement != null) {
rootElement.add(authorElement);
}
if (namespaceElement == null) {
throw new IllegalArgumentException(
"The namespace element is required");
}
else {
rootElement.add(namespaceElement);
}
_addElements(rootElement, entityElements);
if (!exceptionElements.isEmpty()) {
Element exceptionsElement = rootElement.addElement("exceptions");
_addElements(exceptionsElement, exceptionElements);
}
return document.asXML();
}