blob: 59a9b942bd0e43c5ecaf3e496967184790590fac [file] [log] [blame]
Meki Cherkaouif441d3a2012-04-22 15:17:52 -07001/*
2 * @author: ucla-cs
3 * This class represents ContentName
4 */
5
6
Jeff Thompson86aea882012-09-29 17:32:48 -07007var ContentName = function ContentName(_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);
12 this.Components = ContentName.makeBlob(ContentName.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);
17 this.Components = ContentName.makeBlob(_components);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070018
19 }
Jeff Thompson86aea882012-09-29 17:32:48 -070020 else if(_components==null){
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070021 this.Components =[];
22 }
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
Meki Cherkaoui8f173612012-06-06 01:05:40 -070030ContentName.prototype.getName=function(){
31
32 var output = "";
33
34 for(var i=0;i<this.Components.length;i++){
35 output+= "/"+ DataUtils.toString(this.Components[i]);
36 }
37
38 return output;
39
40};
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070041
Meki Cherkaoui8f173612012-06-06 01:05:40 -070042ContentName.makeBlob=function(name){
43
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
58ContentName.createNameArray=function(name){
59
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
76ContentName.prototype.decode = function(/*XMLDecoder*/ decoder) {
77 decoder.readStartElement(this.getElementLabel());
78
79
80 this.Components = new Array(); //new ArrayList<byte []>();
81
82 while (decoder.peekStartElement(CCNProtocolDTags.Component)) {
83 this.add(decoder.readBinaryElement(CCNProtocolDTags.Component));
84 }
85
86 decoder.readEndElement();
87};
88
89ContentName.prototype.encode = function(/*XMLEncoder*/ encoder) {
90
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070091 if( this.Components ==null )
92 throw new Exception("CANNOT ENCODE EMPTY CONTENT NAME");
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070093
94 encoder.writeStartElement(this.getElementLabel());
95 var count = this.Components.length;
96 for (var i=0; i < count; i++) {
97 encoder.writeElement(CCNProtocolDTags.Component, this.Components[i]);
98 }
99 encoder.writeEndElement();
100};
101
102ContentName.prototype.getElementLabel = function(){
103 return CCNProtocolDTags.Name;
104};
105
106ContentName.prototype.add = function(param){
107 return this.Components.push(param);
108};
109