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