blob: fa28fed736d61ed53708240973f4af47a05fc8d7 [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 Thompsonf3bd3592012-09-29 23:25:30 -07004 * This class represents a Name
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('/');
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070061
62 if(name[0]=="/")
Jeff Thompson9b411002012-10-21 17:35:16 -070063 array=array.slice(1,array.length);
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070064 if(name[name.length-1]=="/")
65 array=array.slice(0,array.length-1);
Jeff Thompson9b411002012-10-21 17:35:16 -070066
67 // Unescape the components.
68 for (var i = 0; i < array.length; ++i)
69 array[i] = unescape(array[i]);
70
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070071 return array;
72}
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070073
74
Jeff Thompsonf3bd3592012-09-29 23:25:30 -070075Name.prototype.from_ccnb = function(/*XMLDecoder*/ decoder) {
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070076 decoder.readStartElement(this.getElementLabel());
77
78
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070079 this.components = new Array(); //new ArrayList<byte []>();
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070080
81 while (decoder.peekStartElement(CCNProtocolDTags.Component)) {
82 this.add(decoder.readBinaryElement(CCNProtocolDTags.Component));
83 }
84
85 decoder.readEndElement();
86};
87
Jeff Thompsonf3bd3592012-09-29 23:25:30 -070088Name.prototype.to_ccnb = function(/*XMLEncoder*/ encoder) {
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070089
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070090 if( this.components ==null )
Jeff Thompson34a2ec02012-09-29 21:47:05 -070091 throw new Error("CANNOT ENCODE EMPTY CONTENT NAME");
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070092
93 encoder.writeStartElement(this.getElementLabel());
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070094 var count = this.components.length;
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070095 for (var i=0; i < count; i++) {
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070096 encoder.writeElement(CCNProtocolDTags.Component, this.components[i]);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070097 }
98 encoder.writeEndElement();
99};
100
Jeff Thompsonf3bd3592012-09-29 23:25:30 -0700101Name.prototype.getElementLabel = function(){
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700102 return CCNProtocolDTags.Name;
103};
104
Jeff Thompsonf3bd3592012-09-29 23:25:30 -0700105Name.prototype.add = function(param){
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700106 return this.components.push(param);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700107};
108