-
Notifications
You must be signed in to change notification settings - Fork 0
feat(stax-xml): add synchronous cursor reader API #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Clickin
wants to merge
2
commits into
master
Choose a base branch
from
codex/add-cursor-mode-support-in-stax-xml
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| import { AttributeInfo, StartElementEvent } from './types'; | ||
| import { CursorAttribute } from './types'; | ||
|
|
||
| function parseAttributeName(name: string): Pick<CursorAttribute, 'localName' | 'prefix'> { | ||
| const colonIndex = name.indexOf(':'); | ||
| if (colonIndex === -1) { | ||
| return { localName: name, prefix: undefined }; | ||
| } | ||
|
|
||
| return { | ||
| prefix: name.slice(0, colonIndex), | ||
| localName: name.slice(colonIndex + 1) | ||
| }; | ||
| } | ||
|
|
||
| export class AttrStore { | ||
| private readonly list: CursorAttribute[]; | ||
|
|
||
| constructor(startEvent: StartElementEvent) { | ||
| this.list = AttrStore.fromEvent(startEvent); | ||
| } | ||
|
|
||
| get count(): number { | ||
| return this.list.length; | ||
| } | ||
|
|
||
| getByIndex(index: number): CursorAttribute | undefined { | ||
| return this.list[index]; | ||
| } | ||
|
|
||
| getByName(name: string): CursorAttribute | undefined { | ||
| return this.list.find((attr) => attr.name === name); | ||
| } | ||
|
|
||
| toArray(): CursorAttribute[] { | ||
| return this.list; | ||
| } | ||
|
|
||
| private static fromEvent(event: StartElementEvent): CursorAttribute[] { | ||
| if (event.attributesWithPrefix) { | ||
| return Object.entries(event.attributesWithPrefix).map(([name, info]) => { | ||
| const typedInfo = info as AttributeInfo; | ||
| return { | ||
| name, | ||
| localName: typedInfo.localName, | ||
| prefix: typedInfo.prefix, | ||
| uri: typedInfo.uri, | ||
| value: typedInfo.value | ||
| } satisfies CursorAttribute; | ||
| }); | ||
| } | ||
|
|
||
| return Object.entries(event.attributes).map(([name, value]) => { | ||
| const parsedName = parseAttributeName(name); | ||
| return { | ||
| name, | ||
| localName: parsedName.localName, | ||
| prefix: parsedName.prefix, | ||
| uri: undefined, | ||
| value | ||
| } satisfies CursorAttribute; | ||
| }); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| import { StaxXmlParserSync, StaxXmlParserSyncOptions } from './StaxXmlParserSync'; | ||
| import { AnyXmlEvent, XmlCursorReaderSyncLike } from './types'; | ||
| import { XmlCursorEvent } from './XmlCursorEvent'; | ||
|
|
||
| export interface StaxXmlCursorReaderSyncOptions extends StaxXmlParserSyncOptions {} | ||
|
|
||
| export class StaxXmlCursorReaderSync implements XmlCursorReaderSyncLike { | ||
| private readonly iterator: Iterator<AnyXmlEvent>; | ||
| private buffer: IteratorResult<AnyXmlEvent> | null = null; | ||
| private finished: boolean = false; | ||
| private current: XmlCursorEvent | null = null; | ||
|
|
||
| constructor(xml: string, options: StaxXmlCursorReaderSyncOptions = {}) { | ||
| this.iterator = new StaxXmlParserSync(xml, options); | ||
| } | ||
|
|
||
| hasNext(): boolean { | ||
| if (this.finished) { | ||
| return false; | ||
| } | ||
|
|
||
| if (this.buffer !== null) { | ||
| return !this.buffer.done; | ||
| } | ||
|
|
||
| this.buffer = this.iterator.next(); | ||
| return !this.buffer.done; | ||
| } | ||
|
|
||
| read(): boolean { | ||
| if (this.finished) { | ||
| this.current = null; | ||
| return false; | ||
| } | ||
|
|
||
| const result = this.buffer ?? this.iterator.next(); | ||
| this.buffer = null; | ||
|
|
||
| if (result.done) { | ||
| this.finished = true; | ||
| this.current = null; | ||
| return false; | ||
| } | ||
|
|
||
| this.current = new XmlCursorEvent(result.value); | ||
| return true; | ||
| } | ||
|
|
||
| getEvent(): XmlCursorEvent | null { | ||
| return this.current; | ||
| } | ||
|
|
||
| requireEvent(): XmlCursorEvent { | ||
| if (!this.current) { | ||
| throw new Error('No active cursor event. Call read() first.'); | ||
| } | ||
|
|
||
| return this.current; | ||
| } | ||
| } | ||
|
|
||
| export function createStaxXmlCursorReaderSync( | ||
| xml: string, | ||
| options: StaxXmlCursorReaderSyncOptions = {} | ||
| ): StaxXmlCursorReaderSync { | ||
| return new StaxXmlCursorReaderSync(xml, options); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| import { AttrStore } from './AttrStore'; | ||
| import { | ||
| AnyXmlEvent, | ||
| CursorAttribute, | ||
| CursorXmlEventType, | ||
| StartElementEvent, | ||
| XmlEventType, | ||
| isStartElement | ||
| } from './types'; | ||
|
|
||
| export class XmlCursorEvent { | ||
| private readonly attrStore: AttrStore | null; | ||
|
|
||
| constructor(private readonly raw: AnyXmlEvent) { | ||
| this.attrStore = isStartElement(raw) ? new AttrStore(raw as StartElementEvent) : null; | ||
| } | ||
|
|
||
| get type(): CursorXmlEventType { | ||
| return this.raw.type; | ||
| } | ||
|
|
||
| get name(): string | undefined { | ||
| return 'name' in this.raw ? this.raw.name : undefined; | ||
| } | ||
|
|
||
| get localName(): string | undefined { | ||
| return 'localName' in this.raw ? this.raw.localName : undefined; | ||
| } | ||
|
|
||
| get prefix(): string | undefined { | ||
| return 'prefix' in this.raw ? this.raw.prefix : undefined; | ||
| } | ||
|
|
||
| get uri(): string | undefined { | ||
| return 'uri' in this.raw ? this.raw.uri : undefined; | ||
| } | ||
|
|
||
| get text(): string | undefined { | ||
| return 'value' in this.raw ? this.raw.value : undefined; | ||
| } | ||
|
|
||
| get error(): Error | undefined { | ||
| return 'error' in this.raw ? this.raw.error : undefined; | ||
| } | ||
|
|
||
| isStartElement(): boolean { | ||
| return this.type === XmlEventType.START_ELEMENT; | ||
| } | ||
|
|
||
| isEndElement(): boolean { | ||
| return this.type === XmlEventType.END_ELEMENT; | ||
| } | ||
|
|
||
| isCharacters(): boolean { | ||
| return this.type === XmlEventType.CHARACTERS; | ||
| } | ||
|
|
||
| isCdata(): boolean { | ||
| return this.type === XmlEventType.CDATA; | ||
| } | ||
|
|
||
| isStartDocument(): boolean { | ||
| return this.type === XmlEventType.START_DOCUMENT; | ||
| } | ||
|
|
||
| isEndDocument(): boolean { | ||
| return this.type === XmlEventType.END_DOCUMENT; | ||
| } | ||
|
|
||
| getAttributeCount(): number { | ||
| return this.attrStore?.count ?? 0; | ||
| } | ||
|
|
||
| getAttribute(index: number): CursorAttribute | undefined { | ||
| return this.attrStore?.getByIndex(index); | ||
| } | ||
|
|
||
| getAttributeValue(name: string): string | undefined { | ||
| return this.attrStore?.getByName(name)?.value; | ||
| } | ||
|
|
||
| toEvent(): AnyXmlEvent { | ||
| return this.raw; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,12 @@ | ||
| export * from "./StaxXmlParser.js"; | ||
| export * from "./StaxXmlParserSync.js"; | ||
| export * from "./StaxXmlCursorReaderSync.js"; | ||
| export * from "./StaxXmlWriter.js"; | ||
| export * from "./StaxXmlWriterSync.js"; | ||
| export * from "./XmlCursorEvent.js"; | ||
|
|
||
| export { isCdata, isCharacters, isEndDocument, isEndElement, isError, isStartDocument, isStartElement, XmlEventType } from "./types.js"; | ||
| export type { | ||
| AnyXmlEvent, CdataEvent, CharactersEvent, ErrorEvent, StartElementEvent, WriteElementOptions, XmlAttribute | ||
| AnyXmlEvent, CdataEvent, CharactersEvent, CursorAttribute, CursorXmlEventType, ErrorEvent, StartElementEvent, WriteElementOptions, XmlAttribute, | ||
| XmlCursorEventLike, XmlCursorReaderSyncLike | ||
| } from "./types.js"; | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When
AttrStore.fromEventreadsevent.attributesWithPrefix, it assumes each value already containslocalNameand that the entry key is the full attribute name. ForStartElementEvents produced by the existing async parser, the map is keyed by local name and values do not includelocalName, soXmlCursorEventends up withCursorAttribute.localNameasundefinedand drops prefixes fromname(for example,a:idbecomesid), which also breaksgetAttributeValue('a:id')lookups when wrapping those events.Useful? React with 👍 / 👎.