Latest
Browser interest query
Test file is called index.html
diff --git a/latest/CCNProtocolDTags.js b/latest/CCNProtocolDTags.js
new file mode 100644
index 0000000..4305151
--- /dev/null
+++ b/latest/CCNProtocolDTags.js
@@ -0,0 +1,152 @@
+
+
+
+var CCNProtocolDTags = {
+
+ /**
+ * Note if you add one of these, add it to the reverse string map as well.
+ * Emphasize getting the work done at compile time over trying to make something
+ * flexible and developer error-proof.
+ */
+
+ Any : 13,
+ Name : 14,
+ Component : 15,
+ Certificate : 16,
+ Collection : 17,
+ CompleteName : 18,
+ Content : 19,
+ SignedInfo : 20,
+ ContentDigest : 21,
+ ContentHash : 22,
+ Count : 24,
+ Header : 25,
+ Interest : 26, /* 20090915 */
+ Key : 27,
+ KeyLocator : 28,
+ KeyName : 29,
+ Length : 30,
+ Link : 31,
+ LinkAuthenticator : 32,
+ NameComponentCount : 33, /* DeprecatedInInterest */
+ RootDigest : 36,
+ Signature : 37,
+ Start : 38,
+ Timestamp : 39,
+ Type : 40,
+ Nonce : 41,
+ Scope : 42,
+ Exclude : 43,
+ Bloom : 44,
+ BloomSeed : 45,
+ AnswerOriginKind : 47,
+ InterestLifetime : 48,
+ Witness : 53,
+ SignatureBits : 54,
+ DigestAlgorithm : 55,
+ BlockSize : 56,
+ FreshnessSeconds : 58,
+ FinalBlockID : 59,
+ PublisherPublicKeyDigest : 60,
+ PublisherCertificateDigest : 61,
+ PublisherIssuerKeyDigest : 62,
+ PublisherIssuerCertificateDigest : 63,
+ ContentObject : 64, /* 20090915 */
+ WrappedKey : 65,
+ WrappingKeyIdentifier : 66,
+ WrapAlgorithm : 67,
+ KeyAlgorithm : 68,
+ Label : 69,
+ EncryptedKey : 70,
+ EncryptedNonceKey : 71,
+ WrappingKeyName : 72,
+ Action : 73,
+ FaceID : 74,
+ IPProto : 75,
+ Host : 76,
+ Port : 77,
+ MulticastInterface : 78,
+ ForwardingFlags : 79,
+ FaceInstance : 80,
+ ForwardingEntry : 81,
+ MulticastTTL : 82,
+ MinSuffixComponents : 83,
+ MaxSuffixComponents : 84,
+ ChildSelector : 85,
+ RepositoryInfo : 86,
+ Version : 87,
+ RepositoryVersion : 88,
+ GlobalPrefix : 89,
+ LocalName : 90,
+ Policy : 91,
+ Namespace : 92,
+ GlobalPrefixName : 93,
+ PolicyVersion : 94,
+ KeyValueSet : 95,
+ KeyValuePair : 96,
+ IntegerValue : 97,
+ DecimalValue : 98,
+ StringValue : 99,
+ BinaryValue : 100,
+ NameValue : 101,
+ Entry : 102,
+ ACL : 103,
+ ParameterizedName : 104,
+ Prefix : 105,
+ Suffix : 106,
+ Root : 107,
+ ProfileName : 108,
+ Parameters : 109,
+ InfoString : 110,
+ // 111 unallocated
+ StatusResponse : 112,
+ StatusCode : 113,
+ StatusText : 114,
+
+ // Sync protocol
+ SyncNode : 115,
+ SyncNodeKind : 116,
+ SyncNodeElement : 117,
+ SyncVersion : 118,
+ SyncNodeElements : 119,
+ SyncContentHash : 120,
+ SyncLeafCount : 121,
+ SyncTreeDepth : 122,
+ SyncByteCount : 123,
+ ConfigSlice : 124,
+ ConfigSliceList : 125,
+ ConfigSliceOp : 126,
+
+ // Remember to keep in sync with schema/tagnames.csvsdict
+ CCNProtocolDataUnit : 17702112,
+ CCNPROTOCOL_DATA_UNIT : "CCNProtocolDataUnit"
+};
+
+var CCNProtocolDTagsStrings = [
+ null, null, null, null, null, null, null, null, null, null, null,
+ null, null,
+ "Any", "Name", "Component", "Certificate", "Collection", "CompleteName",
+ "Content", "SignedInfo", "ContentDigest", "ContentHash", null, "Count", "Header",
+ "Interest", "Key", "KeyLocator", "KeyName", "Length", "Link", "LinkAuthenticator",
+ "NameComponentCount", null, null, "RootDigest", "Signature", "Start", "Timestamp", "Type",
+ "Nonce", "Scope", "Exclude", "Bloom", "BloomSeed", null, "AnswerOriginKind",
+ "InterestLifetime", null, null, null, null, "Witness", "SignatureBits", "DigestAlgorithm", "BlockSize",
+ null, "FreshnessSeconds", "FinalBlockID", "PublisherPublicKeyDigest", "PublisherCertificateDigest",
+ "PublisherIssuerKeyDigest", "PublisherIssuerCertificateDigest", "ContentObject",
+ "WrappedKey", "WrappingKeyIdentifier", "WrapAlgorithm", "KeyAlgorithm", "Label",
+ "EncryptedKey", "EncryptedNonceKey", "WrappingKeyName", "Action", "FaceID", "IPProto",
+ "Host", "Port", "MulticastInterface", "ForwardingFlags", "FaceInstance",
+ "ForwardingEntry", "MulticastTTL", "MinSuffixComponents", "MaxSuffixComponents", "ChildSelector",
+ "RepositoryInfo", "Version", "RepositoryVersion", "GlobalPrefix", "LocalName",
+ "Policy", "Namespace", "GlobalPrefixName", "PolicyVersion", "KeyValueSet", "KeyValuePair",
+ "IntegerValue", "DecimalValue", "StringValue", "BinaryValue", "NameValue", "Entry",
+ "ACL", "ParameterizedName", "Prefix", "Suffix", "Root", "ProfileName", "Parameters",
+ "InfoString", null,
+ "StatusResponse", "StatusCode", "StatusText", "SyncNode", "SyncNodeKind", "SyncNodeElement",
+ "SyncVersion", "SyncNodeElements", "SyncContentHash", "SyncLeafCount", "SyncTreeDepth", "SyncByteCount",
+ "ConfigSlice", "ConfigSliceList", "ConfigSliceOp" ];
+
+
+//TESTING
+//console.log(exports.CCNProtocolDTagsStrings[17]);
+
diff --git a/latest/CCNTime.js b/latest/CCNTime.js
new file mode 100644
index 0000000..fc6d2ae
--- /dev/null
+++ b/latest/CCNTime.js
@@ -0,0 +1,226 @@
+
+var CCNTime = function CCNTime(
+ //long
+msec) {
+
+
+
+ //byte []
+ //this.binarytime = null;
+
+
+ this.NANOS_MAX = 999877929;
+
+ this.date = new Date(msec);
+};
+
+
+//public CCNTime(long msec) {
+ //this((msec/1000) * 1000, (msec % 1000) * 1000000L);
+//}
+
+ /**
+ * Create a CCNTime
+ * @param timestamp source timestamp to initialize from, some precision will be lost
+ */
+ //public CCNTime(Timestamp timestamp) {
+ //this(timestamp.getTime(), timestamp.getNanos());
+ //}
+
+ /**
+ * Create a CCNTime
+ * @param time source Date to initialize from, some precision will be lost
+ * as CCNTime does not round to unitary milliseconds
+ */
+CCNTime.prototype.setDate = function(
+ //Date
+ date) {
+
+ this.date = date;
+};
+
+ /**
+ * Create a CCNTime from its binary encoding
+ * @param binaryTime12 the binary representation of a CCNTime
+ */
+CCNTime.prototype.setDateBinary = function(
+ //byte []
+ binaryTime12) {
+
+
+ if ((null == binaryTime12) || (binaryTime12.length == 0)) {
+ throw new IllegalArgumentException("Invalid binary time!");
+ }
+
+
+ value = 0;
+ for(i = 0; i < binaryTime12.length; i++) {
+ value = value << 8;
+ // Java will assume the byte is signed, so extend it and trim it.
+ b = (binaryTime12[i]) & 0xFF;
+ value |= b;
+ }
+
+ this.date = new Date(value);
+
+};
+
+//byte[]
+CCNTime.prototype.toBinaryTime = function() {
+
+
+
+ return unsignedLongToByteArray(this.date.getTime());
+
+}
+
+unsignedLongToByteArray= function( value) {
+ if( 0 == value )
+ return [0];
+
+ if( 0 <= value && value <= 0x00FF ) {
+ //byte []
+ bb = new Array[1];
+ bb[0] = (value & 0x00FF);
+ return bb;
+ }
+
+
+ //byte []
+ out = null;
+ //int
+ offset = -1;
+ for(var i = 7; i >=0; --i) {
+ //byte
+ b = ((value >> (i * 8)) & 0xFF);
+ if( out == null && b != 0 ) {
+ out = new Array(i+1);//byte[i+1];
+ offset = i;
+ }
+ if( out != null )
+ out[ offset - i ] = b;
+ }
+ return out;
+}
+
+/**
+ * Generate the binary representation of a CCNTime
+ * @return the binary representation we use for encoding
+ */
+//byte []
+/*CCCCNTime.prototype.toBinaryTime = function() {
+
+ if( null == _binarytime ) {
+ byte [] b = DataUtils.unsignedLongToByteArray(toBinaryTimeAsLong());
+ _binarytime = b;
+ }
+ return _binarytime;
+};*/
+
+ /**
+ * Generate the internal long representation of a CCNTime, useful for comparisons
+ * and used internally
+ * @return the long representation of this time in our internal units
+ */
+ //public long toBinaryTimeAsLong() {
+ //return toBinaryTimeAsLong(getTime(), getNanos());
+ //}
+
+ /**
+ * Static method to convert from milliseconds and nanoseconds to our
+ * internal long representation.
+ * Assumes that nanos also contains the integral milliseconds for this
+ * time. Ignores msec component in msec.
+ * @param msec milliseconds
+ * @param nanos nanoseconds
+ * @return
+ */
+ //public static long toBinaryTimeAsLong(long msec, long nanos) {
+ //long timeVal = (msec / 1000) * 4096L + (nanos * 4096L + 500000000L) / 1000000000L;
+ //return timeVal;
+ //}
+
+ /*protected void setFromBinaryTimeAsLong(long binaryTimeAsLong) {
+ _binarytime = null;
+ super.setTime((binaryTimeAsLong / 4096L) * 1000L);
+ super.setNanos((int)(((binaryTimeAsLong % 4096L) * 1000000000L) / 4096L));
+ }*/
+
+ /*@Override
+ public void setTime(long msec) {
+ _binarytime = null;
+ long binaryTimeAsLong = toBinaryTimeAsLong((msec/1000) * 1000, (msec % 1000) * 1000000L);
+ super.setTime((binaryTimeAsLong / 4096L) * 1000L);
+ super.setNanos((int)(((binaryTimeAsLong % 4096L) * 1000000000L) / 4096L));
+ }*/
+
+ /*@Override
+ public void setNanos(int nanos) {
+ _binarytime = null;
+ int quantizedNanos = (int)(((((nanos * 4096L + 500000000L) / 1000000000L)) * 1000000000L) / 4096L);
+ if ((quantizedNanos < 0) || (quantizedNanos > 999999999)) {
+ System.out.println("Quantizing nanos " + nanos + " resulted in out of range value " + quantizedNanos + "!");
+ }
+ super.setNanos(quantizedNanos);
+ }*/
+
+ /*public void addNanos(int nanos) {
+ _binarytime = null;
+ setNanos(nanos + getNanos());
+ }*/
+
+ /*
+ public void increment(int timeUnits) {
+ _binarytime = null;
+ long binaryTimeAsLong = toBinaryTimeAsLong();
+ binaryTimeAsLong += timeUnits;
+ setFromBinaryTimeAsLong(binaryTimeAsLong);
+ }
+
+ @Override
+ public boolean equals(Timestamp ts) {
+ return super.equals(new CCNTime(ts));
+ }
+
+ @Override
+ public int compareTo(Date o) {
+ return super.compareTo(new CCNTime(o));
+ }
+
+ @Override
+ public int compareTo(Timestamp ts) {
+ return super.compareTo(new CCNTime(ts));
+ }
+
+ @Override
+ public boolean before(Timestamp ts) {
+ return super.before(new CCNTime(ts));
+ }
+
+ @Override
+ public boolean after(Timestamp ts) {
+ return super.after(new CCNTime(ts));
+ }
+
+ @Override
+ public boolean before(Date when) {
+ return super.before(new CCNTime(when));
+ }
+
+ @Override
+ public boolean after(Date when) {
+ return super.after(new CCNTime(when));
+ }
+
+
+ public static CCNTime now() {
+ return new CCNTime();
+ }
+
+
+ public String toShortString() {
+ // use . instead of : as URI printer will make it look nicer in the logs
+ SimpleDateFormat df = new SimpleDateFormat("yy-MM-dd-HH.mm.ss");
+ return df.format(this);
+ }
+}*/
diff --git a/latest/ContentName.js b/latest/ContentName.js
new file mode 100644
index 0000000..9aa90bc
--- /dev/null
+++ b/latest/ContentName.js
@@ -0,0 +1,49 @@
+
+var ContentName = function ContentName(_Components){
+
+
+ this.SCHEME = "ccnx:";
+
+ this.ORIGINAL_SCHEME = "ccn:";
+
+ this.SEPARATOR = "/";
+ this.ROOT = null;
+
+ this.Components = _Components;
+};
+
+ContentName.prototype.decode = function(/*XMLDecoder*/ decoder) {
+ decoder.readStartElement(this.getElementLabel());
+
+
+ this.Components = new Array(); //new ArrayList<byte []>();
+
+ while (decoder.peekStartElement(CCNProtocolDTags.Component)) {
+ this.add(decoder.readBinaryElement(CCNProtocolDTags.Component));
+ }
+
+ decoder.readEndElement();
+};
+
+ContentName.prototype.encode = function(/*XMLEncoder*/ encoder) {
+ //if (!validate()) {
+ //throw new ContentEncodingException("Cannot encode " + this.getClass().getName() + ": field values missing.");
+ //}
+
+ encoder.writeStartElement(this.getElementLabel());
+ var count = this.Components.length;
+ for (var i=0; i < count; i++) {
+ encoder.writeElement(CCNProtocolDTags.Component, this.Components[i]);
+ }
+
+ encoder.writeEndElement();
+};
+
+ContentName.prototype.getElementLabel = function(){
+ return CCNProtocolDTags.Name;
+};
+
+ContentName.prototype.add = function(param){
+ return this.Components.push(param);
+};
+
diff --git a/latest/ContentObject.js b/latest/ContentObject.js
new file mode 100644
index 0000000..7d1759d
--- /dev/null
+++ b/latest/ContentObject.js
@@ -0,0 +1,61 @@
+
+var ContentObject = function ContentObject(_Name,_SignedInfo,_Content,_Signature){
+
+
+ if (typeof _Name === 'string'){
+ var n = new Name(_Name);
+ }
+ else{
+ //TODO Check the class of _Name
+ this.Name = _Name;
+ }
+ this.SignedInfo = _SignedInfo;
+ this.Content=_Content;
+ this.Signature = _Signature;
+
+};
+
+
+ContentObject.prototype.decode = function(/*XMLDecoder*/ decoder) {
+
+ decoder.readStartElement(this.getElementLabel());
+
+ this.Signature = new Signature();
+ this.Signature.decode(decoder);
+
+ this.Name = new ContentName();
+ this.Name.decode(decoder);
+
+ this.SignedInfo = new SignedInfo();
+ this.SignedInfo.decode(decoder);
+
+ this.Content = decoder.readBinaryElement(CCNProtocolDTags.Content);
+
+ decoder.readEndElement();
+
+};
+
+ContentObject.prototype.encode = function(/*XMLEncoder*/ encoder) {
+
+ /*if((null == this.Name) && (null==this.SignedInfo) && (null ==this.Signature)){
+
+ throw "Illegal input inside encode of Content Object";
+ }*/
+ /*
+ * if (!validate()) {
+ throw new ContentEncodingException("Cannot encode " + this.getClass().getName() + ": field values missing.");
+ }*/
+
+ encoder.writeStartElement(this.getElementLabel());
+
+ if(null!=this.Signature) this.Signature.encode(encoder);
+ if(null!=this.Name) this.Name.encode(encoder);
+ if(null!=this.SignedInfo) this.SignedInfo.encode(encoder);
+
+ encoder.writeElement(CCNProtocolDTags.Content, this.Content);
+
+ encoder.writeEndElement();
+
+};
+
+ContentObject.prototype.getElementLabel= function(){return CCNProtocolDTags.ContentObject;};
diff --git a/latest/DateFormat.js b/latest/DateFormat.js
new file mode 100644
index 0000000..1e63760
--- /dev/null
+++ b/latest/DateFormat.js
@@ -0,0 +1,125 @@
+/*
+ * Date Format 1.2.3
+ * (c) 2007-2009 Steven Levithan <stevenlevithan.com>
+ * MIT license
+ *
+ * Includes enhancements by Scott Trenda <scott.trenda.net>
+ * and Kris Kowal <cixar.com/~kris.kowal/>
+ *
+ * Accepts a date, a mask, or a date and a mask.
+ * Returns a formatted version of the given date.
+ * The date defaults to the current date/time.
+ * The mask defaults to dateFormat.masks.default.
+ */
+
+var DateFormat = function () {
+ var token = /d{1,4}|m{1,4}|yy(?:yy)?|([HhMsTt])\1?|[LloSZ]|"[^"]*"|'[^']*'/g,
+ timezone = /\b(?:[PMCEA][SDP]T|(?:Pacific|Mountain|Central|Eastern|Atlantic) (?:Standard|Daylight|Prevailing) Time|(?:GMT|UTC)(?:[-+]\d{4})?)\b/g,
+ timezoneClip = /[^-+\dA-Z]/g,
+ pad = function (val, len) {
+ val = String(val);
+ len = len || 2;
+ while (val.length < len) val = "0" + val;
+ return val;
+ };
+
+ // Regexes and supporting functions are cached through closure
+ return function (date, mask, utc) {
+ var dF = dateFormat;
+
+ // You can't provide utc if you skip other args (use the "UTC:" mask prefix)
+ if (arguments.length == 1 && Object.prototype.toString.call(date) == "[object String]" && !/\d/.test(date)) {
+ mask = date;
+ date = undefined;
+ }
+
+ // Passing date through Date applies Date.parse, if necessary
+ date = date ? new Date(date) : new Date;
+ if (isNaN(date)) throw SyntaxError("invalid date");
+
+ mask = String(dF.masks[mask] || mask || dF.masks["default"]);
+
+ // Allow setting the utc argument via the mask
+ if (mask.slice(0, 4) == "UTC:") {
+ mask = mask.slice(4);
+ utc = true;
+ }
+
+ var _ = utc ? "getUTC" : "get",
+ d = date[_ + "Date"](),
+ D = date[_ + "Day"](),
+ m = date[_ + "Month"](),
+ y = date[_ + "FullYear"](),
+ H = date[_ + "Hours"](),
+ M = date[_ + "Minutes"](),
+ s = date[_ + "Seconds"](),
+ L = date[_ + "Milliseconds"](),
+ o = utc ? 0 : date.getTimezoneOffset(),
+ flags = {
+ d: d,
+ dd: pad(d),
+ ddd: dF.i18n.dayNames[D],
+ dddd: dF.i18n.dayNames[D + 7],
+ m: m + 1,
+ mm: pad(m + 1),
+ mmm: dF.i18n.monthNames[m],
+ mmmm: dF.i18n.monthNames[m + 12],
+ yy: String(y).slice(2),
+ yyyy: y,
+ h: H % 12 || 12,
+ hh: pad(H % 12 || 12),
+ H: H,
+ HH: pad(H),
+ M: M,
+ MM: pad(M),
+ s: s,
+ ss: pad(s),
+ l: pad(L, 3),
+ L: pad(L > 99 ? Math.round(L / 10) : L),
+ t: H < 12 ? "a" : "p",
+ tt: H < 12 ? "am" : "pm",
+ T: H < 12 ? "A" : "P",
+ TT: H < 12 ? "AM" : "PM",
+ Z: utc ? "UTC" : (String(date).match(timezone) || [""]).pop().replace(timezoneClip, ""),
+ o: (o > 0 ? "-" : "+") + pad(Math.floor(Math.abs(o) / 60) * 100 + Math.abs(o) % 60, 4),
+ S: ["th", "st", "nd", "rd"][d % 10 > 3 ? 0 : (d % 100 - d % 10 != 10) * d % 10]
+ };
+
+ return mask.replace(token, function ($0) {
+ return $0 in flags ? flags[$0] : $0.slice(1, $0.length - 1);
+ });
+ };
+}();
+
+// Some common format strings
+DateFormat.masks = {
+ "default": "ddd mmm dd yyyy HH:MM:ss",
+ shortDate: "m/d/yy",
+ mediumDate: "mmm d, yyyy",
+ longDate: "mmmm d, yyyy",
+ fullDate: "dddd, mmmm d, yyyy",
+ shortTime: "h:MM TT",
+ mediumTime: "h:MM:ss TT",
+ longTime: "h:MM:ss TT Z",
+ isoDate: "yyyy-mm-dd",
+ isoTime: "HH:MM:ss",
+ isoDateTime: "yyyy-mm-dd'T'HH:MM:ss",
+ isoUtcDateTime: "UTC:yyyy-mm-dd'T'HH:MM:ss'Z'"
+};
+
+// Internationalization strings
+DateFormat.i18n = {
+ dayNames: [
+ "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat",
+ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
+ ],
+ monthNames: [
+ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
+ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"
+ ]
+};
+
+// For convenience...
+Date.prototype.format = function (mask, utc) {
+ return dateFormat(this, mask, utc);
+};
diff --git a/latest/Exclude.js b/latest/Exclude.js
new file mode 100644
index 0000000..878a6d9
--- /dev/null
+++ b/latest/Exclude.js
@@ -0,0 +1,47 @@
+
+var Exclude = function Exclude(_Values){
+
+ this.OPTIMUM_FILTER_SIZE = 100;
+
+
+ this.Values = _Values; //array of elements
+
+}
+
+Exclude.prototype.decode = function(/*XMLDecoder*/ decoder) {
+
+
+
+ decoder.readStartElement(this.getElementLabel());
+
+ //TODO
+ /*var component;
+ var any = false;
+ while ((component = decoder.peekStartElement(CCNProtocolDTags.Component)) ||
+ (any = decoder.peekStartElement(CCNProtocolDTags.Any)) ||
+ decoder.peekStartElement(CCNProtocolDTags.Bloom)) {
+ var ee = component?new ExcludeComponent(): any ? new ExcludeAny() : new BloomFilter();
+ ee.decode(decoder);
+ _values.add(ee);
+ }*/
+
+ decoder.readEndElement();
+
+};
+
+Exclude.prototype.encode=function(/*XMLEncoder*/ encoder) {
+ if (!validate()) {
+ throw new ContentEncodingException("Cannot encode " + this.getClass().getName() + ": field values missing.");
+ }
+ // if everything is null, output nothing
+ if (empty())
+ return;
+
+ encoder.writeStartElement(getElementLabel());
+
+ encoder.writeEndElement();
+
+ };
+
+Exclude.prototype.getElementLabel = function() { return CCNProtocolDTags.Exclude; };
+
diff --git a/latest/ExcludeAny.js b/latest/ExcludeAny.js
new file mode 100644
index 0000000..898d250
--- /dev/null
+++ b/latest/ExcludeAny.js
@@ -0,0 +1,17 @@
+
+var ExcludeAny = function ExcludeAny() {
+
+};
+
+ExcludeAny.prototype.decode = function(decoder) {
+ decoder.readStartElement(this.getElementLabel());
+ decoder.readEndElement();
+};
+
+
+ExcludeAny.prototype.encode = function( encoder) {
+ encoder.writeStartElement(this.getElementLabel());
+ encoder.writeEndElement();
+};
+
+ExcludeAny.prototype.getElementLabel=function() { return CCNProtocolDTags.Any; };
diff --git a/latest/ExcludeComponent.js b/latest/ExcludeComponent.js
new file mode 100644
index 0000000..1eac988
--- /dev/null
+++ b/latest/ExcludeComponent.js
@@ -0,0 +1,21 @@
+
+/*
+ * _Body is an array of componenets
+ */
+var ExcludeComponent = function ExcludeComponent(_Body) {
+
+ //TODO Check BODY is an Array of componenets.
+
+ this.Body = _Body
+};
+
+ExcludeComponent.prototype.decode = function( decoder) {
+ body = decoder.readBinaryElement(this.getElementLabel());
+};
+
+ExcludeComponent.prototype.encode = function(encoder) {
+ encoder.writeElement(this.getElementLabel(), body);
+};
+
+ExcludeComponent.prototype.getElementLabel = function() { return CCNProtocolDTags.Component; };
+
diff --git a/latest/Interest.js b/latest/Interest.js
new file mode 100644
index 0000000..152704f
--- /dev/null
+++ b/latest/Interest.js
@@ -0,0 +1,124 @@
+
+var Interest = function Interest(_Name,_MinSuffixComponents,_MaxSuffixComponents,_PublisherPublicKeyDigest, _Exclude, _ChildSelector,_AnswerOriginKind,_Scope,_InterestLifetime,_Nonce){
+
+
+ this.Name = _Name;
+ this.MaxSuffixComponents = _MaxSuffixComponents;
+ this.MinSuffixComponents = _MinSuffixComponents;
+
+ this.PublisherKeyDigest = _PublisherPublicKeyDigest;
+ this.Exclude = _Exclude;
+ this.ChildSelector = _ChildSelector;
+ this.AnswerOriginKind = _AnswerOriginKind;
+ this.Scope = _Scope;
+ this.InterestLifetime = null; // For now we don't have the ability to set an interest lifetime
+ this.Nonce = _Nonce;
+
+ /*Not sure if the rest is needed*/
+ this.Publisher = _Publisher;
+
+ this.UserTime=_UserTime;
+
+ this.RECURSIVE_POSTFIX = "*";
+
+ this.CHILD_SELECTOR_LEFT = 0;
+ this.CHILD_SELECTOR_RIGHT = 1;
+ this.ANSWER_CONTENT_STORE = 1;
+ this.ANSWER_GENERATED = 2;
+ this.ANSWER_STALE = 4; // Stale answer OK
+ this.MARK_STALE = 16; // Must have Scope 0. Michael calls this a "hack"
+
+ this.DEFAULT_ANSWER_ORIGIN_KIND = ANSWER_CONTENT_STORE | ANSWER_GENERATED;
+
+
+};
+
+Interest.prototype.decode = function(/*XMLDecoder*/ decoder) {
+
+ decoder.readStartElement(CCNProtocolDTags.Interest);
+
+ this.Name = new ContentName();
+ this.Name.decode(decoder);
+
+ if (decoder.peekStartElement(CCNProtocolDTags.MinSuffixComponents)) {
+ this.MinSuffixComponents = decoder.readIntegerElement(CCNProtocolDTags.MinSuffixComponents);
+ }
+
+ if (decoder.peekStartElement(CCNProtocolDTags.MaxSuffixComponents)) {
+ this.MaxSuffixComponents = decoder.readIntegerElement(CCNProtocolDTags.MaxSuffixComponents);
+ }
+
+ //TODO something about that guy
+ /*if (PublisherID.peek(decoder)) {
+ this.Publisher = new PublisherID();
+ this.Publisher.decode(decoder);
+ }*/
+
+ if (decoder.peekStartElement(CCNProtocolDTags.Exclude)) {
+ this.Exclude = new Exclude();
+ this.Exclude.decode(decoder);
+ }
+
+ if (decoder.peekStartElement(CCNProtocolDTags.ChildSelector)) {
+ this.ChildSelector = decoder.readIntegerElement(CCNProtocolDTags.ChildSelector);
+ }
+
+ if (decoder.peekStartElement(CCNProtocolDTags.AnswerOriginKind)) {
+ // call setter to handle defaulting
+ this.AnswerOriginKind = decoder.readIntegerElement(CCNProtocolDTags.AnswerOriginKind);
+ }
+
+ if (decoder.peekStartElement(CCNProtocolDTags.Scope)) {
+ this.Scope = decoder.readIntegerElement(CCNProtocolDTags.Scope);
+ }
+
+ if (decoder.peekStartElement(CCNProtocolDTags.InterestLifetime)) {
+ this.InterestLifetime = decoder.readBinaryElement(CCNProtocolDTags.InterestLifetime);
+ }
+
+ if (decoder.peekStartElement(CCNProtocolDTags.Nonce)) {
+ this.Nonce = decoder.readBinaryElement(CCNProtocolDTags.Nonce);
+ }
+
+ decoder.readEndElement();
+};
+
+
+Interest.prototype.encode = function(/*XMLEncoder*/ encoder){
+ if (!validate()) {
+ throw new ContentEncodingException("Cannot encode " + this.getClass().getName() + ": field values missing.");
+ }
+
+ encoder.writeStartElement(CCNProtocolDTags.Interest);
+
+ this.Name.encode(encoder);
+
+ if (null != this.MinSuffixComponents)
+ encoder.writeElement(CCNProtocolDTags.MinSuffixComponents, this.MinSuffixComponents);
+
+ if (null != this.MaxSuffixComponents)
+ encoder.writeElement(CCNProtocolDTags.MaxSuffixComponents, this.MaxSuffixComponents);
+
+ //TODO
+
+ /*if (null != this.PublisherID)
+ publisherID().encode(encoder);*/
+
+ //if (null != this.Exclude)
+ //exclude().encode(encoder);
+
+ if (null != this.ChildSelector)
+ encoder.writeElement(CCNProtocolDTags.ChildSelector, this.ChildSelector);
+
+ if (this.DEFAULT_ANSWER_ORIGIN_KIND != this.AnswerOriginKind)
+ encoder.writeElement(CCNProtocolDTags.AnswerOriginKind, this.AnswerOriginKind);
+
+ if (null != this.Scope)
+ encoder.writeElement(CCNProtocolDTags.Scope, this.Scope);
+
+ if (null != this.Nonce)
+ encoder.writeElement(CCNProtocolDTags.Nonce, this.Nonce);
+
+ encoder.writeEndElement();
+};
+
diff --git a/latest/JavaSocketBridge.jar b/latest/JavaSocketBridge.jar
new file mode 100644
index 0000000..56ef77b
--- /dev/null
+++ b/latest/JavaSocketBridge.jar
Binary files differ
diff --git a/latest/KeyLocator.js b/latest/KeyLocator.js
new file mode 100644
index 0000000..73c95fb
--- /dev/null
+++ b/latest/KeyLocator.js
@@ -0,0 +1,119 @@
+
+
+
+
+
+var KeyLocatorType = {
+ NAME:1,
+ KEY:2,
+ CERTIFICATE:3
+};
+
+var KeyLocator = function KeyLocator(_Input,_Type){
+
+ //this.KeyName = _KeyName;
+ //this.PublicKey = _PublicKey;
+ //this.Certificate = _Certificate;
+
+ this.Type=_Type;
+
+ if (_Type==KeyLocatorType.NAME){
+ this.KeyName = _Input;
+ }
+ else if(_Type==KeyLocatorType.KEY){
+ this.PublicKey = _Input;
+ }
+ else if(_Type==KeyLocatorType.CERTIFICATE){
+ this.Certificate = _Input;
+ }
+
+};
+
+KeyLocator.prototype.decode = function(decoder) {
+
+ decoder.readStartElement(this.getElementLabel());
+
+ if (decoder.peekStartElement(CCNProtocolDTags.Key)) {
+ try {
+ encodedKey = decoder.readBinaryElement(CCNProtocolDTags.Key);
+ // This is a DER-encoded SubjectPublicKeyInfo.
+
+ //TODO FIX THIS, SHOULDN'T be like that
+ this.Key = encodedKey;//CryptoUtil.getPublicKey(encodedKey);
+
+ this.PublicKey = encodedKey;
+
+
+ } catch (e) {
+ console.log("Cannot parse stored key: error: " + e);
+ throw new ContentDecodingException("Cannot parse key: ", e);
+ }
+
+ if (null == this.Key) {
+ throw new Exception("Cannot parse key: ");
+ }
+
+ } else if ( decoder.peekStartElement(CCNProtocolDTags.Certificate)) {
+ try {
+ encodedCert = decoder.readBinaryElement(CCNProtocolDTags.Certificate);
+
+ /*
+ * Certificates not yet working
+ */
+
+ //CertificateFactory factory = CertificateFactory.getInstance("X.509");
+ //this.Certificate = (X509Certificate) factory.generateCertificate(new ByteArrayInputStream(encodedCert));
+
+ } catch ( e) {
+ throw new Exception("Cannot decode certificate: " + e);
+ }
+ if (null == this.Certificate) {
+ throw new Exception("Cannot parse certificate! ");
+ }
+ } else {
+ this.KeyName = new KeyName();
+ this.KeyName.decode(decoder);
+ }
+ decoder.readEndElement();
+ }
+
+
+ KeyLocator.prototype.encode = function( encoder) {
+ /*if (!validate()) {
+ throw new ContentEncodingException("Cannot encode " + this.getClass().getName() + ": field values missing.");
+ }*/
+
+
+ //TODO FIX THIS TOO
+ encoder.writeStartElement(this.getElementLabel());
+
+ if (this._Type == KeyLocatorType.KEY) {
+ console.log('HERE');
+
+ encoder.writeElement(CCNProtocolDTags.Key, this.PublicKey);
+
+ } else if (this.Type == KeyLocatorType.CERTIFICATE) {
+ console.log('NO HERE');
+
+ try {
+ encoder.writeElement(CCNProtocolDTags.Certificate, this.Certificate.getEncoded());
+ } catch ( e) {
+ throw new Exception("CertificateEncodingException attempting to write key locator: " + e);
+ }
+
+ } else if (this.Type == KeyLocatorType.NAME) {
+ console.log('ACTUALLY HERE');
+ this.KeyName.encode(encoder);
+ }
+ encoder.writeEndElement();
+
+};
+
+KeyLocator.prototype.getElementLabel = function() {
+ return CCNProtocolDTags.KeyLocator;
+};
+
+KeyLocator.prototype.validate = function() {
+ return ( (null != this.keyName) || (null != this.PublicKey) || (null != this.Certificate) );
+};
+
\ No newline at end of file
diff --git a/latest/KeyName.js b/latest/KeyName.js
new file mode 100644
index 0000000..9c9ba93
--- /dev/null
+++ b/latest/KeyName.js
@@ -0,0 +1,48 @@
+
+
+var KeyName = function KeyName() {
+
+
+ this.ContentName = this.ContentName;//ContentName
+ this.PublisherID =this.PublisherID;//PublisherID
+
+};
+
+
+KeyName.prototype.decode=function( decoder){
+
+
+ decoder.readStartElement(this.getElementLabel());
+
+ this.ContentName = new ContentName();
+ this.ContentName.decode(decoder);
+
+ if (PublisherID.peek(decoder)) {
+ this.PublisherID = new PublisherID();
+ this.PublisherID.decode(decoder);
+ }
+
+ decoder.readEndElement();
+};
+
+KeyName.prototype.encode = function( encoder) {
+ if (!this.validate()) {
+ throw new Exception("Cannot encode : field values missing.");
+ }
+
+ encoder.writeStartElement(this.getElementLabel());
+
+ this.ContentName.encode(encoder);
+ if (null != this.PublisherID)
+ this.PublisherID.encode(encoder);
+
+ encoder.writeEndElement();
+};
+
+KeyName.prototype.getElementLabel = function() { return CCNProtocolDTags.KeyName; };
+
+KeyName.prototype.validate = function() {
+ // DKS -- do we do recursive validation?
+ // null signedInfo ok
+ return (null != this.ContentName);
+};
diff --git a/latest/PublisherID.js b/latest/PublisherID.js
new file mode 100644
index 0000000..dd90aab
--- /dev/null
+++ b/latest/PublisherID.js
@@ -0,0 +1,73 @@
+
+
+var PublisherType = function PublisherType(_tag){
+ this.KEY =(CCNProtocolDTags.PublisherPublicKeyDigest);
+ this.CERTIFICATE= (CCNProtocolDTags.PublisherCertificateDigest);
+ this.ISSUER_KEY= (CCNProtocolDTags.PublisherIssuerKeyDigest);
+ this.ISSUER_CERTIFICATE =(CCNProtocolDTags.PublisherIssuerCertificateDigest);
+
+ this.Tag = _tag;
+};
+PublisherType.prototype.isTypeTagVal = function(tagVal) {
+ if ((tagVal == CCNProtocolDTags.PublisherPublicKeyDigest) ||
+ (tagVal == CCNProtocolDTags.PublisherCertificateDigest) ||
+ (tagVal == CCNProtocolDTags.PublisherIssuerKeyDigest) ||
+ (tagVal == CCNProtocolDTags.PublisherIssuerCertificateDigest)) {
+ return true;
+ }
+ return false;
+};
+
+
+var PublisherID = function PublisherID() {
+
+ this.PUBLISHER_ID_DIGEST_ALGORITHM = "SHA-256";
+ this.PUBLISHER_ID_LEN = 256/8;
+
+
+ this.PublisherID = generatePublicKeyDigest(key);//ByteArray
+ //CryptoUtil.generateKeyID(PUBLISHER_ID_DIGEST_ALGORITHM, key);
+ this.PublisherType = isIssuer ? PublisherType.ISSUER_KEY : PublisherType.KEY;//publisher Type
+
+};
+
+
+PublisherID.prototype.decode = function(decoder) {
+
+ // We have a choice here of one of 4 binary element types.
+ var nextTag = decoder.peekStartElementAsLong();
+
+ if (null == nextTag) {
+ throw new Exception("Cannot parse publisher ID.");
+ }
+
+ this.PublisherType = new PublisherType(nextTag);
+
+ if (!this.PublisherType.isTypeTagVal(nextTag)) {
+ throw new Exception("Invalid publisher ID, got unexpected type: " + nextTag);
+ }
+ this.PublisherID = decoder.readBinaryElement(nextTag);
+ if (null == _publisherID) {
+ throw new ContentDecodingException("Cannot parse publisher ID of type : " + nextTag + ".");
+ }
+};
+
+PublisherID.prototype.encode = function(encoder) {
+ if (!this.validate()) {
+ throw new Exception("Cannot encode " + this.getClass().getName() + ": field values missing.");
+ }
+
+ encoder.writeElement(this.getElementLabel(), this.PublisherID);
+};
+
+PublisherID.prototype.getElementLabel = function() {
+ return this.PublisherType.Tag;
+};
+
+
+PublisherID.prototype.validate = function(){
+ return ((null != id() && (null != type())));
+};
+
+
+
diff --git a/latest/PublisherPublicKeyDigest.js b/latest/PublisherPublicKeyDigest.js
new file mode 100644
index 0000000..f60c649
--- /dev/null
+++ b/latest/PublisherPublicKeyDigest.js
@@ -0,0 +1,32 @@
+
+var PublisherPublicKeyDigest = function PublisherPublicKeyDigest(_pkd){
+
+ if( typeof _pkd == "ByteArray") this.PublisherPublicKeyDigest = _pkd; // Byte Array
+ else if( typeof _pkd == "PublicKey") ;//TODO...
+
+};
+
+PublisherPublicKeyDigest.prototype.decode = function( decoder) {
+
+ this.PublisherPublicKeyDigest = decoder.readBinaryElement(this.getElementLabel());
+ if (null == this.PublisherPublicKeyDigest) {
+ throw new Exception("Cannot parse publisher key digest.");
+ }
+ /*if (this.PublisherPublicKeyDigest.length != PublisherID.PUBLISHER_ID_LEN) {
+ console.log('SHOULD NOT GO HERE !!!!!!!!!!!!!!!!!!');
+ this.PublisherPublicKeyDigest = new PublisherPublicKeyDigest(this.PublisherPublicKeyDigest).PublisherKeyDigest;
+ }*/
+ };
+
+PublisherPublicKeyDigest.prototype.encode= function( encoder) {
+ /*if (!this.validate()) {
+ throw new Exception("Cannot encode : field values missing.");
+ }*/
+ if (this.PublisherKeyDigest!=null)this.encoder.writeElement(this.getElementLabel(), this.PublisherKeyDigest);
+};
+
+PublisherPublicKeyDigest.prototype.getElementLabel = function() { return CCNProtocolDTags.PublisherPublicKeyDigest; };
+
+PublisherPublicKeyDigest.prototype.validate =function() {
+ return (null != this.PublisherKeyDigest);
+};
diff --git a/latest/Signature.js b/latest/Signature.js
new file mode 100644
index 0000000..c562711
--- /dev/null
+++ b/latest/Signature.js
@@ -0,0 +1,53 @@
+
+
+var Signature = function Signature(_Witness,_Signature,_DigestAlgorithm) {
+
+ this.Witness = _Witness;//byte [] _witness;
+ this.Signature = _Signature;//byte [] _signature;
+ this.DigestAlgorithm = _DigestAlgorithm//String _digestAlgorithm;
+};
+
+
+Signature.prototype.decode =function( decoder) {
+ decoder.readStartElement(this.getElementLabel());
+ if (decoder.peekStartElement(CCNProtocolDTags.DigestAlgorithm)) {
+ this.DigestAlgorithm = decoder.readUTF8Element(CCNProtocolDTags.DigestAlgorithm);
+ }
+ if (decoder.peekStartElement(CCNProtocolDTags.Witness)) {
+ this.Witness = decoder.readBinaryElement(CCNProtocolDTags.Witness);
+ }
+ this.Signature = decoder.readBinaryElement(CCNProtocolDTags.SignatureBits);
+ decoder.readEndElement();
+
+};
+
+
+Signature.prototype.encode= function( encoder){
+
+ if (!this.validate()) {
+ throw new Exception("Cannot encode: field values missing.");
+ }
+
+ encoder.writeStartElement(this.getElementLabel());
+
+ if ((null != this.DigestAlgorithm) && (!this.DigestAlgorithm.equals(CCNDigestHelper.DEFAULT_DIGEST_ALGORITHM))) {
+ encoder.writeElement(CCNProtocolDTags.DigestAlgorithm, OIDLookup.getDigestOID(this.DigestAlgorithm));
+ }
+
+ if (null != this.Witness) {
+ // needs to handle null witness
+ encoder.writeElement(CCNProtocolDTags.Witness, this.Witness);
+ }
+
+ encoder.writeElement(CCNProtocolDTags.SignatureBits, this.Signature);
+
+ encoder.writeEndElement();
+};
+
+Signature.prototype.getElementLabel = function() { return CCNProtocolDTags.Signature; };
+
+
+Signature.prototype.validate = function() {
+ return null != this.Signature;
+};
+
diff --git a/latest/SignedInfo.js b/latest/SignedInfo.js
new file mode 100644
index 0000000..9c7f61e
--- /dev/null
+++ b/latest/SignedInfo.js
@@ -0,0 +1,113 @@
+
+var ContentType = {DATA:0, ENCR:1, GONE:2, KEY:3, LINK:4, NACK:5};
+
+var ContentTypeValue = {0:0x0C04C0, 1:0x10D091,2:0x18E344,3:0x28463F,4:0x2C834A,5:0x34008A};
+var ContentTypeValueReverse = {0x0C04C0:0, 0x10D091:1,0x18E344:2,0x28463F:3,0x2C834A:4,0x34008A:5};
+
+
+var SignedInfo = function SignedInfo(_publisher,_timestamp,_type,_locator,_freshnessSeconds,_finalBlockID){
+
+ //TODO, Check types
+
+ this.Publisher = _publisher; //PublisherPublicKeyDigest
+ this.Timestamp=_timestamp; // CCN Time
+ this.Type=_type; // ContentType
+ this.Locator =_locator;//KeyLocator
+ this.FreshnessSeconds =_freshnessSeconds; // Integer
+ this.FinalBlockID=_finalBlockID; //byte array
+
+};
+
+//exports.SignedInfo.Working;
+
+
+SignedInfo.prototype.decode = function( decoder){
+
+ decoder.readStartElement( this.getElementLabel() );
+
+ if (decoder.peekStartElement(CCNProtocolDTags.PublisherPublicKeyDigest)) {
+ this.Publisher = new PublisherPublicKeyDigest();
+ this.Publisher.decode(decoder);
+ }
+
+ if (decoder.peekStartElement(CCNProtocolDTags.Timestamp)) {
+ this.Timestamp = decoder.readDateTime(CCNProtocolDTags.Timestamp);
+ }
+
+ if (decoder.peekStartElement(CCNProtocolDTags.Type)) {
+ binType = decoder.readBinaryElement(CCNProtocolDTags.Type);//byte []
+ //TODO Implement valueToType
+
+ this.Type = valueToType(binType);
+ //console.log('GOT HERRRRRRRRRRRRRRRRRE');
+ //console.log(binType);
+ if (null == this.Type) {
+ throw new Exception("Cannot parse signedInfo type: bytes.");
+ }
+
+ } else {
+ this.Type = ContentType.DATA; // default
+ }
+
+ if (decoder.peekStartElement(CCNProtocolDTags.FreshnessSeconds)) {
+ this.FreshnessSeconds = decoder.readIntegerElement(CCNProtocolDTags.FreshnessSeconds);
+ }
+
+ if (decoder.peekStartElement(CCNProtocolDTags.FinalBlockID)) {
+ this.FinalBlockID = decoder.readBinaryElement(CCNProtocolDTags.FinalBlockID);
+ }
+
+ if (decoder.peekStartElement(CCNProtocolDTags.KeyLocator)) {
+ this.Locator = new KeyLocator();
+ this.Locator.decode(decoder);
+ }
+
+ decoder.readEndElement();
+};
+
+SignedInfo.prototype.encode = function( encoder) {
+ if (!this.validate()) {
+ throw new Exception("Cannot encode : field values missing.");
+ }
+ encoder.writeStartElement(this.getElementLabel());
+
+ if (null!=this.Publisher) {
+ this.Publisher.encode(encoder);
+ }
+
+ if (null!=this.Timestamp) {
+ encoder.writeDateTime(CCNProtocolDTags.Timestamp, this.Timestamp);
+ }
+
+ if (null!=this.Type && this.Type !=0) {
+
+ encoder.writeElement(CCNProtocolDTags.Type, this.Type);
+ }
+
+ if (null!=this.FreshnessSeconds) {
+ encoder.writeElement(CCNProtocolDTags.FreshnessSeconds, this.FreshnessSeconds);
+ }
+
+ if (null!=this.FinalBlockID) {
+ encoder.writeElement(CCNProtocolDTags.FinalBlockID, this.FinalBlockID);
+ }
+
+ if (null!=this.Locator) {
+ this.Locator.encode(encoder);
+ }
+
+ encoder.writeEndElement();
+};
+
+SignedInfo.prototype.getElementLabel = function() {
+ return CCNProtocolDTags.SignedInfo;
+};
+
+SignedInfo.prototype.validate = function() {
+ // We don't do partial matches any more, even though encoder/decoder
+ // is still pretty generous.
+ if (null ==this.Publisher || null==this.Timestamp ||null== this.Locator)
+ return false;
+ return true;
+ };
+
diff --git a/latest/encoding/BinaryXMLCodec.js b/latest/encoding/BinaryXMLCodec.js
new file mode 100644
index 0000000..349f4e5
--- /dev/null
+++ b/latest/encoding/BinaryXMLCodec.js
@@ -0,0 +1,338 @@
+
+var XML_EXT = 0x00;
+
+var XML_TAG = 0x01;
+
+var XML_DTAG = 0x02;
+
+var XML_ATTR = 0x03;
+
+var XML_DATTR = 0x04;
+
+var XML_BLOB = 0x05;
+
+var XML_UDATA = 0x06;
+
+var XML_CLOSE = 0x0;
+
+var XML_SUBTYPE_PROCESSING_INSTRUCTIONS = 16;
+
+
+var XML_TT_BITS = 3;
+var XML_TT_MASK = ((1 << XML_TT_BITS) - 1);
+var XML_TT_VAL_BITS = XML_TT_BITS + 1;
+var XML_TT_VAL_MASK = ((1 << (XML_TT_VAL_BITS)) - 1);
+var XML_REG_VAL_BITS = 7;
+var XML_REG_VAL_MASK = ((1 << XML_REG_VAL_BITS) - 1);
+var XML_TT_NO_MORE = (1 << XML_REG_VAL_BITS); // 0x80
+var BYTE_MASK = 0xFF;
+var LONG_BYTES = 8;
+var LONG_BITS = 64;
+
+var bits_11 = 0x0000007FF;
+var bits_18 = 0x00003FFFF;
+var bits_32 = 0x0FFFFFFFF;
+
+
+var TypeAndVal = function TypeAndVal(_type,_val) {
+ this.type = _type;
+ this.val = _val;
+
+};
+
+var BinaryXMLCodec = function BinaryXMLCodec(){
+ this.CODEC_NAME = "Binary";
+};
+
+BinaryXMLCodec.prototype.encodeTypeAndValOffset = function(
+ //int
+ type,
+ //long
+ val,
+ //byte []
+ buf,
+ //int
+ offset) {
+
+ if ((type > XML_UDATA) || (type < 0) || (val < 0)) {
+ throw new Exception("Tag and value must be positive, and tag valid.");
+ }
+
+ // Encode backwards. Calculate how many bytes we need:
+ var/*int*/ numEncodingBytes = numEncodingBytes(val);
+
+ if ((offset + numEncodingBytes) > buf.length) {
+ throw new Exception("Buffer space of " + (buf.length-offset) +
+ " bytes insufficient to hold " +
+ numEncodingBytes + " of encoded type and value.");
+ }
+
+
+ buf[offset + numEncodingBytes - 1] =
+ (BYTE_MASK &
+ (((XML_TT_MASK & type) |
+ ((XML_TT_VAL_MASK & val) << XML_TT_BITS))) |
+ XML_TT_NO_MORE);
+ val = val >>> XML_TT_VAL_BITS;;
+
+ var /*int*/ i = offset + numEncodingBytes - 2;
+ while ((0 != val) && (i >= offset)) {
+ buf[i] = (BYTE_MASK &
+ (val & XML_REG_VAL_MASK));
+ val = val >>> XML_REG_VAL_BITS;
+ --i;
+ }
+
+ return numEncodingBytes;
+};
+
+//BinaryXMLCodec.prototype.encodeTypeAndVal = function(
+ // //final int
+ //type,
+ //final long
+ //value,
+ //final OutputStream
+ //ostream,
+ //offset){
+
+ /*
+ We exploit the fact that encoding is done from the right, so this actually means
+ there is a deterministic encoding from a long to a Type/Value pair:
+
+ | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
+ |ABCD.EFGH|IJKL.MNOP|QRST.UVWX|YZ01.2345|6789.abcd|efgh.ijkl|mnop.qrst|uvwx.yz@#
+
+ 60> 53> 46> 39> 32> 25> 18> 11> 4>
+ |_000.ABCD|_EFG.HIJK|_LMN.OPQR|_STU.VWXY|_Z01.2345|_678.9abc|_defg.hij|_klm.nopq|_rst.uvwx|_yz@#___
+
+ What we want to do is compute the result in MSB order and write it directly
+ to the channel without any intermediate form.
+ */
+
+ //var/*int*/ bits;
+ //var/*int*/ count = 0;
+
+ // once we start writing bits, we keep writing bits even if they are "0"
+ // var/*bool*/ writing = false;
+
+ // a few heuristic to catch the small-bit length patterns
+ /*if( value < 0 || value > 15 ) {
+ var start = 60;
+ if( 0 <= value ) {
+ if( value < bits_11 )
+ start = 4;
+ else if( value < bits_18 )
+ start = 11;
+ else if( value < bits_32 )
+ start = 25;
+ }
+
+ for( var i = start; i >= 4; i -= 7) {
+ bits = (value >>> i) & BinaryXMLCodec.XML_REG_VAL_MASK;
+ if( bits != 0 || writing ) {
+ ostream.write(bits);
+ count++;
+ writing = true;
+ }
+ }
+ }
+
+ // Explicit computation of the bottom byte
+ bits = type & BinaryXMLCodec.XML_TT_MASK;
+ var bottom4 = value & BinaryXMLCodec.XML_TT_VAL_MASK;
+ bits |= bottom4 << BinaryXMLCodec.XML_TT_BITS;
+ // the bottom byte always has the NO_MORE flag
+ bits |= BinaryXMLCodec.XML_TT_NO_MORE;
+
+ //console.log(ostream.constructor.name);
+ //console.log(ostream);
+
+ //ostream.writable = true;
+ //console.log(ostream.)
+ ostream.write(bits.toString());
+
+ count++;
+
+// byte [] encoding = encodeTypeAndVal(tag, val);
+// ostream.write(encoding);
+ return count;
+
+}*/
+
+
+BinaryXMLCodec.prototype.encodeTypeAndVal = function(
+ //int
+ type,
+ //long
+ val,
+ //byte []
+ buf,
+ //int
+ offset) {
+
+ if ((type > XML_UDATA) || (type < 0) || (val < 0)) {
+ throw new Exception("Tag and value must be positive, and tag valid.");
+ }
+
+ // Encode backwards. Calculate how many bytes we need:
+ //int
+ var numEncodingBytes = numEncodingBytes(val);
+
+ if ((offset + numEncodingBytes) > buf.length) {
+ throw new Exception("Buffer space of " + (buf.length-offset) +
+ " bytes insufficient to hold " +
+ numEncodingBytes + " of encoded type and value.");
+ }
+
+ // Bottom 4 bits of val go in last byte with tag.
+ buf[offset + numEncodingBytes - 1] =
+ //(byte)
+ (BYTE_MASK &
+ (((XML_TT_MASK & type) |
+ ((XML_TT_VAL_MASK & val) << XML_TT_BITS))) |
+ XML_TT_NO_MORE); // set top bit for last byte
+ val = val >>> XML_TT_VAL_BITS;;
+
+ // Rest of val goes into preceding bytes, 7 bits per byte, top bit
+ // is "more" flag.
+ var i = offset + numEncodingBytes - 2;
+ while ((0 != val) && (i >= offset)) {
+ buf[i] = //(byte)
+ (BYTE_MASK &
+ (val & XML_REG_VAL_MASK)); // leave top bit unset
+ val = val >>> XML_REG_VAL_BITS;
+ --i;
+ }
+ if (val != 0) {
+ throw new Exception( "This should not happen: miscalculated encoding");
+ //Log.warning(Log.FAC_ENCODING, "This should not happen: miscalculated encoding length, have " + val + " left.");
+ }
+
+ return numEncodingBytes;
+}
+
+
+
+BinaryXMLCodec.prototype.decodeTypeAndVal = function(
+ /*InputStream*/
+ istream) {
+
+ /*int*/next;
+ /*int*/type = -1;
+ /*long*/val = 0;
+ /*boolean*/more = true;
+
+ do {
+ next = istream.read();
+
+ if (next < 0) {
+ return null;
+ }
+
+ if ((0 == next) && (0 == val)) {
+ return null;
+ }
+
+ more = (0 == (next & XML_TT_NO_MORE));
+
+ if (more) {
+ val = val << XML_REG_VAL_BITS;
+ val |= (next & XML_REG_VAL_MASK);
+ } else {
+
+ type = next & XML_TT_MASK;
+ val = val << XML_TT_VAL_BITS;
+ val |= ((next >>> XML_TT_BITS) & XML_TT_VAL_MASK);
+ }
+
+ } while (more);
+
+ return new TypeAndVal(type, val);
+};
+
+BinaryXMLCodec.prototype.encodeUString = function(
+ //OutputStream
+ ostream,
+ //String
+ ustring,
+ //byte
+ type,
+ offset) {
+
+ // We elide the encoding of a 0-length UString
+ if ((null == ustring) || (ustring.length == 0)) {
+ //if (Log.isLoggable(Log.FAC_ENCODING, Level.FINER))
+ //Log.finer(Log.FAC_ENCODING, "Eliding 0-length UString.");
+ return;
+ }
+
+ //byte [] data utils
+ /*custom*/
+ //byte[]
+ strBytes = new Array(ustring.Length);
+ var i = 0;
+ for( ;i<ustring.lengh;i++) //in InStr.ToCharArray())
+ {
+ strBytes[i] = ustring[i];
+ }
+ //strBytes = DataUtils.getBytesFromUTF8String(ustring);
+
+ this.encodeTypeAndVal(type,
+ (((type == XML_TAG) || (type == XML_ATTR)) ?
+ (strBytes.length-1) :
+ strBytes.length), ostream);
+ //
+ console.log(strBytes.toString());
+
+ ostream.write(strBytes.toString(),offset);
+
+ //
+};
+
+BinaryXMLCodec.prototype.encodeBlob = function(
+ //OutputStream
+ ostream,
+ //byte []
+ blob,
+ //int
+ offset,
+ //int
+ length) {
+ // We elide the encoding of a 0-length blob
+ if ((null == blob) || (length == 0)) {
+
+ return;
+ }
+
+ encodeTypeAndVal(XML_BLOB, length, ostream,offset);
+ if (null != blob) {
+ ostream.write(blob, this.offset, length);
+ this.offset += length;
+ }
+};
+
+
+var ENCODING_LIMIT_1_BYTE = ((1 << (XML_TT_VAL_BITS)) - 1);
+var ENCODING_LIMIT_2_BYTES = ((1 << (XML_TT_VAL_BITS + XML_REG_VAL_BITS)) - 1);
+var ENCODING_LIMIT_3_BYTES = ((1 << (XML_TT_VAL_BITS + 2 * XML_REG_VAL_BITS)) - 1);
+
+var numEncodingBytes = function(
+ //long
+ x) {
+ if (x <= ENCODING_LIMIT_1_BYTE) return (1);
+ if (x <= ENCODING_LIMIT_2_BYTES) return (2);
+ if (x <= ENCODING_LIMIT_3_BYTES) return (3);
+
+ var numbytes = 1;
+
+ // Last byte gives you XML_TT_VAL_BITS
+ // Remainder each give you XML_REG_VAL_BITS
+ x = x >>> XML_TT_VAL_BITS;
+ while (x != 0) {
+ numbytes++;
+ x = x >>> XML_REG_VAL_BITS;
+ }
+ return (numbytes);
+}
+
+//TODO
\ No newline at end of file
diff --git a/latest/encoding/BinaryXMLDecoder.js b/latest/encoding/BinaryXMLDecoder.js
new file mode 100644
index 0000000..87a3530
--- /dev/null
+++ b/latest/encoding/BinaryXMLDecoder.js
@@ -0,0 +1,812 @@
+//TODO:go back on DEBUG stuff
+//MARK LENGTH STUFF NOT SURE WHAT IT"S ABOUT
+//PUSH instead of PUT on attributes
+var XML_EXT = 0x00;
+
+var XML_TAG = 0x01;
+
+var XML_DTAG = 0x02;
+
+var XML_ATTR = 0x03;
+
+var XML_DATTR = 0x04;
+
+var XML_BLOB = 0x05;
+
+var XML_UDATA = 0x06;
+
+var XML_CLOSE = 0x0;
+
+var XML_SUBTYPE_PROCESSING_INSTRUCTIONS = 16;
+
+
+var XML_TT_BITS = 3;
+var XML_TT_MASK = ((1 << XML_TT_BITS) - 1);
+var XML_TT_VAL_BITS = XML_TT_BITS + 1;
+var XML_TT_VAL_MASK = ((1 << (XML_TT_VAL_BITS)) - 1);
+var XML_REG_VAL_BITS = 7;
+var XML_REG_VAL_MASK = ((1 << XML_REG_VAL_BITS) - 1);
+var XML_TT_NO_MORE = (1 << XML_REG_VAL_BITS); // 0x80
+var BYTE_MASK = 0xFF;
+var LONG_BYTES = 8;
+var LONG_BITS = 64;
+
+var bits_11 = 0x0000007FF;
+var bits_18 = 0x00003FFFF;
+var bits_32 = 0x0FFFFFFFF;
+
+
+
+//returns a string
+tagToString = function(/*long*/ tagVal) {
+ if ((tagVal >= 0) && (tagVal < CCNProtocolDTagsStrings.length)) {
+ return CCNProtocolDTagsStrings[tagVal];
+ } else if (tagVal == CCNProtocolDTags.CCNProtocolDataUnit) {
+ return CCNProtocolDTags.CCNPROTOCOL_DATA_UNIT;
+ }
+ return null;
+};
+
+//returns a Long
+stringToTag = function(/*String*/ tagName) {
+ // the slow way, but right now we don't care.... want a static lookup for the forward direction
+ for (var i=0; i < CCNProtocolDTagsStrings.length; ++i) {
+ if ((null != CCNProtocolDTagsStrings[i]) && (CCNProtocolDTagsStrings[i] == tagName)) {
+ return i;
+ }
+ }
+ if (CCNProtocolDTags.CCNPROTOCOL_DATA_UNIT == tagName) {
+ return CCNProtocolDTags.CCNProtocolDataUnit;
+ }
+ return null;
+};
+
+//console.log(stringToTag(64));
+var BinaryXMLDecoder = function BinaryXMLDecoder(istream){
+ var MARK_LEN=512;
+ var DEBUG_MAX_LEN = 32768;
+
+ this.istream = istream;
+ //console.log('istream is '+ this.istream);
+ this.offset = 0;
+};
+
+
+BinaryXMLDecoder.prototype.readStartElement =function(
+ //String
+ startTag,
+ //TreeMap<String, String>
+ attributes) {
+
+ try {
+ //this.TypeAndVal
+ tv = this.decodeTypeAndVal(this.istream);
+
+ if (null == tv) {
+ throw new Exception("Expected start element: " + startTag + " got something not a tag.");
+ }
+
+ //String
+ decodedTag = null;
+
+ if (tv.type() == XML_TAG) {
+
+ decodedTag = this.decodeUString(this.Istream, tv.val()+1);
+
+ } else if (tv.type() == XML_DTAG) {
+ decodedTag = tagToString(tv.val());
+ }
+
+ if ((null == decodedTag) || decodedTag != startTag) {
+ throw new Exception("Expected start element: " + startTag + " got: " + decodedTag + "(" + tv.val() + ")");
+ }
+
+ if (null != attributes) {
+ readAttributes(attributes);
+ }
+
+ } catch (e) {
+ throw new Exception("readStartElement", e);
+ }
+};
+
+BinaryXMLDecoder.prototype.readAttributes = function(
+ //TreeMap<String,String>
+ attributes){
+
+ if (null == attributes) {
+ return;
+ }
+
+ try {
+
+ //this.TypeAndVal
+ nextTV = this.peekTypeAndVal(this.istream);
+
+ while ((null != nextTV) && ((XML_ATTR == nextTV.type()) ||
+ (XML_DATTR == nextTV.type()))) {
+
+ //this.TypeAndVal
+ thisTV = this.decodeTypeAndVal(this.Istream);
+
+ var attributeName = null;
+ if (XML_ATTR == thisTV.type()) {
+
+ attributeName = this.decodeUString(this.istream, thisTV.val()+1);
+
+ } else if (XML_DATTR == thisTV.type()) {
+ // DKS TODO are attributes same or different dictionary?
+ attributeName = tagToString(thisTV.val());
+ if (null == attributeName) {
+ throw new ContentDecodingException("Unknown DATTR value" + thisTV.val());
+ }
+ }
+
+ var attributeValue = this.decodeUString(this.istream);
+
+ attributes.put(attributeName, attributeValue);
+
+ nextTV = this.peekTypeAndVal(this.istream);
+ }
+
+ } catch ( e) {
+
+ throw new ContentDecodingException("readStartElement", e);
+ }
+};
+
+
+BinaryXMLDecoder.prototype.initializeDecoding = function() {
+ //if (!this.istream.markSupported()) {
+ //throw new IllegalArgumentException(this.getClass().getName() + ": input stream must support marking!");
+ //}
+}
+
+BinaryXMLDecoder.prototype.readStartDocument = function(){
+ // Currently no start document in binary encoding.
+ }
+
+BinaryXMLDecoder.prototype.readEndDocument = function() {
+ // Currently no end document in binary encoding.
+ };
+
+BinaryXMLDecoder.prototype.readStartElement = function(
+ //String
+ startTag,
+ //TreeMap<String, String>
+ attributes) {
+
+
+ //NOT SURE
+ //if(typeof startTag == 'number')
+ //startTag = tagToString(startTag);
+
+ //try {
+ //TypeAndVal
+ tv = this.decodeTypeAndVal(this.istream);
+
+ if (null == tv) {
+ throw new Exception("Expected start element: " + startTag + " got something not a tag.");
+ }
+
+ //String
+ decodedTag = null;
+ console.log(tv);
+ console.log(typeof tv);
+
+ console.log(XML_TAG);
+ if (tv.type() == XML_TAG) {
+ //console.log('got here');
+ //Log.info(Log.FAC_ENCODING, "Unexpected: got tag in readStartElement; looking for tag " + startTag + " got length: " + (int)tv.val()+1);
+ // Tag value represents length-1 as tags can never be empty.
+ var valval ;
+ if(typeof tv.val() == 'string'){
+ valval = (parseInt(tv.val())) + 1;
+ }
+ else
+ valval = (tv.val())+ 1;
+
+ console.log('valval is ' +valval);
+
+ decodedTag = this.decodeUString(this.istream, valval);
+
+ } else if (tv.type() == XML_DTAG) {
+ console.log('gothere');
+ //console.log(tv.val());
+ //decodedTag = tagToString(tv.val());
+ //console.log()
+ decodedTag = tv.val();
+ }
+
+ //console.log(decodedTag);
+ console.log('startTag is '+startTag);
+
+
+ if ((null == decodedTag) || decodedTag != startTag ) {
+ console.log('expecting '+ startag + ' but got '+ decodedTag);
+ throw new Exception("Expected start element: " + startTag + " got: " + decodedTag + "(" + tv.val() + ")");
+ }
+
+ // DKS: does not read attributes out of stream if caller doesn't
+ // ask for them. Should possibly peek and skip over them regardless.
+ // TODO: fix this
+ if (null != attributes) {
+ readAttributes(attributes);
+ }
+
+ //} catch ( e) {
+ //console.log(e);
+ //throw new Exception("readStartElement", e);
+ //}
+ }
+
+
+BinaryXMLDecoder.prototype.readAttributes = function(
+ //TreeMap<String,String>
+ attributes) {
+
+ if (null == attributes) {
+ return;
+ }
+
+ try {
+ // Now need to get attributes.
+ //TypeAndVal
+ nextTV = this.peekTypeAndVal(this.istream);
+
+ while ((null != nextTV) && ((XML_ATTR == nextTV.type()) ||
+ (XML_DATTR == nextTV.type()))) {
+
+ // Decode this attribute. First, really read the type and value.
+ //this.TypeAndVal
+ thisTV = this.decodeTypeAndVal(this.istream);
+
+ //String
+ attributeName = null;
+ if (XML_ATTR == thisTV.type()) {
+ // Tag value represents length-1 as attribute names cannot be empty.
+ var valval ;
+ if(typeof tv.val() == 'string'){
+ valval = (parseInt(tv.val())) + 1;
+ }
+ else
+ valval = (tv.val())+ 1;
+
+ attributeName = this.decodeUString(this.istream,valval);
+
+ } else if (XML_DATTR == thisTV.type()) {
+ // DKS TODO are attributes same or different dictionary?
+ attributeName = tagToString(thisTV.val());
+ if (null == attributeName) {
+ throw new Exception("Unknown DATTR value" + thisTV.val());
+ }
+ }
+ // Attribute values are always UDATA
+ //String
+ attributeValue = this.decodeUString(this.istream);
+
+ //
+ attributes.push([attributeName, attributeValue]);
+
+ nextTV = this.peekTypeAndVal(this.istream);
+ }
+
+ } catch ( e) {
+ Log.logStackTrace(Log.FAC_ENCODING, Level.WARNING, e);
+ throw new Exception("readStartElement", e);
+ }
+};
+
+//returns a string
+BinaryXMLDecoder.prototype.peekStartElementAsString = function() {
+ //this.istream.mark(MARK_LEN);
+
+ //String
+ decodedTag = null;
+ var previousOffset = this.offset;
+ try {
+ // Have to distinguish genuine errors from wrong tags. Could either use
+ // a special exception subtype, or redo the work here.
+ //this.TypeAndVal
+ tv = this.decodeTypeAndVal(this.istream);
+
+ if (null != tv) {
+
+ if (tv.type() == XML_TAG) {
+ /*if (tv.val()+1 > DEBUG_MAX_LEN) {
+ throw new ContentDecodingException("Decoding error: length " + tv.val()+1 + " longer than expected maximum length!");
+ }*/
+
+ // Tag value represents length-1 as tags can never be empty.
+ var valval ;
+ if(typeof tv.val() == 'string'){
+ valval = (parseInt(tv.val())) + 1;
+ }
+ else
+ valval = (tv.val())+ 1;
+
+ decodedTag = this.decodeUString(this.istream, valval);
+
+ //Log.info(Log.FAC_ENCODING, "Unexpected: got text tag in peekStartElement; length: " + valval + " decoded tag = " + decodedTag);
+
+ } else if (tv.type() == XML_DTAG) {
+ decodedTag = tagToString(tv.val());
+ }
+
+ } // else, not a type and val, probably an end element. rewind and return false.
+
+ } catch ( e) {
+ /*try {
+ this.istream.reset();
+ this.istream.mark(MARK_LEN);
+ long ms = System.currentTimeMillis();
+ File tempFile = new File("data_" + Long.toString(ms) + ".ccnb");
+ FileOutputStream fos = new FileOutputStream(tempFile);
+ try {
+ byte buf[] = new byte[1024];
+ while (this.istream.available() > 0) {
+ int count = this.istream.read(buf);
+ fos.write(buf,0, count);
+ }
+ } finally {
+ fos.close();
+ }
+ this.istream.reset();
+ Log.info(Log.FAC_ENCODING, "BinaryXMLDecoder: exception in peekStartElement, dumping offending object to file: " + tempFile.getAbsolutePath());
+ throw e;
+
+ } catch (IOException ie) {
+ Log.warning(Log.FAC_ENCODING, "IOException in BinaryXMLDecoder error handling: " + e.getMessage());
+ Log.logStackTrace(Log.FAC_ENCODING, Level.WARNING, ie);
+ throw new ContentDecodingException("peekStartElement", e);
+
+ }
+ } catch (IOException e) {
+ Log.warning(Log.FAC_ENCODING, "IOException in BinaryXMLDecoder: " + e.getMessage());
+ Log.logStackTrace(Log.FAC_ENCODING, Level.WARNING, e);
+ throw new ContentDecodingException("peekStartElement", e);
+ */
+ } finally {
+ try {
+ //this.istream.reset();
+ this.offset = previousOffset;
+ } catch ( e) {
+ Log.logStackTrace(Log.FAC_ENCODING, Level.WARNING, e);
+ throw new ContentDecodingException("Cannot reset stream! " + e.getMessage(), e);
+ }
+ }
+ return decodedTag;
+};
+
+BinaryXMLDecoder.prototype.peekStartElement = function(
+ //String
+ startTag) {
+ //String
+ if(typeof startTag == 'string'){
+ decodedTag = this.peekStartElementAsString();
+
+ if ((null != decodedTag) && decodedTag == startTag) {
+ return true;
+ }
+ return false;
+ }
+ else if(typeof startTag == 'number'){
+ decodedTag = this.peekStartElementAsLong();
+ if ((null != decodedTag) && decodedTag == startTag) {
+ return true;
+ }
+ return false;
+ }
+ else{
+ throw new Exception("SHOULD BE STRING OR NUMBER");
+ }
+}
+//returns Long
+BinaryXMLDecoder.prototype.peekStartElementAsLong = function() {
+ //this.istream.mark(MARK_LEN);
+
+ //Long
+ decodedTag = null;
+
+ var previousOffset = this.offset;
+
+ try {
+ // Have to distinguish genuine errors from wrong tags. Could either use
+ // a special exception subtype, or redo the work here.
+ //this.TypeAndVal
+ tv = this.decodeTypeAndVal(this.istream);
+
+ if (null != tv) {
+
+ if (tv.type() == XML_TAG) {
+ if (tv.val()+1 > DEBUG_MAX_LEN) {
+ throw new ContentDecodingException("Decoding error: length " + tv.val()+1 + " longer than expected maximum length!");
+ }
+
+ var valval ;
+ if(typeof tv.val() == 'string'){
+ valval = (parseInt(tv.val())) + 1;
+ }
+ else
+ valval = (tv.val())+ 1;
+
+ // Tag value represents length-1 as tags can never be empty.
+ //String
+ strTag = this.decodeUString(this.istream, valval);
+
+ decodedTag = stringToTag(strTag);
+
+ //Log.info(Log.FAC_ENCODING, "Unexpected: got text tag in peekStartElement; length: " + valval + " decoded tag = " + decodedTag);
+
+ } else if (tv.type() == XML_DTAG) {
+ decodedTag = tv.val();
+ }
+
+ } // else, not a type and val, probably an end element. rewind and return false.
+
+ } catch ( e) {
+ /*try {
+ this.istream.reset();
+ this.istream.mark(MARK_LEN);
+ long ms = System.currentTimeMillis();
+ File tempFile = new File("data_" + Long.toString(ms) + ".ccnb");
+ FileOutputStream fos = new FileOutputStream(tempFile);
+ try {
+ byte buf[] = new byte[1024];
+ while (this.istream.available() > 0) {
+ int count = this.istream.read(buf);
+ fos.write(buf,0, count);
+ }
+ } finally {
+ fos.close();
+ }
+ this.istream.reset();
+ Log.info(Log.FAC_ENCODING, "BinaryXMLDecoder: exception in peekStartElement, dumping offending object to file: " + tempFile.getAbsolutePath());
+ throw e;
+
+ } catch (IOException ie) {
+ Log.warning(Log.FAC_ENCODING, "IOException in BinaryXMLDecoder error handling: " + e.getMessage());
+ Log.logStackTrace(Log.FAC_ENCODING, Level.WARNING, e);
+ throw new ContentDecodingException("peekStartElement", e);
+
+ }
+ } catch (IOException e) {
+ Log.warning(Log.FAC_ENCODING, "IOException in BinaryXMLDecoder peekStartElementAsLong: " + e.getMessage());
+ Log.logStackTrace(Log.FAC_ENCODING, Level.WARNING, e);
+ throw new ContentDecodingException("peekStartElement", e);
+ */
+ } finally {
+ try {
+ //this.istream.reset();
+ this.offset = previousOffset;
+ } catch ( e) {
+ Log.logStackTrace(Log.FAC_ENCODING, Level.WARNING, e);
+ throw new Exception("Cannot reset stream! " + e.getMessage(), e);
+ }
+ }
+ return decodedTag;
+ };
+
+
+//byte[]
+BinaryXMLDecoder.prototype.readBinaryElement = function(
+ //long
+ startTag,
+ //TreeMap<String, String>
+ attributes){
+ //byte []
+ blob = null;
+ //try {
+ this.readStartElement(startTag, attributes);
+ blob = this.readBlob();
+ // readEndElement(); // readBlob consumes end element
+ //} catch ( e) {
+ //throw new ContentDecodingException(e.getMessage(), e);
+ //}
+
+ return blob;
+
+};
+
+
+BinaryXMLDecoder.prototype.readEndElement = function(){
+ try {
+ var next = this.istream[this.offset];
+ this.offset++;
+ //read();
+ if (next != XML_CLOSE) {
+ throw new ContentDecodingException("Expected end element, got: " + next);
+ }
+ } catch ( e) {
+ throw new ContentDecodingException(e);
+ }
+ };
+
+ /**
+ * Read a UString. Force this to consume the end element to match the
+ * behavior on the text side.
+ */
+//String
+BinaryXMLDecoder.prototype.readUString = function(){
+ //try {
+ //String
+ ustring = this.decodeUString(this.istream);
+ this.readEndElement();
+ return ustring;
+ //} catch ( e) {
+ //throw new Exception(e.getMessage(),e);
+ //}
+ };
+
+ /**
+ * Read a BLOB. Force this to consume the end element to match the
+ * behavior on the text side.
+ */
+//returns a byte[]
+BinaryXMLDecoder.prototype.readBlob = function() {
+ //try {
+ //byte []
+
+ blob = this.decodeBlob(this.istream);
+ this.readEndElement();
+ return blob;
+ //} catch ( e) {
+ //throw new Exception(e.getMessage(),e);
+ //}
+ };
+
+//read time returns CCNTime
+/*BinaryXMLDecoder.prototype.readDateTime = function(
+ //String
+ startTag) {
+ //byte []
+ byteTimestamp = this.readBinaryElement(startTag);
+ //CCNTime
+ timestamp = new CCNTime(byteTimestamp);
+ if (null == timestamp) {
+ throw new ContentDecodingException("Cannot parse timestamp: " + DataUtils.printHexBytes(byteTimestamp));
+ }
+ return timestamp;
+};
+*/
+//CCNTime
+
+BinaryXMLDecoder.prototype.readDateTime = function(
+ //long
+ startTag) {
+ //byte []
+
+ byteTimestamp = this.readBinaryElement(startTag);
+ //CCNTime
+ timestamp = new CCNTime();
+ timestamp.setDateBinary(byteTimestamp);
+
+ if (null == timestamp) {
+ throw new ContentDecodingException("Cannot parse timestamp: " + DataUtils.printHexBytes(byteTimestamp));
+ }
+ return timestamp;
+};
+
+BinaryXMLDecoder.prototype.decodeTypeAndVal = function(
+ /*InputStream*/
+ istream) {
+
+ /*int*/next;
+ /*int*/type = -1;
+ /*long*/val = 0;
+ /*boolean*/more = true;
+
+
+ //var savedOffset = this.offset;
+ var count = 0;
+
+ do {
+ //next = istream.read();
+ //console.log('a');
+ console.log('offset is' + this.offset);
+ var next = this.istream[this.offset ];
+ console.log('next is' + next);
+ //console.log('istream is ' + this.istream);
+
+
+ if (next < 0) {
+ return null;
+ }
+
+ if ((0 == next) && (0 == val)) {
+ return null;
+ }
+
+ more = (0 == (next & XML_TT_NO_MORE));
+
+ if (more) {
+ val = val << XML_REG_VAL_BITS;
+ val |= (next & XML_REG_VAL_MASK);
+ } else {
+
+ type = next & XML_TT_MASK;
+ val = val << XML_TT_VAL_BITS;
+ val |= ((next >>> XML_TT_BITS) & XML_TT_VAL_MASK);
+ }
+
+ this.offset++;
+
+ } while (more);
+
+ return new TypeAndVal(type, val);
+};
+
+
+
+//TypeAndVal
+BinaryXMLDecoder.peekTypeAndVal = function(
+ //InputStream
+ istream) {
+ //TypeAndVal
+ tv = null;
+
+ //istream.mark(LONG_BYTES*2);
+
+ var previousOffset = this.offset;
+
+ try {
+ tv = this.decodeTypeAndVal(this.istream);
+ } finally {
+ //istream.reset();
+ this.offset = previousOffset;
+ }
+
+ return tv;
+};
+
+//byte []
+/*BinaryXMLDecoder.prototype.decodeBlob = function(
+ //InputStream
+
+ istream) {
+ //istream.mark(LONG_BYTES*2);
+
+ TypeAndVal tv = decodeTypeAndVal(istream);
+ if ((null == tv) || (XML_BLOB != tv.type())) { // if we just have closers left, will get back null
+ if (Log.isLoggable(Log.FAC_ENCODING, Level.FINEST))
+ Log.finest(Log.FAC_ENCODING, "Expected BLOB, got " + ((null == tv) ? " not a tag " : tv.type()) + ", assuming elided 0-length blob.");
+ istream.reset();
+ return new byte[0];
+ }
+
+ var valval ;
+ if(typeof tv.val() == 'string'){
+ valval = (parseInt(tv.val()));
+ }
+ else
+ valval = (tv.val());
+
+ return decodeBlob(istream, valval);
+};*/
+
+//byte[]
+BinaryXMLDecoder.prototype.decodeBlob = function(
+ //InputStream
+ istream,
+ //int
+ blobLength) {
+
+
+ if(null == blobLength){
+ //TypeAndVal
+ tv = this.decodeTypeAndVal(this.istream);
+
+ var valval ;
+ if(typeof tv.val() == 'string'){
+ valval = (parseInt(tv.val()));
+ }
+ else
+ valval = (tv.val());
+
+ console.log('valval here is ' + valval);
+ return this.decodeBlob(this.istream, valval);
+ }
+
+ //
+ //byte []
+ //bytes = new byte[blobLength];
+ console.log('this.offset is '+ this.offset);
+ console.log('blogLength is '+ blobLength);
+
+ bytes = this.istream.slice(this.offset, this.offset+ blobLength);
+ this.offset += blobLength;
+
+ //int
+ return bytes;
+
+ count = 0;
+ /*
+ while (true) {
+ count += istream.read(bytes, count, (blobLength - count));
+ //Library.info("read "+count+" bytes out of "+blobLength+" in decodeBlob");
+ if (count < bytes.length) {
+ //we couldn't read enough.. need to try to read all of the bytes
+ //loop again...
+ //should we add a max number of tries?
+ } else if (count == bytes.length) {
+ //we are done reading! return now.
+ return bytes;
+ } else {
+ //we somehow read more than we should have...
+
+ throw new Exception("Expected to read " + bytes.length +
+ " bytes of data, read: " + count);
+ }
+ }*/
+};
+
+//String
+/*BinaryXMLDecoder.prototype.decodeUString = function(
+ //InputStream
+ istream) {
+ //istream.mark(LONG_BYTES*2);
+
+ //TypeAndVal
+ tv = this.decodeTypeAndVal(istream);
+
+ /*if ((null == tv) || (XML_UDATA != tv.type())) { // if we just have closers left, will get back null
+ if (Log.isLoggable(Log.FAC_ENCODING, Level.FINEST))
+ Log.finest(Log.FAC_ENCODING, "Expected UDATA, got " + ((null == tv) ? " not a tag " : tv.type()) + ", assuming elided 0-length blob.");
+ istream.reset();
+ return "";
+ }*/
+/*
+ var valval ;
+ if(typeof tv.val() == 'string'){
+ valval = (parseInt(tv.val()));
+ }
+ else
+ valval = (tv.val());
+
+ return this.decodeUString(istream, valval);
+}*/
+
+//String
+BinaryXMLDecoder.prototype.decodeUString = function(
+ //InputStream
+ istream,
+ //int
+ byteLength) {
+
+ if(null == byteLength){
+ tv = this.decodeTypeAndVal(this.istream);
+ var valval ;
+ if(typeof tv.val() == 'string'){
+ valval = (parseInt(tv.val()));
+ }
+ else
+ valval = (tv.val());
+
+ byteLength= this.decodeUString(this.istream, valval);
+ }
+
+ //byte []
+ console.log('bytes Length here is '+ byteLength);
+
+ stringBytes = this.decodeBlob(this.istream, byteLength);
+
+ tempBuffer = this.istream.slice(this.offset, this.offset+byteLength);
+ this.offset+= byteLength;
+ console.log('read the String' + tempBuffer.toString('ascii'));
+ return tempBuffer.toString('ascii');//DataUtils.getUTF8StringFromBytes(stringBytes);
+};
+
+var TypeAndVal = function TypeAndVal(_type,_val) {
+ this.t = _type;
+ this.v = _val;
+
+};
+
+TypeAndVal.prototype.type = function(){
+ return this.t;
+};
+
+TypeAndVal.prototype.val = function(){
+ return this.v;
+};
+//TODO
\ No newline at end of file
diff --git a/latest/encoding/BinaryXMLEncoder.js b/latest/encoding/BinaryXMLEncoder.js
new file mode 100644
index 0000000..e5039ee
--- /dev/null
+++ b/latest/encoding/BinaryXMLEncoder.js
@@ -0,0 +1,459 @@
+
+var XML_EXT = 0x00;
+
+var XML_TAG = 0x01;
+
+var XML_DTAG = 0x02;
+
+var XML_ATTR = 0x03;
+
+var XML_DATTR = 0x04;
+
+var XML_BLOB = 0x05;
+
+var XML_UDATA = 0x06;
+
+var XML_CLOSE = 0x0;
+
+var XML_SUBTYPE_PROCESSING_INSTRUCTIONS = 16;
+
+
+var XML_TT_BITS = 3;
+var XML_TT_MASK = ((1 << XML_TT_BITS) - 1);
+var XML_TT_VAL_BITS = XML_TT_BITS + 1;
+var XML_TT_VAL_MASK = ((1 << (XML_TT_VAL_BITS)) - 1);
+var XML_REG_VAL_BITS = 7;
+var XML_REG_VAL_MASK = ((1 << XML_REG_VAL_BITS) - 1);
+var XML_TT_NO_MORE = (1 << XML_REG_VAL_BITS); // 0x80
+var BYTE_MASK = 0xFF;
+var LONG_BYTES = 8;
+var LONG_BITS = 64;
+
+var bits_11 = 0x0000007FF;
+var bits_18 = 0x00003FFFF;
+var bits_32 = 0x0FFFFFFFF;
+
+//var BinaryXMLCodec = require('./BinaryXMLCodec').BinaryXMLCodec;
+
+//var codec = new BinaryXMLCodec();
+
+
+var BinaryXMLEncoder = function BinaryXMLEncoder(){
+
+ this.ostream = new Array(1024);
+
+ this.offset =0;
+
+ this.CODEC_NAME = "Binary";
+
+};
+
+
+/*BinaryXMLEncoder.prototype.beginEncoding = function() {
+ this.ostream = new Buffer(1024);
+ this.offset = 0;
+};
+
+BinaryXMLEncoder.prototype.endEncoding = function(){
+ //this.ostream.end();
+};*/
+
+BinaryXMLEncoder.prototype.writeUString = function(/*String*/ utf8Content){
+ this.encodeUString(this.ostream, utf8Content);
+};
+
+BinaryXMLEncoder.prototype.writeBlob = function(/*byte []*/ binaryContent
+ //, /*int*/ offset, /*int*/ length
+ ) {
+ //console.log(binaryContent);
+ this.encodeBlob(this.ostream, binaryContent, this.offset, binaryContent.length);
+};
+
+BinaryXMLEncoder.prototype.writeStartElement = function(/*String*/ tag, /*TreeMap<String,String>*/ attributes){
+
+ /*Long*/ dictionaryVal = tag;//stringToTag(tag);
+
+ if (null == dictionaryVal) {
+
+ this.encodeUString(this.ostream, tag, XML_TAG);
+
+ } else {
+ this.encodeTypeAndVal(XML_DTAG, dictionaryVal, this.ostream);
+ }
+
+ if (null != attributes) {
+ this.writeAttributes(attributes);
+ }
+};
+
+
+BinaryXMLEncoder.prototype.writeEndElement = function(){
+ //console.log(XML_CLOSE);
+ //console.log(tagToString(XML_CLOSE));
+ //this.ostream.writeUInt8( XML_CLOSE ,this.offset);
+ this.ostream[this.offset] = XML_CLOSE;
+ this.offset+= 1;
+}
+
+BinaryXMLEncoder.prototype.writeAttributes = function(/*TreeMap<String,String>*/ attributes) {
+
+ if (null == attributes) {
+ return;
+ }
+
+ // the keySet of a TreeMap is sorted.
+ /*Set<String> keySet = attributes.keySet();
+ Iterator<String> it = keySet.iterator();*/
+
+ for(var i=0; i<attributes.length;i++){
+ var strAttr = attributes[i].k;
+ var strValue = attributes[i].v;
+
+ var dictionaryAttr = stringToTag(strAttr);
+ if (null == dictionaryAttr) {
+ // not in dictionary, encode as attr
+ // compressed format wants length of tag represented as length-1
+ // to save that extra bit, as tag cannot be 0 length.
+ // encodeUString knows to do that.
+ this.encodeUString(this.ostream, strAttr, XML_ATTR);
+ } else {
+ this.encodeTypeAndVal(XML_DATTR, dictionaryAttr, this.ostream);
+ }
+ // Write value
+ this.encodeUString(this.ostream, strValue);
+
+ }
+
+
+}
+
+//returns a string
+stringToTag = function(/*long*/ tagVal) {
+ if ((tagVal >= 0) && (tagVal < CCNProtocolDTagsStrings.length)) {
+ return CCNProtocolDTagsStrings[tagVal];
+ } else if (tagVal == CCNProtocolDTags.CCNProtocolDataUnit) {
+ return CCNProtocolDTags.CCNPROTOCOL_DATA_UNIT;
+ }
+ return null;
+};
+
+//returns a Long
+tagToString = function(/*String*/ tagName) {
+ // the slow way, but right now we don't care.... want a static lookup for the forward direction
+ for (var i=0; i < CCNProtocolDTagsStrings.length; ++i) {
+ if ((null != CCNProtocolDTagsStrings[i]) && (CCNProtocolDTagsStrings[i] == tagName)) {
+ return i;
+ }
+ }
+ if (CCNProtocolDTags.CCNPROTOCOL_DATA_UNIT == tagName) {
+ return CCNProtocolDTags.CCNProtocolDataUnit;
+ }
+ return null;
+};
+
+
+BinaryXMLEncoder.prototype.writeElement = function(
+ //long
+ tag,
+ //byte[]
+ binaryContent,
+ //TreeMap<String, String>
+ attributes) {
+ this.writeStartElement(tag, attributes);
+ // Will omit if 0-length
+
+ this.writeBlob(binaryContent);
+ this.writeEndElement();
+}
+
+//TODO
+//console.log(stringToTag(0));
+
+
+
+
+
+
+
+
+
+var TypeAndVal = function TypeAndVal(_type,_val) {
+ this.type = _type;
+ this.val = _val;
+
+};
+
+
+
+/*BinaryXMLEncoder.prototype.encodeTypeAndValOffset = function(
+ //int
+ type,
+ //long
+ val,
+ //byte []
+ buf,
+ //int
+ offset) {
+
+ if ((type > XML_UDATA) || (type < 0) || (val < 0)) {
+ throw new Exception("Tag and value must be positive, and tag valid.");
+ }
+
+ // Encode backwards. Calculate how many bytes we need:
+ //int
+ var numEncodingBytes = numEncodingBytes(val);
+
+ if ((offset + numEncodingBytes) > buf.length) {
+ throw new Exception("Buffer space of " + (buf.length-offset) +
+ " bytes insufficient to hold " +
+ numEncodingBytes + " of encoded type and value.");
+ }
+
+
+ buf[offset + numEncodingBytes - 1] =
+ (BYTE_MASK &
+ (((XML_TT_MASK & type) |
+ ((XML_TT_VAL_MASK & val) << XML_TT_BITS))) |
+ XML_TT_NO_MORE);
+ val = val >>> XML_TT_VAL_BITS;;
+ //int
+ var i = offset + numEncodingBytes - 2;
+ while ((0 != val) && (i >= offset)) {
+ buf[i] = (BYTE_MASK &
+ (val & XML_REG_VAL_MASK));
+ val = val >>> XML_REG_VAL_BITS;
+ --i;
+ }
+
+ return numEncodingBytes;
+};*/
+
+//BinaryXMLCodec.prototype.encodeTypeAndVal = function(
+ // //final int
+ //type,
+ //final long
+ //value,
+ //final OutputStream
+ //ostream,
+ //offset){
+
+ /*
+ We exploit the fact that encoding is done from the right, so this actually means
+ there is a deterministic encoding from a long to a Type/Value pair:
+
+ | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
+ |ABCD.EFGH|IJKL.MNOP|QRST.UVWX|YZ01.2345|6789.abcd|efgh.ijkl|mnop.qrst|uvwx.yz@#
+
+ 60> 53> 46> 39> 32> 25> 18> 11> 4>
+ |_000.ABCD|_EFG.HIJK|_LMN.OPQR|_STU.VWXY|_Z01.2345|_678.9abc|_defg.hij|_klm.nopq|_rst.uvwx|_yz@#___
+
+ What we want to do is compute the result in MSB order and write it directly
+ to the channel without any intermediate form.
+ */
+
+ //var/*int*/ bits;
+ //var/*int*/ count = 0;
+
+ // once we start writing bits, we keep writing bits even if they are "0"
+ // var/*bool*/ writing = false;
+
+ // a few heuristic to catch the small-bit length patterns
+ /*if( value < 0 || value > 15 ) {
+ var start = 60;
+ if( 0 <= value ) {
+ if( value < bits_11 )
+ start = 4;
+ else if( value < bits_18 )
+ start = 11;
+ else if( value < bits_32 )
+ start = 25;
+ }
+
+ for( var i = start; i >= 4; i -= 7) {
+ bits = (value >>> i) & BinaryXMLCodec.XML_REG_VAL_MASK;
+ if( bits != 0 || writing ) {
+ ostream.write(bits);
+ count++;
+ writing = true;
+ }
+ }
+ }
+
+ // Explicit computation of the bottom byte
+ bits = type & BinaryXMLCodec.XML_TT_MASK;
+ var bottom4 = value & BinaryXMLCodec.XML_TT_VAL_MASK;
+ bits |= bottom4 << BinaryXMLCodec.XML_TT_BITS;
+ // the bottom byte always has the NO_MORE flag
+ bits |= BinaryXMLCodec.XML_TT_NO_MORE;
+
+ //console.log(ostream.constructor.name);
+ //console.log(ostream);
+
+ //ostream.writable = true;
+ //console.log(ostream.)
+ ostream.write(bits.toString());
+
+ count++;
+
+// byte [] encoding = encodeTypeAndVal(tag, val);
+// ostream.write(encoding);
+ return count;
+
+}*/
+
+
+BinaryXMLEncoder.prototype.encodeTypeAndVal = function(
+ //int
+ type,
+ //long
+ val,
+ //byte []
+ buf) {
+
+ console.log('Encoding type '+ type+ ' and value '+ val);
+
+ if ((type > XML_UDATA) || (type < 0) || (val < 0)) {
+ throw new Exception("Tag and value must be positive, and tag valid.");
+ }
+
+ // Encode backwards. Calculate how many bytes we need:
+ var numEncodingBytes = this.numEncodingBytes(val);
+
+ if ((this.offset + numEncodingBytes) > buf.length) {
+ throw new Exception("Buffer space of " + (buf.length-this.offset) +
+ " bytes insufficient to hold " +
+ numEncodingBytes + " of encoded type and value.");
+ }
+
+ // Bottom 4 bits of val go in last byte with tag.
+ buf[this.offset + numEncodingBytes - 1] =
+ //(byte)
+ (BYTE_MASK &
+ (((XML_TT_MASK & type) |
+ ((XML_TT_VAL_MASK & val) << XML_TT_BITS))) |
+ XML_TT_NO_MORE); // set top bit for last byte
+ val = val >>> XML_TT_VAL_BITS;;
+
+ // Rest of val goes into preceding bytes, 7 bits per byte, top bit
+ // is "more" flag.
+ var i = this.offset + numEncodingBytes - 2;
+ while ((0 != val) && (i >= this.offset)) {
+ buf[i] = //(byte)
+ (BYTE_MASK &
+ (val & XML_REG_VAL_MASK)); // leave top bit unset
+ val = val >>> XML_REG_VAL_BITS;
+ --i;
+ }
+ if (val != 0) {
+ throw new Exception( "This should not happen: miscalculated encoding");
+ //Log.warning(Log.FAC_ENCODING, "This should not happen: miscalculated encoding length, have " + val + " left.");
+ }
+ this.offset+= numEncodingBytes;
+ console.log('offset increased after tag to '+this.offset);
+
+ return numEncodingBytes;
+};
+
+BinaryXMLEncoder.prototype.encodeUString = function(
+ //OutputStream
+ ostream,
+ //String
+ ustring,
+ //byte
+ type) {
+
+ // We elide the encoding of a 0-length UString
+ if ((null == ustring) || (ustring.length == 0)) {
+ //if (Log.isLoggable(Log.FAC_ENCODING, Level.FINER))
+ //Log.finer(Log.FAC_ENCODING, "Eliding 0-length UString.");
+ return;
+ }
+
+ console.log('Writting String to '+ ustring);
+
+ //byte [] data utils
+ /*custom*/
+ //byte[]
+ strBytes = new Array(ustring.Length);
+ var i = 0;
+ for( ;i<ustring.lengh;i++) //in InStr.ToCharArray())
+ {
+ strBytes[i] = ustring[i];
+ }
+ //strBytes = DataUtils.getBytesFromUTF8String(ustring);
+
+ this.encodeTypeAndVal(type,
+ (((type == XML_TAG) || (type == XML_ATTR)) ?
+ (strBytes.length-1) :
+ strBytes.length), ostream);
+
+ //console.log(strBytes.toString());
+
+ ostream.write(strBytes.toString(),this.offset);
+ this.offset+= strBytes.length;
+ console.log('offset increased after String to '+this.offset);
+ //
+};
+
+BinaryXMLEncoder.prototype.encodeBlob = function(
+ //OutputStream
+ ostream,
+ //byte []
+ blob,
+ //int
+ offset,
+ //int
+ length) {
+
+ console.log('Writting Blob ');
+ console.log('length is '+ length);
+
+ // We elide the encoding of a 0-length blob
+ if ((null == blob) || (length == 0)) {
+
+ return;
+ }
+
+
+ this.encodeTypeAndVal(XML_BLOB, length, ostream,offset);
+
+ if (null != blob) {
+ //console.log(blob);
+ //maybe blog.t
+ ostream.write(blob.toString(), this.offset);
+ this.offset += length;
+ console.log('offset increased after blob to '+this.offset);
+ }
+};
+
+
+var ENCODING_LIMIT_1_BYTE = ((1 << (XML_TT_VAL_BITS)) - 1);
+var ENCODING_LIMIT_2_BYTES = ((1 << (XML_TT_VAL_BITS + XML_REG_VAL_BITS)) - 1);
+var ENCODING_LIMIT_3_BYTES = ((1 << (XML_TT_VAL_BITS + 2 * XML_REG_VAL_BITS)) - 1);
+
+BinaryXMLEncoder.prototype.numEncodingBytes = function(
+ //long
+ x) {
+ if (x <= ENCODING_LIMIT_1_BYTE) return (1);
+ if (x <= ENCODING_LIMIT_2_BYTES) return (2);
+ if (x <= ENCODING_LIMIT_3_BYTES) return (3);
+
+ var numbytes = 1;
+
+ // Last byte gives you XML_TT_VAL_BITS
+ // Remainder each give you XML_REG_VAL_BITS
+ x = x >>> XML_TT_VAL_BITS;
+ while (x != 0) {
+ numbytes++;
+ x = x >>> XML_REG_VAL_BITS;
+ }
+ return (numbytes);
+};
+
+BinaryXMLEncoder.prototype.writeDateTime = function(
+ //String
+ tag,
+ //CCNTime
+ dateTime) {
+ this.writeElement(tag, dateTime.toBinaryTime());
+};
diff --git a/latest/encoding/DataUtils.js b/latest/encoding/DataUtils.js
new file mode 100644
index 0000000..8c931bf
--- /dev/null
+++ b/latest/encoding/DataUtils.js
@@ -0,0 +1,135 @@
+
+
+var DataUtils = function DataUtils(){
+
+
+};
+
+//exports.DataUtils =DataUtils;
+
+
+ var keyStr = "ABCDEFGHIJKLMNOP" +
+ "QRSTUVWXYZabcdef" +
+ "ghijklmnopqrstuv" +
+ "wxyz0123456789+/" +
+ "=";
+
+DataUtils.encode64=function encode64(input) {
+ input = escape(input);
+ var output = "";
+ var chr1, chr2, chr3 = "";
+ var enc1, enc2, enc3, enc4 = "";
+ var i = 0;
+
+ do {
+ chr1 = input.charCodeAt(i++);
+ chr2 = input.charCodeAt(i++);
+ chr3 = input.charCodeAt(i++);
+
+ enc1 = chr1 >> 2;
+ enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
+ enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
+ enc4 = chr3 & 63;
+
+ if (isNaN(chr2)) {
+ enc3 = enc4 = 64;
+ } else if (isNaN(chr3)) {
+ enc4 = 64;
+ }
+
+ output = output +
+ keyStr.charAt(enc1) +
+ keyStr.charAt(enc2) +
+ keyStr.charAt(enc3) +
+ keyStr.charAt(enc4);
+ chr1 = chr2 = chr3 = "";
+ enc1 = enc2 = enc3 = enc4 = "";
+ } while (i < input.length);
+
+ return output;
+ }
+
+DataUtils.decode64 = function decode64(input) {
+ var output = "";
+ var chr1, chr2, chr3 = "";
+ var enc1, enc2, enc3, enc4 = "";
+ var i = 0;
+
+ // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
+ var base64test = /[^A-Za-z0-9\+\/\=]/g;
+ if (base64test.exec(input)) {
+ alert("There were invalid base64 characters in the input text.\n" +
+ "Valid base64 characters are A-Z, a-z, 0-9, '+', '/',and '='\n" +
+ "Expect errors in decoding.");
+ }
+ input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
+
+ do {
+ enc1 = keyStr.indexOf(input.charAt(i++));
+ enc2 = keyStr.indexOf(input.charAt(i++));
+ enc3 = keyStr.indexOf(input.charAt(i++));
+ enc4 = keyStr.indexOf(input.charAt(i++));
+
+ chr1 = (enc1 << 2) | (enc2 >> 4);
+ chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
+ chr3 = ((enc3 & 3) << 6) | enc4;
+
+ output = output + String.fromCharCode(chr1);
+
+ if (enc3 != 64) {
+ output = output + String.fromCharCode(chr2);
+ }
+ if (enc4 != 64) {
+ output = output + String.fromCharCode(chr3);
+ }
+
+ chr1 = chr2 = chr3 = "";
+ enc1 = enc2 = enc3 = enc4 = "";
+
+ } while (i < input.length);
+
+ return unescape(output);
+ };
+
+
+
+
+
+//byte []
+DataUtils.prototype.unsignedLongToByteArray= function( value) {
+ if( 0 == value )
+ return [0];
+
+ if( 0 <= value && value <= 0x00FF ) {
+ //byte []
+ bb = new Array[1];
+ bb[0] = (value & 0x00FF);
+ return bb;
+ }
+
+
+ //byte []
+ out = null;
+ //int
+ var offset = -1;
+ for(var i = 7; i >=0; --i) {
+ //byte
+ b = ((value >> (i * 8)) & 0xFF);
+ if( out == null && b != 0 ) {
+ out = new byte[i+1];
+ offset = i;
+ }
+ if( out != null )
+ out[ offset - i ] = b;
+ }
+ return out;
+}
+
+//function init(){
+
+ //var encoded = DataUtils.encode64('abc');
+ //console.log(encoded);
+//}
+//init();
+
+
diff --git a/latest/encoding/TextXMLCodec.js b/latest/encoding/TextXMLCodec.js
new file mode 100644
index 0000000..2d1121b
--- /dev/null
+++ b/latest/encoding/TextXMLCodec.js
@@ -0,0 +1,70 @@
+//todo parse this
+/*
+ *
+ *
+ * static {
+ canonicalWriteDateFormat =
+ new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
+ new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.S'Z'"); // writing ns doesn't format leading 0's correctly
+ canonicalWriteDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
+ canonicalReadDateFormat =
+ new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
+ // new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.S'Z'");
+ canonicalReadDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
+ }
+ *
+ */
+
+var DataUtils = require('./DataUtils').DataUtils;
+
+ var /*DateFormat*/ canonicalWriteDateFormat = null;
+ var /* DateFormat*/ canonicalReadDateFormat = null;
+ var /*String*/ PAD_STRING = "000000000";
+ var /*int*/ NANO_LENGTH = 9;
+
+var TextXMLCodec = function TextXMLCodec(){
+
+ this.CCN_NAMESPACE = "http://www.parc.com/ccn"; // String
+ this.CCN_PREFIX = "ccn"; // String
+ this.CODEC_NAME = "Text";// String
+ this.BINARY_ATTRIBUTE = "ccnbencoding";// String
+ this.BINARY_ATTRIBUTE_VALUE = "base64Binary";// String
+
+
+};
+
+//returns a string
+
+TextXMLCodec.protpotype.codecName = function() { return this.CODEC_NAME; } ;
+
+//returns a string
+TextXMLCodec.protottype.encodeBinaryElement = function(/*byte []*/ element) {
+ if ((null == element) || (0 == element.length))
+ return new String("");
+ return new String(DataUtils.base64Encode(element));
+ };
+
+/* returns a string */
+TextXMLCodec.prototype.encodeBinaryElement = function(/*byte []*/ element, /*int*/ offset, /*int*/ length) {
+ if ((null == element) || (0 == element.length))
+ return new String("");
+ ByteBuffer bbuf = ByteBuffer.wrap(element, offset, length);
+ return new String(DataUtils.base64Encode(bbuf.array()));
+ };
+
+/*returns a byte array*/
+TextXMLCodec.prototype.decodeBinaryElement = function(/*String*/ element) {
+ if ((null == element) || (0 == element.length()))
+ return new byte[0];
+ return DataUtils.base64Decode(element.getBytes());
+ };
+
+
+/*
+ Decode Data
+*/
+
+
+/*
+ Encode Date
+*/
\ No newline at end of file
diff --git a/latest/encoding/TextXMLDecoder.js b/latest/encoding/TextXMLDecoder.js
new file mode 100644
index 0000000..32ac1c5
--- /dev/null
+++ b/latest/encoding/TextXMLDecoder.js
@@ -0,0 +1,225 @@
+
+
+var TextXMLDecoder = function TextXMLDecoder(){
+
+ this.reader = null;
+
+};
+
+
+exports.TextXMLDecoder = TextXMLDecoder;
+
+
+exports.prototype.initializeDecoding = function(){
+ try {
+ XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
+ factory.setNamespaceAware(true);
+ _reader = factory.newPullParser();
+ _reader.setInput(_istream, null);
+ } catch (XmlPullParserException e) {
+ throw new ContentDecodingException(e.getMessage(), e);
+ }
+};
+
+exports.prototype.readStartDocument = function(){
+ try {
+ int event = _reader.getEventType();
+ _reader.next();
+ if (event != XmlPullParser.START_DOCUMENT) {
+ throw new ContentDecodingException("Expected start document, got: " + XmlPullParser.TYPES[event]);
+ }
+ } catch (XmlPullParserException e) {
+ throw new ContentDecodingException(e.getMessage(), e);
+ } catch (IOException e) {
+ throw new ContentDecodingException(e.getMessage(), e);
+ }
+};
+
+public void readEndDocument() throws ContentDecodingException {
+ int event;
+ try {
+ event = _reader.getEventType();
+ } catch (XmlPullParserException e) {
+ throw new ContentDecodingException(e.getMessage(), e);
+ }
+ if (event != XmlPullParser.END_DOCUMENT) {
+ throw new ContentDecodingException("Expected end document, got: " + XmlPullParser.TYPES[event]);
+ }
+};
+
+exports.prototype.readStartElement = function(/*String*/ startTag,
+ TreeMap<String, String> attributes) throws ContentDecodingException {
+
+ int event = readToNextTag(XmlPullParser.START_TAG);
+ if (event != XmlPullParser.START_TAG) {
+ throw new ContentDecodingException("Expected start element, got: " + XmlPullParser.TYPES[event]);
+ }
+ // Use getLocalPart to strip namespaces.
+ // Assumes we are working with a global default namespace of CCN.
+ if (!startTag.equals(_reader.getName())) {
+ // Coming back with namespace decoration doesn't match
+ throw new ContentDecodingException("Expected start element: " + startTag + " got: " + _reader.getName());
+ }
+ if (null != attributes) {
+ // we might be expecting attributes
+ for (int i=0; i < _reader.getAttributeCount(); ++i) {
+ // may need fancier namespace handling.
+ attributes.put(_reader.getAttributeName(i), _reader.getAttributeValue(i));
+ }
+ }
+ try {
+ _reader.next();
+ } catch (XmlPullParserException e) {
+ throw new ContentDecodingException(e.getMessage());
+ } catch (IOException e) {
+ throw new ContentDecodingException(e.getMessage());
+ }
+}
+
+ public void readStartElement(long startTagLong,
+ TreeMap<String, String> attributes) throws ContentDecodingException {
+
+ String startTag = tagToString(startTagLong);
+
+ int event = readToNextTag(XmlPullParser.START_TAG);
+ if (event != XmlPullParser.START_TAG) {
+ throw new ContentDecodingException("Expected start element, got: " + XmlPullParser.TYPES[event]);
+ }
+ // Use getLocalPart to strip namespaces.
+ // Assumes we are working with a global default namespace of CCN.
+ if (!startTag.equals(_reader.getName())) {
+ // Coming back with namespace decoration doesn't match
+ throw new ContentDecodingException("Expected start element: " + startTag + " got: " + _reader.getName());
+ }
+ if (null != attributes) {
+ // we might be expecting attributes
+ for (int i=0; i < _reader.getAttributeCount(); ++i) {
+ // may need fancier namespace handling.
+ attributes.put(_reader.getAttributeName(i), _reader.getAttributeValue(i));
+ }
+ }
+ try {
+ _reader.next();
+ } catch (XmlPullParserException e) {
+ throw new ContentDecodingException(e.getMessage());
+ } catch (IOException e) {
+ throw new ContentDecodingException(e.getMessage());
+ }
+ }
+ public String peekStartElementAsString() throws ContentDecodingException {
+ int event = readToNextTag(XmlPullParser.START_TAG);
+ if (event != XmlPullParser.START_TAG) {
+ return null;
+ }
+ return _reader.getName();
+ }
+
+ public Long peekStartElementAsLong() throws ContentDecodingException {
+ String strTag = peekStartElementAsString();
+ if (null == strTag) {
+ return null; // e.g. hit an end tag...
+ }
+ return stringToTag(strTag);
+ }
+
+ /**
+ * Helper method to decode text (UTF-8) and binary elements. Consumes the end element,
+ * behavior which other decoders are forced to match.
+ * @return the read data, as a String
+ * @throws ContentDecodingException if there is a problem decoding the data
+ */
+ public String readUString() throws ContentDecodingException {
+ StringBuffer buf = new StringBuffer();
+ try {
+ int event = _reader.getEventType();;
+ // Handles empty text element.
+ while (event == XmlPullParser.TEXT) {
+ buf.append(_reader.getText());
+ event = _reader.next();
+ }
+ if (event == XmlPullParser.START_TAG) {
+ throw new ContentDecodingException("readElementText expects start element to have been previously consumed, got: " + XmlPullParser.TYPES[event]);
+ } else if (event != XmlPullParser.END_TAG) {
+ throw new ContentDecodingException("Expected end of text element, got: " + XmlPullParser.TYPES[event]);
+ }
+ readEndElement();
+ return buf.toString();
+ } catch (XmlPullParserException e) {
+ throw new ContentDecodingException(e.getMessage(), e);
+ } catch (IOException e) {
+ throw new ContentDecodingException(e.getMessage(), e);
+ }
+ }
+
+ public void readEndElement() throws ContentDecodingException {
+ int event = readToNextTag(XmlPullParser.END_TAG);
+ if (event != XmlPullParser.END_TAG) {
+ throw new ContentDecodingException("Expected end element, got: " + XmlPullParser.TYPES[event]);
+ }
+ try {
+ _reader.next();
+ } catch (XmlPullParserException e) {
+ throw new ContentDecodingException(e.getMessage());
+ } catch (IOException e) {
+ throw new ContentDecodingException(e.getMessage());
+ }
+ }
+
+ /**
+ * Read a BLOB. Consumes the end element, so force other versions
+ * to match.
+ */
+ public byte [] readBlob() throws ContentDecodingException {
+ try {
+ String strElementText = readUString();
+ // readEndElement(); // readElementText consumes end element
+ return TextXMLCodec.decodeBinaryElement(strElementText);
+ } catch (IOException e) {
+ throw new ContentDecodingException(e.getMessage(),e);
+ }
+ }
+
+ public CCNTime readDateTime(String startTag) throws ContentDecodingException {
+ String strTimestamp = readUTF8Element(startTag);
+ CCNTime timestamp;
+ try {
+ timestamp = TextXMLCodec.parseDateTime(strTimestamp);
+ } catch (ParseException e) {
+ timestamp = null;
+ }
+ if (null == timestamp) {
+ throw new ContentDecodingException("Cannot parse timestamp: " + strTimestamp);
+ }
+ return timestamp;
+ }
+
+ public CCNTime readDateTime(long startTag) throws ContentDecodingException {
+ String strTimestamp = readUTF8Element(startTag);
+ CCNTime timestamp;
+ try {
+ timestamp = TextXMLCodec.parseDateTime(strTimestamp);
+ } catch (ParseException e) {
+ timestamp = null;
+ }
+ if (null == timestamp) {
+ throw new ContentDecodingException("Cannot parse timestamp: " + strTimestamp);
+ }
+ return timestamp;
+ }
+
+ private int readToNextTag(int type) throws ContentDecodingException {
+ int event;
+ try {
+ event = _reader.getEventType();
+ if (event == type)
+ return event;
+ if (event == XmlPullParser.TEXT || event == XmlPullParser.COMMENT)
+ event = _reader.next();
+ } catch (IOException e) {
+ throw new ContentDecodingException(e.getMessage(), e);
+ } catch (XmlPullParserException e) {
+ throw new ContentDecodingException(e.getMessage(), e);
+ }
+ return event;
+ }
+};
diff --git a/latest/encoding/TextXMLEncoder.js b/latest/encoding/TextXMLEncoder.js
new file mode 100644
index 0000000..29291ea
--- /dev/null
+++ b/latest/encoding/TextXMLEncoder.js
@@ -0,0 +1,134 @@
+var Stream = require('stream').Stream;
+var TextXMLCodec = require('TextXMLCodec').TextXMLCodec;
+
+
+
+
+var TextXMLEncoder = function TextXMLEncoder(){
+
+
+ this.ostream = new String();
+};
+
+exports.TextXMLEncoder = TextXMLEncoder;
+
+TextXMLEncoder.prototype.beginEncoding = function(/*OutputStream*/ ostream){
+ if (null == ostream)
+ throw new IllegalArgumentException("TextXMLEncoder: output stream cannot be null!");
+
+
+ /*Start by encoing the begining*/
+ //this.IStream = ostream;
+ this.ostream.write('<?xml version="1.0" encoding="UTF-8"?>');
+};
+
+TextXMLEncoder.prototype.endEncoding =function() {
+ this.IStream.end();
+}
+
+//TextXMLEncoder.prototype.writeStartElement = function(/*long*/ tag, /*TreeMap<String, String>*/ attributes){
+ /* String strTag = tagToString(tag);
+ if (null == strTag) {
+ strTag = XMLDictionaryStack.unknownTagMarker(tag);
+ }
+ writeStartElement(strTag, attributes);
+};*/
+
+
+TextXMLEncoder.prorotype.writeStartElement(/*String*/ tag, /*TreeMap<String, String>*/ attributes) {
+
+
+ this.ostream.write('<'+tab);
+
+ if (null != attributes) {
+
+ for(var i=0; i<attributes.length;i++){
+ this.ostream.write(' '+attributes[i].key +'='+attributes[i].value);
+ }
+
+ // keySet of a TreeMap is ordered
+ }
+ this.ostream.write('>');
+};
+
+TextXMLEncoder.prototype.writeUString = function(/*String*/ utf8Content) {
+
+ this.ostream.write(utf8Content);
+
+};
+
+
+TextXMLEncoder.prototype.writeBlob = function(/*byte []*/ binaryContent, /*int*/ offset, /*int*/ length) {
+
+ this.ostream.write(TextXMLCodec.encodeBinaryElement(binaryContent, offset, length));
+
+};
+
+TextXMLEncoder.prototype.writeElement = function(/*String*/ tag, /*byte[]*/ binaryContent,
+ /*TreeMap<String, String>*/ attributes) {
+
+ /*if (null == attributes) {
+
+ attributes = new TreeMap<String,String>();
+ }*/
+ if (!attributes.containsKey(TextXMLCodec.BINARY_ATTRIBUTE)) {
+ attributes.put(TextXMLCodec.BINARY_ATTRIBUTE, TextXMLCodec.BINARY_ATTRIBUTE_VALUE);
+ }
+ super.writeElement(tag, binaryContent, attributes);
+}
+
+//TextXMLEncoder.prototype.writeElement = function(/*String*/ tag, /*byte[]*/ binaryContent, /*int*/ offset, int length,
+ /* TreeMap<String, String> attributes) throws ContentEncodingException {
+ if (null == attributes) {
+ attributes = new TreeMap<String,String>();
+ }
+ if (!attributes.containsKey(TextXMLCodec.BINARY_ATTRIBUTE)) {
+ attributes.put(TextXMLCodec.BINARY_ATTRIBUTE, TextXMLCodec.BINARY_ATTRIBUTE_VALUE);
+ }
+ super.writeElement(tag, binaryContent, offset, length, attributes);
+ }*/
+
+/* public void writeDateTime(String tag, CCNTime dateTime) throws ContentEncodingException {
+ writeElement(tag, TextXMLCodec.formatDateTime(dateTime));
+ }
+
+ public void writeDateTime(long tag, CCNTime dateTime) throws ContentEncodingException {
+ writeElement(tag, TextXMLCodec.formatDateTime(dateTime));
+ }*/
+
+TextXMLEncoder.prototype.writeEndElement(tag) {
+
+ this.ostream.write('<'+tab+'>');
+
+ };
+
+
+//returns number long
+stringToTag = function(/*String*/ tagName) {
+
+ if (null == tagName) {
+ return null;
+ }
+ Long tagVal = null;
+ if (null != _dictionaryStack) {
+ for (/*XMLDictionary*/ dictionary in _dictionaryStack) {
+ tagVal = dictionary.stringToTag(tagName);
+ if (null != tagVal) {
+ return tagVal;
+ }
+ }
+ }
+
+ /*for (XMLDictionary dictionary : XMLDictionaryStack.getGlobalDictionaries()) {
+ tagVal = dictionary.stringToTag(tagName);
+ if (null != tagVal) {
+ return tagVal;
+ }
+ }*/
+
+ if (XMLDictionaryStack.isUnknownTag(tagName)) {
+ return XMLDictionaryStack.decodeUnknownTag(tagName);
+ }
+ return null;
+};
+
diff --git a/latest/encoding/dom-js.js b/latest/encoding/dom-js.js
new file mode 100644
index 0000000..3cabaec
--- /dev/null
+++ b/latest/encoding/dom-js.js
@@ -0,0 +1,188 @@
+var sax = require("./sax");
+var strict = true;
+
+/**
+ * Really simple XML DOM implementation based on sax that works with Strings.
+ *
+ * If you have an XML string and want a DOM this utility is convenient.
+ *
+ * var domjs = new DomJS();
+ * domjs.parse(xmlString, function(err, dom) {
+ *
+ * });
+ *
+ * If you want to compile C there are versions based on libxml2
+ * and jsdom is full featured but complicated.
+ *
+ * This is "lightweight" meaning really simple and serves my purpose, it does not support namespaces or all
+ * of the features of XML 1.0 it just takes a string and returns a JavaScript object graph.
+ *
+ * There are only three types of object supported Element, Text and Comment.
+ *
+ * e.g.
+ *
+ * take <xml><elem att="val1"/><elem att="val1"/><elem att="val1"/></xml>
+ *
+ * return { name : "xml",
+ * attributes : {}
+ * children [
+ * { name : "elem", attributes : {att:'val1'}, children [] },
+ * { name : "elem", attributes : {att:'val1'}, children [] },
+ * { name : "elem", attributes : {att:'val1'}, children [] }
+ * ]
+ * }
+ *
+ * The object returned can be serialized back out with obj.toXml();
+ *
+ *
+ * @constructor DomJS
+ */
+DomJS = function() {
+ this.root = null;
+ this.stack = new Array();
+ this.currElement = null;
+ this.error = false;
+};
+
+DomJS.prototype.parse = function(string, cb) {
+ if (typeof string != 'string') {
+ cb(true, 'Data is not a string');
+ return;
+ }
+ var self = this;
+ parser = sax.parser(strict);
+
+ parser.onerror = function (err) {
+ self.error = true;
+ cb(true, err);
+ };
+ parser.ontext = function (text) {
+ if (self.currElement == null) {
+ // console.log("Content in the prolog " + text);
+ return;
+ }
+ var textNode = new Text(text);
+ self.currElement.children.push(textNode);
+ };
+ parser.onopencdata = function () {
+ var cdataNode = new CDATASection();
+ self.currElement.children.push(cdataNode);
+ };
+ parser.oncdata = function (data) {
+ var cdataNode = self.currElement.children[self.currElement.children.length - 1];
+ cdataNode.appendData(data);
+ };
+ // do nothing on parser.onclosecdata
+ parser.onopentag = function (node) {
+ var elem = new Element(node.name, node.attributes);
+ if (self.root == null) {
+ self.root = elem;
+ }
+ if (self.currElement != null) {
+ self.currElement.children.push(elem);
+ }
+ self.currElement = elem;
+ self.stack.push(self.currElement);
+ };
+ parser.onclosetag = function (node) {
+ self.stack.pop();
+ self.currElement = self.stack[self.stack.length - 1 ];// self.stack.peek();
+ };
+ parser.oncomment = function (comment) {
+ if (self.currElement == null) {
+ //console.log("Comments in the prolog discarded " + comment);
+ return;
+ }
+ var commentNode = new Comment(comment);
+ self.currElement.children.push(commentNode);
+ };
+
+ parser.onend = function () {
+ if ( self.error == false) {
+ cb(false, self.root);
+ }
+ };
+
+ parser.write(string).close();
+};
+
+DomJS.prototype.reset = function() {
+ this.root = null;
+ this.stack = new Array();
+ this.currElement = null;
+ this.error = false;
+};
+
+var escape = function(string) {
+ return string.replace(/&/g, '&').replace(/>/g, '>').replace(/</g, '<').replace(/"/g, '"').replace(/'/g, ''');
+};
+
+
+Element = function(name, attributes, children ) {
+ this.name = name;
+ this.attributes = attributes || [];
+ this.children = children || [];
+}
+Element.prototype.toXml = function(sb) {
+ if (typeof sb == 'undefined') {
+ sb = {buf:''}; // Strings are pass by value in JS it seems
+ }
+ sb.buf += '<' + this.name;
+ for (att in this.attributes) {
+
+ sb.buf += ' ' + att + '="' + escape(this.attributes[att]) + '"';
+ }
+ if (this.children.length != 0) {
+ sb.buf += '>';
+ for (var i = 0 ; i < this.children.length ; i++) {
+ this.children[i].toXml(sb);
+ }
+ sb.buf += '</' + this.name + '>';
+ }
+ else {
+ sb.buf += '/>';
+ }
+ return sb.buf;
+};
+Element.prototype.firstChild = function() {
+ if ( this.children.length > 0) {
+ return this.children[0];
+ }
+ return null;
+};
+Element.prototype.text = function() {
+ if ( this.children.length > 0) {
+ if (typeof this.children[0].text == 'string') {
+ return this.children[0].text;
+ };
+ }
+ return null;
+};
+
+Text = function(data){
+ this.text = data;
+};
+Text.prototype.toXml = function(sb) {
+ sb.buf += escape(this.text);
+};
+
+Comment = function(comment) {
+ this.comment = comment;
+};
+Comment.prototype.toXml = function(sb) {
+ sb.buf += '<!--' + this.comment + '-->';
+};
+
+CDATASection = function(data){
+ this.text = data || '';
+};
+CDATASection.prototype.toXml = function(sb) {
+ sb.buf += '<![CDATA[' + this.text + ']]>';
+};
+CDATASection.prototype.appendData = function(data) {
+ this.text += data;
+};
+
+
+
+
diff --git a/latest/encoding/sax.js b/latest/encoding/sax.js
new file mode 100644
index 0000000..f982712
--- /dev/null
+++ b/latest/encoding/sax.js
@@ -0,0 +1,1005 @@
+// wrapper for non-node envs
+;(function (sax) {
+
+sax.parser = function (strict, opt) { return new SAXParser(strict, opt) }
+sax.SAXParser = SAXParser
+sax.SAXStream = SAXStream
+sax.createStream = createStream
+
+// When we pass the MAX_BUFFER_LENGTH position, start checking for buffer overruns.
+// When we check, schedule the next check for MAX_BUFFER_LENGTH - (max(buffer lengths)),
+// since that's the earliest that a buffer overrun could occur. This way, checks are
+// as rare as required, but as often as necessary to ensure never crossing this bound.
+// Furthermore, buffers are only tested at most once per write(), so passing a very
+// large string into write() might have undesirable effects, but this is manageable by
+// the caller, so it is assumed to be safe. Thus, a call to write() may, in the extreme
+// edge case, result in creating at most one complete copy of the string passed in.
+// Set to Infinity to have unlimited buffers.
+sax.MAX_BUFFER_LENGTH = 64 * 1024
+
+var buffers = [
+ "comment", "sgmlDecl", "textNode", "tagName", "doctype",
+ "procInstName", "procInstBody", "entity", "attribName",
+ "attribValue", "cdata", "script"
+]
+
+sax.EVENTS = // for discoverability.
+ [ "text"
+ , "processinginstruction"
+ , "sgmldeclaration"
+ , "doctype"
+ , "comment"
+ , "attribute"
+ , "opentag"
+ , "closetag"
+ , "opencdata"
+ , "cdata"
+ , "closecdata"
+ , "error"
+ , "end"
+ , "ready"
+ , "script"
+ , "opennamespace"
+ , "closenamespace"
+ ]
+
+function SAXParser (strict, opt) {
+ if (!(this instanceof SAXParser)) return new SAXParser(strict, opt)
+
+ var parser = this
+ clearBuffers(parser)
+ parser.q = parser.c = ""
+ parser.bufferCheckPosition = sax.MAX_BUFFER_LENGTH
+ parser.opt = opt || {}
+ parser.tagCase = parser.opt.lowercasetags ? "toLowerCase" : "toUpperCase"
+ parser.tags = []
+ parser.closed = parser.closedRoot = parser.sawRoot = false
+ parser.tag = parser.error = null
+ parser.strict = !!strict
+ parser.noscript = !!(strict || parser.opt.noscript)
+ parser.state = S.BEGIN
+ parser.ENTITIES = Object.create(sax.ENTITIES)
+ parser.attribList = []
+
+ // namespaces form a prototype chain.
+ // it always points at the current tag,
+ // which protos to its parent tag.
+ if (parser.opt.xmlns) parser.ns = Object.create(rootNS)
+
+ // mostly just for error reporting
+ parser.position = parser.line = parser.column = 0
+ emit(parser, "onready")
+}
+
+if (!Object.create) Object.create = function (o) {
+ function f () { this.__proto__ = o }
+ f.prototype = o
+ return new f
+}
+
+if (!Object.getPrototypeOf) Object.getPrototypeOf = function (o) {
+ return o.__proto__
+}
+
+if (!Object.keys) Object.keys = function (o) {
+ var a = []
+ for (var i in o) if (o.hasOwnProperty(i)) a.push(i)
+ return a
+}
+
+function checkBufferLength (parser) {
+ var maxAllowed = Math.max(sax.MAX_BUFFER_LENGTH, 10)
+ , maxActual = 0
+ for (var i = 0, l = buffers.length; i < l; i ++) {
+ var len = parser[buffers[i]].length
+ if (len > maxAllowed) {
+ // Text/cdata nodes can get big, and since they're buffered,
+ // we can get here under normal conditions.
+ // Avoid issues by emitting the text node now,
+ // so at least it won't get any bigger.
+ switch (buffers[i]) {
+ case "textNode":
+ closeText(parser)
+ break
+
+ case "cdata":
+ emitNode(parser, "oncdata", parser.cdata)
+ parser.cdata = ""
+ break
+
+ case "script":
+ emitNode(parser, "onscript", parser.script)
+ parser.script = ""
+ break
+
+ default:
+ error(parser, "Max buffer length exceeded: "+buffers[i])
+ }
+ }
+ maxActual = Math.max(maxActual, len)
+ }
+ // schedule the next check for the earliest possible buffer overrun.
+ parser.bufferCheckPosition = (sax.MAX_BUFFER_LENGTH - maxActual)
+ + parser.position
+}
+
+function clearBuffers (parser) {
+ for (var i = 0, l = buffers.length; i < l; i ++) {
+ parser[buffers[i]] = ""
+ }
+}
+
+SAXParser.prototype =
+ { end: function () { end(this) }
+ , write: write
+ , resume: function () { this.error = null; return this }
+ , close: function () { return this.write(null) }
+ }
+
+try {
+ var Stream = require("stream").Stream
+} catch (ex) {
+ var Stream = function () {}
+}
+
+
+var streamWraps = sax.EVENTS.filter(function (ev) {
+ return ev !== "error" && ev !== "end"
+})
+
+function createStream (strict, opt) {
+ return new SAXStream(strict, opt)
+}
+
+function SAXStream (strict, opt) {
+ if (!(this instanceof SAXStream)) return new SAXStream(strict, opt)
+
+ Stream.apply(me)
+
+ this._parser = new SAXParser(strict, opt)
+ this.writable = true
+ this.readable = true
+
+
+ var me = this
+
+ this._parser.onend = function () {
+ me.emit("end")
+ }
+
+ this._parser.onerror = function (er) {
+ me.emit("error", er)
+
+ // if didn't throw, then means error was handled.
+ // go ahead and clear error, so we can write again.
+ me._parser.error = null
+ }
+
+ streamWraps.forEach(function (ev) {
+ Object.defineProperty(me, "on" + ev, {
+ get: function () { return me._parser["on" + ev] },
+ set: function (h) {
+ if (!h) {
+ me.removeAllListeners(ev)
+ return me._parser["on"+ev] = h
+ }
+ me.on(ev, h)
+ },
+ enumerable: true,
+ configurable: false
+ })
+ })
+}
+
+SAXStream.prototype = Object.create(Stream.prototype,
+ { constructor: { value: SAXStream } })
+
+SAXStream.prototype.write = function (data) {
+ this._parser.write(data.toString())
+ this.emit("data", data)
+ return true
+}
+
+SAXStream.prototype.end = function (chunk) {
+ if (chunk && chunk.length) this._parser.write(chunk.toString())
+ this._parser.end()
+ return true
+}
+
+SAXStream.prototype.on = function (ev, handler) {
+ var me = this
+ if (!me._parser["on"+ev] && streamWraps.indexOf(ev) !== -1) {
+ me._parser["on"+ev] = function () {
+ var args = arguments.length === 1 ? [arguments[0]]
+ : Array.apply(null, arguments)
+ args.splice(0, 0, ev)
+ me.emit.apply(me, args)
+ }
+ }
+
+ return Stream.prototype.on.call(me, ev, handler)
+}
+
+
+
+// character classes and tokens
+var whitespace = "\r\n\t "
+ // this really needs to be replaced with character classes.
+ // XML allows all manner of ridiculous numbers and digits.
+ , number = "0124356789"
+ , letter = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
+ // (Letter | "_" | ":")
+ , nameStart = letter+"_:"
+ , nameBody = nameStart+number+"-."
+ , quote = "'\""
+ , entity = number+letter+"#"
+ , attribEnd = whitespace + ">"
+ , CDATA = "[CDATA["
+ , DOCTYPE = "DOCTYPE"
+ , XML_NAMESPACE = "http://www.w3.org/XML/1998/namespace"
+ , XMLNS_NAMESPACE = "http://www.w3.org/2000/xmlns/"
+ , rootNS = { xml: XML_NAMESPACE, xmlns: XMLNS_NAMESPACE }
+
+// turn all the string character sets into character class objects.
+whitespace = charClass(whitespace)
+number = charClass(number)
+letter = charClass(letter)
+nameStart = charClass(nameStart)
+nameBody = charClass(nameBody)
+quote = charClass(quote)
+entity = charClass(entity)
+attribEnd = charClass(attribEnd)
+
+function charClass (str) {
+ return str.split("").reduce(function (s, c) {
+ s[c] = true
+ return s
+ }, {})
+}
+
+function is (charclass, c) {
+ return charclass[c]
+}
+
+function not (charclass, c) {
+ return !charclass[c]
+}
+
+var S = 0
+sax.STATE =
+{ BEGIN : S++
+, TEXT : S++ // general stuff
+, TEXT_ENTITY : S++ // & and such.
+, OPEN_WAKA : S++ // <
+, SGML_DECL : S++ // <!BLARG
+, SGML_DECL_QUOTED : S++ // <!BLARG foo "bar
+, DOCTYPE : S++ // <!DOCTYPE
+, DOCTYPE_QUOTED : S++ // <!DOCTYPE "//blah
+, DOCTYPE_DTD : S++ // <!DOCTYPE "//blah" [ ...
+, DOCTYPE_DTD_QUOTED : S++ // <!DOCTYPE "//blah" [ "foo
+, COMMENT_STARTING : S++ // <!-
+, COMMENT : S++ // <!--
+, COMMENT_ENDING : S++ // <!-- blah -
+, COMMENT_ENDED : S++ // <!-- blah --
+, CDATA : S++ // <![CDATA[ something
+, CDATA_ENDING : S++ // ]
+, CDATA_ENDING_2 : S++ // ]]
+, PROC_INST : S++ // <?hi
+, PROC_INST_BODY : S++ // <?hi there
+, PROC_INST_QUOTED : S++ // <?hi "there
+, PROC_INST_ENDING : S++ // <?hi "there" ?
+, OPEN_TAG : S++ // <strong
+, OPEN_TAG_SLASH : S++ // <strong /
+, ATTRIB : S++ // <a
+, ATTRIB_NAME : S++ // <a foo
+, ATTRIB_NAME_SAW_WHITE : S++ // <a foo _
+, ATTRIB_VALUE : S++ // <a foo=
+, ATTRIB_VALUE_QUOTED : S++ // <a foo="bar
+, ATTRIB_VALUE_UNQUOTED : S++ // <a foo=bar
+, ATTRIB_VALUE_ENTITY_Q : S++ // <foo bar="""
+, ATTRIB_VALUE_ENTITY_U : S++ // <foo bar="
+, CLOSE_TAG : S++ // </a
+, CLOSE_TAG_SAW_WHITE : S++ // </a >
+, SCRIPT : S++ // <script> ...
+, SCRIPT_ENDING : S++ // <script> ... <
+}
+
+sax.ENTITIES =
+{ "apos" : "'"
+, "quot" : "\""
+, "amp" : "&"
+, "gt" : ">"
+, "lt" : "<"
+}
+
+for (var S in sax.STATE) sax.STATE[sax.STATE[S]] = S
+
+// shorthand
+S = sax.STATE
+
+function emit (parser, event, data) {
+ parser[event] && parser[event](data)
+}
+
+function emitNode (parser, nodeType, data) {
+ if (parser.textNode) closeText(parser)
+ emit(parser, nodeType, data)
+}
+
+function closeText (parser) {
+ parser.textNode = textopts(parser.opt, parser.textNode)
+ if (parser.textNode) emit(parser, "ontext", parser.textNode)
+ parser.textNode = ""
+}
+
+function textopts (opt, text) {
+ if (opt.trim) text = text.trim()
+ if (opt.normalize) text = text.replace(/\s+/g, " ")
+ return text
+}
+
+function error (parser, er) {
+ closeText(parser)
+ er += "\nLine: "+parser.line+
+ "\nColumn: "+parser.column+
+ "\nChar: "+parser.c
+ er = new Error(er)
+ parser.error = er
+ emit(parser, "onerror", er)
+ return parser
+}
+
+function end (parser) {
+ if (parser.state !== S.TEXT) error(parser, "Unexpected end")
+ closeText(parser)
+ parser.c = ""
+ parser.closed = true
+ emit(parser, "onend")
+ SAXParser.call(parser, parser.strict, parser.opt)
+ return parser
+}
+
+function strictFail (parser, message) {
+ if (parser.strict) error(parser, message)
+}
+
+function newTag (parser) {
+ if (!parser.strict) parser.tagName = parser.tagName[parser.tagCase]()
+ var parent = parser.tags[parser.tags.length - 1] || parser
+ , tag = parser.tag = { name : parser.tagName, attributes : {} }
+
+ // will be overridden if tag contails an xmlns="foo" or xmlns:foo="bar"
+ if (parser.opt.xmlns) tag.ns = parent.ns
+ parser.attribList.length = 0
+}
+
+function qname (name) {
+ var i = name.indexOf(":")
+ , qualName = i < 0 ? [ "", name ] : name.split(":")
+ , prefix = qualName[0]
+ , local = qualName[1]
+
+ // <x "xmlns"="http://foo">
+ if (name === "xmlns") {
+ prefix = "xmlns"
+ local = ""
+ }
+
+ return { prefix: prefix, local: local }
+}
+
+function attrib (parser) {
+ if (parser.opt.xmlns) {
+ var qn = qname(parser.attribName)
+ , prefix = qn.prefix
+ , local = qn.local
+
+ if (prefix === "xmlns") {
+ // namespace binding attribute; push the binding into scope
+ if (local === "xml" && parser.attribValue !== XML_NAMESPACE) {
+ strictFail( parser
+ , "xml: prefix must be bound to " + XML_NAMESPACE + "\n"
+ + "Actual: " + parser.attribValue )
+ } else if (local === "xmlns" && parser.attribValue !== XMLNS_NAMESPACE) {
+ strictFail( parser
+ , "xmlns: prefix must be bound to " + XMLNS_NAMESPACE + "\n"
+ + "Actual: " + parser.attribValue )
+ } else {
+ var tag = parser.tag
+ , parent = parser.tags[parser.tags.length - 1] || parser
+ if (tag.ns === parent.ns) {
+ tag.ns = Object.create(parent.ns)
+ }
+ tag.ns[local] = parser.attribValue
+ }
+ }
+
+ // defer onattribute events until all attributes have been seen
+ // so any new bindings can take effect; preserve attribute order
+ // so deferred events can be emitted in document order
+ parser.attribList.push([parser.attribName, parser.attribValue])
+ } else {
+ // in non-xmlns mode, we can emit the event right away
+ parser.tag.attributes[parser.attribName] = parser.attribValue
+ emitNode( parser
+ , "onattribute"
+ , { name: parser.attribName
+ , value: parser.attribValue } )
+ }
+
+ parser.attribName = parser.attribValue = ""
+}
+
+function openTag (parser, selfClosing) {
+ if (parser.opt.xmlns) {
+ // emit namespace binding events
+ var tag = parser.tag
+
+ // add namespace info to tag
+ var qn = qname(parser.tagName)
+ tag.prefix = qn.prefix
+ tag.local = qn.local
+ tag.uri = tag.ns[qn.prefix] || qn.prefix
+
+ if (tag.prefix && !tag.uri) {
+ strictFail(parser, "Unbound namespace prefix: "
+ + JSON.stringify(parser.tagName))
+ }
+
+ var parent = parser.tags[parser.tags.length - 1] || parser
+ if (tag.ns && parent.ns !== tag.ns) {
+ Object.keys(tag.ns).forEach(function (p) {
+ emitNode( parser
+ , "onopennamespace"
+ , { prefix: p , uri: tag.ns[p] } )
+ })
+ }
+
+ // handle deferred onattribute events
+ for (var i = 0, l = parser.attribList.length; i < l; i ++) {
+ var nv = parser.attribList[i]
+ var name = nv[0]
+ , value = nv[1]
+ , qualName = qname(name)
+ , prefix = qualName.prefix
+ , local = qualName.local
+ , uri = tag.ns[prefix] || ""
+ , a = { name: name
+ , value: value
+ , prefix: prefix
+ , local: local
+ , uri: uri
+ }
+
+ // if there's any attributes with an undefined namespace,
+ // then fail on them now.
+ if (prefix && prefix != "xmlns" && !uri) {
+ strictFail(parser, "Unbound namespace prefix: "
+ + JSON.stringify(prefix))
+ a.uri = prefix
+ }
+ parser.tag.attributes[name] = a
+ emitNode(parser, "onattribute", a)
+ }
+ parser.attribList.length = 0
+ }
+
+ // process the tag
+ parser.sawRoot = true
+ parser.tags.push(parser.tag)
+ emitNode(parser, "onopentag", parser.tag)
+ if (!selfClosing) {
+ // special case for <script> in non-strict mode.
+ if (!parser.noscript && parser.tagName.toLowerCase() === "script") {
+ parser.state = S.SCRIPT
+ } else {
+ parser.state = S.TEXT
+ }
+ parser.tag = null
+ parser.tagName = ""
+ }
+ parser.attribName = parser.attribValue = ""
+ parser.attribList.length = 0
+}
+
+function closeTag (parser) {
+ if (!parser.tagName) {
+ strictFail(parser, "Weird empty close tag.")
+ parser.textNode += "</>"
+ parser.state = S.TEXT
+ return
+ }
+ // first make sure that the closing tag actually exists.
+ // <a><b></c></b></a> will close everything, otherwise.
+ var t = parser.tags.length
+ var tagName = parser.tagName
+ if (!parser.strict) tagName = tagName[parser.tagCase]()
+ var closeTo = tagName
+ while (t --) {
+ var close = parser.tags[t]
+ if (close.name !== closeTo) {
+ // fail the first time in strict mode
+ strictFail(parser, "Unexpected close tag")
+ } else break
+ }
+
+ // didn't find it. we already failed for strict, so just abort.
+ if (t < 0) {
+ strictFail(parser, "Unmatched closing tag: "+parser.tagName)
+ parser.textNode += "</" + parser.tagName + ">"
+ parser.state = S.TEXT
+ return
+ }
+ parser.tagName = tagName
+ var s = parser.tags.length
+ while (s --> t) {
+ var tag = parser.tag = parser.tags.pop()
+ parser.tagName = parser.tag.name
+ emitNode(parser, "onclosetag", parser.tagName)
+
+ var x = {}
+ for (var i in tag.ns) x[i] = tag.ns[i]
+
+ var parent = parser.tags[parser.tags.length - 1] || parser
+ if (parser.opt.xmlns && tag.ns !== parent.ns) {
+ // remove namespace bindings introduced by tag
+ Object.keys(tag.ns).forEach(function (p) {
+ var n = tag.ns[p]
+ emitNode(parser, "onclosenamespace", { prefix: p, uri: n })
+ })
+ }
+ }
+ if (t === 0) parser.closedRoot = true
+ parser.tagName = parser.attribValue = parser.attribName = ""
+ parser.attribList.length = 0
+ parser.state = S.TEXT
+}
+
+function parseEntity (parser) {
+ var entity = parser.entity.toLowerCase()
+ , num
+ , numStr = ""
+ if (parser.ENTITIES[entity]) return parser.ENTITIES[entity]
+ if (entity.charAt(0) === "#") {
+ if (entity.charAt(1) === "x") {
+ entity = entity.slice(2)
+ num = parseInt(entity, 16)
+ numStr = num.toString(16)
+ } else {
+ entity = entity.slice(1)
+ num = parseInt(entity, 10)
+ numStr = num.toString(10)
+ }
+ }
+ entity = entity.replace(/^0+/, "")
+ if (numStr.toLowerCase() !== entity) {
+ strictFail(parser, "Invalid character entity")
+ return "&"+parser.entity + ";"
+ }
+ return String.fromCharCode(num)
+}
+
+function write (chunk) {
+ var parser = this
+ if (this.error) throw this.error
+ if (parser.closed) return error(parser,
+ "Cannot write after close. Assign an onready handler.")
+ if (chunk === null) return end(parser)
+ var i = 0, c = ""
+ while (parser.c = c = chunk.charAt(i++)) {
+ parser.position ++
+ if (c === "\n") {
+ parser.line ++
+ parser.column = 0
+ } else parser.column ++
+ switch (parser.state) {
+
+ case S.BEGIN:
+ if (c === "<") parser.state = S.OPEN_WAKA
+ else if (not(whitespace,c)) {
+ // have to process this as a text node.
+ // weird, but happens.
+ strictFail(parser, "Non-whitespace before first tag.")
+ parser.textNode = c
+ parser.state = S.TEXT
+ }
+ continue
+
+ case S.TEXT:
+ if (parser.sawRoot && !parser.closedRoot) {
+ var starti = i-1
+ while (c && c!=="<" && c!=="&") {
+ c = chunk.charAt(i++)
+ if (c) {
+ parser.position ++
+ if (c === "\n") {
+ parser.line ++
+ parser.column = 0
+ } else parser.column ++
+ }
+ }
+ parser.textNode += chunk.substring(starti, i-1)
+ }
+ if (c === "<") parser.state = S.OPEN_WAKA
+ else {
+ if (not(whitespace, c) && (!parser.sawRoot || parser.closedRoot))
+ strictFail("Text data outside of root node.")
+ if (c === "&") parser.state = S.TEXT_ENTITY
+ else parser.textNode += c
+ }
+ continue
+
+ case S.SCRIPT:
+ // only non-strict
+ if (c === "<") {
+ parser.state = S.SCRIPT_ENDING
+ } else parser.script += c
+ continue
+
+ case S.SCRIPT_ENDING:
+ if (c === "/") {
+ emitNode(parser, "onscript", parser.script)
+ parser.state = S.CLOSE_TAG
+ parser.script = ""
+ parser.tagName = ""
+ } else {
+ parser.script += "<" + c
+ parser.state = S.SCRIPT
+ }
+ continue
+
+ case S.OPEN_WAKA:
+ // either a /, ?, !, or text is coming next.
+ if (c === "!") {
+ parser.state = S.SGML_DECL
+ parser.sgmlDecl = ""
+ } else if (is(whitespace, c)) {
+ // wait for it...
+ } else if (is(nameStart,c)) {
+ parser.startTagPosition = parser.position - 1
+ parser.state = S.OPEN_TAG
+ parser.tagName = c
+ } else if (c === "/") {
+ parser.startTagPosition = parser.position - 1
+ parser.state = S.CLOSE_TAG
+ parser.tagName = ""
+ } else if (c === "?") {
+ parser.state = S.PROC_INST
+ parser.procInstName = parser.procInstBody = ""
+ } else {
+ strictFail(parser, "Unencoded <")
+ parser.textNode += "<" + c
+ parser.state = S.TEXT
+ }
+ continue
+
+ case S.SGML_DECL:
+ if ((parser.sgmlDecl+c).toUpperCase() === CDATA) {
+ emitNode(parser, "onopencdata")
+ parser.state = S.CDATA
+ parser.sgmlDecl = ""
+ parser.cdata = ""
+ } else if (parser.sgmlDecl+c === "--") {
+ parser.state = S.COMMENT
+ parser.comment = ""
+ parser.sgmlDecl = ""
+ } else if ((parser.sgmlDecl+c).toUpperCase() === DOCTYPE) {
+ parser.state = S.DOCTYPE
+ if (parser.doctype || parser.sawRoot) strictFail(parser,
+ "Inappropriately located doctype declaration")
+ parser.doctype = ""
+ parser.sgmlDecl = ""
+ } else if (c === ">") {
+ emitNode(parser, "onsgmldeclaration", parser.sgmlDecl)
+ parser.sgmlDecl = ""
+ parser.state = S.TEXT
+ } else if (is(quote, c)) {
+ parser.state = S.SGML_DECL_QUOTED
+ parser.sgmlDecl += c
+ } else parser.sgmlDecl += c
+ continue
+
+ case S.SGML_DECL_QUOTED:
+ if (c === parser.q) {
+ parser.state = S.SGML_DECL
+ parser.q = ""
+ }
+ parser.sgmlDecl += c
+ continue
+
+ case S.DOCTYPE:
+ if (c === ">") {
+ parser.state = S.TEXT
+ emitNode(parser, "ondoctype", parser.doctype)
+ parser.doctype = true // just remember that we saw it.
+ } else {
+ parser.doctype += c
+ if (c === "[") parser.state = S.DOCTYPE_DTD
+ else if (is(quote, c)) {
+ parser.state = S.DOCTYPE_QUOTED
+ parser.q = c
+ }
+ }
+ continue
+
+ case S.DOCTYPE_QUOTED:
+ parser.doctype += c
+ if (c === parser.q) {
+ parser.q = ""
+ parser.state = S.DOCTYPE
+ }
+ continue
+
+ case S.DOCTYPE_DTD:
+ parser.doctype += c
+ if (c === "]") parser.state = S.DOCTYPE
+ else if (is(quote,c)) {
+ parser.state = S.DOCTYPE_DTD_QUOTED
+ parser.q = c
+ }
+ continue
+
+ case S.DOCTYPE_DTD_QUOTED:
+ parser.doctype += c
+ if (c === parser.q) {
+ parser.state = S.DOCTYPE_DTD
+ parser.q = ""
+ }
+ continue
+
+ case S.COMMENT:
+ if (c === "-") parser.state = S.COMMENT_ENDING
+ else parser.comment += c
+ continue
+
+ case S.COMMENT_ENDING:
+ if (c === "-") {
+ parser.state = S.COMMENT_ENDED
+ parser.comment = textopts(parser.opt, parser.comment)
+ if (parser.comment) emitNode(parser, "oncomment", parser.comment)
+ parser.comment = ""
+ } else {
+ parser.comment += "-" + c
+ parser.state = S.COMMENT
+ }
+ continue
+
+ case S.COMMENT_ENDED:
+ if (c !== ">") {
+ strictFail(parser, "Malformed comment")
+ // allow <!-- blah -- bloo --> in non-strict mode,
+ // which is a comment of " blah -- bloo "
+ parser.comment += "--" + c
+ parser.state = S.COMMENT
+ } else parser.state = S.TEXT
+ continue
+
+ case S.CDATA:
+ if (c === "]") parser.state = S.CDATA_ENDING
+ else parser.cdata += c
+ continue
+
+ case S.CDATA_ENDING:
+ if (c === "]") parser.state = S.CDATA_ENDING_2
+ else {
+ parser.cdata += "]" + c
+ parser.state = S.CDATA
+ }
+ continue
+
+ case S.CDATA_ENDING_2:
+ if (c === ">") {
+ if (parser.cdata) emitNode(parser, "oncdata", parser.cdata)
+ emitNode(parser, "onclosecdata")
+ parser.cdata = ""
+ parser.state = S.TEXT
+ } else if (c === "]") {
+ parser.cdata += "]"
+ } else {
+ parser.cdata += "]]" + c
+ parser.state = S.CDATA
+ }
+ continue
+
+ case S.PROC_INST:
+ if (c === "?") parser.state = S.PROC_INST_ENDING
+ else if (is(whitespace, c)) parser.state = S.PROC_INST_BODY
+ else parser.procInstName += c
+ continue
+
+ case S.PROC_INST_BODY:
+ if (!parser.procInstBody && is(whitespace, c)) continue
+ else if (c === "?") parser.state = S.PROC_INST_ENDING
+ else if (is(quote, c)) {
+ parser.state = S.PROC_INST_QUOTED
+ parser.q = c
+ parser.procInstBody += c
+ } else parser.procInstBody += c
+ continue
+
+ case S.PROC_INST_ENDING:
+ if (c === ">") {
+ emitNode(parser, "onprocessinginstruction", {
+ name : parser.procInstName,
+ body : parser.procInstBody
+ })
+ parser.procInstName = parser.procInstBody = ""
+ parser.state = S.TEXT
+ } else {
+ parser.procInstBody += "?" + c
+ parser.state = S.PROC_INST_BODY
+ }
+ continue
+
+ case S.PROC_INST_QUOTED:
+ parser.procInstBody += c
+ if (c === parser.q) {
+ parser.state = S.PROC_INST_BODY
+ parser.q = ""
+ }
+ continue
+
+ case S.OPEN_TAG:
+ if (is(nameBody, c)) parser.tagName += c
+ else {
+ newTag(parser)
+ if (c === ">") openTag(parser)
+ else if (c === "/") parser.state = S.OPEN_TAG_SLASH
+ else {
+ if (not(whitespace, c)) strictFail(
+ parser, "Invalid character in tag name")
+ parser.state = S.ATTRIB
+ }
+ }
+ continue
+
+ case S.OPEN_TAG_SLASH:
+ if (c === ">") {
+ openTag(parser, true)
+ closeTag(parser)
+ } else {
+ strictFail(parser, "Forward-slash in opening tag not followed by >")
+ parser.state = S.ATTRIB
+ }
+ continue
+
+ case S.ATTRIB:
+ // haven't read the attribute name yet.
+ if (is(whitespace, c)) continue
+ else if (c === ">") openTag(parser)
+ else if (c === "/") parser.state = S.OPEN_TAG_SLASH
+ else if (is(nameStart, c)) {
+ parser.attribName = c
+ parser.attribValue = ""
+ parser.state = S.ATTRIB_NAME
+ } else strictFail(parser, "Invalid attribute name")
+ continue
+
+ case S.ATTRIB_NAME:
+ if (c === "=") parser.state = S.ATTRIB_VALUE
+ else if (is(whitespace, c)) parser.state = S.ATTRIB_NAME_SAW_WHITE
+ else if (is(nameBody, c)) parser.attribName += c
+ else strictFail(parser, "Invalid attribute name")
+ continue
+
+ case S.ATTRIB_NAME_SAW_WHITE:
+ if (c === "=") parser.state = S.ATTRIB_VALUE
+ else if (is(whitespace, c)) continue
+ else {
+ strictFail(parser, "Attribute without value")
+ parser.tag.attributes[parser.attribName] = ""
+ parser.attribValue = ""
+ emitNode(parser, "onattribute",
+ { name : parser.attribName, value : "" })
+ parser.attribName = ""
+ if (c === ">") openTag(parser)
+ else if (is(nameStart, c)) {
+ parser.attribName = c
+ parser.state = S.ATTRIB_NAME
+ } else {
+ strictFail(parser, "Invalid attribute name")
+ parser.state = S.ATTRIB
+ }
+ }
+ continue
+
+ case S.ATTRIB_VALUE:
+ if (is(whitespace, c)) continue
+ else if (is(quote, c)) {
+ parser.q = c
+ parser.state = S.ATTRIB_VALUE_QUOTED
+ } else {
+ strictFail(parser, "Unquoted attribute value")
+ parser.state = S.ATTRIB_VALUE_UNQUOTED
+ parser.attribValue = c
+ }
+ continue
+
+ case S.ATTRIB_VALUE_QUOTED:
+ if (c !== parser.q) {
+ if (c === "&") parser.state = S.ATTRIB_VALUE_ENTITY_Q
+ else parser.attribValue += c
+ continue
+ }
+ attrib(parser)
+ parser.q = ""
+ parser.state = S.ATTRIB
+ continue
+
+ case S.ATTRIB_VALUE_UNQUOTED:
+ if (not(attribEnd,c)) {
+ if (c === "&") parser.state = S.ATTRIB_VALUE_ENTITY_U
+ else parser.attribValue += c
+ continue
+ }
+ attrib(parser)
+ if (c === ">") openTag(parser)
+ else parser.state = S.ATTRIB
+ continue
+
+ case S.CLOSE_TAG:
+ if (!parser.tagName) {
+ if (is(whitespace, c)) continue
+ else if (not(nameStart, c)) strictFail(parser,
+ "Invalid tagname in closing tag.")
+ else parser.tagName = c
+ }
+ else if (c === ">") closeTag(parser)
+ else if (is(nameBody, c)) parser.tagName += c
+ else {
+ if (not(whitespace, c)) strictFail(parser,
+ "Invalid tagname in closing tag")
+ parser.state = S.CLOSE_TAG_SAW_WHITE
+ }
+ continue
+
+ case S.CLOSE_TAG_SAW_WHITE:
+ if (is(whitespace, c)) continue
+ if (c === ">") closeTag(parser)
+ else strictFail("Invalid characters in closing tag")
+ continue
+
+ case S.TEXT_ENTITY:
+ case S.ATTRIB_VALUE_ENTITY_Q:
+ case S.ATTRIB_VALUE_ENTITY_U:
+ switch(parser.state) {
+ case S.TEXT_ENTITY:
+ var returnState = S.TEXT, buffer = "textNode"
+ break
+
+ case S.ATTRIB_VALUE_ENTITY_Q:
+ var returnState = S.ATTRIB_VALUE_QUOTED, buffer = "attribValue"
+ break
+
+ case S.ATTRIB_VALUE_ENTITY_U:
+ var returnState = S.ATTRIB_VALUE_UNQUOTED, buffer = "attribValue"
+ break
+ }
+ if (c === ";") {
+ parser[buffer] += parseEntity(parser)
+ parser.entity = ""
+ parser.state = returnState
+ }
+ else if (is(entity, c)) parser.entity += c
+ else {
+ strictFail("Invalid character entity")
+ parser[buffer] += "&" + parser.entity + c
+ parser.entity = ""
+ parser.state = returnState
+ }
+ continue
+
+ default:
+ throw new Error(parser, "Unknown state: " + parser.state)
+ }
+ } // while
+ // cdata blocks can get very big under normal conditions. emit and move on.
+ // if (parser.state === S.CDATA && parser.cdata) {
+ // emitNode(parser, "oncdata", parser.cdata)
+ // parser.cdata = ""
+ // }
+ if (parser.position >= parser.bufferCheckPosition) checkBufferLength(parser)
+ return parser
+}
+
+})(typeof exports === "undefined" ? sax = {} : exports)
\ No newline at end of file
diff --git a/latest/encoding/xml2object.js b/latest/encoding/xml2object.js
new file mode 100644
index 0000000..c6711b6
--- /dev/null
+++ b/latest/encoding/xml2object.js
@@ -0,0 +1,170 @@
+/**
+ * Simple xml 2 javascript object parser based on sax.js
+ *
+ * https://github.com/emberfeather/node-xml2object
+ */
+var emitter = require('events').EventEmitter;
+var fs = require('fs');
+var sax = require('./sax');
+var util = require('util');
+
+var xml2object = function(xmlFile, elements) {
+ elements = elements || [];
+
+ this._hasStarted = false;
+
+ var self = this;
+ var currentObject;
+ var inObject = false;
+ var inObjectName;
+ var ancestors = [];
+
+ this.fileStream = fs.createReadStream(xmlFile);
+ this.saxStream = sax.createStream(true);
+
+ this.saxStream.on("opentag", function (args) {
+ if(!inObject) {
+ // If we are not in an object and not tracking the element
+ // then we don't need to do anything
+ if (elements.indexOf(args.name) < 0) {
+ return;
+ }
+
+ // Start tracking a new object
+ inObject = true;
+ inObjectName = args.name;
+
+ currentObject = {};
+ }
+
+ if (!(args.name in currentObject)) {
+ currentObject[args.name] = args.attributes;
+ } else if (!util.isArray(currentObject[args.name])) {
+ // Put the existing object in an array.
+ var newArray = [currentObject[args.name]];
+
+ // Add the new object to the array.
+ newArray.push(args.attributes);
+
+ // Point to the new array.
+ currentObject[args.name] = newArray;
+ } else {
+ // An array already exists, push the attributes on to it.
+ currentObject[args.name].push(args.attributes);
+ }
+
+ // Store the current (old) parent.
+ ancestors.push(currentObject);
+
+ // We are now working with this object, so it becomes the current parent.
+ if (currentObject[args.name] instanceof Array) {
+ // If it is an array, get the last element of the array.
+ currentObject = currentObject[args.name][currentObject[args.name].length - 1];
+ } else {
+ // Otherwise, use the object itself.
+ currentObject = currentObject[args.name];
+ }
+ });
+
+ this.saxStream.on("text", function (data) {
+ if(!inObject) {
+ return;
+ }
+
+ data = data.trim();
+
+ if (!data.length) {
+ return;
+ }
+
+ currentObject['$t'] = (currentObject['$t'] || "") + data;
+ });
+
+ this.saxStream.on("closetag", function (name) {
+ if(!inObject) {
+ return;
+ }
+
+ if(inObject && inObjectName === name) {
+ // Finished building the object
+ self.emit('object', name, currentObject);
+
+ inObject = false;
+ ancestors = [];
+
+ return;
+ }
+
+ if(ancestors.length) {
+ var ancestor = ancestors.pop();
+ var keys = Object.keys(currentObject);
+
+ if (keys.length == 1 && '$t' in currentObject) {
+ // Convert the text only objects into just the text
+ if (ancestor[name] instanceof Array) {
+ ancestor[name].push(ancestor[name].pop()['$t']);
+ } else {
+ ancestor[name] = currentObject['$t'];
+ }
+ } else if (!keys.length) {
+ // Remove empty keys
+ delete ancestor[name];
+ }
+
+ currentObject = ancestor;
+ } else {
+ currentObject = {};
+ }
+ });
+
+ // Rebroadcast the error and keep going
+ this.saxStream.on("error", function (e) {
+ this.emit('error', e);
+
+ // clear the error and resume
+ this._parser.error = null;
+ this._parser.resume();
+ });
+
+ // Rebroadcast the end of the file read
+ this.fileStream.on("end", function() {
+ self.emit("end");
+ });
+};
+
+util.inherits(xml2object, emitter);
+
+xml2object.prototype.start = function() {
+ // Can only start once
+ if(this._hasStarted) {
+ return;
+ }
+
+ this._hasStarted = true;
+
+ this.emit('start');
+
+ // Start the streaming!
+ this.fileStream.pipe(this.saxStream);
+};
+
+
+//TEST///////////////////////////////////////////////////////////////////////////////
+//var xml2object = require('xml2object');
+
+// Create a new xml parser with an array of xml elements to look for
+/*var parser = new xml2object('./src/encoding/ContentObject1.xml', [ 'ContentObject' ]);
+
+// Bind to the object event to work with the objects found in the XML file
+parser.on('object', function(name, obj) {
+ console.log('Found an object: %s', name);
+ console.log(obj);
+});
+
+// Bind to the file end event to tell when the file is done being streamed
+parser.on('end', function(name, obj) {
+ console.log('Finished parsing xml!');
+});
+
+// Start parsing the XML
+parser.start();*/
\ No newline at end of file
diff --git a/latest/index.html b/latest/index.html
new file mode 100644
index 0000000..0ff6078
--- /dev/null
+++ b/latest/index.html
@@ -0,0 +1,74 @@
+<?xml version = "1.0" encoding="utf-8" ?>
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
+"DTD/xhtml1-strict.dtd">
+<html xmlns = "http://www.w3.org/1999/xhtml">
+
+<head>
+ <title>NDN Request Example</title>
+ <script type="text/javascript" src="java_socket_bridge.js"></script>
+
+ <script type="text/javascript" src="CCNProtocolDTags.js"></script>
+
+ <script type="text/javascript" src="CCNTime.js"></script>
+
+ <script type="text/javascript" src="ContentName.js"></script>
+
+ <script type="text/javascript" src="ContentObject.js"></script>
+
+ <script type="text/javascript" src="DateFormat.js"></script>
+
+ <script type="text/javascript" src="Exclude.js"></script>
+
+ <script type="text/javascript" src="ExcludeAny.js"></script>
+
+ <script type="text/javascript" src="ExcludeComponent.js"></script>
+
+ <script type="text/javascript" src="Interest.js"></script>
+
+ <script type="text/javascript" src="KeyLocator.js"></script>
+
+ <script type="text/javascript" src="KeyName.js"></script>
+
+ <script type="text/javascript" src="PublisherID.js"></script>
+ <script type="text/javascript" src="Signature.js"></script>
+
+ <script type="text/javascript" src="SignedInfo.js"></script>
+
+
+
+ <script type="text/javascript" src="encoding/BinaryXMLCodec.js"></script>
+
+ <script type="text/javascript" src="encoding/BinaryXMLDecoder.js"></script>
+
+ <script type="text/javascript" src="encoding/DataUtils.js"></script>
+
+
+
+ <script type="text/javascript">
+ function run(){
+ console.log(document.getElementById('ip').value);
+ createRoute(document.getElementById('ip').value,document.getElementById('port').value);
+ document.getElementById('result').innerHTML = queryPrefix(document.getElementById('contentname').value);
+
+ //createRoute('localhost', 9695);
+ //document.getElementById('result').innerHTML = queryPrefix('01d2f2faa55041524300fa9d000102000000');
+ }
+ </script>
+
+</head>
+<body>
+ <form>
+ <input id="ip" type="text" name="IP" value="localhost" /> IP<br />
+ <input id="port" type="text" name="PORT" value="9695" /> URL<br />
+ <input id="contentname" type="text" name="CONTENTNAME" value="/PARC/%00%01%02" /> Content Name<br />
+ </form>
+ <button onclick="run()">Run!</button>
+ <div>
+ <applet id="JavaSocketBridge" archive="JavaSocketBridge.jar" code="JavaSocketBridge.class" width="0" height="0">
+ </applet>
+ </div>
+
+ <p id="result"></p>
+
+</body>
+</html>
\ No newline at end of file
diff --git a/latest/java_socket_bridge.js b/latest/java_socket_bridge.js
new file mode 100644
index 0000000..ee91dcb
--- /dev/null
+++ b/latest/java_socket_bridge.js
@@ -0,0 +1,63 @@
+// Global variables
+var java_socket_bridge_ready_flag = false;
+
+var ndnport =null;
+var ndnurl=null;
+
+// Applet reports it is ready to use
+function java_socket_bridge_ready(){
+ java_socket_bridge_ready_flag = true;
+}
+
+function createRoute(url, port){
+ ndnport = port;
+ ndnurl=url;
+}
+
+// Connect to a given url and port
+function queryPrefix(message){
+ if(ndnport!=null && ndnurl!=null){
+ var newMessage ='';
+
+ enc = new BinaryXMLEncoder();
+
+ //encoder.beginEncoding();
+
+
+ int = new Interest(new ContentName(['PARC','%00','%01','%02']));
+
+ int.encode(enc);
+
+ var hex = byte2hex(enc.ostream);
+
+ console.log('Conect and start '+ ndnurl +':'+ndnport+'-'+message);
+
+ return get_java_socket_bridge().connectAndStart(ndnurl,ndnport,message);
+ }
+ else{
+ console.log('ERROR URL OR PORT NOT SET');
+ return '';
+ }
+
+}
+
+
+function byte2hex(bytearray){var result = [];
+ var length = bytearray.length;
+ for (var i = 0;i < length;++i) {
+ result.push(AddFillerLeft(bytearray[i].toString(16).toUpperCase(), '0', 2));
+ }
+ return result.join('');
+}
+// Get something from the socket
+function on_socket_get(message){}
+
+// Report an error
+function on_socket_error(message){
+ alert(message);
+}
+
+// Get the applet object
+function get_java_socket_bridge(){
+ return document.getElementById('JavaSocketBridge');
+}
\ No newline at end of file