@selenite/commons
Version:
This typescript package provides a set of frequently used utilities, types and svelte actions for building projects with Typescript and Svelte.
156 lines (155 loc) • 5.02 kB
TypeScript
/**
* Functions and types to parse XML schema definitions (ie. .xsd files).
* @module
*/
import type { SaveData } from '../type';
/**
* A simple datatype in an XML schema.
*/
export type SimpleType = {
/** Name of the simple type. */
name: string;
/** Documentation of the simple type. Extracted from comments or annotations. */
doc?: string;
/** Regex to validate the type. */
pattern?: string;
/** Options if type is an enum. */
options?: string[];
};
/**
* An attribute of a complex type in an XML schema.
*/
export type Attribute = {
/** Name of the attribute. */
name: string;
/** Default value of the attribute. */
default: unknown;
/** Documentation of the attribute. Extracted from comment or annotation. */
doc?: string;
/** Data type of the attribute. */
type: string;
/** Is the attribute required. */
required: boolean;
};
export declare class ChildProps {
type: string;
minOccurs?: number;
maxOccurs?: number;
constructor(params: {
type: string;
minOccurs?: number;
maxOccurs?: number;
});
get unique(): boolean;
get required(): boolean;
static fromObject(obj: SaveData<ChildProps>): ChildProps;
toJSON(): {
type: string;
minOccurs: number | undefined;
maxOccurs: number | undefined;
};
}
/** A complex type in an XML schema definition.
*/
export declare class ComplexType {
/** Name of the complex type. */
name: string;
/** Documentation of the complex type. */
doc?: string;
/** Attributes of the complex type. */
get attrs(): Attribute[];
attributes: Map<string, Attribute>;
children: ChildProps[];
constructor(params: {
name: string;
attributes?: Attribute[];
doc?: string;
children?: ChildProps[];
});
get childTypes(): string[];
get optionalChildren(): ChildProps[];
get optionalChildTypes(): string[];
get requiredChildren(): ChildProps[];
get uniqueChildren(): ChildProps[];
toJSON(): {
name: string;
doc: string | undefined;
attributes: Attribute[];
children: {
type: string;
minOccurs: number | undefined;
maxOccurs: number | undefined;
}[];
};
static fromObject({ name, attributes, children, doc }: SaveData<ComplexType>): ComplexType;
}
/**
* A tree view of an XML schema.
*
* It is a recursive structure that represents the hierarchy of types in a schema.
*/
export type XMLTypeTree = {
[key: string]: XMLTypeTree | 'recursive';
};
/** Type alias to make code more explicit. */
export type XMLTypeName = string;
/**
* Type of an XML schema definition.
*
* It defines the types allowed in an xml file, usually derived from an xsd file.
*/
export declare class XmlSchema {
static fromJSON(data: SaveData<XmlSchema>): XmlSchema;
toJSON(): {
simpleTypes: SimpleType[];
complexTypes: {
name: string;
doc: string | undefined;
attributes: Attribute[];
children: {
type: string;
minOccurs: number | undefined;
maxOccurs: number | undefined;
}[];
}[];
};
/** Simple types of the xml schema. */
simpleTypes: Map<string, SimpleType>;
/** Complex types of the xml schema. */
complexTypes: Map<string, ComplexType>;
/** Map which associates the names of simple XML types to their definition. */
get simpleTypeMap(): Map<XMLTypeName, SimpleType>;
/** Map which associates the names of complex XML types to their definitions. */
get typeMap(): Map<XMLTypeName, ComplexType>;
addComplexType(...complexs: ComplexType[]): void;
addSimpleType(...simples: SimpleType[]): void;
/** Map which associates the names of complex XML types to their parents' names. */
get parentsMap(): Map<XMLTypeName, XMLTypeName[]>;
/** Roots of the XML schema. */
get roots(): string[];
/**
* Tree representation of the XML schema.
*
* It is a recursive structure that represents the hierarchy of the types in the schema.
*/
get tree(): XMLTypeTree;
/**
* Map which associates the names of complex XML types to their possible paths in an XML file.
*
* The paths are represented as an array of type names.
* If a type is recursive, the value is 'infinite'.
*/
get typePaths(): Map<XMLTypeName, XMLTypeName[][] | 'infinite'>;
}
/**
* Parses an XML schema from an XSD string.
* @param xsd - xsd string
* @returns XMLSchema, or undefined if the string could not be parsed
*/
export declare function parseXsd(xsd: string): Promise<XmlSchema | undefined>;
/**
* Parses an XML schema from the url of an XSD file.
* @param url - url of the xsd file
* @returns XMLSchema, or undefined if the file could not be fetched or parsed
*/
export declare function parseXsdFromUrl(url: string): Promise<XmlSchema | undefined>;