This repository was archived by the owner on May 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Add interface for complex LSInput #6
Open
physikerwelt
wants to merge
1
commit into
dswarm:master
Choose a base branch
from
physikerwelt:master
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -76,6 +76,7 @@ public class JsonSchemaParser { | |
| //private static final XSImplementation impl = (XSImplementation) new XSImplementationImpl(); | ||
|
|
||
| private static final XSLoader LOADER; | ||
| private static final int MAX_RECURSION_DEPTH = 7; | ||
|
|
||
| static { | ||
| System.setProperty(DOMImplementationRegistry.PROPERTY, "com.sun.org.apache.xerces.internal.dom.DOMXSImplementationSourceImpl"); | ||
|
|
@@ -94,22 +95,22 @@ public class JsonSchemaParser { | |
|
|
||
| private XSModel model; | ||
|
|
||
| private Collection<JSElement> iterateParticle(final XSParticle particle) { | ||
| private Collection<JSElement> iterateParticle(final XSParticle particle, int depth) { | ||
|
|
||
| final XSTerm term = particle.getTerm(); | ||
|
|
||
| if (term instanceof XSModelGroup) { | ||
|
|
||
| final XSModelGroup modelGroup = (XSModelGroup) term; | ||
| return iterateModelGroup(modelGroup); | ||
| return iterateModelGroup(modelGroup,depth); | ||
| } | ||
| else if (term instanceof XSModelGroupDefinition) { | ||
| final XSModelGroupDefinition xsModelGroupDefinition = (XSModelGroupDefinition) term; | ||
| return iterateModelGroup(xsModelGroupDefinition.getModelGroup()); | ||
| return iterateModelGroup(xsModelGroupDefinition.getModelGroup(), depth); | ||
| } | ||
|
|
||
| final Collection<JSElement> jsElements = new ArrayList<>(1); | ||
| final Optional<JSElement> optionalJSElement = iterateSingleParticle(particle); | ||
| final Optional<JSElement> optionalJSElement = iterateSingleParticle(particle, depth); | ||
|
|
||
| if (optionalJSElement.isPresent()) { | ||
|
|
||
|
|
@@ -119,14 +120,14 @@ else if (term instanceof XSModelGroupDefinition) { | |
| return jsElements; | ||
| } | ||
|
|
||
| private Optional<JSElement> iterateSingleParticle(final XSParticle particle) { | ||
| private Optional<JSElement> iterateSingleParticle(final XSParticle particle, int depth) { | ||
|
|
||
| final XSTerm term = particle.getTerm(); | ||
| if (term instanceof XSElementDeclaration) { | ||
|
|
||
| final XSElementDeclaration xsElementDecl = (XSElementDeclaration) term; | ||
|
|
||
| final Optional<JSElement> optionalElement = iterateElement(xsElementDecl); | ||
| final Optional<JSElement> optionalElement = iterateElement(xsElementDecl, depth); | ||
|
|
||
| return optionalElement.map(el -> isRepeated(particle) ? new JSArray(el) : el); | ||
| } | ||
|
|
@@ -136,7 +137,7 @@ else if (term instanceof XSModelGroupDefinition) { | |
| final XSModelGroupDefinition xsModelGroupDefinition = (XSModelGroupDefinition) term; | ||
| final String name = getDeclarationName(xsModelGroupDefinition); | ||
|
|
||
| final List<JSElement> elements = iterateModelGroup(xsModelGroupDefinition.getModelGroup()); | ||
| final List<JSElement> elements = iterateModelGroup(xsModelGroupDefinition.getModelGroup(), depth); | ||
| return Optional.of(new JSObject(name, elements)); | ||
| } | ||
|
|
||
|
|
@@ -162,7 +163,7 @@ else if (term instanceof XSWildcard) { | |
| return Optional.of(new JSNull(NULL)); | ||
| } | ||
|
|
||
| private List<JSElement> iterateModelGroup(final XSModelGroup modelGroup) { | ||
| private List<JSElement> iterateModelGroup(final XSModelGroup modelGroup, int depth) { | ||
|
|
||
| final List<JSElement> list = new ArrayList<>(); | ||
|
|
||
|
|
@@ -174,31 +175,35 @@ private List<JSElement> iterateModelGroup(final XSModelGroup modelGroup) { | |
| final XSTerm term = xsParticle.getTerm(); | ||
| if (term instanceof XSModelGroup) { | ||
|
|
||
| list.addAll(iterateParticle(xsParticle)); | ||
| list.addAll(iterateParticle(xsParticle,depth+1)); | ||
| } | ||
|
|
||
| else if (term instanceof XSModelGroupDefinition) { | ||
| final XSModelGroupDefinition xsModelGroupDefinition = (XSModelGroupDefinition) term; | ||
| list.addAll(iterateModelGroup(xsModelGroupDefinition.getModelGroup())); | ||
| list.addAll(iterateModelGroup(xsModelGroupDefinition.getModelGroup(),depth + 1 )); | ||
| } | ||
|
|
||
| else { | ||
|
|
||
| final Optional<JSElement> optionalJSElement = iterateSingleParticle(xsParticle); | ||
| final Optional<JSElement> optionalJSElement = iterateSingleParticle(xsParticle, depth+1); | ||
| optionalJSElement.ifPresent(list::add); | ||
| } | ||
| } | ||
|
|
||
| return list; | ||
| } | ||
|
|
||
| private Optional<JSElement> iterateElement(final XSElementDeclaration elementDecl) { | ||
|
|
||
| private Optional<JSElement> iterateElement(final XSElementDeclaration elementDecl, int depth) { | ||
| if(depth>MAX_RECURSION_DEPTH) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is it possible to make this check optional? or to set the default value for MAX_RECURSION_DEPTH to a higher value? |
||
| LOG.error("Structure deeper than " , MAX_RECURSION_DEPTH ); | ||
| return Optional.empty(); | ||
| } | ||
| try { | ||
|
|
||
| final String elementName = getDeclarationName(elementDecl); | ||
|
|
||
| //System.out.println(elementName); | ||
| // System.out.println(elementName); | ||
| // System.out.println(depth); | ||
|
|
||
| final XSTypeDefinition xsElementDeclType = elementDecl.getTypeDefinition(); | ||
|
|
||
|
|
@@ -242,7 +247,7 @@ private Optional<JSElement> iterateElement(final XSElementDeclaration elementDec | |
| }).orElseGet(() -> { | ||
| final JSObject jsElements = new JSObject(elementName, isMixed); | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I looked into that deeper some time (until I figured out that I will be better off with parsing the rng file rather than the xsd file). In my second attempt I did drop the depth counter and installed bookkeeping for the jsobjects, here. The drawback of this approach is that it introduces state. Do you think that's a better way of fixing that? |
||
|
|
||
| final List<JSElement> elements = iterateComplexType(xsComplexType); | ||
| final List<JSElement> elements = iterateComplexType(xsComplexType, depth); | ||
|
|
||
| if (elements.size() == 1 && elements.get(0) instanceof JSOther) { | ||
|
|
||
|
|
@@ -265,14 +270,14 @@ private Optional<JSElement> iterateElement(final XSElementDeclaration elementDec | |
| return Optional.empty(); | ||
| } | ||
|
|
||
| private List<JSElement> iterateComplexType(final XSComplexTypeDefinition complexType) { | ||
| private List<JSElement> iterateComplexType(final XSComplexTypeDefinition complexType, int depth) { | ||
|
|
||
| final List<JSElement> result = new ArrayList<>(); | ||
|
|
||
| final XSParticle xsParticle = complexType.getParticle(); | ||
| if (xsParticle != null) { | ||
|
|
||
| result.addAll(iterateParticle(xsParticle)); | ||
| result.addAll(iterateParticle(xsParticle,depth)); | ||
| } else { | ||
| final XSSimpleTypeDefinition xsSimpleType = complexType.getSimpleType(); | ||
| if (xsSimpleType != null) { | ||
|
|
@@ -307,13 +312,17 @@ private static JSElement iterateSimpleType(final XSObject simpleType) { | |
| return new JSString(simpleTypeName); | ||
| } | ||
|
|
||
| public void parse(final LSInput input) { | ||
| model = LOADER.load(input); | ||
| } | ||
|
|
||
| public void parse(final InputStream is) throws SAXException { | ||
| Preconditions.checkNotNull(LOADER, "The parser could not be initialised"); | ||
|
|
||
| final LSInput input = new DOMInputImpl(); | ||
| input.setByteStream(is); | ||
| parse(input); | ||
|
|
||
| model = LOADER.load(input); | ||
| } | ||
|
|
||
| public void parse(final Reader reader) throws SAXException { | ||
|
|
@@ -355,7 +364,6 @@ public JSRoot apply(final String rootName) throws SAXException { | |
| //final XSSchema xsSchema = xsSchemaIterator.next(); | ||
|
|
||
| final XSNamedMap elements = model.getComponents(XSConstants.ELEMENT_DECLARATION); | ||
|
|
||
| for (int i = 0; i < elements.getLength(); i++) { | ||
|
|
||
| final XSObject object = elements.item(i); | ||
|
|
@@ -370,8 +378,7 @@ public JSRoot apply(final String rootName) throws SAXException { | |
|
|
||
| continue; | ||
| } | ||
|
|
||
| iterateElement(elementDecl).ifPresent(root::add); | ||
| iterateElement(elementDecl, 1).ifPresent(root::add); | ||
| } | ||
| } | ||
| // | ||
|
|
||
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
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.
is it possible to provide this attribute/information via a parameter? - since, "7" seems to be rather arbitrary ;)