Skip to content

Commit

Permalink
Refactor schema root into its own module.
Browse files Browse the repository at this point in the history
  • Loading branch information
jjrv committed Dec 17, 2015
1 parent 0d23cdf commit a6fb9b4
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 47 deletions.
6 changes: 3 additions & 3 deletions src/XsdParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ import * as util from 'util';
function parseRule(ctor: types.BaseClass) {
if(ctor.rule) return(ctor.rule as Rule);

var rule = new Rule(new QName().parseClass(ctor.name), ctor);
var rule = new Rule(new QName().parseClass(ctor.name, ctor.namespace), ctor);

ctor.rule = rule;

for(var follower of ctor.mayContain()) {
var followerName = new QName().parseClass(follower.name);
var followerName = new QName().parseClass(follower.name, follower.namespace);

rule.followerTbl[followerName.nameFull] = parseRule(follower);
rule.followerTbl[followerName.name] = parseRule(follower);
Expand All @@ -39,7 +39,7 @@ function parseRule(ctor: types.BaseClass) {

export class XsdParser {
constructor() {
this.rootRule = parseRule(types.XsdRoot);
this.rootRule = parseRule(types.Root);
}

startElement(state: State, name: string, attrTbl: {[name: string]: string}) {
Expand Down
11 changes: 6 additions & 5 deletions src/xsd/QName.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@ export class QName {

/** Parse a class name internally used by the XSD parser. */

parseClass(name: string) {
var partList = name.match(/([A-Za-z][a-z]*)([A-Za-z]+)/);
parseClass(name: string, namespace: Namespace) {
// TODO: remove following line.
name = name.replace(/^Xsd/, '').toLowerCase();

this.namespace = Namespace.lookup(partList[1].toLowerCase());
this.name = partList[2].toLowerCase();
this.nameFull = this.namespace.id + ':' + this.name;
this.namespace = namespace;
this.name = name;
this.nameFull = namespace.id + ':' + name;

return(this);
}
Expand Down
39 changes: 2 additions & 37 deletions src/xsd/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {QName} from './QName';

import {Base, BaseClass} from './types/Base';
export {Base, BaseClass};
import {Schema, Root} from './types/Schema';
export {Schema, Root};

export type XmlAttribute = string | number;
type XmlAttributeTbl = {[name: string]: XmlAttribute};
Expand All @@ -29,43 +31,6 @@ export class MissingReferenceError extends Error {



// Schema root

export class XsdRoot extends Base {
static mayContain = () => [
XsdSchema
];
}

// <xsd:schema>

export class XsdSchema extends Base {
static mayContain = () => [
XsdImport,
XsdInclude,
XsdAttributeGroup,
XsdSimpleType,
XsdComplexType,
XsdGroup,
XsdAttribute,
XsdElement
];

init(state: State) {
// Ultimately the schema exports elements and types in the global scope
// (meaning they are children of this, the root element).

state.source.parse(state.attributeTbl);
var scope = state.source.targetNamespace.getScope();

state.setScope(scope);
this.scope = scope;
}
}




// Element support

export class XsdElementBase extends Base {
Expand Down
7 changes: 5 additions & 2 deletions src/xsd/types/Base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@
// Released under the MIT license, see LICENSE.

import {State, Rule} from '../../XsdState';
import {Scope} from '../Scope'
import {QName} from '../QName'
import {Namespace} from '../Namespace';
import {Scope} from '../Scope';
import {QName} from '../QName';

/** Common base for all schema tags */

export interface BaseClass {
new(...args: any[]): Base;
mayContain(): BaseClass[];

namespace: Namespace;
name: string;
rule: Rule;
}
Expand All @@ -35,6 +37,7 @@ export class Base {
lineNumber: number;
name: string;

static namespace: Namespace = Namespace.register('http://www.w3.org/2001/XMLSchema', 'http://www.w3.org/2009/XMLSchema/XMLSchema.xsd', 'xsd');
static name: string;
static rule: Rule;
}
39 changes: 39 additions & 0 deletions src/xsd/types/Schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// This file is part of fast-xml, copyright (c) 2015 BusFaster Ltd.
// Released under the MIT license, see LICENSE.

import {State} from '../../XsdState';
import * as types from '../types';

/** Schema root element */

export class Root extends types.Base {
static mayContain = () => [
Schema
];
}

/** <xsd:schema> */

export class Schema extends types.Base {
static mayContain = () => [
types.XsdImport,
types.XsdInclude,
types.XsdAttributeGroup,
types.XsdSimpleType,
types.XsdComplexType,
types.XsdGroup,
types.XsdAttribute,
types.XsdElement
];

init(state: State) {
// Ultimately the schema exports elements and types in the global scope
// (meaning they are children of this, the root element).

state.source.parse(state.attributeTbl);
var scope = state.source.targetNamespace.getScope();

state.setScope(scope);
this.scope = scope;
}
}

0 comments on commit a6fb9b4

Please sign in to comment.