blob: aa8b6121fb831989f9f45877c60fba49e6d91b73 [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 Thompsonf3bd3592012-09-29 23:25:30 -070059Name.createNameArray=function(name){
Meki Cherkaoui8f173612012-06-06 01:05:40 -070060
61
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070062 name = unescape(name);
63
64 var array = name.split('/');
65
66
67 if(name[0]=="/")
68 array=array.slice(1,array.length);
69
70 if(name[name.length-1]=="/")
71 array=array.slice(0,array.length-1);
72
73 return array;
74}
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070075
76
Jeff Thompsonf3bd3592012-09-29 23:25:30 -070077Name.prototype.from_ccnb = function(/*XMLDecoder*/ decoder) {
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070078 decoder.readStartElement(this.getElementLabel());
79
80
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070081 this.components = new Array(); //new ArrayList<byte []>();
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070082
83 while (decoder.peekStartElement(CCNProtocolDTags.Component)) {
84 this.add(decoder.readBinaryElement(CCNProtocolDTags.Component));
85 }
86
87 decoder.readEndElement();
88};
89
Jeff Thompsonf3bd3592012-09-29 23:25:30 -070090Name.prototype.to_ccnb = function(/*XMLEncoder*/ encoder) {
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070091
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070092 if( this.components ==null )
Jeff Thompson34a2ec02012-09-29 21:47:05 -070093 throw new Error("CANNOT ENCODE EMPTY CONTENT NAME");
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070094
95 encoder.writeStartElement(this.getElementLabel());
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070096 var count = this.components.length;
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070097 for (var i=0; i < count; i++) {
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070098 encoder.writeElement(CCNProtocolDTags.Component, this.components[i]);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070099 }
100 encoder.writeEndElement();
101};
102
Jeff Thompsonf3bd3592012-09-29 23:25:30 -0700103Name.prototype.getElementLabel = function(){
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700104 return CCNProtocolDTags.Name;
105};
106
Jeff Thompsonf3bd3592012-09-29 23:25:30 -0700107Name.prototype.add = function(param){
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700108 return this.components.push(param);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700109};
110