blob: 495984b83516ef099cfec88a7774e7b8f18be426 [file] [log] [blame]
Meki Cherkaouif441d3a2012-04-22 15:17:52 -07001//TODO INCOMPLETE
2/*
3 * @author: ucla-cs
4 *
5 * Encodes CCN object into xml
6 */
7
8var Stream = require('stream').Stream;
9var TextXMLCodec = require('TextXMLCodec').TextXMLCodec;
10
11
12
13
14var TextXMLEncoder = function TextXMLEncoder(){
15
16
17 this.ostream = new String();
18};
19
20exports.TextXMLEncoder = TextXMLEncoder;
21
22TextXMLEncoder.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
32TextXMLEncoder.prototype.endEncoding =function() {
33 this.IStream.end();
34}
35
36
37TextXMLEncoder.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
53TextXMLEncoder.prototype.writeUString = function(/*String*/ utf8Content) {
54
55 this.ostream.write(utf8Content);
56
57};
58
59
60TextXMLEncoder.prototype.writeBlob = function(/*byte []*/ binaryContent, /*int*/ offset, /*int*/ length) {
61
62 this.ostream.write(TextXMLCodec.encodeBinaryElement(binaryContent, offset, length));
63
64};
65
66TextXMLEncoder.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
80TextXMLEncoder.prototype.writeEndElement(tag) {
81
82 this.ostream.write('<'+tab+'>');
83
84 };
85
86
87//returns number long
88stringToTag = 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