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 org.jdom.Element;
018    
019    /**
020     * This class represents a warning or error message of the ACE parser.
021     * 
022     * @author Tobias Kuhn
023     * @author Kaarel Kaljurand
024     */
025    public class Message {
026    
027            private boolean isError;
028            private String type, value, repair;
029            private Integer sentenceId, tokenId;
030    
031            Message(Element xmlElement) {
032                    isError = (xmlElement.getAttribute("importance").getValue().equals("error"));
033                    type = xmlElement.getAttribute("type").getValue();
034                    value = xmlElement.getAttribute("value").getValue();
035                    repair = xmlElement.getAttribute("repair").getValue();
036                    try {
037                            sentenceId = new Integer(xmlElement.getAttribute("sentence").getValue());
038                    } catch (NumberFormatException ex) {}
039                    try {
040                            tokenId = new Integer(xmlElement.getAttribute("token").getValue());
041                    } catch (NumberFormatException ex) {}
042            }
043    
044            /**
045             * Returns true if this message is an error message, or false if it is only a warning
046             * message.
047             * 
048             * @return true if this is an error message.
049             */
050            public boolean isError() {
051                    return isError;
052            }
053    
054            /**
055             * Returns the type of the error/warning message.
056             * 
057             * @return The type of the message.
058             */
059            public String getType() {
060                    return type;
061            }
062    
063            /**
064             * Returns the sentence number that locates the reason for the error/warning message.
065             * In some cases, this can be null.
066             * 
067             * @return The sentence number.
068             */
069            public Integer getSentenceId() {
070                    return sentenceId;
071            }
072    
073            /**
074             * Returns the token number that locates the reason for the error/warning message.
075             * In some cases, this can be null.
076             * 
077             * @return The token number.
078             */
079            public Integer getTokenId() {
080                    return tokenId;
081            }
082    
083            /**
084             * Returns the value of the error/warning message. This is for example the word that
085             * caused the error or warning.
086             * 
087             * @return The value of the message.
088             */
089            public String getValue() {
090                    return value;
091            }
092    
093            /**
094             * Returns an suggestion how to react on the error or warning.
095             * 
096             * @return Suggestion how to react on the error/warning.
097             */
098            public String getRepair() {
099                    return repair;
100            }
101    
102            /**
103             * Returns a pretty-printed error/warning message.
104             */
105            public String toString() {
106                    return (isError ? "ERROR: [" : "WARNING: [") + type + "] at " + (sentenceId == null ? "?" : sentenceId) + "-" + (tokenId == null ? "?" : tokenId) + ": '" + value + "'. " + repair;
107            }
108    }