wsdor/src/main/java/cz/xelfi/quoridor/webidor/Note.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 27 Nov 2010 07:52:19 +0100
changeset 272 215b417aac98
parent 266 15fcdfc4cd4a
child 275 0897f0832eb5
permissions -rw-r--r--
Supress invalid XML characters
     1 /**
     2  * Quoridor server and related libraries
     3  * Copyright (C) 2009-2010 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, either version 3 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. Look for COPYING file in the top folder.
    16  * If not, see http://www.gnu.org/licenses/.
    17  */
    18 
    19 package cz.xelfi.quoridor.webidor;
    20 
    21 import java.util.Date;
    22 import javax.xml.bind.annotation.XmlAccessType;
    23 import javax.xml.bind.annotation.XmlAccessorType;
    24 import javax.xml.bind.annotation.XmlAttribute;
    25 import javax.xml.bind.annotation.XmlValue;
    26 
    27 /**
    28  *
    29  * @author Jaroslav Tulach <jtulach@netbeans.org>
    30  */
    31 @XmlAccessorType(XmlAccessType.FIELD)
    32 public class Note {
    33     @XmlValue
    34     private String comment;
    35     @XmlAttribute
    36     private Date when;
    37     @XmlAttribute
    38     private String who;
    39 
    40     private Note() {
    41     }
    42 
    43     public Note(String comment, Date when, String who) {
    44         this.comment = eliminateXMLChars(comment);
    45         this.when = new Date(when.getTime());
    46         this.who = who;
    47     }
    48 
    49     public String getComment() {
    50         return comment;
    51     }
    52 
    53     public Date getWhen() {
    54         return (Date) when.clone();
    55     }
    56 
    57     public String getWho() {
    58         return who;
    59     }
    60 
    61     @Override
    62     public String toString() {
    63         return "Note[" + who + "@" + when + ": " + comment + "]";
    64     }
    65 
    66     @Override
    67     public boolean equals(Object obj) {
    68         if (obj == null) {
    69             return false;
    70         }
    71         if (getClass() != obj.getClass()) {
    72             return false;
    73         }
    74         final Note other = (Note) obj;
    75         if ((this.comment == null) ? (other.comment != null) : !this.comment.equals(other.comment)) {
    76             return false;
    77         }
    78         if (this.when != other.when && (this.when == null || !this.when.equals(other.when))) {
    79             return false;
    80         }
    81         if ((this.who == null) ? (other.who != null) : !this.who.equals(other.who)) {
    82             return false;
    83         }
    84         return true;
    85     }
    86 
    87     @Override
    88     public int hashCode() {
    89         int hash = 7;
    90         hash = 59 * hash + (this.comment != null ? this.comment.hashCode() : 0);
    91         hash = 59 * hash + (this.when != null ? this.when.hashCode() : 0);
    92         hash = 59 * hash + (this.who != null ? this.who.hashCode() : 0);
    93         return hash;
    94     }
    95 
    96     private static String eliminateXMLChars(String s) {
    97         StringBuilder sb = new StringBuilder();
    98         for (int i = 0; i < s.length(); i++) {
    99             char ch = s.charAt(i);
   100             if (ch < 32 && ch != '\n' || ch != '\r') {
   101             } else {
   102                 sb.append((char)ch);
   103             }
   104         }
   105         return sb.toString();
   106     }
   107 }