summaryrefslogtreecommitdiff
path: root/js/src/Ice/IdentityUtil.js
blob: b6652d0d7fc27f98209f3554b7e29b4d3f4df154 (plain)
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
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
// **********************************************************************
//
// Copyright (c) 2003-2017 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************

const Ice = require("../Ice/ModuleRegistry").Ice;
Ice._ModuleRegistry.require(module, [ "../Ice/StringUtil", "../Ice/Identity", "../Ice/LocalException"]);

const StringUtil = Ice.StringUtil;
const Identity = Ice.Identity;
const IdentityParseException = Ice.IdentityParseException;

/**
* Converts a string to an object identity.
*
* @param s The string to convert.
*
* @return The converted object identity.
**/
Ice.stringToIdentity = function(s)
{
    const ident = new Identity();

    //
    // Find unescaped separator; note that the string may contain an escaped
    // backslash before the separator.
    //
    let slash = -1;
    let pos = 0;
    while((pos = s.indexOf('/', pos)) !== -1)
    {
        let escapes = 0;
        while(pos - escapes > 0 && s.charAt(pos - escapes - 1) == '\\')
        {
            escapes++;
        }
            
        //
        // We ignore escaped escapes
        //
        if(escapes % 2 === 0)
        {
            if(slash == -1)
            {
                slash = pos;
            }
            else
            {
                //
                // Extra unescaped slash found.
                //
                throw new IdentityParseException(`unescaped backslash in identity \`${s}'`);
            }
        }
        pos++;
    }

    if(slash == -1)
    {
        ident.category = "";
        try
        {
            ident.name = StringUtil.unescapeString(s, 0, s.length, "/");
        }
        catch(e)
        {
            throw new IdentityParseException(`invalid identity name \`${s}': ${e.toString()}`);
        }
    }
    else
    {
        try
        {
            ident.category = StringUtil.unescapeString(s, 0, slash, "/");
        }
        catch(e)
        {
            throw new IdentityParseException(`invalid category in identity \`${s}': ${e.toString()}`);
        }
        if(slash + 1 < s.length)
        {
            try
            {
                ident.name = StringUtil.unescapeString(s, slash + 1, s.length, "/");
            }
            catch(e)
            {
                throw new IdentityParseException(`invalid name in identity \`${s}': ${e.toString()}`);
            }
        }
        else
        {
            ident.name = "";
        }
    }

    return ident;
};

/**
* Converts an object identity to a string.
*
* @param ident The object identity to convert.
*
* @param toStringMode Specifies if and how non-printable ASCII characters are escaped in the result.
*
* @return The string representation of the object identity.
**/
Ice.identityToString = function(ident, toStringMode = Ice.ToStringMode.Unicode)
{
    if(ident.category === null || ident.category.length === 0)
    {
        return StringUtil.escapeString(ident.name, "/", toStringMode);
    }
    else
    {
        return StringUtil.escapeString(ident.category, "/", toStringMode) + '/' + StringUtil.escapeString(ident.name, "/", toStringMode);
    }
};

/**
* Compares the object identities of two proxies.
*
* @param lhs A proxy.
* @param rhs A proxy.
* @return -1 if the identity in <code>lhs</code> compares
* less than the identity in <code>rhs</code>; 0 if the identities
* compare equal; 1, otherwise.
*
* @see ProxyIdentityKey
* @see ProxyIdentityAndFacetKey
* @see ProxyIdentityAndFacetCompare
**/
Ice.proxyIdentityCompare = function(lhs, rhs)
{
    if(lhs === rhs)
    {
        return 0;
    }
    else if(lhs === null && rhs !== null)
    {
        return -1;
    }
    else if(lhs !== null && rhs === null)
    {
        return 1;
    }
    else
    {
        const lhsIdentity = lhs.ice_getIdentity();
        const rhsIdentity = rhs.ice_getIdentity();
        const n = lhsIdentity.name.localeCompare(rhsIdentity.name);
        return (n !== 0) ? n : lhsIdentity.category.localeCompare(rhsIdentity.category);
    }
};

/**
* Compares the object identities and facets of two proxies.
*
* @param lhs A proxy.
* @param rhs A proxy.
* @return -1 if the identity and facet in <code>lhs</code> compare
* less than the identity and facet in <code>rhs</code>; 0 if the identities
* and facets compare equal; 1, otherwise.
*
* @see ProxyIdentityAndFacetKey
* @see ProxyIdentityKey
* @see ProxyIdentityCompare
**/
Ice.proxyIdentityAndFacetCompare = function(lhs, rhs)
{
    if(lhs === rhs)
    {
        return 0;
    }
    else if(lhs === null && rhs !== null)
    {
        return -1;
    }
    else if(lhs !== null && rhs === null)
    {
        return 1;
    }
    else
    {
        const lhsIdentity = lhs.ice_getIdentity();
        const rhsIdentity = rhs.ice_getIdentity();
        let n = lhsIdentity.name.localeCompare(rhsIdentity.name);
        if(n !== 0)
        {
            return n;
        }
        n = lhsIdentity.category.localeCompare(rhsIdentity.category);
        if(n !== 0)
        {
            return n;
        }

        const lhsFacet = lhs.ice_getFacet();
        const rhsFacet = rhs.ice_getFacet();
        if(lhsFacet === null && rhsFacet === null)
        {
            return 0;
        }
        else if(lhsFacet === null)
        {
            return -1;
        }
        else if(rhsFacet === null)
        {
            return 1;
        }
        return lhsFacet.localeCompare(rhsFacet);
    }
};

module.exports.Ice = Ice;