Skip to content

Commit e0f15c3

Browse files
author
Maarten
committed
Implement a bit of XML SAX parser
1 parent 948d60e commit e0f15c3

File tree

3 files changed

+61
-5
lines changed

3 files changed

+61
-5
lines changed

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
"private": true,
1010
"license": "MIT",
1111
"dependencies": {
12+
"@types/sax": "0.0.28",
13+
"sax": "^1.2.1"
1214
},
1315
"devDependencies": {
1416
"@types/chai": "^3.4.34",

src/stemmer/StemmerParser.ts

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import {createStream} from "sax";
2+
3+
4+
const strict = true; // set to false for html-mode
5+
// const parser = p(strict, {});
6+
//
7+
// parser.onerror = function (ignored) {
8+
// // an error happened.
9+
// };
10+
// parser.ontext = function (text) {
11+
// console.log(text);
12+
// };
13+
// parser.onopentag = function (node) {
14+
// // opened a tag. node has "name" and "attributes"
15+
// console.log(node);
16+
// };
17+
// parser.onattribute = function (attr) {
18+
// // an attribute. attr has "name" and "value"
19+
// console.log(attr);
20+
// };
21+
// parser.onend = function () {
22+
// // parser stream is done, and ready to have more stuff written to it.
23+
// };
24+
25+
// stream usage
26+
// takes the same options as the parser
27+
const parseStream = createStream(strict, {});
28+
29+
parseStream.on("error", function (e: any) {
30+
// unhandled errors will throw, since this is a proper node
31+
// event emitter.
32+
console.error("error!", e);
33+
// clear the error
34+
this._parser.error = undefined;
35+
this._parser.resume();
36+
});
37+
parseStream.on("text", function (t: any) {
38+
console.log(t);
39+
});
40+
parseStream.on("attribute", function (t: any) {
41+
console.log(t);
42+
});
43+
44+
parseStream.on("opentag", function (tag: any) {
45+
// same object as above
46+
console.log(tag);
47+
});
48+
49+
export default parseStream;

test/stemmer/Stemmer.spec.ts

+10-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1+
import {createReadStream, createWriteStream} from "fs";
2+
import parseStream from "../../src/stemmer/StemmerParser";
3+
14
describe("Stemmer", () => {
2-
it("should load", () => {
3-
// FIXME: Work around https://code.google.com/p/android/issues/detail?id=64887 using an absolute path.
4-
// FileInputStream stream = new FileInputStream("D:/Users/Jeroen/AndroidstudioProjects/DictionaryApp/stemmer/src/test/resources/ph/bohol/util/stemmer/stemmerTest.xml");
5-
//
5+
it("should load", (done) => {
6+
createReadStream("test/stemmerTest.xml")
7+
.pipe(parseStream)
8+
.pipe(createWriteStream("file-copy.xml"))
9+
.on("close", () => done());
10+
611
// StemmerParser parser = new StemmerParser();
712
// Stemmer stemmer = parser.parse(stream);
813
//
@@ -29,7 +34,7 @@ describe("Stemmer", () => {
2934
// public final void testLargeLoad() throws FileNotFoundException {
3035
//
3136
// // FIXME: Work around https://code.google.com/p/android/issues/detail?id=64887 using an absolute path.
32-
// FileInputStream stream = new FileInputStream("D:/Users/Jeroen/AndroidstudioProjects/DictionaryApp/stemmer/src/test/resources/ph/bohol/util/stemmer/stemmerLargeTest.xml");
37+
// FileInputStream stream = new FileInputStream("stemmerLargeTest.xml");
3338
//
3439
// StemmerParser parser = new StemmerParser();
3540
// Stemmer stemmer = parser.parse(stream);

0 commit comments

Comments
 (0)