16-documents/04-xml-events.protos
- Repository
- guillermomolina/protos
- Commit
379c6fa63685c39a5954cfbd577bfda3b4df8e75- Tree
8cd4f4afe29ae4c3866381f38c1858108e9799f6- Blob
72876eb95a66e049bf03c20d4ddc2516cb30cff8- Source
- View exact source on GitHub
// THE LICENSED WORK IS PROVIDED UNDER THE TERMS OF THE ADAPTIVE PUBLIC LICENSE
// ("LICENSE") AS FIRST COMPLETED BY: Guillermo Adrián Molina. ANY USE, PUBLIC
// DISPLAY, PUBLIC PERFORMANCE, REPRODUCTION OR DISTRIBUTION OF, OR PREPARATION OF
// DERIVATIVE WORKS BASED ON, THE LICENSED WORK CONSTITUTES RECIPIENT'S ACCEPTANCE
// OF THIS LICENSE AND ITS TERMS, WHETHER OR NOT SUCH RECIPIENT READS THE TERMS OF
// THE LICENSE. "LICENSED WORK" AND "RECIPIENT" ARE DEFINED IN THE LICENSE. A COPY
// OF THE LICENSE IS LOCATED IN THE TEXT FILE ENTITLED "LICENSE.TXT" ACCOMPANYING
// THE CONTENTS OF THIS FILE. IF A COPY OF THE LICENSE DOES NOT ACCOMPANY THIS
// FILE, A COPY OF THE LICENSE MAY ALSO BE OBTAINED AT THE FOLLOWING WEB SITE:
// https://github.com/guillermomolina/protos
//
// Software distributed under the License is distributed on an "AS IS" basis,
// WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
// the specific language governing rights and limitations under the License.
// Incremental XML with eventParser.
//
// The event parser accepts text in chunks and emits ordered events of kind
// declaration, startElement, text, comment, processingInstruction and
// endElement. Chunk boundaries never split a logical text node, even when they
// fall inside a tag or an entity reference.
XML: import("std:xml/xml")
Arrays: import("std:collections/array")
kinds: Array()
texts: Array()
parser: XML.eventParser((event) => {
kinds = Arrays.append(kinds, event.kind)
if(event.kind == "text") {
texts = Arrays.append(texts, event.value)
}
}, { maxDepth: 8; maxDocumentScalars: 1024 })
// The boundaries fall inside a start tag, inside "&" and inside an end tag.
for(chunk in ["<order xmlns='urn:shop'><it", "em>Pens &am", "p; ink</item></or", "der>"]) {
parser.feed(chunk)
}
parser.finish()
for(kind in kinds) {
print(kind)
}
// startElement
// startElement
// text
// endElement
// endElement
print(texts[0]) // Pens & ink
// A terminal parser rejects reuse.
print(XML.XmlError.handle(
() => { parser.feed("<more/>"); "reused" },
(error) => { error.kind }
)) // lifecycle
// Resource budgets are inclusive and deterministic: five nested elements
// cannot fit a depth budget of 2, so the parser fails with a limit Error that
// names the exceeded option.
shallow: XML.eventParser((event) => { null }, { maxDepth: 2 })
print(XML.XmlError.handle(
() => { shallow.feed("<a><b><c><d><e/></d></c></b></a>"); shallow.finish(); "accepted" },
(error) => { error.limit }
)) // maxDepth
// The event writer turns events back into XML text.
output: ""
writer: XML.eventWriter((chunk) => { output = output + chunk })
copier: XML.eventParser((event) => { writer.feed(event) })
copier.feed("<r a='1'>x < y</r>")
copier.finish()
writer.finish()
print(output) // <r a="1">x < y</r>