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 | 0d3f203 | 2012-11-04 12:55:27 -0800 | [diff] [blame^] | 31 | Name.prototype.getName = function() { |
| 32 | return this.to_uri(); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 33 | }; |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 34 | |
Jeff Thompson | f3bd359 | 2012-09-29 23:25:30 -0700 | [diff] [blame] | 35 | Name.makeBlob=function(name){ |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 36 | |
| 37 | var blobArrays = new Array(name.length); |
| 38 | |
| 39 | for(var i=0;i<name.length;i++){ |
| 40 | if(typeof name[i] == 'string') |
| 41 | blobArrays[i]= DataUtils.toNumbersFromString( name[i] ); |
| 42 | else if(typeof name[i] == 'object') |
| 43 | blobArrays[i]= name[i] ; |
| 44 | else |
| 45 | if(LOG>4)console.log('NAME COMPONENT INVALID'); |
| 46 | } |
| 47 | |
| 48 | return blobArrays; |
| 49 | }; |
| 50 | |
Jeff Thompson | 0d3f203 | 2012-11-04 12:55:27 -0800 | [diff] [blame^] | 51 | Name.createNameArray = function(name) { |
| 52 | name = name.trim(); |
| 53 | if (name.length <= 0) |
| 54 | return []; |
| 55 | |
| 56 | var iColon = name.indexOf(':'); |
| 57 | if (iColon >= 0) { |
| 58 | // Make sure the colon came before a '/'. |
| 59 | var iFirstSlash = name.indexOf('/'); |
| 60 | if (iFirstSlash < 0 || iColon < iFirstSlash) |
| 61 | // Omit the leading protocol such as ccnx: |
| 62 | name = name.substr(iColon + 1, name.length - iColon - 1).trim(); |
Jeff Thompson | 4e8b770 | 2012-10-21 23:42:27 -0700 | [diff] [blame] | 63 | } |
Jeff Thompson | 0d3f203 | 2012-11-04 12:55:27 -0800 | [diff] [blame^] | 64 | |
| 65 | if (name[0] == '/') { |
| 66 | if (name.length >= 2 && name[1] == '/') { |
| 67 | // Strip the authority following "//". |
| 68 | var iAfterAuthority = name.indexOf('/', 2); |
| 69 | if (iAfterAuthority < 0) |
| 70 | // Unusual case: there was only an authority. |
| 71 | return []; |
| 72 | else |
| 73 | name = name.substr(iAfterAuthority + 1, name.length - iAfterAuthority - 1).trim(); |
| 74 | } |
| 75 | else |
| 76 | name = name.substr(1, name.length - 1).trim(); |
| 77 | } |
| 78 | |
| 79 | var array = name.split('/'); |
Jeff Thompson | 9b41100 | 2012-10-21 17:35:16 -0700 | [diff] [blame] | 80 | |
| 81 | // Unescape the components. |
Jeff Thompson | 4e8b770 | 2012-10-21 23:42:27 -0700 | [diff] [blame] | 82 | for (var i = 0; i < array.length; ++i) { |
Jeff Thompson | 0d3f203 | 2012-11-04 12:55:27 -0800 | [diff] [blame^] | 83 | var component = unescape(array[i].trim()); |
Jeff Thompson | 4e8b770 | 2012-10-21 23:42:27 -0700 | [diff] [blame] | 84 | |
| 85 | if (component.match(/[^.]/) == null) { |
Jeff Thompson | 0d3f203 | 2012-11-04 12:55:27 -0800 | [diff] [blame^] | 86 | // Special case for component of only periods. |
| 87 | if (component.length <= 2) { |
| 88 | // Zero, one or two periods is illegal. Ignore this componenent to be |
| 89 | // consistent with the C implmentation. |
| 90 | // This also gets rid of a trailing '/'. |
| 91 | array = array.slice(0, i).concat(array.slice(i + 1, array.length)); |
| 92 | --i; |
| 93 | } |
Jeff Thompson | 4e8b770 | 2012-10-21 23:42:27 -0700 | [diff] [blame] | 94 | else |
Jeff Thompson | 0d3f203 | 2012-11-04 12:55:27 -0800 | [diff] [blame^] | 95 | // Remove 3 periods. |
Jeff Thompson | 4e8b770 | 2012-10-21 23:42:27 -0700 | [diff] [blame] | 96 | array[i] = component.substr(3, component.length - 3); |
| 97 | } |
| 98 | else |
| 99 | array[i] = component; |
| 100 | } |
Jeff Thompson | 9b41100 | 2012-10-21 17:35:16 -0700 | [diff] [blame] | 101 | |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 102 | return array; |
| 103 | } |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 104 | |
| 105 | |
Jeff Thompson | f3bd359 | 2012-09-29 23:25:30 -0700 | [diff] [blame] | 106 | Name.prototype.from_ccnb = function(/*XMLDecoder*/ decoder) { |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 107 | decoder.readStartElement(this.getElementLabel()); |
| 108 | |
| 109 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 110 | this.components = new Array(); //new ArrayList<byte []>(); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 111 | |
| 112 | while (decoder.peekStartElement(CCNProtocolDTags.Component)) { |
| 113 | this.add(decoder.readBinaryElement(CCNProtocolDTags.Component)); |
| 114 | } |
| 115 | |
| 116 | decoder.readEndElement(); |
| 117 | }; |
| 118 | |
Jeff Thompson | f3bd359 | 2012-09-29 23:25:30 -0700 | [diff] [blame] | 119 | Name.prototype.to_ccnb = function(/*XMLEncoder*/ encoder) { |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 120 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 121 | if( this.components ==null ) |
Jeff Thompson | 34a2ec0 | 2012-09-29 21:47:05 -0700 | [diff] [blame] | 122 | throw new Error("CANNOT ENCODE EMPTY CONTENT NAME"); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 123 | |
| 124 | encoder.writeStartElement(this.getElementLabel()); |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 125 | var count = this.components.length; |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 126 | for (var i=0; i < count; i++) { |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 127 | encoder.writeElement(CCNProtocolDTags.Component, this.components[i]); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 128 | } |
| 129 | encoder.writeEndElement(); |
| 130 | }; |
| 131 | |
Jeff Thompson | f3bd359 | 2012-09-29 23:25:30 -0700 | [diff] [blame] | 132 | Name.prototype.getElementLabel = function(){ |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 133 | return CCNProtocolDTags.Name; |
| 134 | }; |
| 135 | |
Jeff Thompson | f3bd359 | 2012-09-29 23:25:30 -0700 | [diff] [blame] | 136 | Name.prototype.add = function(param){ |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 137 | return this.components.push(param); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 138 | }; |
| 139 | |
Jeff Thompson | 4e8b770 | 2012-10-21 23:42:27 -0700 | [diff] [blame] | 140 | // Return the escaped name string according to "CCNx URI Scheme". Does not include "ccnx:". |
| 141 | Name.prototype.to_uri = function() { |
| 142 | var result = ""; |
| 143 | |
| 144 | for(var i = 0; i < this.components.length; ++i) |
| 145 | result += "/"+ Name.toEscapedString(this.components[i]); |
| 146 | |
| 147 | return result; |
| 148 | }; |
| 149 | |
| 150 | /** |
| 151 | * Return component as an escaped string according to "CCNx URI Scheme". |
| 152 | * We can't use encodeURIComponent because that doesn't encode all the characters we want to. |
| 153 | */ |
| 154 | Name.toEscapedString = function(component) { |
| 155 | var result = ""; |
| 156 | var gotNonDot = false; |
| 157 | for (var i = 0; i < component.length; ++i) { |
| 158 | if (component[i] != 0x2e) { |
| 159 | gotNonDot = true; |
| 160 | break; |
| 161 | } |
| 162 | } |
| 163 | if (!gotNonDot) { |
| 164 | // Special case for component of zero or more periods. Add 3 periods. |
| 165 | result = "..."; |
| 166 | for (var i = 0; i < component.length; ++i) |
| 167 | result += "."; |
| 168 | } |
| 169 | else { |
| 170 | for (var i = 0; i < component.length; ++i) { |
| 171 | var value = component[i]; |
| 172 | // Check for 0-9, A-Z, a-z, (+), (-), (.), (_) |
| 173 | if (value >= 0x30 && value <= 0x39 || value >= 0x41 && value <= 0x5a || |
| 174 | value >= 0x61 && value <= 0x7a || value == 0x2b || value == 0x2d || |
| 175 | value == 0x2e || value == 0x5f) |
| 176 | result += String.fromCharCode(value); |
| 177 | else |
| 178 | result += "%" + (value < 16 ? "0" : "") + value.toString(16).toUpperCase(); |
| 179 | } |
| 180 | } |
| 181 | return result; |
| 182 | }; |