Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 1 | //TODO INCOMPLETE |
| 2 | /* |
| 3 | * @author: ucla-cs |
| 4 | * |
| 5 | * Encodes CCN object into xml |
| 6 | */ |
| 7 | |
| 8 | var Stream = require('stream').Stream; |
| 9 | var TextXMLCodec = require('TextXMLCodec').TextXMLCodec; |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | var TextXMLEncoder = function TextXMLEncoder(){ |
| 15 | |
| 16 | |
| 17 | this.ostream = new String(); |
| 18 | }; |
| 19 | |
| 20 | exports.TextXMLEncoder = TextXMLEncoder; |
| 21 | |
| 22 | TextXMLEncoder.prototype.beginEncoding = function(/*OutputStream*/ ostream){ |
| 23 | if (null == ostream) |
| 24 | throw new IllegalArgumentException("TextXMLEncoder: output stream cannot be null!"); |
| 25 | |
| 26 | |
| 27 | /*Start by encoing the begining*/ |
| 28 | //this.IStream = ostream; |
| 29 | this.ostream.write('<?xml version="1.0" encoding="UTF-8"?>'); |
| 30 | }; |
| 31 | |
| 32 | TextXMLEncoder.prototype.endEncoding =function() { |
| 33 | this.IStream.end(); |
| 34 | } |
| 35 | |
| 36 | |
| 37 | TextXMLEncoder.prorotype.writeStartElement(/*String*/ tag, /*TreeMap<String, String>*/ attributes) { |
| 38 | |
| 39 | |
| 40 | this.ostream.write('<'+tab); |
| 41 | |
| 42 | if (null != attributes) { |
| 43 | |
| 44 | for(var i=0; i<attributes.length;i++){ |
| 45 | this.ostream.write(' '+attributes[i].key +'='+attributes[i].value); |
| 46 | } |
| 47 | |
| 48 | // keySet of a TreeMap is ordered |
| 49 | } |
| 50 | this.ostream.write('>'); |
| 51 | }; |
| 52 | |
| 53 | TextXMLEncoder.prototype.writeUString = function(/*String*/ utf8Content) { |
| 54 | |
| 55 | this.ostream.write(utf8Content); |
| 56 | |
| 57 | }; |
| 58 | |
| 59 | |
| 60 | TextXMLEncoder.prototype.writeBlob = function(/*byte []*/ binaryContent, /*int*/ offset, /*int*/ length) { |
| 61 | |
| 62 | this.ostream.write(TextXMLCodec.encodeBinaryElement(binaryContent, offset, length)); |
| 63 | |
| 64 | }; |
| 65 | |
| 66 | TextXMLEncoder.prototype.writeElement = function(/*String*/ tag, /*byte[]*/ binaryContent, |
| 67 | /*TreeMap<String, String>*/ attributes) { |
| 68 | |
| 69 | /*if (null == attributes) { |
| 70 | |
| 71 | attributes = new TreeMap<String,String>(); |
| 72 | }*/ |
| 73 | if (!attributes.containsKey(TextXMLCodec.BINARY_ATTRIBUTE)) { |
| 74 | attributes.put(TextXMLCodec.BINARY_ATTRIBUTE, TextXMLCodec.BINARY_ATTRIBUTE_VALUE); |
| 75 | } |
| 76 | super.writeElement(tag, binaryContent, attributes); |
| 77 | } |
| 78 | |
| 79 | |
| 80 | TextXMLEncoder.prototype.writeEndElement(tag) { |
| 81 | |
| 82 | this.ostream.write('<'+tab+'>'); |
| 83 | |
| 84 | }; |
| 85 | |
| 86 | |
| 87 | //returns number long |
| 88 | stringToTag = function(/*String*/ tagName) { |
| 89 | |
| 90 | if (null == tagName) { |
| 91 | return null; |
| 92 | } |
| 93 | Long tagVal = null; |
| 94 | if (null != _dictionaryStack) { |
| 95 | for (/*XMLDictionary*/ dictionary in _dictionaryStack) { |
| 96 | tagVal = dictionary.stringToTag(tagName); |
| 97 | if (null != tagVal) { |
| 98 | return tagVal; |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | |
| 104 | if (XMLDictionaryStack.isUnknownTag(tagName)) { |
| 105 | return XMLDictionaryStack.decodeUnknownTag(tagName); |
| 106 | } |
| 107 | return null; |
| 108 | }; |
| 109 | |