001    /*
002     * File:    $HeadURL: https://jvoicexml.svn.sourceforge.net/svnroot/jvoicexml/core/trunk/org.jvoicexml/src/org/jvoicexml/interpreter/dialog/ExecutablePlainForm.java $
003     * Version: $LastChangedRevision: 2612 $
004     * Date:    $Date: 2011-02-28 18:58:33 +0100 (Mo, 28 Feb 2011) $
005     * Author:  $LastChangedBy: schnelle $
006     *
007     * JVoiceXML - A free VoiceXML implementation.
008     *
009     * Copyright (C) 2006-2009 JVoiceXML group - http://jvoicexml.sourceforge.net
010     *
011     *  This library is free software; you can redistribute it and/or
012     *  modify it under the terms of the GNU Library General Public
013     *  License as published by the Free Software Foundation; either
014     *  version 2 of the License, or (at your option) any later version.
015     *
016     *  This library is distributed in the hope that it will be useful,
017     *  but WITHOUT ANY WARRANTY; without even the implied warranty of
018     *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
019     *  Library General Public License for more details.
020     *
021     *  You should have received a copy of the GNU Library General Public
022     *  License along with this library; if not, write to the Free Software
023     *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
024     *
025     */
026    
027    package org.jvoicexml.interpreter.dialog;
028    import java.util.Collection;
029    
030    import org.jvoicexml.event.error.BadFetchError;
031    import org.jvoicexml.interpreter.Dialog;
032    import org.jvoicexml.interpreter.FormItem;
033    import org.jvoicexml.interpreter.VoiceXmlInterpreterContext;
034    import org.jvoicexml.interpreter.formitem.FormItemFactory;
035    import org.jvoicexml.xml.VoiceXmlNode;
036    import org.jvoicexml.xml.XmlNode;
037    import org.jvoicexml.xml.vxml.AbstractCatchElement;
038    import org.jvoicexml.xml.vxml.Filled;
039    import org.jvoicexml.xml.vxml.Form;
040    import org.w3c.dom.Node;
041    import org.w3c.dom.NodeList;
042    
043    
044    /**
045     * Implementation of a {@link Dialog} for the
046     * <code>&lt;form&gt;</code> tag.
047     *
048     * @see org.jvoicexml.xml.vxml.Form
049     *
050     * @author Dirk Schnelle-Walka
051     * @version $Revision: 2612 $
052     *
053     * @since 0.4
054     */
055    public final class ExecutablePlainForm
056            implements Dialog {
057        /** The encapsulated form. */
058        private Form form;
059    
060        /** Id of this dialog. */
061        private String id;
062    
063        /** Form items of this dialog. */
064        private Collection<FormItem> items;
065    
066        /**
067         * Constructs a new object.
068         */
069        public ExecutablePlainForm() {
070        }
071    
072        /**
073         * {@inheritDoc}
074         */
075        @Override
076        public void setNode(final XmlNode node) throws IllegalArgumentException {
077            if (!(node instanceof Form)) {
078                throw new IllegalArgumentException("'" + node + "' is not a from!");
079            }
080            form = (Form) node;
081            id = DialogIdFactory.getId(form);
082        }
083    
084        /**
085         * {@inheritDoc}
086         */
087        public String getId() {
088            return id;
089        }
090    
091        /**
092         * {@inheritDoc}
093         */
094        public Collection<XmlNode> getChildNodes() {
095            return form.getChildren();
096        }
097    
098        /**
099         * {@inheritDoc}
100         */
101        public Collection<FormItem> getFormItems(
102                final VoiceXmlInterpreterContext context) throws BadFetchError {
103            if (items == null) {
104                items = new java.util.ArrayList<FormItem>();
105    
106                final NodeList children = form.getChildNodes();
107                for (int i = 0; i < children.getLength(); i++) {
108                    final VoiceXmlNode node = (VoiceXmlNode) children.item(i);
109                    final FormItem item =
110                        FormItemFactory.getFormItem(context, node);
111                    if (item != null) {
112                        items.add(item);
113                    }
114                }
115            }
116            return items;
117        }
118    
119        /**
120         * {@inheritDoc}
121         */
122        public Collection<Filled> getFilledElements() {
123            return form.getChildNodes(Filled.class);
124        }
125    
126        /**
127         * {@inheritDoc}
128         */
129        public Collection<AbstractCatchElement> getCatchElements() {
130            if (form == null) {
131                return null;
132            }
133    
134            final Collection<AbstractCatchElement> catches =
135                    new java.util.ArrayList<AbstractCatchElement>();
136            final NodeList children = form.getChildNodes();
137            for (int i = 0; i < children.getLength(); i++) {
138                final Node child = children.item(i);
139                if (child instanceof AbstractCatchElement) {
140                    final AbstractCatchElement catchElement =
141                            (AbstractCatchElement) child;
142                    catches.add(catchElement);
143                }
144            }
145    
146            return catches;
147        }
148    
149        /**
150         * {@inheritDoc}
151         */
152        @Override
153        public Dialog clone() {
154            try {
155                return (Dialog) super.clone();
156            } catch (CloneNotSupportedException e) {
157                return null;
158            }
159        }
160    }