Skip to content
Snippets Groups Projects
AvatarMethod.java 14.5 KiB
Newer Older
/* Copyright or (C) or Copr. GET / ENST, Telecom-Paris, Ludovic Apvrille
 * 
 * ludovic.apvrille AT enst.fr
 * 
 * This software is a computer program whose purpose is to allow the
 * edition of TURTLE analysis, design and deployment diagrams, to
 * allow the generation of RT-LOTOS or Java code from this diagram,
 * and at last to allow the analysis of formal validation traces
 * obtained from external tools, e.g. RTL from LAAS-CNRS and CADP
 * from INRIA Rhone-Alpes.
 * 
 * This software is governed by the CeCILL  license under French law and
 * abiding by the rules of distribution of free software.  You can  use,
 * modify and/ or redistribute the software under the terms of the CeCILL
 * license as circulated by CEA, CNRS and INRIA at the following URL
 * "http://www.cecill.info".
 * 
 * As a counterpart to the access to the source code and  rights to copy,
 * modify and redistribute granted by the license, users are provided only
 * with a limited warranty  and the software's author,  the holder of the
 * economic rights,  and the successive licensors  have only  limited
 * liability.
 * 
 * In this respect, the user's attention is drawn to the risks associated
 * with loading,  using,  modifying and/or developing or reproducing the
 * software by the user in light of its specific status of free software,
 * that may mean  that it is complicated to manipulate,  and  that  also
 * therefore means  that it is reserved for developers  and  experienced
 * professionals having in-depth computer knowledge. Users are therefore
 * encouraged to load and test the software's suitability as regards their
 * requirements in conditions enabling the security of their systems and/or
 * data to be ensured and,  more generally, to use and operate it in the
 * same conditions as regards security.
 * 
 * The fact that you are presently reading this means that you have had
 * knowledge of the CeCILL license and that you accept its terms.
 */





package ui;

import myutil.Conversion;
import myutil.TraceManager;
import translator.RTLOTOSKeyword;
import translator.JKeyword;

import java.util.Vector;

   * Class AvatarMethod
   * Method in Avatar ...
   * Creation: 08/04/2010
   * @version 1.0 08/04/2010
   * @author Ludovic APVRILLE
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
public class AvatarMethod {

    public static final String[] cryptoMethods = {
        "Message aencrypt(Message msg, Key k)",
        "Message adecrypt(Message msg, Key k)",
        "Key pk(Key k)",
        "Message sign(Message msg, Key k)",
        "bool verifySign(Message msg1, Message sig, Key k)",
        "Message cert(Key k, Message msg)",
        "bool verifyCert(Message cert, Key k)",
        "Key getpk(Message cert)",
        "Message sencrypt(Message msg, Key k)",
        "Message sdecrypt(Message msg, Key k)",
        "Key DH(Key pubK, Key privK)",
        "Message hash(Message msg)",
        "Message MAC(Message msg, Key k)",
        "bool verifyMAC(Message msg, Key k, Message macmsg)",
        "Message concat2(Message msg1, Message msg2)",
        "Message concat3(Message msg1, Message msg2, Message msg3)",
        "Message concat4(Message msg1, Message msg2, Message msg3, Message msg4)",
        "get2(Message msg, Message msg1, Message msg2)",
        "get3(Message msg, Message msg1, Message msg2, Message msg3)",
        "get4(Message msg, Message msg1, Message msg2, Message msg3, Message msg4)"};
        
    // Types of parameters
    public final static int NATURAL = 0;
    public final static int BOOLEAN = 1;
    public final static int INTEGER = 2;
    public final static int OTHER = 3;

    protected String id;
    protected String typeIds[];
    protected String types[];

    protected String returnType;

    protected boolean implementationProvided;


    public AvatarMethod(String _id, String _types[], String _typeIds[]) {
        id = _id;
        types = _types;
        typeIds = _typeIds;
        returnType = "";
        implementationProvided = false;
        //TraceManager.addDev("(Cons) Implementation of " + this + " is now set to: " + implementationProvided);
    }

    public AvatarMethod(String _id, String _types[], String _typeIds[], String _returnType) {
        id = _id;
        types = _types;
        typeIds = _typeIds;
        returnType = _returnType;
        implementationProvided = false;
        //TraceManager.addDev("(Cons) Implementation of " + this + " is now set to: " + implementationProvided);
    }

    public boolean isImplementationProvided() {
        return implementationProvided;
    }

    public void setImplementationProvided(boolean _imp) {
        //TraceManager.addDev("Implementation of " + this + " is now set to: " + _imp);
        implementationProvided = _imp;
    }


    // An operation must be of the form: "id(type id0, type id1, ...)"
    // Or 'returntype id(type id0, type id1, ...)'
    // Returns null in case the method is not valid
    public static AvatarMethod isAValidMethod(String _method) {

        //TraceManager.addDev("Is a valid method? " + _method);

        String method, tmp, id;
        String rt = "";

        if (_method == null) {
            return null;
        }

        method = _method.trim();
        // Must replace all "more than one space" by only one space
        method = Conversion.replaceAllString(method, "\t", " ");
        method = Conversion.replaceAllString(method, "  ", " ");
        //TraceManager.addDev("Method=" + method);

        if (method.length() == 0) {
            return null;
        }


        // If has opening parenthesis, remove all spaces before
        int index0 = method.indexOf('(');
        int index1;

        index0 = method.indexOf('(');
        if (index0 != -1) {
            if (index0 == 0) {
                return null;
            } else if (method.charAt(index0-1) == ' ') {
                method = method.substring(0, index0-1) + method.substring(index0, method.length());
            }
        }



        // Check whether there is a return type or not
        int index2 = method.indexOf(' ');
        if (index2 != -1) {
            tmp = method.substring(0, index2);
            // No parenthesis?
            if ((tmp.indexOf('(') == -1) && (tmp.indexOf(')') == -1)) {
                // So, there is a return type!
                rt = tmp.trim();
                method = method.substring(index2+1, method.length()).trim();
                if (!isAValidId(rt, false, false, false)) {
                    TraceManager.addDev("Unvalid type: " + rt);
                    return null;
                }
                //TraceManager.addDev("Found a return type: " + rt);
                //TraceManager.addDev("Now working with method: " + method);
            }
        }

        //TraceManager.addDev("Valid type stage 1");


        index0 = method.indexOf('(');
        index1 = method.indexOf(')');
        // Only one of the two parenthesis
        if ((index0 == -1) && (index1 > -1)) {
            return null;
        }

        if ((index1 == -1) && (index0 > -1)) {
            return null;
        }

        // No parenthesis at all
        if ((index0 == -1) && (index1 == -1)) {
            if (isAValidId(method, true, true, true)) {
                return new AvatarMethod(method, new String [0], new String[0], rt);
            } else {
                return null;
            }
        }

        // Check parenthesis order
        if (index0 > index1) {
            return null;
        }

        // Check that only one parenthesis of each type
        tmp = method.substring(Math.min(index0+1, method.length()), method.length());
        if (tmp.indexOf('(') > -1) {
            return null;
        }
        tmp = method.substring(Math.min(index1+1, method.length()), method.length());
        if (tmp.indexOf(')') > -1) {
            return null;
        }

        // And so: parenthesis are in the right order, and are used only one for each


        //TraceManager.addDev("Checking for an id before parenthesis index0=" + index0 + " method=" + method);
        // Before parenthesis -> id
        tmp = method.substring(0, index0).trim();
        //TraceManager.addDev("Checking for an id before parenthesis; tmp=" + tmp);
        if (!isAValidId(tmp, true, true, true)) {
            return null;
        }
        id = tmp;

        // Between parenthesis: parameters of the form: String space String comma
        // We replace double space by spaces and then spaces by commas
        tmp = method.substring(index0+1, index1).trim();

        // no parameter?
        if (tmp.length() == 0) {
            return new AvatarMethod(id, new String [0], new String[0], rt);
        }

        // Has parameters...
        tmp = Conversion.replaceAllString(tmp, "  ", " ");
        tmp = Conversion.replaceAllString(tmp, " ,", ",");
        tmp = Conversion.replaceAllString(tmp, ", ", ",");
        tmp = Conversion.replaceAllChar(tmp, ' ', ",");

        //TraceManager.addDev("tmp=" + tmp);

        String splitted[] = tmp.split(",");
        int size = splitted.length/2;
        // TraceManager.addDev("Nb of parameters=" + size);
        String types[] = new String[size];
        String typeIds[] = new String[size];
        boolean b0, b1;
        int i;

        //TraceManager.addDev("splitted");
        //for(i=0; i<splitted.length; i++) {
        //      TraceManager.addDev("splitted[" + i + "]: " + splitted[i]);
        //}

        try {
            for(i=0; i<splitted.length; i = i + 2){
                if (splitted[i].length() == 0) {
                    return null;
                }
                if (splitted[i+1].length() == 0) {
                    return null;
                }
                if (!isAValidId(splitted[i], false, false, false)) {
                    TraceManager.addDev("Unvalid type: " + splitted[i]);
                    return null;
                }
                if (!isAValidId(splitted[i+1], true, true, true)) {
                    TraceManager.addDev("Unvalid id of parameter " + splitted[i+1]);
                    return null;
                }
                //TraceManager.addDev("Adding parameter: " + splitted[i] + " " + splitted[i+1]);
                types[i/2] = splitted[i];
                typeIds[i/2] = splitted[i+1];
            }
        } catch (Exception e) {
            TraceManager.addDev("AvatarMethod Exception:" + e.getMessage());
            return null;
        }

        //TraceManager.addDev("Returning method");

        return new AvatarMethod(id, types, typeIds, rt);
    }

    public String getId() { return id;}
    public String[] getTypes(){ return types;}
    public String[] getTypeIds(){ return typeIds;}
    public String getReturnType() { return returnType;}

    public String getType(int _index) {
        if ((_index <0) || (_index>=types.length)) {
            return null;
        }
        return types[_index];
    }

    public String getTypeId(int _index) {
        if ((_index <0) || (_index>=typeIds.length)) {
            return null;
        }
        return typeIds[_index];
    }


    public static boolean isAValidId(String id, boolean checkKeyword, boolean checkJavaKeyword, boolean checkTypes) {
        // test whether _id is a word

        if ((id == null) || (id.length() < 1)) {
            return false;
        }

        String lowerid = id.toLowerCase();
        boolean b1, b2, b3, b4, b5;
        b1 = (id.substring(0,1)).matches("[a-zA-Z]");
        b2 = id.matches("\\w*");
        if (checkKeyword) {
            b3 = !RTLOTOSKeyword.isAKeyword(lowerid);
        } else {
            b3 = true;
        }
        if (checkJavaKeyword) {
            b5 = !JKeyword.isAKeyword(lowerid);
        } else {
            b5 = true;
        }

        if (checkTypes) {
            b4 = !((lowerid.equals(getStringType(0).toLowerCase())) || (lowerid.equals(getStringType(1).toLowerCase())) || (lowerid.equals(getStringType(2).toLowerCase())) || (lowerid.equals(getStringType(3).toLowerCase())) || (lowerid.equals(getStringType(4).toLowerCase())));
        } else {
            b4 = true;
        }

        return (b1 && b2 && b3 && b4 && b5);
    }

    public static boolean notIn(String s, Vector forbidden) {
        if (forbidden == null) {
            return true;
        }

        AvatarMethod am;

        for(int i= 0; i<forbidden.size(); i++) {
            am = (AvatarMethod)(forbidden.elementAt(i));
            if (s.compareTo(am.getId()) ==0) {
                return false;
            }
        }

        return true;
    }

    public static int getType(String s) {
        if (s.equals("nat")) {
            return      NATURAL;
        } else if (s.equals("bool")) {
            return      BOOLEAN;
        } else if (s.equals("int")) {
            return      INTEGER;
        } else if (s.equals("Integer")) {
            return      INTEGER;
        }
        return OTHER;
    }

    public static String getStringType(int type) {
        switch(type) {
        case NATURAL:
            return "nat";
        case BOOLEAN:
            return "bool";
        case INTEGER:
            return "int";
        }
        return "";
    }

    public String toString() {
        int cpt = 0;

        String method = "";
        if (returnType.length() > 0) {
            method += returnType + " ";
        }

        method += id + "(";
        for (int i=0; i<types.length; i++) {
            method += types[i] + " " + typeIds[i];
            if (i<(types.length - 1)) {
                method += ", ";
            }
        }
        method += ")";
        return method;
    }

    public String toSaveString() {
        String ret = "";
        if (implementationProvided) {
            //TraceManager.addDev("Implementation provided for " + toString());
            ret += "$";
        }
        return ret + toString();
    }

    // Comparison on id only
    public boolean equals(Object o) {
        if (!(o instanceof AvatarMethod)) {
            return false;
        }

        AvatarMethod am = (AvatarMethod)o;
        return getId().equals(am.getId());

    }


    public String getUseDescription() {
        String s = getId() + "(";
        for(int i=0; i<typeIds.length; i++) {
            s += typeIds[i];
            if (i < (typeIds.length - 1)) {
                s += ", ";
            }
        }
        s += ")";
        return s;
    }

    // Comparison on all fields
    /*public int compareTo(Object o){
      if (!(o instanceof AvatarMethod)) {
      return 1;
      }

      AvatarMethod am = (AvatarMethod)o;
      if (!(getId().equals(am.getId()))) {
      return 1;
      }


      return 0;

      }*/

    public AvatarMethod makeClone() {
        AvatarMethod am = isAValidMethod(toString());
        am.setImplementationProvided(isImplementationProvided());
        return am;
    }
}