    001    // This file is part of the Attempto Java Packages.
    002    // Copyright 2008, Attempto Group, University of Zurich (see http://attempto.ifi.uzh.ch).
    003    //
    004    // The Attempto Java Packages is free software: you can redistribute it and/or modify it under the
    005    // terms of the GNU Lesser General Public License as published by the Free Software Foundation,
    006    // either version 3 of the License, or (at your option) any later version.
    007    //
    008    // The Attempto Java Packages is distributed in the hope that it will be useful, but WITHOUT ANY
    009    // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
    010    // PURPOSE. See the GNU Lesser General Public License for more details.
    011    //
    012    // You should have received a copy of the GNU Lesser General Public License along with the Attempto
    013    // Java Packages. If not, see http://www.gnu.org/licenses/.
    014
    015    package ch.uzh.ifi.attempto.ape;
    016
    017    import java.util.ArrayList;
    018    import java.util.List;
    019
    020    import org.jdom.Element;
    021
    022    /**
    023     * This class is a container for error and warning messages.
    024     *
    025     * @author Tobias Kuhn
    026     */
    027    public class MessageContainer {
    028
    029            private ArrayList<Message> messages = new ArrayList<Message>();
    030
    031            MessageContainer() {
    032            }
    033
    034            MessageContainer(Element xmlElement) {
    035                    if (xmlElement == null) return;
    036                    for (Object o : xmlElement.getChildren("message")) {
    037                            if (!(o instanceof Element)) continue;
    038                            messages.add(new Message((Element) o));
    039                    }
    040            }
    041
    042            /**
    043             * Returns a list that contains all messages.
    044             *
    045             * @return A list of all messages.
    046             */
    047            public List<Message> getMessages() {
    048                    return new ArrayList<Message>(messages);
    049            }
    050
    051            /**
    052             * Returns a list that contains all error messages.
    053             *
    054             * @return A list of all error messages.
    055             */
    056            public List<Message> getErrorMessages() {
    057                    List<Message> list = new ArrayList<Message>();
    058                    for (Message m : messages) {
    059                            if (m.isError()) list.add(m);
    060                    }
    061                    return list;
    062            }
    063
    064            /**
    065             * Returns a list that contains all warning messages.
    066             *
    067             * @return A list of all warning messages.
    068             */
    069            public List<Message> getWarningMessages() {
    070                    List<Message> list = new ArrayList<Message>();
    071                    for (Message m : messages) {
    072                            if (!m.isError()) list.add(m);
    073                    }
    074                    return list;
    075            }
    076
    077            /**
    078             * Returns a list that contains all messages of a given type.
    079             *
    080             * @param type The type of the messages to be returned.
    081             * @return A list of all messages with the given type.
    082             */
    083            public List<Message> getMessages(String type) {
    084                    List<Message> list = new ArrayList<Message>();
    085                    for (Message m : messages) {
    086                            if (m.getType().equals(type)) list.add(m);
    087                    }
    088                    return list;
    089            }
    090
    091            /**
    092             * Returns a list that contains all error messages of a given type.
    093             *
    094             * @param type The type of the error messages to be returned.
    095             * @return A list of all error messages with the given type.
    096             */
    097            public List<Message> getErrorMessages(String type) {
    098                    List<Message> list = new ArrayList<Message>();
    099                    for (Message m : messages) {
    100                            if (m.isError() && m.getType().equals(type)) list.add(m);
    101                    }
    102                    return list;
    103            }
    104
    105            /**
    106             * Returns a list that contains all warning messages of a given type.
    107             *
    108             * @param type The type of the warning message to be returned.
    109             * @return A list of all warning messages with the given type.
    110             */
    111            public List<Message> getWarningMessages(String type) {
    112                    List<Message> list = new ArrayList<Message>();
    113                    for (Message m : messages) {
    114                            if (!m.isError() && m.getType().equals(type)) list.add(m);
    115                    }
    116                    return list;
    117            }
    118
    119            public String toString() {
    120                    String s = "";
    121                    for (Message m : messages) {
    122                            s += m;
    123                    }
    124                    return s;
    125            }
    126
    127    }