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 | 4e8b770 | 2012-10-21 23:42:27 -0700 | [diff] [blame] | 4 | * This class represents a Name as an array of components where each is a byte array. |
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('/'); |
Jeff Thompson | 4e8b770 | 2012-10-21 23:42:27 -0700 | [diff] [blame] | 61 | var colonIndex = array[0].indexOf(':'); |
| 62 | if (colonIndex >= 0) { |
| 63 | name = name.substr(colonIndex + 1, name.length - colonIndex - 1); |
| 64 | array = name.split('/'); |
| 65 | } |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 66 | |
| 67 | if(name[0]=="/") |
Jeff Thompson | 9b41100 | 2012-10-21 17:35:16 -0700 | [diff] [blame] | 68 | array=array.slice(1,array.length); |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 69 | if(name[name.length-1]=="/") |
| 70 | array=array.slice(0,array.length-1); |
Jeff Thompson | 9b41100 | 2012-10-21 17:35:16 -0700 | [diff] [blame] | 71 | |
| 72 | // Unescape the components. |
Jeff Thompson | 4e8b770 | 2012-10-21 23:42:27 -0700 | [diff] [blame] | 73 | for (var i = 0; i < array.length; ++i) { |
| 74 | var component = unescape(array[i]); |
| 75 | |
| 76 | if (component.match(/[^.]/) == null) { |
| 77 | // Special case for component of only periods. Remove 3 periods. |
| 78 | if (component.length <= 3) |
| 79 | array[i] = ""; |
| 80 | else |
| 81 | array[i] = component.substr(3, component.length - 3); |
| 82 | } |
| 83 | else |
| 84 | array[i] = component; |
| 85 | } |
Jeff Thompson | 9b41100 | 2012-10-21 17:35:16 -0700 | [diff] [blame] | 86 | |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 87 | return array; |
| 88 | } |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 89 | |
| 90 | |
Jeff Thompson | f3bd359 | 2012-09-29 23:25:30 -0700 | [diff] [blame] | 91 | Name.prototype.from_ccnb = function(/*XMLDecoder*/ decoder) { |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 92 | decoder.readStartElement(this.getElementLabel()); |
| 93 | |
| 94 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 95 | this.components = new Array(); //new ArrayList<byte []>(); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 96 | |
| 97 | while (decoder.peekStartElement(CCNProtocolDTags.Component)) { |
| 98 | this.add(decoder.readBinaryElement(CCNProtocolDTags.Component)); |
| 99 | } |
| 100 | |
| 101 | decoder.readEndElement(); |
| 102 | }; |
| 103 | |
Jeff Thompson | f3bd359 | 2012-09-29 23:25:30 -0700 | [diff] [blame] | 104 | Name.prototype.to_ccnb = function(/*XMLEncoder*/ encoder) { |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 105 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 106 | if( this.components ==null ) |
Jeff Thompson | 34a2ec0 | 2012-09-29 21:47:05 -0700 | [diff] [blame] | 107 | throw new Error("CANNOT ENCODE EMPTY CONTENT NAME"); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 108 | |
| 109 | encoder.writeStartElement(this.getElementLabel()); |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 110 | var count = this.components.length; |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 111 | for (var i=0; i < count; i++) { |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 112 | encoder.writeElement(CCNProtocolDTags.Component, this.components[i]); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 113 | } |
| 114 | encoder.writeEndElement(); |
| 115 | }; |
| 116 | |
Jeff Thompson | f3bd359 | 2012-09-29 23:25:30 -0700 | [diff] [blame] | 117 | Name.prototype.getElementLabel = function(){ |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 118 | return CCNProtocolDTags.Name; |
| 119 | }; |
| 120 | |
Jeff Thompson | f3bd359 | 2012-09-29 23:25:30 -0700 | [diff] [blame] | 121 | Name.prototype.add = function(param){ |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 122 | return this.components.push(param); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 123 | }; |
| 124 | |
Jeff Thompson | 4e8b770 | 2012-10-21 23:42:27 -0700 | [diff] [blame] | 125 | // Return the escaped name string according to "CCNx URI Scheme". Does not include "ccnx:". |
| 126 | Name.prototype.to_uri = function() { |
| 127 | var result = ""; |
| 128 | |
| 129 | for(var i = 0; i < this.components.length; ++i) |
| 130 | result += "/"+ Name.toEscapedString(this.components[i]); |
| 131 | |
| 132 | return result; |
| 133 | }; |
| 134 | |
| 135 | /** |
| 136 | * Return component as an escaped string according to "CCNx URI Scheme". |
| 137 | * We can't use encodeURIComponent because that doesn't encode all the characters we want to. |
| 138 | */ |
| 139 | Name.toEscapedString = function(component) { |
| 140 | var result = ""; |
| 141 | var gotNonDot = false; |
| 142 | for (var i = 0; i < component.length; ++i) { |
| 143 | if (component[i] != 0x2e) { |
| 144 | gotNonDot = true; |
| 145 | break; |
| 146 | } |
| 147 | } |
| 148 | if (!gotNonDot) { |
| 149 | // Special case for component of zero or more periods. Add 3 periods. |
| 150 | result = "..."; |
| 151 | for (var i = 0; i < component.length; ++i) |
| 152 | result += "."; |
| 153 | } |
| 154 | else { |
| 155 | for (var i = 0; i < component.length; ++i) { |
| 156 | var value = component[i]; |
| 157 | // Check for 0-9, A-Z, a-z, (+), (-), (.), (_) |
| 158 | if (value >= 0x30 && value <= 0x39 || value >= 0x41 && value <= 0x5a || |
| 159 | value >= 0x61 && value <= 0x7a || value == 0x2b || value == 0x2d || |
| 160 | value == 0x2e || value == 0x5f) |
| 161 | result += String.fromCharCode(value); |
| 162 | else |
| 163 | result += "%" + (value < 16 ? "0" : "") + value.toString(16).toUpperCase(); |
| 164 | } |
| 165 | } |
| 166 | return result; |
| 167 | }; |