本文整理汇总了TypeScript中sax.createStream函数的典型用法代码示例。如果您正苦于以下问题:TypeScript createStream函数的具体用法?TypeScript createStream怎么用?TypeScript createStream使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了createStream函数的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: updatePathway
export function updatePathway(ajaxResponse: AjaxResponse): Observable<{
status: number,
statusText: string,
message: string
}> {
const xhr = ajaxResponse.xhr;
const strict = true;
const saxStream = sax.createStream(strict, {xmlns: true, encoding: 'utf8'});
function createSaxStreamEventObservable(eventName) {
return Observable.fromEventPattern(
function addHandler(h) {
saxStream.on(eventName, h);
},
function delHandler() {}
);
}
// TODO get the webservice to return JSON so we don't need to parse XML
const parsedStream = createSaxStreamEventObservable('opentag')
.filter(function(node: any) {
return node.name === 'ns1:success' || node.name === 'ns1:failure';
})
.mergeMap(function(node) {
return createSaxStreamEventObservable('text')
.takeUntil(
createSaxStreamEventObservable('opentag')
.concat(
createSaxStreamEventObservable('closetag')
.filter(function(node: any) {
return node.name === 'ns1:success' || node.name === 'ns1:failure';
})
)
);
})
// TODO why do I need this filter?
.filter(x => x !== '\n')
.map(function(message) {
return {
status: xhr.status,
statusText: xhr.statusText,
message: message,
};
});
// NOTE: this kludge was added so that the Observable
// could be observed by its subscriber.
// It wasn't observed otherwise, probably because
// it's hot.
setTimeout(function() {
saxStream.write(xhr.response);
saxStream.end();
}, 20);
return parsedStream;
};
示例2: Promise
return new Promise((resolve, reject) => {
const metadata: any = {};
const saxStream = createStream(true, {});
saxStream.on('error', this.error(reject, 'Error parsing Directb2s metadata.'));
saxStream.on('opentag', node => {
switch (node.name) {
case 'DirectB2SData': metadata.version = node.attributes.Version; break;
case 'Name': metadata.name = node.attributes.Value; break;
case 'TableType': metadata.table_type = node.attributes.Value; break;
case 'DMDType': metadata.dmd_type = node.attributes.Value; break;
case 'GrillHeight': metadata.grill_height = node.attributes.Value; break;
case 'DualBackglass': metadata.dual_backglass = node.attributes.Value; break;
case 'Author': metadata.author = node.attributes.Value; break;
case 'Artwork': metadata.artwork = node.attributes.Value; break;
case 'GameName': metadata.gamename = node.attributes.Value; break;
}
});
saxStream.on('end', () => {
logger.info(requestState, '[Directb2sMetadata] Retrieved metadata in %sms.', Date.now() - now);
resolve(metadata);
});
createReadStream(path).on('error', this.error(reject, 'Error reading file at ' + path))
.pipe(saxStream).on('error', this.error(reject, 'Error parsing XML metadata.'));
});
示例3: State
_parse<Output extends HandlerInstance>(
stream: stream.Readable | string,
output: Output,
context: Context,
resolve: (item: Output) => void,
reject: (err: any) => void
) {
this.context = context || defaultContext;
var xml = sax.createStream(true, { position: true });
var type = (output.constructor as TypeClass).type;
var xmlSpace = this.context.registerNamespace('http://www.w3.org/XML/1998/namespace');
var state = new State(null, null, type, new type.handler());
var rootState = state;
state.addNamespace('', type.namespace);
if(xmlSpace) state.addNamespace('xml', xmlSpace);
xml.on('opentag', (node: sax.Tag) => {
var attrTbl = node.attributes;
var attr: string;
var nodePrefix = '';
var name = node.name;
var splitter = name.indexOf(':');
var item: HandlerInstance = null;
// Read xmlns namespace prefix definitions before parsing node name.
for(var key of Object.keys(attrTbl)) {
if(key.substr(0, 5) == 'xmlns') {
var nsParts = key.match(/^xmlns(:(.+))?$/);
if(nsParts) {
state.addNamespace(nsParts[2] || '', this.context.registerNamespace(attrTbl[key]));
}
}
}
// Parse node name and possible namespace prefix.
if(splitter >= 0) {
nodePrefix = name.substr(0, splitter);
name = name.substr(splitter + 1);
}
// Add internal surrogate key namespace prefix to node name.
var nodeNamespace = state.namespaceTbl[nodePrefix];
name = nodeNamespace[1] + name;
var child: MemberRef;
var type: Type;
if(state.type) {
child = state.type.childTbl[name];
if(child) {
if(child.proxy) {
type = child.proxy.member.type;
state = new State(state, child.proxy, type, new type.handler());
}
type = child.member.type;
}
}
if(type && !type.isPlainPrimitive) {
item = new type.handler();
// Parse all attributes.
for(var key of Object.keys(attrTbl)) {
splitter = key.indexOf(':');
if(splitter >= 0) {
var attrPrefix = key.substr(0, splitter);
if(attrPrefix == 'xmlns') continue;
var attrNamespace = state.namespaceTbl[attrPrefix];
if(attrNamespace) {
attr = attrNamespace[1] + key.substr(splitter + 1);
} else {
console.log('Namespace not found for ' + key);
continue;
}
} else {
attr = nodeNamespace[1] + key;
}
var ref = type.attributeTbl[attr];
if(ref && ref.member.type.isPlainPrimitive) {
item[ref.safeName] = convertPrimitive(attrTbl[key], ref.member.type);
}
}
if(item._before) item._before();
}
//.........这里部分代码省略.........
示例4: addNamespace
_parse<Output extends HandlerInstance>(
stream: string | stream.Readable | NodeJS.ReadableStream,
output: Output,
context: Context,
resolve: (item: Output) => void,
reject: (err: any) => void
) {
var xml = sax.createStream(true, { position: true });
let rule = (output.constructor as RuleClass).rule;
var xmlSpace = context.registerNamespace('http://www.w3.org/XML/1998/namespace');
let namespaceTbl: { [short: string]: [ Namespace, string ] } = {
'': [rule.namespace, rule.namespace.getPrefix()],
'xml': [xmlSpace, xmlSpace.getPrefix()]
};
var state = new State(null, null, rule, new rule.handler(), namespaceTbl);
var rootState = state;
let parentItem: HandlerInstance;
/** Add a new xmlns prefix recognized inside current tag and its children. */
function addNamespace(short: string, namespace: Namespace) {
if(namespaceTbl[short] && namespaceTbl[short][0] == namespace) return;
if(namespaceTbl == state.namespaceTbl) {
// Copy parent namespace table on first write.
namespaceTbl = {};
for(let key of Object.keys(state.namespaceTbl)) {
namespaceTbl[key] = state.namespaceTbl[key];
}
}
namespaceTbl[short] = [ namespace, namespace.getPrefix() ];
}
xml.on('opentag', (node: sax.Tag) => {
var attrTbl = node.attributes;
var attr: string;
var nodePrefix = '';
var name = node.name;
var splitter = name.indexOf(':');
var item: HandlerInstance = null;
namespaceTbl = state.namespaceTbl;
// Read xmlns namespace prefix definitions before parsing node name.
for(var key of Object.keys(attrTbl)) {
if(key.substr(0, 5) == 'xmlns') {
var nsParts = key.match(/^xmlns(:(.+))?$/);
if(nsParts) {
addNamespace(nsParts[2] || '', context.registerNamespace(attrTbl[key]));
}
}
}
// Parse node name and possible namespace prefix.
if(splitter >= 0) {
nodePrefix = name.substr(0, splitter);
name = name.substr(splitter + 1);
}
// Add internal surrogate key namespace prefix to node name.
var nodeNamespace = namespaceTbl[nodePrefix];
name = nodeNamespace[1] + name;
var child: MemberRef;
let rule: Rule;
if(state.rule) {
child = state.rule.childTbl[name];
if(child) {
if(child.proxy) {
rule = child.proxy.member.rule;
state = new State(state, child.proxy, rule, new rule.handler(), namespaceTbl);
}
rule = child.member.rule;
}
}
if(rule && !rule.isPlainPrimitive) {
item = new rule.handler();
// Parse all attributes.
for(var key of Object.keys(attrTbl)) {
splitter = key.indexOf(':');
if(splitter >= 0) {
var attrPrefix = key.substr(0, splitter);
if(attrPrefix == 'xmlns') continue;
var attrNamespace = namespaceTbl[attrPrefix];
//.........这里部分代码省略.........