@amazon/vinyl-xml 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (85) hide show
  1. package/dist/index.cjs +976 -0
  2. package/dist/index.cjs.map +7 -0
  3. package/dist/index.js +986 -0
  4. package/dist/index.js.map +7 -0
  5. package/dist/types/current/index.d.ts +14 -0
  6. package/dist/types/current/index.d.ts.map +1 -0
  7. package/dist/types/current/index.js +13 -0
  8. package/dist/types/current/tsconfig.tsbuildinfo +1 -0
  9. package/dist/types/current/xlink/actuateValidator.d.ts +3 -0
  10. package/dist/types/current/xlink/actuateValidator.d.ts.map +1 -0
  11. package/dist/types/current/xlink/actuateValidator.js +11 -0
  12. package/dist/types/current/xml/ParseXmlHandler.d.ts +56 -0
  13. package/dist/types/current/xml/ParseXmlHandler.d.ts.map +1 -0
  14. package/dist/types/current/xml/ParseXmlHandler.js +5 -0
  15. package/dist/types/current/xml/ParseXmlHandlerImpl.d.ts +49 -0
  16. package/dist/types/current/xml/ParseXmlHandlerImpl.d.ts.map +1 -0
  17. package/dist/types/current/xml/ParseXmlHandlerImpl.js +198 -0
  18. package/dist/types/current/xml/QName.d.ts +33 -0
  19. package/dist/types/current/xml/QName.d.ts.map +1 -0
  20. package/dist/types/current/xml/QName.js +19 -0
  21. package/dist/types/current/xml/XmlSchemaError.d.ts +15 -0
  22. package/dist/types/current/xml/XmlSchemaError.d.ts.map +1 -0
  23. package/dist/types/current/xml/XmlSchemaError.js +23 -0
  24. package/dist/types/current/xml/mappedXmlRules.d.ts +118 -0
  25. package/dist/types/current/xml/mappedXmlRules.d.ts.map +1 -0
  26. package/dist/types/current/xml/mappedXmlRules.js +122 -0
  27. package/dist/types/current/xml/mergeXmlRules.d.ts +62 -0
  28. package/dist/types/current/xml/mergeXmlRules.d.ts.map +1 -0
  29. package/dist/types/current/xml/mergeXmlRules.js +89 -0
  30. package/dist/types/current/xml/parseXml.d.ts +76 -0
  31. package/dist/types/current/xml/parseXml.d.ts.map +1 -0
  32. package/dist/types/current/xml/parseXml.js +305 -0
  33. package/dist/types/current/xml/stringifyXml.d.ts +67 -0
  34. package/dist/types/current/xml/stringifyXml.d.ts.map +1 -0
  35. package/dist/types/current/xml/stringifyXml.js +225 -0
  36. package/dist/types/current/xml/xmlRules.d.ts +207 -0
  37. package/dist/types/current/xml/xmlRules.d.ts.map +1 -0
  38. package/dist/types/current/xml/xmlRules.js +134 -0
  39. package/dist/types/current/xmlns/org/w3/1999/xlink.d.ts +7 -0
  40. package/dist/types/current/xmlns/org/w3/1999/xlink.d.ts.map +1 -0
  41. package/dist/types/current/xmlns/org/w3/1999/xlink.js +5 -0
  42. package/dist/types/current/xmlns/org/w3/2001/XmlSchema.d.ts +17 -0
  43. package/dist/types/current/xmlns/org/w3/2001/XmlSchema.d.ts.map +1 -0
  44. package/dist/types/current/xmlns/org/w3/2001/XmlSchema.js +5 -0
  45. package/dist/types/ts3.7/index.d.ts +14 -0
  46. package/dist/types/ts3.7/xlink/actuateValidator.d.ts +3 -0
  47. package/dist/types/ts3.7/xml/ParseXmlHandler.d.ts +56 -0
  48. package/dist/types/ts3.7/xml/ParseXmlHandlerImpl.d.ts +49 -0
  49. package/dist/types/ts3.7/xml/QName.d.ts +33 -0
  50. package/dist/types/ts3.7/xml/XmlSchemaError.d.ts +15 -0
  51. package/dist/types/ts3.7/xml/mappedXmlRules.d.ts +118 -0
  52. package/dist/types/ts3.7/xml/mergeXmlRules.d.ts +118 -0
  53. package/dist/types/ts3.7/xml/parseXml.d.ts +76 -0
  54. package/dist/types/ts3.7/xml/stringifyXml.d.ts +67 -0
  55. package/dist/types/ts3.7/xml/xmlRules.d.ts +207 -0
  56. package/dist/types/ts3.7/xmlns/org/w3/1999/xlink.d.ts +7 -0
  57. package/dist/types/ts3.7/xmlns/org/w3/2001/XmlSchema.d.ts +17 -0
  58. package/dist/types/ts4.1/index.d.ts +14 -0
  59. package/dist/types/ts4.1/xlink/actuateValidator.d.ts +3 -0
  60. package/dist/types/ts4.1/xml/ParseXmlHandler.d.ts +56 -0
  61. package/dist/types/ts4.1/xml/ParseXmlHandlerImpl.d.ts +49 -0
  62. package/dist/types/ts4.1/xml/QName.d.ts +33 -0
  63. package/dist/types/ts4.1/xml/XmlSchemaError.d.ts +15 -0
  64. package/dist/types/ts4.1/xml/mappedXmlRules.d.ts +118 -0
  65. package/dist/types/ts4.1/xml/mergeXmlRules.d.ts +118 -0
  66. package/dist/types/ts4.1/xml/parseXml.d.ts +77 -0
  67. package/dist/types/ts4.1/xml/stringifyXml.d.ts +67 -0
  68. package/dist/types/ts4.1/xml/xmlRules.d.ts +207 -0
  69. package/dist/types/ts4.1/xmlns/org/w3/1999/xlink.d.ts +7 -0
  70. package/dist/types/ts4.1/xmlns/org/w3/2001/XmlSchema.d.ts +17 -0
  71. package/dist/types/ts4.7/index.d.ts +14 -0
  72. package/dist/types/ts4.7/xlink/actuateValidator.d.ts +3 -0
  73. package/dist/types/ts4.7/xml/ParseXmlHandler.d.ts +56 -0
  74. package/dist/types/ts4.7/xml/ParseXmlHandlerImpl.d.ts +49 -0
  75. package/dist/types/ts4.7/xml/QName.d.ts +33 -0
  76. package/dist/types/ts4.7/xml/XmlSchemaError.d.ts +15 -0
  77. package/dist/types/ts4.7/xml/mappedXmlRules.d.ts +118 -0
  78. package/dist/types/ts4.7/xml/mergeXmlRules.d.ts +118 -0
  79. package/dist/types/ts4.7/xml/parseXml.d.ts +76 -0
  80. package/dist/types/ts4.7/xml/stringifyXml.d.ts +67 -0
  81. package/dist/types/ts4.7/xml/xmlRules.d.ts +207 -0
  82. package/dist/types/ts4.7/xmlns/org/w3/1999/xlink.d.ts +7 -0
  83. package/dist/types/ts4.7/xmlns/org/w3/2001/XmlSchema.d.ts +17 -0
  84. package/docs/XML_PARSING.md +210 -0
  85. package/package.json +82 -0
@@ -0,0 +1,77 @@
1
+ import type { MutableDeep } from '@amazon/vinyl-util';
2
+ import { StringReader } from '@amazon/vinyl-util';
3
+ import type { Uri } from '../xmlns/org/w3/2001/XmlSchema';
4
+ import type { Attributes, ParseXmlHandler } from './ParseXmlHandler';
5
+ import type { QName } from './QName';
6
+ import type { XmlElement } from './ParseXmlHandlerImpl';
7
+ export declare const XMLNS = "http://www.w3.org/2000/xmlns/";
8
+ /**
9
+ * Nested xml elements push current namespace information onto a stack.
10
+ *
11
+ * @private
12
+ */
13
+ export interface StackElement {
14
+ /**
15
+ * The qualified name of the current element on the stack. Used to validate expected closing
16
+ * element.
17
+ */
18
+ readonly qName: string;
19
+ /**
20
+ * The (mutable) namespaces map for the current stack.
21
+ */
22
+ readonly namespaceMap: Namespaces;
23
+ /**
24
+ * The default namespace.
25
+ */
26
+ defaultNamespaceUri: Uri | null;
27
+ }
28
+ /**
29
+ * Parses an XML document using a partial sax-style content handler.
30
+ * This allows for parsing without the entire document being read into memory first.
31
+ *
32
+ * XML Documents are mapped to data structures through content handlers instead of needing an
33
+ * intermediate model.
34
+ */
35
+ export declare function parseXml<T>(str: string, handler: ParseXmlHandler<T>): T & XmlElement<T>;
36
+ /**
37
+ * @private
38
+ */
39
+ export type Namespaces = Map<string, Uri | null>;
40
+ /**
41
+ * Given that the cursor is at a ' or ", reads the sequence of characters until an end quote is
42
+ * reached.
43
+ * The returned unescaped string is the text within the quotes.
44
+ * @private
45
+ */
46
+ export declare function readAttributeValue(c: StringReader): string;
47
+ /**
48
+ * Reads until the end of the tag >
49
+ * @private
50
+ */
51
+ export declare function untilTagClose(c: StringReader): void;
52
+ /**
53
+ * Returns true if the char code is a valid xml name character.
54
+ * @see https://www.w3.org/TR/2008/REC-xml-20081126/#NT-NameChar
55
+ * @param charCode
56
+ * @private
57
+ */
58
+ export declare function isNameChar(charCode: number): boolean;
59
+ /**
60
+ * Reads an xml {@link QName}.
61
+ * The {@link QName.namespaceUri} returned will be null; it must be separately looked up.
62
+ *
63
+ * @param reader The current string reader.
64
+ * @private
65
+ */
66
+ export declare function readQName(reader: StringReader): MutableDeep<QName>;
67
+ /**
68
+ * Reads a series of xml attributes, separated by whitespace.
69
+ * Example: `ns1:key1="value1" key2 key3='value3'`
70
+ * Note: consumes all whitespace before and after attributes series.
71
+ *
72
+ * @return Returns a map of attribute `qName:string` to `Attribute` objects.
73
+ *
74
+ * @private
75
+ */
76
+ export declare function xmlAttributes(reader: StringReader, stackElement: StackElement): MutableDeep<Attributes>;
77
+ //# sourceMappingURL=parseXml.d.ts.map
@@ -0,0 +1,67 @@
1
+ import type { OptionalDeep } from '@amazon/vinyl-util';
2
+ import type { Uri } from '../xmlns/org/w3/2001/XmlSchema';
3
+ import type { MappedXmlRules } from './mappedXmlRules';
4
+ import type { ReservedXmlRuleKeys } from '../xml/xmlRules';
5
+ export interface StringifyOptions {
6
+ /**
7
+ * The indentation. Default: 4 spaces
8
+ */
9
+ readonly indent: string;
10
+ /**
11
+ * The character(s) between lines. Default: \n
12
+ */
13
+ readonly newline: string;
14
+ /**
15
+ * A map of namespace uri to prefix strings.
16
+ * If a namespace is not found, a prefix will be generated.
17
+ */
18
+ readonly prefixMap: ReadonlyMap<Uri, string>;
19
+ /**
20
+ * If true, will include the XML declaration at the beginning of the stringified document.
21
+ * `<?xml version="1.0"?>`
22
+ */
23
+ readonly includeXmlDeclaration: boolean;
24
+ }
25
+ export declare class XmlWriter<T> {
26
+ private readonly rules;
27
+ private readonly options;
28
+ private readonly usedPrefixes;
29
+ private readonly traversed;
30
+ constructor(rules: MappedXmlRules<T>, options?: Partial<StringifyOptions>);
31
+ stringify(object: OptionalDeep<T, ReservedXmlRuleKeys>): string;
32
+ private qName;
33
+ /**
34
+ * Stringifies an element.
35
+ *
36
+ * @param object
37
+ * @param rule
38
+ * @param state
39
+ */
40
+ private stringifyElement;
41
+ /**
42
+ * Stringifies an element's attributes.
43
+ *
44
+ * @param object
45
+ * @param attributes
46
+ * @return Returns the attributes string. This will include a leading space if there are any
47
+ * found attributes.
48
+ */
49
+ private stringifyAttributes;
50
+ /**
51
+ * Stringifies a property as a CDATA node.
52
+ *
53
+ * @param object
54
+ * @param charactersRule
55
+ */
56
+ private stringifyCharacters;
57
+ /**
58
+ * Stringifies an element's child elements.
59
+ *
60
+ * @param object
61
+ * @param elements
62
+ * @param state
63
+ */
64
+ private stringifyChildren;
65
+ }
66
+ export declare function stringifyXml<T>(object: OptionalDeep<T, ReservedXmlRuleKeys>, rules: MappedXmlRules<T>, options?: Partial<StringifyOptions>): string;
67
+ //# sourceMappingURL=stringifyXml.d.ts.map
@@ -0,0 +1,207 @@
1
+ /*
2
+ * A set of utilities for easily writing schema-based rule definitions to be used with the
3
+ * `parseXml` parser.
4
+ *
5
+ * @module
6
+ */
7
+ import type { Invariant, Maybe, ReadonlyDate } from '@amazon/vinyl-util';
8
+ import type { Uri } from '../xmlns/org/w3/2001/XmlSchema';
9
+ export declare enum XmlRuleType {
10
+ CHARACTERS = "characters",
11
+ ATTRIBUTE = "attribute",
12
+ ELEMENT = "element",
13
+ ELEMENTS = "elements"
14
+ }
15
+ /**
16
+ * Reserved keys that may not be used in XmlRules.
17
+ *
18
+ * If a schema uses a reserved word, set `localPart` in the rule options to associate.
19
+ */
20
+ export type ReservedXmlRuleKeys = 'parent' | 'clone' | 'toJSON';
21
+ /**
22
+ * A schema for XML serialization and deserialization.
23
+ *
24
+ * The parser values may be lazy, represented as a function that returns a property parser.
25
+ * @typeParam T The object type these parsing rules model.
26
+ */
27
+ export type XmlRules<T extends object> = {
28
+ readonly [P in Exclude<keyof T, ReservedXmlRuleKeys>]-?: XmlRule<T[P]> | LazyXmlRule<T[P]>;
29
+ };
30
+ /**
31
+ * A union of the supported parsing types.
32
+ */
33
+ export type XmlRule<T> = AttributeRule<T> | CharactersRule<T> | ElementsRule<T & (readonly object[] | undefined)> | ElementRule<T & (object | undefined)>;
34
+ /**
35
+ * A function that returns a property handler.
36
+ * Useful when there are recursive rules.
37
+ */
38
+ export type LazyXmlRule<T> = () => XmlRule<T>;
39
+ export interface NamespacedRuleOptions {
40
+ /**
41
+ * Matches the `QName.localPart` of the element. If undefined, this will match against
42
+ * the property name from {@link XmlRules}.
43
+ */
44
+ readonly localPart?: string | undefined;
45
+ /**
46
+ * Matches the `namespaceUri` of the element or attribute.
47
+ *
48
+ * If null, this will only match against a null namespace.
49
+ * If undefined, this will default to the namespace of the parent element for elements, or
50
+ * null for attributes.
51
+ *
52
+ * Note that attributes do not inherit default namespaces as elements do.
53
+ */
54
+ readonly namespaceUri?: Maybe<Uri>;
55
+ }
56
+ /**
57
+ * A rule definition for a list of elements.
58
+ */
59
+ export interface ElementsRule<T extends readonly object[] | undefined> extends Required<ElementsRuleOptions> {
60
+ readonly type: XmlRuleType.ELEMENTS;
61
+ /**
62
+ * Prevents rule from losing nullable type information.
63
+ */
64
+ readonly __invariant?: Invariant<T>;
65
+ /**
66
+ * Returns the schema for matched elements.
67
+ * These rules will be applied per element, collecting into an array.
68
+ */
69
+ readonly rules: XmlRules<NonNullable<T>[number]>;
70
+ }
71
+ export interface ElementsRuleOptions extends NamespacedRuleOptions {
72
+ /**
73
+ * The minimum number of elements.
74
+ */
75
+ readonly minOccurs?: number;
76
+ /**
77
+ * The maximum number of elements. Use null for unbounded.
78
+ */
79
+ readonly maxOccurs?: number | null;
80
+ /**
81
+ * If true and there are 0 occurrences of the element, the parsed value will be an empty array
82
+ * instead of undefined.
83
+ */
84
+ readonly useEmptyArrays?: boolean;
85
+ }
86
+ /**
87
+ * A rule definition for a single element.
88
+ */
89
+ export interface ElementRule<T extends object | undefined> extends Required<ElementRuleOptions> {
90
+ /**
91
+ * Prevents rule from losing nullable type information.
92
+ */
93
+ readonly __invariant?: Invariant<T>;
94
+ readonly type: XmlRuleType.ELEMENT;
95
+ /**
96
+ * Returns the schema for the matched element.
97
+ */
98
+ readonly rules: XmlRules<NonNullable<T>>;
99
+ }
100
+ export interface ElementRuleOptions extends NamespacedRuleOptions {
101
+ /**
102
+ * If true, a parsing error will be thrown if the value is undefined.
103
+ */
104
+ readonly required?: boolean;
105
+ }
106
+ /**
107
+ * A rule definition for an attribute.
108
+ */
109
+ export interface AttributeRule<T> extends Required<AttributeRuleOptions<T>> {
110
+ readonly type: XmlRuleType.ATTRIBUTE;
111
+ /**
112
+ * Parses the attribute value.
113
+ */
114
+ readonly parse: (value: string) => T;
115
+ /**
116
+ * Stringifies the value.
117
+ */
118
+ readonly stringify: (value: NonNullable<T>) => string;
119
+ }
120
+ export interface AttributeRuleOptions<T> extends NamespacedRuleOptions {
121
+ /**
122
+ * If true, a parsing error will be thrown if the value is undefined.
123
+ */
124
+ readonly required?: boolean;
125
+ /**
126
+ * If the current element has no matching attribute, default will be used.
127
+ */
128
+ readonly default?: T | undefined;
129
+ /**
130
+ * Matches the `namespaceUri` of the attribute.
131
+ *
132
+ * If null, this will only match against a null namespace.
133
+ *
134
+ * Note that attributes do not inherit default namespaces as elements do.
135
+ */
136
+ readonly namespaceUri?: Uri | null;
137
+ }
138
+ /**
139
+ * A rule definition for an element's text content.
140
+ */
141
+ export interface CharactersRule<T> {
142
+ readonly type: XmlRuleType.CHARACTERS;
143
+ /**
144
+ * Parses the unescaped/processed characters.
145
+ * This will always be called, but characters may be an empty string if the element has no
146
+ * text content.
147
+ */
148
+ readonly parse: (characters: string) => T;
149
+ /**
150
+ * Stringifies the value.
151
+ */
152
+ readonly stringify: (value: NonNullable<T>) => string;
153
+ }
154
+ export interface RequiredElementRuleOptions extends ElementRuleOptions {
155
+ readonly required: true;
156
+ }
157
+ export declare function element<T extends object>(rules: XmlRules<T>, options: RequiredElementRuleOptions): ElementRule<T>;
158
+ export declare function element<T extends object | undefined>(rules: XmlRules<NonNullable<T>>, options?: ElementRuleOptions): ElementRule<T | undefined>;
159
+ /**
160
+ * Elements options that guarantee that there is a non-null array.
161
+ * For minOccurs types greater than 10, a type cast is required.
162
+ */
163
+ export type RequiredElementsOptions = (ElementsRuleOptions & {
164
+ readonly useEmptyArrays: true;
165
+ }) | (ElementsRuleOptions & {
166
+ readonly minOccurs: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10;
167
+ });
168
+ export declare function elements<T extends object>(rules: XmlRules<T>, options: RequiredElementsOptions): ElementsRule<readonly T[]>;
169
+ export declare function elements<T extends object>(rules: XmlRules<T>, options?: ElementsRuleOptions): ElementsRule<readonly T[] | undefined>;
170
+ /**
171
+ * Parses the text content of the current element via the given parse and stringify functions.
172
+ *
173
+ * Does not currently support mixing text and element nodes.
174
+ * E.g. the XML `<note>Dear <name>John</name>, I regret to inform you...</note>` could not be
175
+ * serialized or deserialized using xml rule mappings.
176
+ *
177
+ * @param parse The string passed to this parser is the processed text content of the current
178
+ * element.
179
+ * @param stringify Serializes the value.
180
+ *
181
+ * @see processCharacters
182
+ */
183
+ export declare function characters<T>(parse: (str: string) => T, stringify: (value: T) => string): CharactersRule<T>;
184
+ /**
185
+ * Handles the processed text content without any modification.
186
+ *
187
+ * @see characters
188
+ */
189
+ export declare const charactersString: CharactersRule<string>;
190
+ export type RequiredAttributeOptions<T> = AttributeRuleOptions<T> & ({
191
+ required: true;
192
+ } | {
193
+ default: T;
194
+ });
195
+ export declare function attr<T>(parse: (value: string) => T, stringify: (value: T) => string, options: RequiredAttributeOptions<T>): AttributeRule<T>;
196
+ export declare function attr<T>(parse: (value: string) => T, stringify: (value: NonNullable<T>) => string, options?: AttributeRuleOptions<T>): AttributeRule<T | undefined>;
197
+ export declare function attrString(options: RequiredAttributeOptions<string>): AttributeRule<string>;
198
+ export declare function attrString(options?: AttributeRuleOptions<string>): AttributeRule<string | undefined>;
199
+ export declare function attrInt(options: RequiredAttributeOptions<number>): AttributeRule<number>;
200
+ export declare function attrInt(options?: AttributeRuleOptions<number>): AttributeRule<number | undefined>;
201
+ export declare function attrFloat(options: RequiredAttributeOptions<number>): AttributeRule<number>;
202
+ export declare function attrFloat(options?: AttributeRuleOptions<number>): AttributeRule<number | undefined>;
203
+ export declare function attrBoolean(options: RequiredAttributeOptions<boolean>): AttributeRule<boolean>;
204
+ export declare function attrBoolean(options?: AttributeRuleOptions<boolean>): AttributeRule<boolean | undefined>;
205
+ export declare function attrDateTime(options: RequiredAttributeOptions<Date>): AttributeRule<ReadonlyDate>;
206
+ export declare function attrDateTime(options?: AttributeRuleOptions<ReadonlyDate>): AttributeRule<ReadonlyDate | undefined>;
207
+ //# sourceMappingURL=xmlRules.d.ts.map
@@ -0,0 +1,7 @@
1
+ /**
2
+ * A subset of types defined in xlink xml schema needed for dash parsing.
3
+ * Source: https://www.w3.org/TR/xlink/
4
+ * @module
5
+ */
6
+ export type actuateType = 'onLoad' | 'onRequest';
7
+ //# sourceMappingURL=xlink.d.ts.map
@@ -0,0 +1,17 @@
1
+ /**
2
+ * A subset of types defined in 2001 XmlSchema needed for dash parsing.
3
+ * Source: https://www.w3.org/2001/XmlSchema.xsd
4
+ *
5
+ * @module
6
+ */
7
+ /**
8
+ * A Uniform Resource Identifier Reference
9
+ */
10
+ export type Uri = string;
11
+ export interface Base64BinaryType {
12
+ _content: string;
13
+ }
14
+ export interface UriType {
15
+ _content: Uri;
16
+ }
17
+ //# sourceMappingURL=XmlSchema.d.ts.map
@@ -0,0 +1,14 @@
1
+ /** Auto-generated from build scripts */
2
+ export * from './xmlns/org/w3/2001/XmlSchema';
3
+ export * from './xmlns/org/w3/1999/xlink';
4
+ export * from './xml/xmlRules';
5
+ export * from './xml/stringifyXml';
6
+ export * from './xml/parseXml';
7
+ export * from './xml/mergeXmlRules';
8
+ export * from './xml/mappedXmlRules';
9
+ export * from './xml/XmlSchemaError';
10
+ export * from './xml/QName';
11
+ export * from './xml/ParseXmlHandlerImpl';
12
+ export * from './xml/ParseXmlHandler';
13
+ export * from './xlink/actuateValidator';
14
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1,3 @@
1
+ import type { actuateType } from '../xmlns/org/w3/1999/xlink';
2
+ export declare function assertActuateEnum(str: string): actuateType;
3
+ //# sourceMappingURL=actuateValidator.d.ts.map
@@ -0,0 +1,56 @@
1
+ import type { QName } from './QName';
2
+ import type { XmlElement } from '../xml/ParseXmlHandlerImpl';
3
+ /**
4
+ * A handler for XML deserialization.
5
+ */
6
+ export interface ParseXmlHandler<T> {
7
+ /**
8
+ * A new document has begun.
9
+ */
10
+ startDocument(): void;
11
+ /**
12
+ * A document has completed parsing, the final object is ready.
13
+ */
14
+ endDocument(): T & XmlElement<T>;
15
+ /**
16
+ * Invoked when a new element has been started, after the element's attributes have been read.
17
+ *
18
+ * @param name An object representing the element's name and namespace.
19
+ * @param attributes The attributes associated with the currently opening element.
20
+ */
21
+ startElement(name: QName, attributes: Attributes): void;
22
+ /**
23
+ * Invoked after an element closing tag.
24
+ */
25
+ endElement(): void;
26
+ /**
27
+ * Provides a raw substring as a text node. The given text is expected to be decoded using
28
+ * `decodeEntities`.
29
+ *
30
+ * @param str
31
+ */
32
+ textNode(str: string): void;
33
+ /**
34
+ * Provides a raw substring as a cdata node. This text is _not_ expected to be decoded.
35
+ *
36
+ * @param str
37
+ */
38
+ cDataNode(str: string): void;
39
+ }
40
+ /**
41
+ * An attribute map, where the keys are the fully qualified attribute names.
42
+ */
43
+ export interface Attributes {
44
+ readonly [qName: string]: Attribute | undefined;
45
+ }
46
+ export interface Attribute {
47
+ /**
48
+ * This attribute's name.
49
+ */
50
+ readonly name: QName;
51
+ /**
52
+ * The unescaped attribute value.
53
+ */
54
+ readonly value: string;
55
+ }
56
+ //# sourceMappingURL=ParseXmlHandler.d.ts.map
@@ -0,0 +1,49 @@
1
+ import { type Cloneable } from '@amazon/vinyl-util';
2
+ import type { XmlRules } from './xmlRules';
3
+ import type { MappedXmlRules } from './mappedXmlRules';
4
+ import type { Attributes, ParseXmlHandler } from './ParseXmlHandler';
5
+ import type { QName } from './QName';
6
+ export interface ParseXmlHandlerOptions {
7
+ readonly validateRules: boolean;
8
+ }
9
+ /**
10
+ * A {@link ParseXmlHandler} implementation that follows {@link XmlRules}, building an object of
11
+ * the parameterized type.
12
+ */
13
+ export declare class ParseXmlHandlerImpl<T> implements ParseXmlHandler<T> {
14
+ private readonly rules;
15
+ private stack;
16
+ private options;
17
+ private busy;
18
+ constructor(rules: MappedXmlRules<T>, options?: Partial<ParseXmlHandlerOptions>);
19
+ startDocument(): void;
20
+ startElement(name: QName, attributes: Attributes): void;
21
+ textNode(str: string): void;
22
+ cDataNode(str: string): void;
23
+ endElement(): void;
24
+ endDocument(): T & XmlElement<T>;
25
+ private validate;
26
+ }
27
+ /**
28
+ * Creates an XML content handler for the given parsing rules.
29
+ *
30
+ * @param rules
31
+ * @param options
32
+ */
33
+ export declare function parseXmlHandler<T extends object>(rules: XmlRules<T>, options?: Partial<ParseXmlHandlerOptions>): ParseXmlHandlerImpl<T>;
34
+ declare const parentSymbol: unique symbol;
35
+ export interface XmlElement<T = any> extends Cloneable<T & XmlElement<T>> {
36
+ get parent(): XmlElement | null;
37
+ toJSON(): any;
38
+ }
39
+ export declare class XmlElementImpl<T> implements XmlElement<T> {
40
+ [prop: keyof any]: any;
41
+ private [parentSymbol];
42
+ constructor(parent?: XmlElement | null);
43
+ get parent(): XmlElement | null;
44
+ set parent(value: XmlElement | null);
45
+ clone(): T & XmlElementImpl<T>;
46
+ toJSON(): any;
47
+ }
48
+ export {};
49
+ //# sourceMappingURL=ParseXmlHandlerImpl.d.ts.map
@@ -0,0 +1,33 @@
1
+ import type { Uri } from '../xmlns/org/w3/2001/XmlSchema';
2
+ /**
3
+ * An XML name for an element or attribute.
4
+ */
5
+ export interface QName {
6
+ /**
7
+ * The fully qualified name.
8
+ * This will be the prefixed name or the unprefixed name if the prefix is empty.
9
+ * The prefixed name will be `prefix ':' localPart`
10
+ */
11
+ readonly qName: string;
12
+ /**
13
+ * The xmlns prefix.
14
+ */
15
+ readonly prefix: string | null;
16
+ /**
17
+ * The local part of this name.
18
+ */
19
+ readonly localPart: string;
20
+ /**
21
+ * The resolved xmlns uri for the associated prefix.
22
+ */
23
+ readonly namespaceUri: Uri | null;
24
+ }
25
+ /**
26
+ * Creates a new {@link QName} object.
27
+ *
28
+ * @param localPart
29
+ * @param prefix
30
+ * @param namespaceUri
31
+ */
32
+ export declare function qName(localPart: string, prefix?: string | null, namespaceUri?: Uri | null): QName;
33
+ //# sourceMappingURL=QName.d.ts.map
@@ -0,0 +1,15 @@
1
+ import { ReportableError } from '@amazon/vinyl-util';
2
+ /**
3
+ * @private
4
+ */
5
+ export declare const xmlSchemaErrorLocale: {
6
+ readonly atLeastElements: "Expected at least {expected} occurrences of {property} but was {actual}";
7
+ readonly atMostElements: "Expected at most {expected} occurrences of {property} but was {actual}";
8
+ readonly cyclicReferences: "Cyclic reference detected";
9
+ readonly requiredAttribute: "Attribute '{property}' is required";
10
+ };
11
+ export declare class XmlSchemaError extends ReportableError {
12
+ get [Symbol.toStringTag](): string;
13
+ constructor(message: string);
14
+ }
15
+ //# sourceMappingURL=XmlSchemaError.d.ts.map
@@ -0,0 +1,118 @@
1
+ import { type AnyRecord, type Invariant } from '@amazon/vinyl-util';
2
+ import type { XmlRules } from './xmlRules';
3
+ import type { Uri } from '../xmlns/org/w3/2001/XmlSchema';
4
+ /**
5
+ * A machine-readable representation of parse rules for random access based on qualified namespace
6
+ * keys.
7
+ *
8
+ * Rule keys will either be `namespaceUri:localPart` or just `localPart` for wildcard rules if
9
+ * namespaceUri is not defined in the handler.
10
+ */
11
+ export interface MappedXmlRules<T> {
12
+ /**
13
+ * Enforces that this rule set can only be used to parse the expected type T.
14
+ */
15
+ readonly __type: Invariant<T>;
16
+ readonly attributes: ReadonlyMap<string, MappedAttributeRule>;
17
+ readonly elements: ReadonlyMap<string, MappedElementsRule>;
18
+ readonly characters: MappedCharactersRule | undefined;
19
+ }
20
+ export interface MappedAttributeRule {
21
+ /**
22
+ * The property name.
23
+ */
24
+ readonly property: keyof any;
25
+ /**
26
+ * If true, a parsing error will be thrown if the value is undefined.
27
+ */
28
+ readonly required: boolean;
29
+ /**
30
+ * If the current element has no matching attribute, default will be used.
31
+ */
32
+ readonly default: any;
33
+ /**
34
+ * Parses the attribute value.
35
+ */
36
+ readonly parse: (value: string) => any;
37
+ /**
38
+ * Converts the attribute value to a string.
39
+ */
40
+ readonly stringify: (value: any) => string;
41
+ /**
42
+ * The local part of the QName.
43
+ */
44
+ readonly localPart: string;
45
+ /**
46
+ * The namespace uri that must match.
47
+ */
48
+ readonly namespaceUri: Uri | null;
49
+ }
50
+ export interface MappedElementsRule {
51
+ /**
52
+ * The name of the property to set on the parent element.
53
+ */
54
+ readonly property: keyof any;
55
+ /**
56
+ * True if the parsed elements should populate into an array.
57
+ */
58
+ readonly array: boolean;
59
+ /**
60
+ * The minimum number of elements.
61
+ */
62
+ readonly minOccurs: number;
63
+ /**
64
+ * The maximum number of elements. Use null for unbounded.
65
+ */
66
+ readonly maxOccurs: number | null;
67
+ /**
68
+ * If true and there are 0 occurrences of the element, the parsed value will be an empty array
69
+ * instead of undefined.
70
+ */
71
+ readonly useEmptyArrays: boolean;
72
+ /**
73
+ * Returns the mappings for matched element(s).
74
+ */
75
+ readonly rules: () => MappedXmlRules<any>;
76
+ /**
77
+ * The local part of the QName.
78
+ */
79
+ readonly localPart: string;
80
+ /**
81
+ * The namespace uri that must match.
82
+ */
83
+ readonly namespaceUri: Uri | null;
84
+ }
85
+ export interface MappedCharactersRule {
86
+ readonly property: keyof any;
87
+ /**
88
+ * Parses the unescaped/processed characters.
89
+ * This will always be called, but characters may be an empty string if the element has no
90
+ * text content.
91
+ */
92
+ readonly parse: (characters: string) => any;
93
+ /**
94
+ * Converts the value to a string.
95
+ */
96
+ readonly stringify: (value: any) => string;
97
+ }
98
+ /**
99
+ * An empty rules map, used when there was no match for the started element.
100
+ * @private
101
+ */
102
+ export declare const EmptyRules: MappedXmlRules<AnyRecord>;
103
+ /**
104
+ * Creates machine-friendly content handler rules from the given {@link XmlRules}.
105
+ *
106
+ * @param rules The {@link XmlRules} to convert.
107
+ * @param parentNamespaceUri
108
+ */
109
+ export declare function mapXmlRules<T extends object>(rules: XmlRules<T>, parentNamespaceUri?: Uri | null): MappedXmlRules<T>;
110
+ /**
111
+ * Creates a rule key for mapped xml rules.
112
+ *
113
+ * @private
114
+ * @param localPart The {@link QName.localPart}
115
+ * @param namespaceUri The `namespaceUri` of the element.
116
+ */
117
+ export declare function xmlRuleKey(localPart: string, namespaceUri?: Uri | null): string;
118
+ //# sourceMappingURL=mappedXmlRules.d.ts.map