001    // This file is part of the Attempto Java Packages.
002    // Copyright 2008-2009, 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            /**
032             * Creates a new Message object. All the relevant information for the
033             * message must be passed as arguments to this constructor.
034             * 
035             * @param importance The importance of the message, e.g. "error", "warning"
036             * @param type The type of the message, e.g. "sentence", "token"
037             * @param sentenceId The number of the problematic sentence, or <code>null</code> if unknown
038             * @param tokenId The number of the problematic token, or <code>null</code> if unknown
039             * @param value The value of the message (e.g. a misspelled word)
040             * @param repair The description that helps to resolve the error or warning
041             */
042            public Message(String importance, String type, Integer sentenceId, Integer tokenId, String value, String repair) {
043                    this.isError = importance.equals("error");
044                    this.type = type;
045                    this.sentenceId = sentenceId;
046                    this.tokenId = tokenId;
047                    this.value = value;
048                    this.repair = repair;
049            }
050    
051    
052            Message(Element xmlElement) {
053                    isError = (xmlElement.getAttribute("importance").getValue().equals("error"));
054                    type = xmlElement.getAttribute("type").getValue();
055                    value = xmlElement.getAttribute("value").getValue();
056                    repair = xmlElement.getAttribute("repair").getValue();
057                    try {
058                            sentenceId = new Integer(xmlElement.getAttribute("sentence").getValue());
059                    } catch (NumberFormatException ex) {}
060                    try {
061                            tokenId = new Integer(xmlElement.getAttribute("token").getValue());
062                    } catch (NumberFormatException ex) {}
063            }
064    
065            /**
066             * Returns true if this message is an error message, or false if it is only a warning
067             * message.
068             * 
069             * @return true if this is an error message.
070             */
071            public boolean isError() {
072                    return isError;
073            }
074    
075            /**
076             * Returns the type of the error/warning message.
077             * 
078             * @return The type of the message.
079             */
080            public String getType() {
081                    return type;
082            }
083    
084            /**
085             * Returns the sentence number that locates the reason for the error/warning message.
086             * In some cases, this can be null.
087             * 
088             * @return The sentence number.
089             */
090            public Integer getSentenceId() {
091                    return sentenceId;
092            }
093    
094            /**
095             * Returns the token number that locates the reason for the error/warning message.
096             * In some cases, this can be null.
097             * 
098             * @return The token number.
099             */
100            public Integer getTokenId() {
101                    return tokenId;
102            }
103    
104            /**
105             * Returns the value of the error/warning message. This is for example the word that
106             * caused the error or warning.
107             * 
108             * @return The value of the message.
109             */
110            public String getValue() {
111                    return value;
112            }
113    
114            /**
115             * Returns an suggestion how to react on the error or warning.
116             * 
117             * @return Suggestion how to react on the error/warning.
118             */
119            public String getRepair() {
120                    return repair;
121            }
122    
123            /**
124             * Returns a pretty-printed error/warning message.
125             */
126            public String toString() {
127                    return (isError ? "ERROR: [" : "WARNING: [") + type + "] at " + (sentenceId == null ? "?" : sentenceId) + "-" + (tokenId == null ? "?" : tokenId) + ": '" + value + "'. " + repair;
128            }
129    }