001    /******************************************************************************
002     * Copyright (C) MActor Developers. All rights reserved.                        *
003     * ---------------------------------------------------------------------------*
004     * This file is part of MActor.                                               *
005     *                                                                            *
006     * MActor is free software; you can redistribute it and/or modify             *
007     * it under the terms of the GNU General Public License as published by       *
008     * the Free Software Foundation; either version 2 of the License, or          *
009     * (at your option) any later version.                                        *
010     *                                                                            *
011     * MActor is distributed in the hope that it will be useful,                  *
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
014     * GNU General Public License for more details.                               *
015     *                                                                            *
016     * You should have received a copy of the GNU General Public License          *
017     * along with MActor; if not, write to the Free Software                      *
018     * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA *
019     ******************************************************************************/
020    package org.mactor.framework.spec;
021    
022    import java.util.Iterator;
023    import java.util.LinkedList;
024    import java.util.List;
025    
026    import org.dom4j.Element;
027    import org.mactor.framework.ConfigException;
028    import org.mactor.framework.MactorException;
029    
030    public abstract class ContainerSpec extends SpecNode {
031            private List<SpecNode> specNodes = new LinkedList<SpecNode>();
032            public List<SpecNode> getSpecNodes() {
033                    return specNodes;
034            }
035            public void addSpecNode(SpecNode node) throws MactorException {
036                    specNodes.add(node);
037            }
038            public void loadContainedNodes(Element element) throws MactorException {
039                    if (element == null)
040                            return;
041                    Iterator it = element.elementIterator();
042                    while (it.hasNext()) {
043                            Element el = (Element) it.next();
044                            SpecNode spec = null;
045                            if (el.getName().equals("message_subscribe")) {
046                                    spec = MessageSubscribeSpec.loadSpec(el);
047                            } else if (el.getName().equals("message_publish")) {
048                                    spec = MessagePublishSpec.loadSpec(el);
049                            } else if (el.getName().equals("message_receive")) {
050                                    spec = MessageReceiveSpec.loadSpec(el);
051                            } else if (el.getName().equals("action")) {
052                                    spec = ActionSpec.loadSpec(el);
053                            } else if (el.getName().equals("value")) {
054                                    spec = ValueSpec.loadSpec(el);
055                            } else if (el.getName().equals("message_respond")) {
056                                    spec = MessageRespondSpec.loadSpec(el);
057                            } else if (el.getName().equals("loop")) {
058                                    spec = LoopSpec.loadSpec(el);
059                            } else if (el.getName().equals("condition")) {
060                                    spec = ConditionSpec.loadSpec(el);
061                            } else {
062                                    throw new ConfigException("Unsupported node type '" + el.getName() + "'");
063                            }
064                            spec.parentNode = this;
065                            addSpecNode(spec);
066                    }
067                    return;
068            }
069            public void writeContainedNodesToElement(Element element) {
070                    for (SpecNode n : specNodes) {
071                            n.addToElement(element);
072                    }
073            }
074    }