blob: 9a3cfbb5311904525399a2c978b14d6049d81481 [file] [log] [blame]
Meki Cherkaouif441d3a2012-04-22 15:17:52 -07001/*
2 * @author: ucla-cs
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
7
Jeff Thompsonf3bd3592012-09-29 23:25:30 -07008var Name = function Name(_components){
Meki Cherkaouif441d3a2012-04-22 15:17:52 -07009
Jeff Thompson86aea882012-09-29 17:32:48 -070010 if( typeof _components == 'string') {
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070011
Jeff Thompson86aea882012-09-29 17:32:48 -070012 if(LOG>3)console.log('Content Name String '+_components);
Jeff Thompsonf3bd3592012-09-29 23:25:30 -070013 this.components = Name.makeBlob(Name.createNameArray(_components));
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070014 }
Jeff Thompson86aea882012-09-29 17:32:48 -070015 else if(typeof _components === 'object' && _components instanceof Array ){
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070016
Jeff Thompson86aea882012-09-29 17:32:48 -070017 if(LOG>4)console.log('Content Name Array '+_components);
Jeff Thompsonf3bd3592012-09-29 23:25:30 -070018 this.components = Name.makeBlob(_components);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070019
20 }
Jeff Thompson86aea882012-09-29 17:32:48 -070021 else if(_components==null){
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070022 this.components =[];
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070023 }
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070024 else{
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070025
26 if(LOG>1)console.log("NO CONTENT NAME GIVEN");
27
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070028 }
29};
30
Jeff Thompsonf3bd3592012-09-29 23:25:30 -070031Name.prototype.getName=function(){
Meki Cherkaoui8f173612012-06-06 01:05:40 -070032
33 var output = "";
34
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070035 for(var i=0;i<this.components.length;i++){
36 output+= "/"+ DataUtils.toString(this.components[i]);
Meki Cherkaoui8f173612012-06-06 01:05:40 -070037 }
38
39 return output;
40
41};
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070042
Jeff Thompsonf3bd3592012-09-29 23:25:30 -070043Name.makeBlob=function(name){
Meki Cherkaoui8f173612012-06-06 01:05:40 -070044
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 Thompson9b411002012-10-21 17:35:16 -070059Name.createNameArray=function(name) {
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070060 var array = name.split('/');
Jeff Thompson4e8b7702012-10-21 23:42:27 -070061 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 Cherkaouif3d8f692012-05-18 15:44:28 -070066
67 if(name[0]=="/")
Jeff Thompson9b411002012-10-21 17:35:16 -070068 array=array.slice(1,array.length);
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070069 if(name[name.length-1]=="/")
70 array=array.slice(0,array.length-1);
Jeff Thompson9b411002012-10-21 17:35:16 -070071
72 // Unescape the components.
Jeff Thompson4e8b7702012-10-21 23:42:27 -070073 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 Thompson9b411002012-10-21 17:35:16 -070086
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070087 return array;
88}
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070089
90
Jeff Thompsonf3bd3592012-09-29 23:25:30 -070091Name.prototype.from_ccnb = function(/*XMLDecoder*/ decoder) {
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070092 decoder.readStartElement(this.getElementLabel());
93
94
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070095 this.components = new Array(); //new ArrayList<byte []>();
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070096
97 while (decoder.peekStartElement(CCNProtocolDTags.Component)) {
98 this.add(decoder.readBinaryElement(CCNProtocolDTags.Component));
99 }
100
101 decoder.readEndElement();
102};
103
Jeff Thompsonf3bd3592012-09-29 23:25:30 -0700104Name.prototype.to_ccnb = function(/*XMLEncoder*/ encoder) {
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700105
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700106 if( this.components ==null )
Jeff Thompson34a2ec02012-09-29 21:47:05 -0700107 throw new Error("CANNOT ENCODE EMPTY CONTENT NAME");
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700108
109 encoder.writeStartElement(this.getElementLabel());
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700110 var count = this.components.length;
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700111 for (var i=0; i < count; i++) {
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700112 encoder.writeElement(CCNProtocolDTags.Component, this.components[i]);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700113 }
114 encoder.writeEndElement();
115};
116
Jeff Thompsonf3bd3592012-09-29 23:25:30 -0700117Name.prototype.getElementLabel = function(){
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700118 return CCNProtocolDTags.Name;
119};
120
Jeff Thompsonf3bd3592012-09-29 23:25:30 -0700121Name.prototype.add = function(param){
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700122 return this.components.push(param);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700123};
124
Jeff Thompson4e8b7702012-10-21 23:42:27 -0700125// Return the escaped name string according to "CCNx URI Scheme". Does not include "ccnx:".
126Name.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 */
139Name.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};