66
77namespace JsonLD . Core
88{
9+ #if ! PORTABLE
910 internal class NormalizeUtils
1011 {
1112 private readonly UniqueNamer namer ;
1213
13- private readonly JObject bnodes ;
14+ private readonly IDictionary < string , IDictionary < string , object > > bnodes ;
1415
15- private readonly JArray quads ;
16+ private readonly IList < RDFDataset . Quad > quads ;
1617
1718 private readonly JsonLdOptions options ;
1819
19- public NormalizeUtils ( JArray quads , JObject bnodes , UniqueNamer
20+ public NormalizeUtils ( IList < RDFDataset . Quad > quads , IDictionary < string , IDictionary < string , object > > bnodes , UniqueNamer
2021 namer , JsonLdOptions options )
2122 {
2223 this . options = options ;
@@ -27,7 +28,7 @@ public NormalizeUtils(JArray quads, JObject bnodes, UniqueNamer
2728
2829 // generates unique and duplicate hashes for bnodes
2930 /// <exception cref="JsonLD.Core.JsonLdError"></exception>
30- public virtual JToken HashBlankNodes ( IEnumerable < string > unnamed_ )
31+ public virtual object HashBlankNodes ( IEnumerable < string > unnamed_ )
3132 {
3233 IList < string > unnamed = new List < string > ( unnamed_ ) ;
3334 IList < string > nextUnnamed = new List < string > ( ) ;
@@ -91,22 +92,21 @@ public virtual JToken HashBlankNodes(IEnumerable<string> unnamed_)
9192 // update bnode names in each quad and serialize
9293 for ( int cai = 0 ; cai < quads . Count ; ++ cai )
9394 {
94- JObject quad = ( JObject ) quads [ cai ] ;
95+ RDFDataset . Quad quad = quads [ cai ] ;
9596 foreach ( string attr in new string [ ] { "subject" , "object" , "name" } )
9697 {
9798 if ( quad . ContainsKey ( attr ) )
9899 {
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
101102 ( "_:c14n" ) != 0 )
102103 {
103- qa [ "value" ] = namer . GetName ( ( string ) qa [ ( "value" ) ] ) ;
104+ qa [ "value" ] = namer . GetName ( ( string ) qa [ "value" ] ) ;
104105 }
105106 }
106107 }
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 ) ) ;
110110 }
111111 // sort normalized output
112112 normalized . SortInPlace ( ) ;
@@ -239,22 +239,22 @@ private class HashResult
239239 /// <param name="namer">the canonical bnode namer.</param>
240240 /// <param name="pathNamer">the namer used to assign names to adjacent bnodes.</param>
241241 /// <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 )
243243 {
244244 try
245245 {
246246 // create SHA-1 digest
247247 MessageDigest md = MessageDigest . GetInstance ( "SHA-1" ) ;
248248 JObject groups = new JObject ( ) ;
249249 IList < string > groupHashes ;
250- JArray quads = ( JArray ) ( ( JObject ) bnodes [ id ] ) [ "quads" ] ;
250+ IList < RDFDataset . Quad > quads = ( IList < RDFDataset . Quad > ) bnodes [ id ] [ "quads" ] ;
251251 for ( int hpi = 0 ; ; hpi ++ )
252252 {
253253 if ( hpi == quads . Count )
254254 {
255255 // done , hash groups
256256 groupHashes = new List < string > ( groups . GetKeys ( ) ) ;
257- groupHashes . SortInPlace ( ) ;
257+ ( ( List < string > ) groupHashes ) . Sort ( StringComparer . CurrentCultureIgnoreCase ) ;
258258 for ( int hgi = 0 ; ; hgi ++ )
259259 {
260260 if ( hgi == groupHashes . Count )
@@ -381,8 +381,8 @@ private static NormalizeUtils.HashResult HashPaths(string id, JObject bnodes, Un
381381 }
382382 }
383383 // 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"
386386 ] , id ) ;
387387 string direction = null ;
388388 if ( bnode_2 != null )
@@ -392,7 +392,7 @@ private static NormalizeUtils.HashResult HashPaths(string id, JObject bnodes, Un
392392 }
393393 else
394394 {
395- bnode_2 = GetAdjacentBlankNodeName ( ( JObject ) quad [ "object" ] , id
395+ bnode_2 = GetAdjacentBlankNodeName ( ( IDictionary < string , object > ) quad [ "object" ] , id
396396 ) ;
397397 if ( bnode_2 != null )
398398 {
@@ -424,7 +424,7 @@ private static NormalizeUtils.HashResult HashPaths(string id, JObject bnodes, Un
424424 // String toHash = direction + (String) ((Map<String,
425425 // Object>) quad.get("predicate")).get("value") + name;
426426 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" ) ) ;
428428 md1 . Update ( JsonLD . JavaCompat . GetBytesForString ( name , "UTF-8" ) ) ;
429429 string groupHash = EncodeHex ( md1 . Digest ( ) ) ;
430430 if ( groups . ContainsKey ( groupHash ) )
@@ -455,28 +455,27 @@ private static NormalizeUtils.HashResult HashPaths(string id, JObject bnodes, Un
455455 /// <param name="bnodes">the mapping of bnodes to quads.</param>
456456 /// <param name="namer">the canonical bnode namer.</param>
457457 /// <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
459459 namer )
460460 {
461461 // return cached hash
462- if ( ( ( JObject ) bnodes [ id ] ) . ContainsKey ( "hash" ) )
462+ if ( bnodes [ id ] . ContainsKey ( "hash" ) )
463463 {
464- return ( string ) ( ( JObject ) bnodes [ id ] ) [ "hash" ] ;
464+ return ( string ) bnodes [ id ] [ "hash" ] ;
465465 }
466466 // 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" ] ;
468468 IList < string > nquads = new List < string > ( ) ;
469469 for ( int i = 0 ; i < quads . Count ; ++ i )
470470 {
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 ) ) ;
474473 }
475474 // sort serialized quads
476475 nquads . SortInPlace ( ) ;
477476 // return hashed quads
478477 string hash = Sha1hash ( nquads ) ;
479- ( ( JObject ) bnodes [ id ] ) [ "hash" ] = hash ;
478+ ( ( IDictionary < string , object > ) bnodes [ id ] ) [ "hash" ] = hash ;
480479 return hash ;
481480 }
482481
@@ -511,7 +510,7 @@ private static string EncodeHex(byte[] data)
511510 string rval = string . Empty ;
512511 foreach ( byte b in data )
513512 {
514- rval += string . Format ( "%02x" , b ) ;
513+ rval += b . ToString ( "x2" ) ;
515514 }
516515 return rval ;
517516 }
@@ -528,10 +527,9 @@ private static string EncodeHex(byte[] data)
528527 /// <param name="node">the RDF quad node.</param>
529528 /// <param name="id">the ID of the blank node to look next to.</param>
530529 /// <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 )
533531 {
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 ;
535533 }
536534
537535 private class Permutator
@@ -614,4 +612,5 @@ public virtual JArray Next()
614612 }
615613 }
616614 }
615+ #endif
617616}
0 commit comments