6
6
7
7
namespace JsonLD . Core
8
8
{
9
+ #if ! PORTABLE
9
10
internal class NormalizeUtils
10
11
{
11
12
private readonly UniqueNamer namer ;
12
13
13
- private readonly JObject bnodes ;
14
+ private readonly IDictionary < string , IDictionary < string , object > > bnodes ;
14
15
15
- private readonly JArray quads ;
16
+ private readonly IList < RDFDataset . Quad > quads ;
16
17
17
18
private readonly JsonLdOptions options ;
18
19
19
- public NormalizeUtils ( JArray quads , JObject bnodes , UniqueNamer
20
+ public NormalizeUtils ( IList < RDFDataset . Quad > quads , IDictionary < string , IDictionary < string , object > > bnodes , UniqueNamer
20
21
namer , JsonLdOptions options )
21
22
{
22
23
this . options = options ;
@@ -27,7 +28,7 @@ public NormalizeUtils(JArray quads, JObject bnodes, UniqueNamer
27
28
28
29
// generates unique and duplicate hashes for bnodes
29
30
/// <exception cref="JsonLD.Core.JsonLdError"></exception>
30
- public virtual JToken HashBlankNodes ( IEnumerable < string > unnamed_ )
31
+ public virtual object HashBlankNodes ( IEnumerable < string > unnamed_ )
31
32
{
32
33
IList < string > unnamed = new List < string > ( unnamed_ ) ;
33
34
IList < string > nextUnnamed = new List < string > ( ) ;
@@ -91,22 +92,21 @@ public virtual JToken HashBlankNodes(IEnumerable<string> unnamed_)
91
92
// update bnode names in each quad and serialize
92
93
for ( int cai = 0 ; cai < quads . Count ; ++ cai )
93
94
{
94
- JObject quad = ( JObject ) quads [ cai ] ;
95
+ RDFDataset . Quad quad = quads [ cai ] ;
95
96
foreach ( string attr in new string [ ] { "subject" , "object" , "name" } )
96
97
{
97
98
if ( quad . ContainsKey ( attr ) )
98
99
{
99
- JObject qa = ( JObject ) quad [ attr ] ;
100
- if ( qa != null && qa [ "type" ] . SafeCompare ( "blank node" ) && ( ( string ) qa [ "value" ] ) . IndexOf
100
+ IDictionary < string , object > qa = ( IDictionary < string , object > ) quad [ attr ] ;
101
+ if ( qa != null && ( string ) qa [ "type" ] == "blank node" && ( ( string ) qa [ "value" ] ) . IndexOf
101
102
( "_:c14n" ) != 0 )
102
103
{
103
- qa [ "value" ] = namer . GetName ( ( string ) qa [ ( "value" ) ] ) ;
104
+ qa [ "value" ] = namer . GetName ( ( string ) qa [ "value" ] ) ;
104
105
}
105
106
}
106
107
}
107
- normalized . Add ( RDFDatasetUtils . ToNQuad ( ( RDFDataset . Quad ) quad , quad . ContainsKey ( "name"
108
- ) && ! quad [ "name" ] . IsNull ( ) ? ( string ) ( ( JObject ) quad [ "name" ] ) [
109
- "value" ] : null ) ) ;
108
+ normalized . Add ( RDFDatasetUtils . ToNQuad ( quad , quad . ContainsKey ( "name"
109
+ ) && ! ( quad [ "name" ] == null ) ? ( string ) ( ( IDictionary < string , object > ) ( ( IDictionary < string , object > ) quad ) [ "name" ] ) [ "value" ] : null ) ) ;
110
110
}
111
111
// sort normalized output
112
112
normalized . SortInPlace ( ) ;
@@ -239,22 +239,22 @@ private class HashResult
239
239
/// <param name="namer">the canonical bnode namer.</param>
240
240
/// <param name="pathNamer">the namer used to assign names to adjacent bnodes.</param>
241
241
/// <param name="callback">(err, result) called once the operation completes.</param>
242
- private static NormalizeUtils . HashResult HashPaths ( string id , JObject bnodes , UniqueNamer namer , UniqueNamer pathNamer )
242
+ private static NormalizeUtils . HashResult HashPaths ( string id , IDictionary < string , IDictionary < string , object > > bnodes , UniqueNamer namer , UniqueNamer pathNamer )
243
243
{
244
244
try
245
245
{
246
246
// create SHA-1 digest
247
247
MessageDigest md = MessageDigest . GetInstance ( "SHA-1" ) ;
248
248
JObject groups = new JObject ( ) ;
249
249
IList < string > groupHashes ;
250
- JArray quads = ( JArray ) ( ( JObject ) bnodes [ id ] ) [ "quads" ] ;
250
+ IList < RDFDataset . Quad > quads = ( IList < RDFDataset . Quad > ) bnodes [ id ] [ "quads" ] ;
251
251
for ( int hpi = 0 ; ; hpi ++ )
252
252
{
253
253
if ( hpi == quads . Count )
254
254
{
255
255
// done , hash groups
256
256
groupHashes = new List < string > ( groups . GetKeys ( ) ) ;
257
- groupHashes . SortInPlace ( ) ;
257
+ ( ( List < string > ) groupHashes ) . Sort ( StringComparer . CurrentCultureIgnoreCase ) ;
258
258
for ( int hgi = 0 ; ; hgi ++ )
259
259
{
260
260
if ( hgi == groupHashes . Count )
@@ -381,8 +381,8 @@ private static NormalizeUtils.HashResult HashPaths(string id, JObject bnodes, Un
381
381
}
382
382
}
383
383
// get adjacent bnode
384
- JObject quad = ( JObject ) quads [ hpi ] ;
385
- string bnode_2 = GetAdjacentBlankNodeName ( ( JObject ) quad [ "subject"
384
+ IDictionary < string , object > quad = ( IDictionary < string , object > ) quads [ hpi ] ;
385
+ string bnode_2 = GetAdjacentBlankNodeName ( ( IDictionary < string , object > ) quad [ "subject"
386
386
] , id ) ;
387
387
string direction = null ;
388
388
if ( bnode_2 != null )
@@ -392,7 +392,7 @@ private static NormalizeUtils.HashResult HashPaths(string id, JObject bnodes, Un
392
392
}
393
393
else
394
394
{
395
- bnode_2 = GetAdjacentBlankNodeName ( ( JObject ) quad [ "object" ] , id
395
+ bnode_2 = GetAdjacentBlankNodeName ( ( IDictionary < string , object > ) quad [ "object" ] , id
396
396
) ;
397
397
if ( bnode_2 != null )
398
398
{
@@ -424,7 +424,7 @@ private static NormalizeUtils.HashResult HashPaths(string id, JObject bnodes, Un
424
424
// String toHash = direction + (String) ((Map<String,
425
425
// Object>) quad.get("predicate")).get("value") + name;
426
426
md1 . Update ( JsonLD . JavaCompat . GetBytesForString ( direction , "UTF-8" ) ) ;
427
- md1 . Update ( JsonLD . JavaCompat . GetBytesForString ( ( ( string ) ( ( JObject ) quad [ "predicate" ] ) [ "value" ] ) , "UTF-8" ) ) ;
427
+ md1 . Update ( JsonLD . JavaCompat . GetBytesForString ( ( ( string ) ( ( IDictionary < string , object > ) quad [ "predicate" ] ) [ "value" ] ) , "UTF-8" ) ) ;
428
428
md1 . Update ( JsonLD . JavaCompat . GetBytesForString ( name , "UTF-8" ) ) ;
429
429
string groupHash = EncodeHex ( md1 . Digest ( ) ) ;
430
430
if ( groups . ContainsKey ( groupHash ) )
@@ -455,28 +455,27 @@ private static NormalizeUtils.HashResult HashPaths(string id, JObject bnodes, Un
455
455
/// <param name="bnodes">the mapping of bnodes to quads.</param>
456
456
/// <param name="namer">the canonical bnode namer.</param>
457
457
/// <returns>the new hash.</returns>
458
- private static string HashQuads ( string id , JObject bnodes , UniqueNamer
458
+ private static string HashQuads ( string id , IDictionary < string , IDictionary < string , object > > bnodes , UniqueNamer
459
459
namer )
460
460
{
461
461
// return cached hash
462
- if ( ( ( JObject ) bnodes [ id ] ) . ContainsKey ( "hash" ) )
462
+ if ( bnodes [ id ] . ContainsKey ( "hash" ) )
463
463
{
464
- return ( string ) ( ( JObject ) bnodes [ id ] ) [ "hash" ] ;
464
+ return ( string ) bnodes [ id ] [ "hash" ] ;
465
465
}
466
466
// serialize all of bnode's quads
467
- JArray quads = ( JArray ) ( ( JObject ) bnodes [ id ] ) [ "quads" ] ;
467
+ IList < RDFDataset . Quad > quads = ( IList < RDFDataset . Quad > ) bnodes [ id ] [ "quads" ] ;
468
468
IList < string > nquads = new List < string > ( ) ;
469
469
for ( int i = 0 ; i < quads . Count ; ++ i )
470
470
{
471
- nquads . Add ( RDFDatasetUtils . ToNQuad ( ( RDFDataset . Quad ) quads [ i ] , quads [ i ] [ "name" ] !=
472
- null ? ( string ) ( ( JObject ) quads [ i ] [ "name" ] ) [ "value" ] : null ,
473
- id ) ) ;
471
+ object name ;
472
+ nquads . Add ( RDFDatasetUtils . ToNQuad ( ( RDFDataset . Quad ) quads [ i ] , quads [ i ] . TryGetValue ( "name" , out name ) ? ( string ) ( ( IDictionary < string , object > ) name ) [ "value" ] : null , id ) ) ;
474
473
}
475
474
// sort serialized quads
476
475
nquads . SortInPlace ( ) ;
477
476
// return hashed quads
478
477
string hash = Sha1hash ( nquads ) ;
479
- ( ( JObject ) bnodes [ id ] ) [ "hash" ] = hash ;
478
+ ( ( IDictionary < string , object > ) bnodes [ id ] ) [ "hash" ] = hash ;
480
479
return hash ;
481
480
}
482
481
@@ -511,7 +510,7 @@ private static string EncodeHex(byte[] data)
511
510
string rval = string . Empty ;
512
511
foreach ( byte b in data )
513
512
{
514
- rval += string . Format ( "%02x" , b ) ;
513
+ rval += b . ToString ( "x2" ) ;
515
514
}
516
515
return rval ;
517
516
}
@@ -528,10 +527,9 @@ private static string EncodeHex(byte[] data)
528
527
/// <param name="node">the RDF quad node.</param>
529
528
/// <param name="id">the ID of the blank node to look next to.</param>
530
529
/// <returns>the adjacent blank node name or null if none was found.</returns>
531
- private static string GetAdjacentBlankNodeName ( JObject node ,
532
- string id )
530
+ private static string GetAdjacentBlankNodeName ( IDictionary < string , object > node , string id )
533
531
{
534
- return node [ "type" ] . SafeCompare ( "blank node" ) && ( ! node . ContainsKey ( "value" ) || ! node [ "value" ] . SafeCompare ( id ) ) ? ( string ) node [ "value" ] : null ;
532
+ return ( string ) node [ "type" ] == "blank node" && ( ! node . ContainsKey ( "value" ) || ( string ) node [ "value" ] == id ) ? ( string ) node [ "value" ] : null ;
535
533
}
536
534
537
535
private class Permutator
@@ -614,4 +612,5 @@ public virtual JArray Next()
614
612
}
615
613
}
616
614
}
615
+ #endif
617
616
}
0 commit comments