-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Corrected bug in DataTableXmlSerializer
- Loading branch information
1 parent
e92eb32
commit 9f20837
Showing
5 changed files
with
133 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
export class XmlEncode{ | ||
|
||
// https://www.w3.org/TR/REC-xml/#sec-common-syn | ||
// http://www.java2s.com/Code/Java/XML/EncoderandDecoderforXMLelementandattributenames.htm | ||
// http://www.fileformat.info/info/charset/UTF-16/list.htm | ||
|
||
static XML_NAME_NOT_ALLOWED_CHARACTERS = [' ','<','>','"','\'','&',';']; | ||
static XML_BEGINNING_NAME_NOT_ALLOWED_CHARACTERS = ['0','1','2','3','4','5','6','7','8','9']; | ||
|
||
static encodeName(value:string):string{ | ||
|
||
var encodedValues: string[] = []; | ||
var encodedName: string = null; | ||
|
||
if (value !== null && value.length > 0) { | ||
|
||
// first character | ||
if (XmlEncode.XML_BEGINNING_NAME_NOT_ALLOWED_CHARACTERS.indexOf(value[0]) != -1 || | ||
XmlEncode.XML_NAME_NOT_ALLOWED_CHARACTERS.indexOf(value[0]) != -1) { | ||
encodedValues.push(`_x00${value.charCodeAt(0).toString(16)}_`); | ||
} | ||
else { | ||
encodedValues.push(value[0]); | ||
} | ||
|
||
for (var i = 1; i < value.length; i++) { | ||
var char = value[i]; | ||
|
||
if (XmlEncode.XML_NAME_NOT_ALLOWED_CHARACTERS.indexOf(value[i]) != -1) { | ||
encodedValues.push(`_x00${value.charCodeAt(i).toString(16)}_`); | ||
} | ||
else { | ||
encodedValues.push(value[i]); | ||
} | ||
} | ||
} | ||
|
||
if (encodedValues.length > 0) { | ||
encodedName = encodedValues.join(''); | ||
} | ||
|
||
return encodedName; | ||
|
||
} | ||
|
||
static decodeName(encodedName:string){ | ||
|
||
var decodedNameParts:string[]=[]; | ||
var decodedName:string=null; | ||
|
||
if (encodedName !== null && encodedName.length > 0) { | ||
|
||
for (var i = 0; i < encodedName.length; i++) { | ||
var char = encodedName[i]; | ||
var encodedChar=false; | ||
|
||
if (char === '_') { | ||
if (encodedName.length - i > 6 && encodedName[i + 1] === 'x' && encodedName[i + 6] === '_') { | ||
var charCodeStr=encodedName.substr(i+2,4); | ||
var charCode = parseInt(charCodeStr,16); | ||
if (!isNaN(charCode) && isFinite(charCode)) { | ||
encodedChar=true; | ||
var decodedChar = String.fromCharCode(charCode); | ||
decodedNameParts.push(decodedChar); | ||
i+=6; | ||
} | ||
} | ||
} | ||
|
||
if(!encodedChar){ | ||
// just a regular char not encoded | ||
decodedNameParts.push(char); | ||
} | ||
} | ||
} | ||
|
||
if(decodedNameParts.length>0){ | ||
decodedName = decodedNameParts.join(''); | ||
} | ||
|
||
return decodedName; | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import assert = require("assert"); | ||
import { Guid } from "../src/Guid"; | ||
import {XmlEncode} from '../src/XmlEncode'; | ||
|
||
describe('XmlEncode', function () { | ||
it('Encodes and decodes a Name that begins with a number', function () { | ||
var name = '1 abcdef abcdef'; | ||
var encodedName = XmlEncode.encodeName(name); | ||
assert.equal(encodedName,'_x0031__x0020_abcdef_x0020_abcdef'); | ||
var decodedName = XmlEncode.decodeName(encodedName); | ||
assert.equal(decodedName,name); | ||
}); | ||
it('Encodes and decodes a Name that begins with a space', function () { | ||
var name = ' abcdef abcdef'; | ||
var encodedName = XmlEncode.encodeName(name); | ||
assert.equal(encodedName,'_x0020_abcdef_x0020_abcdef'); | ||
var decodedName = XmlEncode.decodeName(encodedName); | ||
assert.equal(decodedName,name); | ||
}); | ||
it('Encodes and decodes a very short Name', function () { | ||
var name = 'a'; | ||
var encodedName = XmlEncode.encodeName(name); | ||
assert.equal(encodedName,'a'); | ||
var decodedName = XmlEncode.decodeName(encodedName); | ||
assert.equal(decodedName,name); | ||
}); | ||
it('Encodes and decodes a very short encoded Name', function () { | ||
var name = '1'; | ||
var encodedName = XmlEncode.encodeName(name); | ||
assert.equal(encodedName,'_x0031_'); | ||
var decodedName = XmlEncode.decodeName(encodedName); | ||
assert.equal(decodedName,name); | ||
}); | ||
it('Returns null for a null or empty input', function () { | ||
var name = null; | ||
var encodedName = XmlEncode.encodeName(name); | ||
assert.ok(encodedName===null); | ||
|
||
var decodedName = XmlEncode.decodeName(encodedName); | ||
assert.ok(decodedName === null); | ||
}); | ||
|
||
}); |