blob: 7c2725b72d257b50312d8390f2b995b31008d123 [file] [log] [blame]
Meki Cherkaouif441d3a2012-04-22 15:17:52 -07001/*
Jeff Thompson87021f72012-11-18 17:40:06 -08002 * @author: Meki Cheraoui, Jeff Thompson
Jeff Thompson745026e2012-10-13 12:49:20 -07003 * See COPYING for copyright and distribution information.
Jeff Thompson4e8b7702012-10-21 23:42:27 -07004 * This class represents a Name as an array of components where each is a byte array.
Meki Cherkaouif441d3a2012-04-22 15:17:52 -07005 */
6
Jeff Thompson87021f72012-11-18 17:40:06 -08007/*
8 * Create a new Name from _components.
9 * If _components is a string, parse it as a URI. Otherwise it is an array of components
10 * where each is a string, byte array, ArrayBuffer or Uint8Array.
11 * Convert and store as an array of Uint8Array.
12 * If a component is a string, encode as utf8.
13 */
Jeff Thompsonf3bd3592012-09-29 23:25:30 -070014var Name = function Name(_components){
Jeff Thompson87021f72012-11-18 17:40:06 -080015 if( typeof _components == 'string') {
Jeff Thompson86aea882012-09-29 17:32:48 -070016 if(LOG>3)console.log('Content Name String '+_components);
Jeff Thompson87021f72012-11-18 17:40:06 -080017 this.components = Name.createNameArray(_components);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070018 }
Jeff Thompson87021f72012-11-18 17:40:06 -080019 else if(typeof _components === 'object'){
Jeff Thompson86aea882012-09-29 17:32:48 -070020 if(LOG>4)console.log('Content Name Array '+_components);
Jeff Thompson87021f72012-11-18 17:40:06 -080021 this.components = [];
22 for (var i = 0; i < _components.length; ++i)
23 this.add(_components[i]);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070024 }
Jeff Thompson87021f72012-11-18 17:40:06 -080025 else if(_components==null)
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070026 this.components =[];
Jeff Thompson87021f72012-11-18 17:40:06 -080027 else
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070028 if(LOG>1)console.log("NO CONTENT NAME GIVEN");
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070029};
30
Jeff Thompson0d3f2032012-11-04 12:55:27 -080031Name.prototype.getName = function() {
32 return this.to_uri();
Meki Cherkaoui8f173612012-06-06 01:05:40 -070033};
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070034
Jeff Thompson87021f72012-11-18 17:40:06 -080035/* Parse name as a URI and return an array of Uint8Array components.
36 *
37 */
Jeff Thompson0d3f2032012-11-04 12:55:27 -080038Name.createNameArray = function(name) {
39 name = name.trim();
40 if (name.length <= 0)
41 return [];
42
43 var iColon = name.indexOf(':');
44 if (iColon >= 0) {
45 // Make sure the colon came before a '/'.
46 var iFirstSlash = name.indexOf('/');
47 if (iFirstSlash < 0 || iColon < iFirstSlash)
48 // Omit the leading protocol such as ccnx:
49 name = name.substr(iColon + 1, name.length - iColon - 1).trim();
Jeff Thompson4e8b7702012-10-21 23:42:27 -070050 }
Jeff Thompson0d3f2032012-11-04 12:55:27 -080051
52 if (name[0] == '/') {
53 if (name.length >= 2 && name[1] == '/') {
54 // Strip the authority following "//".
55 var iAfterAuthority = name.indexOf('/', 2);
56 if (iAfterAuthority < 0)
57 // Unusual case: there was only an authority.
58 return [];
59 else
60 name = name.substr(iAfterAuthority + 1, name.length - iAfterAuthority - 1).trim();
61 }
62 else
63 name = name.substr(1, name.length - 1).trim();
64 }
65
66 var array = name.split('/');
Jeff Thompson9b411002012-10-21 17:35:16 -070067
68 // Unescape the components.
Jeff Thompson4e8b7702012-10-21 23:42:27 -070069 for (var i = 0; i < array.length; ++i) {
Jeff Thompson0d3f2032012-11-04 12:55:27 -080070 var component = unescape(array[i].trim());
Jeff Thompson4e8b7702012-10-21 23:42:27 -070071
72 if (component.match(/[^.]/) == null) {
Jeff Thompson0d3f2032012-11-04 12:55:27 -080073 // Special case for component of only periods.
74 if (component.length <= 2) {
75 // Zero, one or two periods is illegal. Ignore this componenent to be
76 // consistent with the C implmentation.
77 // This also gets rid of a trailing '/'.
78 array = array.slice(0, i).concat(array.slice(i + 1, array.length));
79 --i;
80 }
Jeff Thompson4e8b7702012-10-21 23:42:27 -070081 else
Jeff Thompson0d3f2032012-11-04 12:55:27 -080082 // Remove 3 periods.
Jeff Thompson4e8b7702012-10-21 23:42:27 -070083 array[i] = component.substr(3, component.length - 3);
84 }
85 else
86 array[i] = component;
Jeff Thompson87021f72012-11-18 17:40:06 -080087
88 // Change the component to Uint8Array now.
89 array[i] = DataUtils.toNumbersFromString(array[i]);
Jeff Thompson4e8b7702012-10-21 23:42:27 -070090 }
Jeff Thompson9b411002012-10-21 17:35:16 -070091
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070092 return array;
93}
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070094
95
Jeff Thompsonf3bd3592012-09-29 23:25:30 -070096Name.prototype.from_ccnb = function(/*XMLDecoder*/ decoder) {
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070097 decoder.readStartElement(this.getElementLabel());
98
99
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700100 this.components = new Array(); //new ArrayList<byte []>();
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700101
102 while (decoder.peekStartElement(CCNProtocolDTags.Component)) {
103 this.add(decoder.readBinaryElement(CCNProtocolDTags.Component));
104 }
105
106 decoder.readEndElement();
107};
108
Jeff Thompsonf3bd3592012-09-29 23:25:30 -0700109Name.prototype.to_ccnb = function(/*XMLEncoder*/ encoder) {
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700110
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700111 if( this.components ==null )
Jeff Thompson34a2ec02012-09-29 21:47:05 -0700112 throw new Error("CANNOT ENCODE EMPTY CONTENT NAME");
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700113
114 encoder.writeStartElement(this.getElementLabel());
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700115 var count = this.components.length;
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700116 for (var i=0; i < count; i++) {
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700117 encoder.writeElement(CCNProtocolDTags.Component, this.components[i]);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700118 }
119 encoder.writeEndElement();
120};
121
Jeff Thompsonf3bd3592012-09-29 23:25:30 -0700122Name.prototype.getElementLabel = function(){
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700123 return CCNProtocolDTags.Name;
124};
125
Jeff Thompson87021f72012-11-18 17:40:06 -0800126/*
127 * component is a string, byte array, ArrayBuffer or Uint8Array.
128 * Convert to Uint8Array and add to this Name.
129 * If a component is a string, encode as utf8.
130 * Return the converted value.
131 */
132Name.prototype.add = function(component){
Jeff Thompson9ab151a2012-11-24 21:22:30 -0800133 var result;
Jeff Thompson87021f72012-11-18 17:40:06 -0800134 if(typeof component == 'string')
135 result = DataUtils.stringToUtf8Array(component);
136 else if(typeof component == 'object' && component instanceof Array)
137 result = new Uint8Array(component);
138 else if(typeof component == 'object' && component instanceof Uint8Array)
139 result = new Uint8Array(component);
140 else if(typeof component == 'object' && component instanceof ArrayBuffer)
141 // Make a copy.
142 result = new Uint8Array(component.slice(0, component.byteLength));
143 else
Jeff Thompson9ab151a2012-11-24 21:22:30 -0800144 throw new Error("Cannot add Name element at index " + this.components.length +
145 ": Invalid type");
Jeff Thompson87021f72012-11-18 17:40:06 -0800146
147 return this.components.push(result);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700148};
149
Jeff Thompson4e8b7702012-10-21 23:42:27 -0700150// Return the escaped name string according to "CCNx URI Scheme". Does not include "ccnx:".
151Name.prototype.to_uri = function() {
152 var result = "";
153
154 for(var i = 0; i < this.components.length; ++i)
155 result += "/"+ Name.toEscapedString(this.components[i]);
156
157 return result;
158};
159
Jeff Thompson87021f72012-11-18 17:40:06 -0800160/*
161 * Return a new Name with the first nComponents components of this Name.
162 */
163Name.prototype.getPrefix = function(nComponents) {
164 return new Name(this.components.slice(0, nComponents));
165}
166
167/*
168 * Return a new ArrayBuffer of the component at i.
169 */
170Name.prototype.getComponent = function(i) {
171 var result = new ArrayBuffer(this.components[i].length);
172 new Uint8Array(result).set(this.components[i]);
173 return result;
174}
175
Jeff Thompson4e8b7702012-10-21 23:42:27 -0700176/**
177 * Return component as an escaped string according to "CCNx URI Scheme".
178 * We can't use encodeURIComponent because that doesn't encode all the characters we want to.
179 */
180Name.toEscapedString = function(component) {
181 var result = "";
182 var gotNonDot = false;
183 for (var i = 0; i < component.length; ++i) {
184 if (component[i] != 0x2e) {
185 gotNonDot = true;
186 break;
187 }
188 }
189 if (!gotNonDot) {
190 // Special case for component of zero or more periods. Add 3 periods.
191 result = "...";
192 for (var i = 0; i < component.length; ++i)
193 result += ".";
194 }
195 else {
196 for (var i = 0; i < component.length; ++i) {
197 var value = component[i];
198 // Check for 0-9, A-Z, a-z, (+), (-), (.), (_)
199 if (value >= 0x30 && value <= 0x39 || value >= 0x41 && value <= 0x5a ||
200 value >= 0x61 && value <= 0x7a || value == 0x2b || value == 0x2d ||
201 value == 0x2e || value == 0x5f)
202 result += String.fromCharCode(value);
203 else
204 result += "%" + (value < 16 ? "0" : "") + value.toString(16).toUpperCase();
205 }
206 }
207 return result;
208};