Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 1 | /* |
| 2 | * @author: ucla-cs |
Jeff Thompson | 745026e | 2012-10-13 12:49:20 -0700 | [diff] [blame] | 3 | * See COPYING for copyright and distribution information. |
Jeff Thompson | f3bd359 | 2012-09-29 23:25:30 -0700 | [diff] [blame] | 4 | * This class represents a Name |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | |
Jeff Thompson | f3bd359 | 2012-09-29 23:25:30 -0700 | [diff] [blame] | 8 | var Name = function Name(_components){ |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 9 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 10 | if( typeof _components == 'string') { |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 11 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 12 | if(LOG>3)console.log('Content Name String '+_components); |
Jeff Thompson | f3bd359 | 2012-09-29 23:25:30 -0700 | [diff] [blame] | 13 | this.components = Name.makeBlob(Name.createNameArray(_components)); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 14 | } |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 15 | else if(typeof _components === 'object' && _components instanceof Array ){ |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 16 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 17 | if(LOG>4)console.log('Content Name Array '+_components); |
Jeff Thompson | f3bd359 | 2012-09-29 23:25:30 -0700 | [diff] [blame] | 18 | this.components = Name.makeBlob(_components); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 19 | |
| 20 | } |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 21 | else if(_components==null){ |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 22 | this.components =[]; |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 23 | } |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 24 | else{ |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 25 | |
| 26 | if(LOG>1)console.log("NO CONTENT NAME GIVEN"); |
| 27 | |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 28 | } |
| 29 | }; |
| 30 | |
Jeff Thompson | f3bd359 | 2012-09-29 23:25:30 -0700 | [diff] [blame] | 31 | Name.prototype.getName=function(){ |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 32 | |
| 33 | var output = ""; |
| 34 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 35 | for(var i=0;i<this.components.length;i++){ |
| 36 | output+= "/"+ DataUtils.toString(this.components[i]); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | return output; |
| 40 | |
| 41 | }; |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 42 | |
Jeff Thompson | f3bd359 | 2012-09-29 23:25:30 -0700 | [diff] [blame] | 43 | Name.makeBlob=function(name){ |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 44 | |
| 45 | var blobArrays = new Array(name.length); |
| 46 | |
| 47 | for(var i=0;i<name.length;i++){ |
| 48 | if(typeof name[i] == 'string') |
| 49 | blobArrays[i]= DataUtils.toNumbersFromString( name[i] ); |
| 50 | else if(typeof name[i] == 'object') |
| 51 | blobArrays[i]= name[i] ; |
| 52 | else |
| 53 | if(LOG>4)console.log('NAME COMPONENT INVALID'); |
| 54 | } |
| 55 | |
| 56 | return blobArrays; |
| 57 | }; |
| 58 | |
Jeff Thompson | 9b41100 | 2012-10-21 17:35:16 -0700 | [diff] [blame] | 59 | Name.createNameArray=function(name) { |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 60 | var array = name.split('/'); |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 61 | |
| 62 | if(name[0]=="/") |
Jeff Thompson | 9b41100 | 2012-10-21 17:35:16 -0700 | [diff] [blame] | 63 | array=array.slice(1,array.length); |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 64 | if(name[name.length-1]=="/") |
| 65 | array=array.slice(0,array.length-1); |
Jeff Thompson | 9b41100 | 2012-10-21 17:35:16 -0700 | [diff] [blame] | 66 | |
| 67 | // Unescape the components. |
| 68 | for (var i = 0; i < array.length; ++i) |
| 69 | array[i] = unescape(array[i]); |
| 70 | |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 71 | return array; |
| 72 | } |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 73 | |
| 74 | |
Jeff Thompson | f3bd359 | 2012-09-29 23:25:30 -0700 | [diff] [blame] | 75 | Name.prototype.from_ccnb = function(/*XMLDecoder*/ decoder) { |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 76 | decoder.readStartElement(this.getElementLabel()); |
| 77 | |
| 78 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 79 | this.components = new Array(); //new ArrayList<byte []>(); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 80 | |
| 81 | while (decoder.peekStartElement(CCNProtocolDTags.Component)) { |
| 82 | this.add(decoder.readBinaryElement(CCNProtocolDTags.Component)); |
| 83 | } |
| 84 | |
| 85 | decoder.readEndElement(); |
| 86 | }; |
| 87 | |
Jeff Thompson | f3bd359 | 2012-09-29 23:25:30 -0700 | [diff] [blame] | 88 | Name.prototype.to_ccnb = function(/*XMLEncoder*/ encoder) { |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 89 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 90 | if( this.components ==null ) |
Jeff Thompson | 34a2ec0 | 2012-09-29 21:47:05 -0700 | [diff] [blame] | 91 | throw new Error("CANNOT ENCODE EMPTY CONTENT NAME"); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 92 | |
| 93 | encoder.writeStartElement(this.getElementLabel()); |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 94 | var count = this.components.length; |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 95 | for (var i=0; i < count; i++) { |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 96 | encoder.writeElement(CCNProtocolDTags.Component, this.components[i]); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 97 | } |
| 98 | encoder.writeEndElement(); |
| 99 | }; |
| 100 | |
Jeff Thompson | f3bd359 | 2012-09-29 23:25:30 -0700 | [diff] [blame] | 101 | Name.prototype.getElementLabel = function(){ |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 102 | return CCNProtocolDTags.Name; |
| 103 | }; |
| 104 | |
Jeff Thompson | f3bd359 | 2012-09-29 23:25:30 -0700 | [diff] [blame] | 105 | Name.prototype.add = function(param){ |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 106 | return this.components.push(param); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 107 | }; |
| 108 | |