all files / framework/core/js/ JavaProperties.js

93.44% Statements 57/61
87.1% Branches 27/31
100% Functions 5/5
94.83% Lines 55/58
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 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                                  58× 66× 66× 66×   58× 58× 80× 80× 44×   36× 14×   22× 22× 22× 22× 22× 22×   44×           44× 44× 12×   32× 20× 20× 20×         20× 20× 20×       20× 20×     12×     32× 32× 20×     12×                               10×       18×          
/*
Copyright 2008-2010 University of Cambridge
Copyright 2008-2009 University of Toronto
Copyright 2010 Lucendo Development Ltd.
 
Licensed under the Educational Community License (ECL), Version 2.0 or the New
BSD license. You may not use this file except in compliance with one these
Licenses.
 
You may obtain a copy of the ECL 2.0 License and BSD License at
https://github.com/fluid-project/infusion/raw/master/Infusion-LICENSE.txt
*/
 
fluid_3_0_0 = fluid_3_0_0 || {};
 
(function ($, fluid) {
    "use strict";
 
    var unUnicode = /(\\u[\dabcdef]{4}|\\x[\dabcdef]{2})/g;
 
    fluid.unescapeProperties = function (string) {
        string = string.replace(unUnicode, function (match) {
            var code = match.substring(2);
            var parsed = parseInt(code, 16);
            return String.fromCharCode(parsed);
        });
        var pos = 0;
        while (true) {
            var backpos = string.indexOf("\\", pos);
            if (backpos === -1) {
                break;
            }
            if (backpos === string.length - 1) {
                return [string.substring(0, string.length - 1), true];
            }
            var replace = string.charAt(backpos + 1);
            if (replace === "n") { replace = "\n"; }
            Iif (replace === "r") { replace = "\r"; }
            if (replace === "t") { replace = "\t"; }
            string = string.substring(0, backpos) + replace + string.substring(backpos + 2);
            pos = backpos + 1;
        }
        return [string, false];
    };
 
    var breakPos = /[^\\][\s:=]/;
 
    fluid.parseJavaProperties = function (text) {
        // File format described at http://java.sun.com/javase/6/docs/api/java/util/Properties.html#load(java.io.Reader)
        var togo = {};
        text = text.replace(/\r\n/g, "\n");
        text = text.replace(/\r/g, "\n");
        var lines = text.split("\n");
        var contin, key, valueComp, valueRaw, valueEsc;
        for (var i = 0; i < lines.length; ++i) {
            var line = $.trim(lines[i]);
            if (!line || line.charAt(0) === "#" || line.charAt(0) === "!") {
                continue;
            }
            if (!contin) {
                valueComp = "";
                var breakpos = line.search(breakPos);
                Iif (breakpos === -1) {
                    key = line;
                    valueRaw = "";
                }
                else {
                    key = $.trim(line.substring(0, breakpos + 1)); // +1 since first char is escape exclusion
                    valueRaw = $.trim(line.substring(breakpos + 2));
                    if (valueRaw.charAt(0) === ":" || valueRaw.charAt(0) === "=") {
                        valueRaw = $.trim(valueRaw.substring(1));
                    }
                }
 
                key = fluid.unescapeProperties(key)[0];
                valueEsc = fluid.unescapeProperties(valueRaw);
            }
            else {
                valueEsc = fluid.unescapeProperties(line);
            }
 
            contin = valueEsc[1];
            if (!valueEsc[1]) { // this line was not a continuation line - store the value
                togo[key] = valueComp + valueEsc[0];
            }
            else {
                valueComp += valueEsc[0];
            }
        }
        return togo;
    };
 
    /**
    * Expand a message string with respect to a set of arguments, following a basic
    * subset of the Java MessageFormat rules.
    * http://java.sun.com/j2se/1.4.2/docs/api/java/text/MessageFormat.html
    *
    * The message string is expected to contain replacement specifications such
    * as {0}, {1}, {2}, etc.
    * @param messageString {String} The message key to be expanded
    * @param args {String/Array of String} An array of arguments to be substituted into the message.
    * @return The expanded message string.
    */
    fluid.formatMessage = function (messageString, args) {
        if (!args) {
            return messageString;
        }
        Iif (typeof(args) === "string") {
            args = [args];
        }
        for (var i = 0; i < args.length; ++i) {
            messageString = messageString.replace("{" + i + "}", args[i]);
        }
        return messageString;
    };
 
})(jQuery, fluid_3_0_0);