Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 1 | /* |
| 2 | * @author: ucla-cs |
| 3 | * This class represents ContentName |
| 4 | */ |
| 5 | |
| 6 | |
| 7 | var 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 Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 18 | |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame^] | 19 | if(LOG>3)console.log('Content Name String '+_Components); |
| 20 | this.Components = createNameArray(_Components); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 21 | } |
| 22 | else if(typeof _Components === 'object' && _Components instanceof Array ){ |
| 23 | |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame^] | 24 | if(LOG>4)console.log('Content Name Array '+_Components); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 25 | this.Components = _Components; |
| 26 | |
| 27 | } |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame^] | 28 | else if(_Components==null){ |
| 29 | this.Components =[]; |
| 30 | } |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 31 | else{ |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame^] | 32 | |
| 33 | if(LOG>1)console.log("NO CONTENT NAME GIVEN"); |
| 34 | |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 35 | } |
| 36 | }; |
| 37 | |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame^] | 38 | function createNameArray(name){ |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 39 | |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame^] | 40 | |
| 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 Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 55 | |
| 56 | |
| 57 | ContentName.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 | |
| 70 | ContentName.prototype.encode = function(/*XMLEncoder*/ encoder) { |
| 71 | |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame^] | 72 | if( this.Components ==null ) |
| 73 | throw new Exception("CANNOT ENCODE EMPTY CONTENT NAME"); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 74 | |
| 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 | |
| 83 | ContentName.prototype.getElementLabel = function(){ |
| 84 | return CCNProtocolDTags.Name; |
| 85 | }; |
| 86 | |
| 87 | ContentName.prototype.add = function(param){ |
| 88 | return this.Components.push(param); |
| 89 | }; |
| 90 | |