blob: 29291ead256946156e2fe211b217562395abe1e9 [file] [log] [blame]
Meki Cherkaoui97e7a592012-04-14 02:50:06 -07001var Stream = require('stream').Stream;
2var TextXMLCodec = require('TextXMLCodec').TextXMLCodec;
3
4
5
6
7var TextXMLEncoder = function TextXMLEncoder(){
8
9
10 this.ostream = new String();
11};
12
13exports.TextXMLEncoder = TextXMLEncoder;
14
15TextXMLEncoder.prototype.beginEncoding = function(/*OutputStream*/ ostream){
16 if (null == ostream)
17 throw new IllegalArgumentException("TextXMLEncoder: output stream cannot be null!");
18
19
20 /*Start by encoing the begining*/
21 //this.IStream = ostream;
22 this.ostream.write('<?xml version="1.0" encoding="UTF-8"?>');
23};
24
25TextXMLEncoder.prototype.endEncoding =function() {
26 this.IStream.end();
27}
28
29//TextXMLEncoder.prototype.writeStartElement = function(/*long*/ tag, /*TreeMap<String, String>*/ attributes){
30 /* String strTag = tagToString(tag);
31 if (null == strTag) {
32 strTag = XMLDictionaryStack.unknownTagMarker(tag);
33 }
34 writeStartElement(strTag, attributes);
35};*/
36
37
38TextXMLEncoder.prorotype.writeStartElement(/*String*/ tag, /*TreeMap<String, String>*/ attributes) {
39
40
41 this.ostream.write('<'+tab);
42
43 if (null != attributes) {
44
45 for(var i=0; i<attributes.length;i++){
46 this.ostream.write(' '+attributes[i].key +'='+attributes[i].value);
47 }
48
49 // keySet of a TreeMap is ordered
50 }
51 this.ostream.write('>');
52};
53
54TextXMLEncoder.prototype.writeUString = function(/*String*/ utf8Content) {
55
56 this.ostream.write(utf8Content);
57
58};
59
60
61TextXMLEncoder.prototype.writeBlob = function(/*byte []*/ binaryContent, /*int*/ offset, /*int*/ length) {
62
63 this.ostream.write(TextXMLCodec.encodeBinaryElement(binaryContent, offset, length));
64
65};
66
67TextXMLEncoder.prototype.writeElement = function(/*String*/ tag, /*byte[]*/ binaryContent,
68 /*TreeMap<String, String>*/ attributes) {
69
70 /*if (null == attributes) {
71
72 attributes = new TreeMap<String,String>();
73 }*/
74 if (!attributes.containsKey(TextXMLCodec.BINARY_ATTRIBUTE)) {
75 attributes.put(TextXMLCodec.BINARY_ATTRIBUTE, TextXMLCodec.BINARY_ATTRIBUTE_VALUE);
76 }
77 super.writeElement(tag, binaryContent, attributes);
78}
79
80//TextXMLEncoder.prototype.writeElement = function(/*String*/ tag, /*byte[]*/ binaryContent, /*int*/ offset, int length,
81 /* TreeMap<String, String> attributes) throws ContentEncodingException {
82 if (null == attributes) {
83 attributes = new TreeMap<String,String>();
84 }
85 if (!attributes.containsKey(TextXMLCodec.BINARY_ATTRIBUTE)) {
86 attributes.put(TextXMLCodec.BINARY_ATTRIBUTE, TextXMLCodec.BINARY_ATTRIBUTE_VALUE);
87 }
88 super.writeElement(tag, binaryContent, offset, length, attributes);
89 }*/
90
91/* public void writeDateTime(String tag, CCNTime dateTime) throws ContentEncodingException {
92 writeElement(tag, TextXMLCodec.formatDateTime(dateTime));
93 }
94
95 public void writeDateTime(long tag, CCNTime dateTime) throws ContentEncodingException {
96 writeElement(tag, TextXMLCodec.formatDateTime(dateTime));
97 }*/
98
99TextXMLEncoder.prototype.writeEndElement(tag) {
100
101 this.ostream.write('<'+tab+'>');
102
103 };
104
105
106//returns number long
107stringToTag = function(/*String*/ tagName) {
108
109 if (null == tagName) {
110 return null;
111 }
112 Long tagVal = null;
113 if (null != _dictionaryStack) {
114 for (/*XMLDictionary*/ dictionary in _dictionaryStack) {
115 tagVal = dictionary.stringToTag(tagName);
116 if (null != tagVal) {
117 return tagVal;
118 }
119 }
120 }
121
122 /*for (XMLDictionary dictionary : XMLDictionaryStack.getGlobalDictionaries()) {
123 tagVal = dictionary.stringToTag(tagName);
124 if (null != tagVal) {
125 return tagVal;
126 }
127 }*/
128
129 if (XMLDictionaryStack.isUnknownTag(tagName)) {
130 return XMLDictionaryStack.decodeUnknownTag(tagName);
131 }
132 return null;
133};
134