-
Notifications
You must be signed in to change notification settings - Fork 72
WIP: Metadata walker for JAPI #1092
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
Closed
Closed
Changes from all commits
Commits
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
135 changes: 135 additions & 0 deletions
135
daffodil-japi/src/main/scala/org/apache/daffodil/japi/schema/RuntimeSchemaWalker.scala
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,135 @@ | ||
| package org.apache.daffodil.japi.schema | ||
mbeckerle marked this conversation as resolved.
Show resolved
Hide resolved
mbeckerle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| import org.apache.daffodil.japi.{ DataProcessor => JDataProcessor } | ||
| import org.apache.daffodil.lib.exceptions.Assert | ||
| import org.apache.daffodil.runtime1.dpath.NodeInfo.PrimType | ||
| import org.apache.daffodil.runtime1.processors.{ | ||
| ChoiceRuntimeData, | ||
| DataProcessor => SDataProcessor, | ||
| ElementRuntimeData, | ||
| ErrorERD, | ||
| SequenceRuntimeData, | ||
| TermRuntimeData, | ||
mbeckerle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /** | ||
| * Base class used by japi clients who want to walk the runtime schema information. | ||
| */ | ||
| abstract class RuntimeSchemaHandler() { | ||
mbeckerle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * Called for simple type element declarations. | ||
| * | ||
| * @param qnameString | ||
| * @param primTypeName | ||
| * @param isArray only true if more than one occurrence is possible. Disjoint with isOptional | ||
| * @param isOptional true if 0 or 1 occurrence only. Allows for a different representation of optional from a 0..1 array | ||
| */ | ||
| def elementSimple( | ||
mbeckerle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| qnameString: String, | ||
| primTypeName: String, | ||
| isArray: Boolean, | ||
| isOptional: Boolean, | ||
| ): Unit | ||
|
|
||
| /** | ||
| * Called for complex type element declarations | ||
| * | ||
| * Subsequent calls will be for the model group making up the content | ||
| * of the element. | ||
| * | ||
| * @param qnameString | ||
| */ | ||
| def startElementComplex(qnameString: String, isArray: Boolean, isOptional: Boolean): Unit | ||
| def endElementComplex(qnameString: String, isArray: Boolean, isOptional: Boolean): Unit | ||
|
|
||
| def startSequence(): Unit | ||
|
|
||
| def endSequence(): Unit | ||
|
|
||
| def startChoice(): Unit | ||
|
|
||
| def endChoice(): Unit | ||
|
|
||
| } | ||
|
|
||
| /** | ||
| * Walks the schema, but not the DSOM schema, it walks the RuntimeData objects that | ||
| * represent the DFDL schema at runtime. | ||
| * | ||
| * @param dp | ||
| */ | ||
| class RuntimeSchemaWalker(private val dp: SDataProcessor) { | ||
|
|
||
| // provided so that people can write various tests from JAPI only. | ||
| // we wouldn't need this otherwise. | ||
| def this(jdp: JDataProcessor) = this(jdp.getUnderlyingDataProcessor) | ||
|
|
||
| private lazy val rootERD = dp.ssrd.elementRuntimeData | ||
|
|
||
| def walk(handler: RuntimeSchemaHandler): Unit = { | ||
| walkTerm(handler, rootERD) | ||
| } | ||
|
|
||
| private def walkTerm(handler: RuntimeSchemaHandler, trd: TermRuntimeData): Unit = { | ||
mbeckerle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| trd match { | ||
| case err: ErrorERD => Assert.invariantFailed("should not get ErrorERDs") | ||
| case erd: ElementRuntimeData => walkElement(handler, erd) | ||
| case srd: SequenceRuntimeData => walkSequence(handler, srd) | ||
| case crd: ChoiceRuntimeData => walkChoice(handler, crd) | ||
| case _ => Assert.invariantFailed(s"unrecognized TermRuntimeData subtype: $trd") | ||
| } | ||
| } | ||
|
|
||
| private def walkElement(handler: RuntimeSchemaHandler, erd: ElementRuntimeData): Unit = { | ||
| if (erd.optComplexTypeModelGroupRuntimeData.isDefined) | ||
| walkComplexElement(handler, erd) | ||
| else | ||
| walkSimpleElement(handler, erd) | ||
| } | ||
|
|
||
| private def walkComplexElement( | ||
| handler: RuntimeSchemaHandler, | ||
| erd: ElementRuntimeData, | ||
| ): Unit = { | ||
| val qname = erd.namedQName.toQNameString | ||
| val isArray = erd.isArray | ||
| val isOptional = erd.isOptional | ||
| val mgrd = erd.optComplexTypeModelGroupRuntimeData.getOrElse { | ||
| Assert.invariantFailed("not a complex type element") | ||
| } | ||
| handler.startElementComplex(qname, isArray, isOptional) | ||
| walkTerm(handler, mgrd) | ||
| handler.endElementComplex(qname, isArray, isOptional) | ||
| } | ||
|
|
||
| private def walkSimpleElement( | ||
| handler: RuntimeSchemaHandler, | ||
| erd: ElementRuntimeData, | ||
| ): Unit = { | ||
| val qname = erd.namedQName.toQNameString | ||
| val isArray = erd.isArray | ||
| val isOptional = erd.isOptional | ||
| val primType: PrimType = erd.optPrimType.getOrElse { | ||
| erd.optSimpleTypeRuntimeData.map { _.primType }.get | ||
| } | ||
| handler.elementSimple(qname, primType.toString, isArray, isOptional) | ||
| } | ||
|
|
||
| private def walkSequence(handler: RuntimeSchemaHandler, srd: SequenceRuntimeData): Unit = { | ||
| handler.startSequence() | ||
| srd.groupMembers.map { trd => | ||
| walkTerm(handler, trd) | ||
| } | ||
| handler.endSequence() | ||
| } | ||
|
|
||
| private def walkChoice(handler: RuntimeSchemaHandler, crd: ChoiceRuntimeData): Unit = { | ||
| handler.startChoice() | ||
| crd.groupMembers.map { trd => | ||
| walkTerm(handler, trd) | ||
| } | ||
| handler.endChoice() | ||
| } | ||
mbeckerle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.