Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
node_modules/
typings/
cache/
*.js
*.d.ts
dist/
*.log.*
*.log
*.tgz
1 change: 0 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
node_modules/
typings/
cache/
src/
.travis.yml
Expand Down
19 changes: 11 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"scripts": {
"tsc": "tsc",
"typings": "typings",
"prepublish": "typings install && tsc",
"prepublish": "tsc -p src",
"test": "node cxsd-cli.js --help"
},
"author": "Juha Järvi",
Expand All @@ -30,14 +30,17 @@
"typescript"
],
"dependencies": {
"bluebird": "^3.4.1",
"cget": "~0.0.5",
"commander": "~2.9.0",
"cxml": "~0.1.0",
"node-expat": "~2.3.15"
"@types/bluebird": "^3.5.18",
"@types/commander": "^2.11.0",
"@types/node": "^8.0.47",
"bluebird": "^3.4.7",
"cget": "^0.2.1",
"commander": "^2.11.0",
"cxml": "~0.1.1",
"node-expat": "^2.3.16"
},
"devDependencies": {
"typescript": "^1.8.10",
"typings": "^1.3.2"
"tslint": "^5.8.0",
"typescript": "^2.5.3"
}
}
15 changes: 9 additions & 6 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {exportNamespace} from './xsd/Exporter';
import * as schema from './schema';
import {AddImports} from './schema/transform/AddImports';
import {Sanitize} from './schema/transform/Sanitize';
import * as URL from 'url';

type _ICommand = typeof cmd;
interface ICommand extends _ICommand {
Expand Down Expand Up @@ -40,14 +41,16 @@ function handleConvert(urlRemote: string, opts: { [key: string]: any }) {
var fetchOptions: FetchOptions = {};

if(opts['forceHost']) {
fetchOptions.forceHost = opts['forceHost'];
if(opts['forcePort']) fetchOptions.forcePort = opts['forcePort'];

Cache.patchRequest();
fetchOptions.rewrite = (url: string) => {
const currentURL = URL.parse(url);
currentURL.hostname = opts.forceHost;
if(opts['forcePort']) currentURL.port = opts['forcePort'];
return currentURL.toString();
}
}

var jsCache = new Cache(opts['outJs'] || 'xmlns', '_index.js');
var tsCache = new Cache(opts['outTs'] || 'xmlns', '_index.d.ts');
var jsCache = new Cache(opts['outJs'] || 'xmlns', { indexName: '_index.js' });
var tsCache = new Cache(opts['outTs'] || 'xmlns', { indexName: '_index.d.ts' });

var loader = new Loader(xsdContext, fetchOptions);

Expand Down
File renamed without changes.
23 changes: 16 additions & 7 deletions src/schema/exporter/Exporter.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
// This file is part of cxsd, copyright (c) 2015-2016 BusFaster Ltd.
// Released under the MIT license, see LICENSE.
import { Packet } from '_debugger';

import * as Promise from 'bluebird';
import * as path from 'path';

import {Address, Cache} from 'cget'
import { FileSystemCache } from 'cget/dist/strategy';

import {Transform} from '../transform/Transform';
import {Type} from '../Type';

export interface State {
cache: Cache;
fsStrategy: FileSystemCache;
}

export abstract class Exporter extends Transform<Exporter, boolean, State> {
export abstract class Exporter extends Transform<Exporter, {}, State> {
constructor(doc: Type, cache: Cache) {
super(doc);
this.state = { cache: cache };
this.state = {
cache,
fsStrategy: cache && cache.storePipeline[0] as FileSystemCache
}
}

writeHeader() {
Expand All @@ -41,12 +47,15 @@ export abstract class Exporter extends Transform<Exporter, boolean, State> {
if(!doc) return(null);

this.cacheDir = path.dirname(
this.state.cache.getCachePathSync(new Address(doc.namespace.name))
this.state.fsStrategy.getCachePathSync(doc.namespace.name)
);

let outName = this.getOutName(doc.namespace.name);
if (doc.namespace.isPrimitiveSpace || new Address(outName).isLocal) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@charto This is a big hack - Although I suspect there is a better way of doing this, this is the best I could come up with.

outName = `urn:cxsd:${outName}` // FIXME: Hacl to generate "remote" UR{I,L}
}

var outName = this.getOutName(doc.namespace.name);

return(this.state.cache.ifCached(outName).then((isCached: boolean) => {
return((this.state.fsStrategy.isCached(outName) as Promise<boolean>).then((isCached: boolean) => {
if(isCached) return(null)

return(this.state.cache.store(
Expand All @@ -64,7 +73,7 @@ export abstract class Exporter extends Transform<Exporter, boolean, State> {
// Append and then strip a file extension so references to a parent
// directory will target the directory by name instead of .. or similar.

var targetPath = this.state.cache.getCachePathSync(new Address(name)) + '.js';
const targetPath = this.state.fsStrategy.getCachePathSync(name) + '.js';

// Always output forward slashes.
// If path.sep is a backslash as on Windows, we need to escape it (as a double-backslash) for it to be a valid Regex.
Expand Down
12 changes: 7 additions & 5 deletions tsconfig.json → src/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
"module": "commonjs",
"moduleResolution": "node",
"noImplicitAny": true,
"outDir": "dist",
"target": "es5"
"noImplicitThis": true,
"outDir": "../dist",
"removeComments": false,
"target": "es5",
"lib": ["es6"]
},
"files": [
"typings/index.d.ts",
"typings-custom/node-expat.d.ts",
"node-expat.d.ts",

"src/cli.ts"
"cli.ts"
]
}
27 changes: 21 additions & 6 deletions src/xsd/Loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,35 @@

import * as Promise from 'bluebird';

import {Address, FetchOptions, Cache, CacheResult, util} from 'cget';
import {FetchOptions, Cache, CacheResult} from 'cget';

import {Context} from './Context';
import {Namespace} from './Namespace';
import {Source} from './Source';
import {Parser} from './Parser';

/** Copy all members of src object to dst object. */

export function extend(dst: {[key: string]: any}, src: {[key: string]: any}) {
for(var key of Object.keys(src)) {
dst[key] = src[key];
}
return(dst);
}

/** Make shallow clone of object. */

export function clone(src: Object) {
return(extend({}, src));
}


/** Loader handles caching schema definitions and calling parser stages. */

export class Loader {
constructor(context: Context, options?: FetchOptions) {
this.context = context;
this.options = util.clone(options);
this.options = clone(options || {});
this.parser = new Parser(context);
}

Expand All @@ -31,15 +47,14 @@ export class Loader {
}

importFile(urlRemote: string, namespace?: Namespace) {
var options = this.options;
options.address = new Address(urlRemote);
const options = this.options;

var source = Loader.sourceTbl[urlRemote];

if(!source) {
source = new Source(urlRemote, this.context, namespace);

Loader.cache.fetch(options).then((cached: CacheResult) => {
Loader.cache.fetch(urlRemote, options).then((cached: CacheResult) => {
source.updateUrl(urlRemote, cached.address.url);

return(this.parser.init(cached, source, this));
Expand All @@ -64,7 +79,7 @@ export class Loader {

getOptions() { return(this.options); }

private static cache = new Cache('cache/xsd', '_index.xsd');
private static cache = new Cache('cache/xsd', { indexName: '_index.xsd'});
private static sourceTbl: {[url: string]: Source} = {};

private context: Context;
Expand Down
4 changes: 2 additions & 2 deletions src/xsd/types/Attribute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ export class Attribute extends MemberBase {
var attribute = this.resolveMember(state, 'attribute') as Attribute;
}

use: string = null;
default: XmlAttribute = null;
use: string | null = null;
default: XmlAttribute | null = null;
}
11 changes: 6 additions & 5 deletions src/xsd/types/AttributeGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ export class AttributeGroup extends types.Base {
}

resolve(state: State) {
var attributeGroup: AttributeGroup = this;
let attributeGroup: AttributeGroup = this;
let ref: QName = null;

if(this.ref) {
var ref = new QName(this.ref, state.source);
ref = new QName(this.ref, state.source);
attributeGroup = this.scope.lookup(ref, 'attributeGroup') as AttributeGroup;
}

Expand All @@ -38,7 +39,7 @@ export class AttributeGroup extends types.Base {
}
}

id: string = null;
name: string = null;
ref: string = null;
id: string | null = null;
name: string | null = null;
ref: string | null = null;
}
1 change: 0 additions & 1 deletion src/xsd/types/Base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,5 @@ export class Base {
bytePos: number;
name: string;

static name: string;
static rule: Rule;
}
4 changes: 2 additions & 2 deletions src/xsd/types/DerivationBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ export class DerivationBase extends types.Base {
this.scope.addAllToParent('attributeGroup');
}

id: string = null;
base: string = null;
id: string | null = null;
base: string | null = null;
}
6 changes: 3 additions & 3 deletions src/xsd/types/Element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ export class Element extends MemberBase implements ElementLike {
minOccurs: string = "1";
maxOccurs: string = "1";

default: string = null;
default: string | null = null;

/** Abstract elements are disallowed in the XML document,
* and another member of the same substitution group should be used. */
abstract: string = null; // boolean
substitutionGroup: string = null;
abstract: string | null = null; // boolean
substitutionGroup: string | null = null;

substitutes: Element;
isSubstituted: boolean;
Expand Down
8 changes: 4 additions & 4 deletions src/xsd/types/MemberBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ export class MemberBase extends TypedBase {
return(false);
}

id: string = null;
name: string = null;
ref: string = null;
type: string = null;
id: string | null = null
name: string | null = null
ref: string | null = null
type: string | null = null

min: number;
max: number;
Expand Down
16 changes: 9 additions & 7 deletions src/xsd/types/TypeBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {QName} from '../QName';
import {NamedTypeMember} from '../Scope';
import * as schema from '../../schema';

export interface TypeBaseChild extends TypeBase {}

export class TypeBase extends Base {
init(state: State) {
if(!this.scope) this.scope = state.getScope();
Expand Down Expand Up @@ -37,9 +39,9 @@ export class TypeBase extends Base {

/** Find parent type inheriting from a base type. */

getParent(base: BaseClass, breakAtContent: boolean) {
var next: TypeBase | QName = this;
var type: TypeBase | QName;
getParent(base: BaseClass, breakAtContent: boolean): TypeBase {
var next: TypeBaseChild | QName = this;
var type: TypeBaseChild | QName;
/** Maximum iterations in case type inheritance forms a loop. */
var iter = 100;

Expand All @@ -48,11 +50,11 @@ export class TypeBase extends Base {

if(!(type instanceof TypeBase)) break;
else if(type instanceof base) return(type);
else if(breakAtContent && type.scope && (
type.scope.dumpTypes('attribute') ||
type.scope.dumpTypes('attributeGroup')
else if(breakAtContent && (type as TypeBase).scope && (
(type as TypeBase).scope.dumpTypes('attribute') ||
(type as TypeBase).scope.dumpTypes('attributeGroup')
)) break;
else next = type.parent;
else next = (type as TypeBase).parent;
}

return(null);
Expand Down
11 changes: 0 additions & 11 deletions typings.json

This file was deleted.

Loading