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 | f3bd359 | 2012-09-29 23:25:30 -0700 | [diff] [blame] | 59 | Name.createNameArray=function(name){ |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 60 | |
| 61 | |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 62 | name = unescape(name); |
| 63 | |
| 64 | var array = name.split('/'); |
| 65 | |
| 66 | |
| 67 | if(name[0]=="/") |
| 68 | array=array.slice(1,array.length); |
| 69 | |
| 70 | if(name[name.length-1]=="/") |
| 71 | array=array.slice(0,array.length-1); |
| 72 | |
| 73 | return array; |
| 74 | } |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 75 | |
| 76 | |
Jeff Thompson | f3bd359 | 2012-09-29 23:25:30 -0700 | [diff] [blame] | 77 | Name.prototype.from_ccnb = function(/*XMLDecoder*/ decoder) { |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 78 | decoder.readStartElement(this.getElementLabel()); |
| 79 | |
| 80 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 81 | this.components = new Array(); //new ArrayList<byte []>(); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 82 | |
| 83 | while (decoder.peekStartElement(CCNProtocolDTags.Component)) { |
| 84 | this.add(decoder.readBinaryElement(CCNProtocolDTags.Component)); |
| 85 | } |
| 86 | |
| 87 | decoder.readEndElement(); |
| 88 | }; |
| 89 | |
Jeff Thompson | f3bd359 | 2012-09-29 23:25:30 -0700 | [diff] [blame] | 90 | Name.prototype.to_ccnb = function(/*XMLEncoder*/ encoder) { |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 91 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 92 | if( this.components ==null ) |
Jeff Thompson | 34a2ec0 | 2012-09-29 21:47:05 -0700 | [diff] [blame] | 93 | throw new Error("CANNOT ENCODE EMPTY CONTENT NAME"); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 94 | |
| 95 | encoder.writeStartElement(this.getElementLabel()); |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 96 | var count = this.components.length; |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 97 | for (var i=0; i < count; i++) { |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 98 | encoder.writeElement(CCNProtocolDTags.Component, this.components[i]); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 99 | } |
| 100 | encoder.writeEndElement(); |
| 101 | }; |
| 102 | |
Jeff Thompson | f3bd359 | 2012-09-29 23:25:30 -0700 | [diff] [blame] | 103 | Name.prototype.getElementLabel = function(){ |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 104 | return CCNProtocolDTags.Name; |
| 105 | }; |
| 106 | |
Jeff Thompson | f3bd359 | 2012-09-29 23:25:30 -0700 | [diff] [blame] | 107 | Name.prototype.add = function(param){ |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 108 | return this.components.push(param); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 109 | }; |
| 110 | |