Skip to content

Commit

Permalink
update restriction
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Radtke authored and Daniel Radtke committed Feb 10, 2024
1 parent 9af34c3 commit 859d897
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ public class XsdRestriction extends XsdAnnotatedElements {
private XsdMinLength minLength;

/**
* A {@link XsdPattern} instance that specifies a regex pattern that a String type should follow.
* A List of{@link XsdPattern} items, that represent a set of pattern that a String type should follow.
*/
private XsdPattern pattern;
private List<XsdPattern> pattern = new ArrayList<>();

/**
* A {@link XsdTotalDigits} instance that specifies the total number of digits that a numeric type is allowed to have.
Expand Down Expand Up @@ -217,7 +217,7 @@ public XsdRestriction clone(@NotNull Map<String, String> placeHolderAttributes)
}

if (this.pattern != null){
elementCopy.pattern = (XsdPattern) this.pattern.clone(pattern.getAttributesMap(), elementCopy);
elementCopy.pattern = this.pattern.stream().map(patternObj -> (XsdPattern) patternObj.clone(patternObj.getAttributesMap(), elementCopy)).collect(Collectors.toList());
}

if (this.totalDigits != null){
Expand Down Expand Up @@ -389,14 +389,18 @@ public void setMinLength(XsdMinLength minLength) {
this.minLength = minLength;
}

public XsdPattern getPattern() {
public List<XsdPattern> getPattern() {
return pattern;
}

public void setPattern(XsdPattern pattern) {
public void setPattern(List<XsdPattern> pattern) {
this.pattern = pattern;
}

public void add(XsdPattern patternMember) {
pattern.add(patternMember);
}

public XsdTotalDigits getTotalDigits() {
return totalDigits;
}
Expand Down
30 changes: 24 additions & 6 deletions src/main/java/org/xmlet/xsdparser/xsdelements/XsdSimpleType.java
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,6 @@ public List<XsdRestriction> getAllRestrictions() {
* @param newRestriction The new restriction.
*/
private void updateExistingRestriction(XsdRestriction existing, XsdRestriction newRestriction) {
XsdPattern pattern = newRestriction.getPattern();
XsdMaxExclusive maxExclusive = newRestriction.getMaxExclusive();
XsdMaxInclusive maxInclusive = newRestriction.getMaxInclusive();
XsdMaxLength maxLength = newRestriction.getMaxLength();
Expand All @@ -207,9 +206,6 @@ private void updateExistingRestriction(XsdRestriction existing, XsdRestriction n
XsdTotalDigits totalDigits = newRestriction.getTotalDigits();
XsdWhiteSpace whiteSpace = newRestriction.getWhiteSpace();

if (pattern != null){
existing.setPattern(pattern);
}

if (maxExclusive != null){
existing.setMaxExclusive(maxExclusive);
Expand Down Expand Up @@ -252,6 +248,29 @@ private void updateExistingRestriction(XsdRestriction existing, XsdRestriction n
}

updateExistingRestrictionEnumerations(existing, newRestriction);
updateExistingRestricionPatterns(existing, newRestriction);
}

/**
* Updates the existing {@link XsdRestriction} with the restrictions of the new {@link XsdRestriction} instance.
* @param existing The existing {@link XsdRestriction} instance.
* @param newRestriction The new {@link XsdRestriction} instance.
*/
private void updateExistingRestricionPatterns(XsdRestriction existing, XsdRestriction newRestriction) {
List<XsdPattern> existingPattern = existing.getPattern();
List<XsdPattern> newRestrictionPattern = newRestriction.getPattern();

if (existingPattern == null){
existing.setPattern(newRestrictionPattern);
} else {
if (newRestrictionPattern != null){
for (XsdPattern patternElem : newRestrictionPattern){
if (existingPattern.stream().noneMatch(existingPatternElem -> existingPatternElem.getValue().equals(patternElem.getValue()))){
existingPattern.add(patternElem);
}
}
}
}
}

/**
Expand Down Expand Up @@ -283,8 +302,7 @@ private void updateExistingRestrictionEnumerations(XsdRestriction existing, XsdR
* @return True if an overlap between the restrictions occur, false if it doesn't occur.
*/
private boolean existsRestrictionOverlap(XsdRestriction existing, XsdRestriction newRestriction) {
return hasDifferentValue(existing.getPattern(), newRestriction.getPattern()) ||
hasDifferentValue(existing.getWhiteSpace(), newRestriction.getWhiteSpace()) ||
return hasDifferentValue(existing.getWhiteSpace(), newRestriction.getWhiteSpace()) ||
hasDifferentValue(existing.getTotalDigits(), newRestriction.getTotalDigits()) ||
hasDifferentValue(existing.getFractionDigits(), newRestriction.getFractionDigits()) ||
hasDifferentValue(existing.getMaxExclusive(), newRestriction.getMaxExclusive()) ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public void visit(XsdMinLength element) {
public void visit(XsdPattern element) {
super.visit(element);

owner.setPattern(element);
owner.add(element);
}

@Override
Expand Down

0 comments on commit 859d897

Please sign in to comment.