001package org.javasimon.console.action;
002
003import org.javasimon.Simon;
004import org.javasimon.console.ActionContext;
005import org.w3c.dom.Document;
006import org.w3c.dom.Element;
007
008/**
009 * Export Simons as a hierarchical XML document.
010 * All attributes of simons are exported.
011 *
012 * @author gquintana
013 */
014public class TreeXmlAction extends AbstractXmlAction {
015
016        public static final String PATH = "/data/tree.xml";
017
018        /**
019         * Name of the simon from where to start.
020         * {@code null} means root.
021         */
022        private String name;
023
024        public TreeXmlAction(ActionContext context) {
025                super(context);
026        }
027
028        @Override
029        public void readParameters() {
030                super.readParameters();
031                name = getContext().getParameterAsString("name", null);
032        }
033
034        @Override
035        protected Element createElement(Document document, Simon simon) {
036                Element element = super.createElement(document, simon);
037                for (Simon child : simon.getChildren()) {
038                        Element childElement = createElement(document, child);
039                        element.appendChild(childElement);
040                }
041                return element;
042        }
043
044        @Override
045        protected void fillDocument(Document document) {
046                Simon simon = name == null ? getContext().getManager().getRootSimon() : getContext().getManager().getSimon(name);
047                Element rootElement = createElement(document, simon);
048                document.appendChild(rootElement);
049        }
050}