blob: 69e3098b0b50235b3eff7673ec3c550b33efd2b1 [file] [log] [blame]
Meki Cherkaouif441d3a2012-04-22 15:17:52 -07001/*
2 * @author: ucla-cs
Jeff Thompsonf3bd3592012-09-29 23:25:30 -07003 * This class represents a Name
Meki Cherkaouif441d3a2012-04-22 15:17:52 -07004 */
5
6
Jeff Thompsonf3bd3592012-09-29 23:25:30 -07007var Name = function Name(_components){
Meki Cherkaouif441d3a2012-04-22 15:17:52 -07008
Jeff Thompson86aea882012-09-29 17:32:48 -07009 if( typeof _components == 'string') {
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070010
Jeff Thompson86aea882012-09-29 17:32:48 -070011 if(LOG>3)console.log('Content Name String '+_components);
Jeff Thompsonf3bd3592012-09-29 23:25:30 -070012 this.components = Name.makeBlob(Name.createNameArray(_components));
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070013 }
Jeff Thompson86aea882012-09-29 17:32:48 -070014 else if(typeof _components === 'object' && _components instanceof Array ){
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070015
Jeff Thompson86aea882012-09-29 17:32:48 -070016 if(LOG>4)console.log('Content Name Array '+_components);
Jeff Thompsonf3bd3592012-09-29 23:25:30 -070017 this.components = Name.makeBlob(_components);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070018
19 }
Jeff Thompson86aea882012-09-29 17:32:48 -070020 else if(_components==null){
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070021 this.components =[];
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070022 }
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070023 else{
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070024
25 if(LOG>1)console.log("NO CONTENT NAME GIVEN");
26
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070027 }
28};
29
Jeff Thompsonf3bd3592012-09-29 23:25:30 -070030Name.prototype.getName=function(){
Meki Cherkaoui8f173612012-06-06 01:05:40 -070031
32 var output = "";
33
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070034 for(var i=0;i<this.components.length;i++){
35 output+= "/"+ DataUtils.toString(this.components[i]);
Meki Cherkaoui8f173612012-06-06 01:05:40 -070036 }
37
38 return output;
39
40};
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070041
Jeff Thompsonf3bd3592012-09-29 23:25:30 -070042Name.makeBlob=function(name){
Meki Cherkaoui8f173612012-06-06 01:05:40 -070043
44 var blobArrays = new Array(name.length);
45
46 for(var i=0;i<name.length;i++){
47 if(typeof name[i] == 'string')
48 blobArrays[i]= DataUtils.toNumbersFromString( name[i] );
49 else if(typeof name[i] == 'object')
50 blobArrays[i]= name[i] ;
51 else
52 if(LOG>4)console.log('NAME COMPONENT INVALID');
53 }
54
55 return blobArrays;
56};
57
Jeff Thompsonf3bd3592012-09-29 23:25:30 -070058Name.createNameArray=function(name){
Meki Cherkaoui8f173612012-06-06 01:05:40 -070059
60
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070061 name = unescape(name);
62
63 var array = name.split('/');
64
65
66 if(name[0]=="/")
67 array=array.slice(1,array.length);
68
69 if(name[name.length-1]=="/")
70 array=array.slice(0,array.length-1);
71
72 return array;
73}
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070074
75
Jeff Thompsonf3bd3592012-09-29 23:25:30 -070076Name.prototype.from_ccnb = function(/*XMLDecoder*/ decoder) {
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070077 decoder.readStartElement(this.getElementLabel());
78
79
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070080 this.components = new Array(); //new ArrayList<byte []>();
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070081
82 while (decoder.peekStartElement(CCNProtocolDTags.Component)) {
83 this.add(decoder.readBinaryElement(CCNProtocolDTags.Component));
84 }
85
86 decoder.readEndElement();
87};
88
Jeff Thompsonf3bd3592012-09-29 23:25:30 -070089Name.prototype.to_ccnb = function(/*XMLEncoder*/ encoder) {
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070090
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070091 if( this.components ==null )
Jeff Thompson34a2ec02012-09-29 21:47:05 -070092 throw new Error("CANNOT ENCODE EMPTY CONTENT NAME");
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070093
94 encoder.writeStartElement(this.getElementLabel());
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070095 var count = this.components.length;
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070096 for (var i=0; i < count; i++) {
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070097 encoder.writeElement(CCNProtocolDTags.Component, this.components[i]);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070098 }
99 encoder.writeEndElement();
100};
101
Jeff Thompsonf3bd3592012-09-29 23:25:30 -0700102Name.prototype.getElementLabel = function(){
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700103 return CCNProtocolDTags.Name;
104};
105
Jeff Thompsonf3bd3592012-09-29 23:25:30 -0700106Name.prototype.add = function(param){
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700107 return this.components.push(param);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700108};
109