blob: 8c12188e3311edcda1e83358c0b124f1c0b72247 [file] [log] [blame]
Meki Cherkaouif441d3a2012-04-22 15:17:52 -07001/*
2 * @author: ucla-cs
3 * This class represents ContentName
4 */
5
6
7var ContentName = function ContentName(_Components){
8
9
10 this.SCHEME = "ccnx:";
11
12 this.ORIGINAL_SCHEME = "ccn:";
13
14 this.SEPARATOR = "/";
15 this.ROOT = null;
16
17 if( typeof _Components == 'string') {
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070018
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070019 if(LOG>3)console.log('Content Name String '+_Components);
20 this.Components = createNameArray(_Components);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070021 }
22 else if(typeof _Components === 'object' && _Components instanceof Array ){
23
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070024 if(LOG>4)console.log('Content Name Array '+_Components);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070025 this.Components = _Components;
26
27 }
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070028 else if(_Components==null){
29 this.Components =[];
30 }
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070031 else{
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070032
33 if(LOG>1)console.log("NO CONTENT NAME GIVEN");
34
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070035 }
36};
37
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070038function createNameArray(name){
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070039
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070040
41 //message = decodeURIComponent(message);
42 name = unescape(name);
43
44 var array = name.split('/');
45
46
47 if(name[0]=="/")
48 array=array.slice(1,array.length);
49
50 if(name[name.length-1]=="/")
51 array=array.slice(0,array.length-1);
52
53 return array;
54}
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070055
56
57ContentName.prototype.decode = function(/*XMLDecoder*/ decoder) {
58 decoder.readStartElement(this.getElementLabel());
59
60
61 this.Components = new Array(); //new ArrayList<byte []>();
62
63 while (decoder.peekStartElement(CCNProtocolDTags.Component)) {
64 this.add(decoder.readBinaryElement(CCNProtocolDTags.Component));
65 }
66
67 decoder.readEndElement();
68};
69
70ContentName.prototype.encode = function(/*XMLEncoder*/ encoder) {
71
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070072 if( this.Components ==null )
73 throw new Exception("CANNOT ENCODE EMPTY CONTENT NAME");
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070074
75 encoder.writeStartElement(this.getElementLabel());
76 var count = this.Components.length;
77 for (var i=0; i < count; i++) {
78 encoder.writeElement(CCNProtocolDTags.Component, this.Components[i]);
79 }
80 encoder.writeEndElement();
81};
82
83ContentName.prototype.getElementLabel = function(){
84 return CCNProtocolDTags.Name;
85};
86
87ContentName.prototype.add = function(param){
88 return this.Components.push(param);
89};
90