001package org.nuxeo.apidoc.documentation;
002
003import java.io.StringReader;
004import java.util.ArrayList;
005import java.util.Iterator;
006import java.util.List;
007
008import org.dom4j.Document;
009import org.dom4j.DocumentException;
010import org.dom4j.Element;
011import org.dom4j.Visitor;
012import org.dom4j.VisitorSupport;
013import org.dom4j.io.SAXReader;
014
015public class XMLContributionParser {
016
017    public static String prettyfy(String xml) throws DocumentException {
018
019        StringBuffer writter = new StringBuffer();
020
021        SAXReader reader = new SAXReader();
022        Document document = reader.read(new StringReader(xml));
023
024        Element root = document.getRootElement();
025
026        for (Iterator i = root.elementIterator(); i.hasNext();) {
027            Element element = (Element) i.next();
028            if (!element.getName().equals("documentation")) {
029                ContributionItem fragment = parseContrib(element);
030                fragment.write(writter);
031            }
032        }
033        return writter.toString();
034    }
035
036    public static List<ContributionItem> extractContributionItems(String xml) throws DocumentException {
037
038        List<ContributionItem> items = new ArrayList<ContributionItem>();
039
040        SAXReader reader = new SAXReader();
041        Document document = reader.read(new StringReader(xml));
042
043        Element root = document.getRootElement();
044
045        for (Iterator i = root.elementIterator(); i.hasNext();) {
046            Element element = (Element) i.next();
047            if (!element.getName().equals("documentation")) {
048                ContributionItem fragment = parseContrib(element);
049                items.add(fragment);
050            }
051        }
052        return items;
053    }
054
055    protected static ContributionItem parseContrib(Element element) {
056
057        final ContributionItem fragment = new ContributionItem();
058        fragment.tagName = element.getName();
059
060        fragment.nameOrId = element.attributeValue("name");
061        if (fragment.nameOrId == null) {
062            fragment.nameOrId = element.attributeValue("id");
063        }
064
065        Visitor docExtractor = new VisitorSupport() {
066            @Override
067            public void visit(Element node) {
068                if ("documentation".equalsIgnoreCase(node.getName())) {
069                    fragment.documentation = getNodeAsString(node);
070                } else if ("description".equalsIgnoreCase(node.getName())) {
071                    fragment.documentation = getNodeAsString(node);
072                } else {
073                    super.visit(node);
074                }
075            }
076        };
077        element.accept(docExtractor);
078
079        fragment.xml = element.asXML();
080
081        return fragment;
082    }
083
084    protected static String getNodeAsString(Element node) {
085        String data = node.asXML();
086        data = data.substring(node.getName().length() + 2);
087        if (data.length() > node.getName().length()) {
088            data = data.substring(0, data.length() - node.getName().length() - 3);
089        }
090        data = data.trim();
091        return data;
092    }
093}