Bug 5410 - XSD regular expressions are interpreted as Java regexes (1/2)
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / type / AbstractConstraintEffectiveStatement.java
1 /**
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type;
9
10 import com.google.common.collect.ImmutableList;
11 import java.util.ArrayList;
12 import java.util.List;
13 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
14 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
15 import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint;
16 import org.opendaylight.yangtools.yang.model.api.type.ModifierKind;
17 import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
18 import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint;
19 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
20 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.DeclaredEffectiveStatementBase;
21 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.DescriptionEffectiveStatementImpl;
22 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.ErrorAppTagEffectiveStatementImpl;
23 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.ErrorMessageEffectiveStatementImpl;
24 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.ReferenceEffectiveStatementImpl;
25 import org.opendaylight.yangtools.yang.parser.stmt.rfc7950.effective.ModifierEffectiveStatementImpl;
26
27 abstract class AbstractConstraintEffectiveStatement<A, D extends DeclaredStatement<A>> extends
28         DeclaredEffectiveStatementBase<A, D> {
29     private final String description;
30     private final String reference;
31     private final String errorAppTag;
32     private final String errorMessage;
33     private final ModifierKind modifier;
34     private final A constraints;
35
36     public AbstractConstraintEffectiveStatement(final StmtContext<A, D, ?> ctx,
37             final ConstraintFactory<A> constraintFactory) {
38         super(ctx);
39         String descriptionInit = null;
40         String referenceInit = null;
41         String errorAppTagInit = null;
42         String errorMessageInit = null;
43         ModifierKind modifierInit = null;
44
45         for (final EffectiveStatement<?, ?> stmt : effectiveSubstatements()) {
46             if (stmt instanceof DescriptionEffectiveStatementImpl) {
47                 descriptionInit = ((DescriptionEffectiveStatementImpl) stmt).argument();
48             }
49             if (stmt instanceof ReferenceEffectiveStatementImpl) {
50                 referenceInit = ((ReferenceEffectiveStatementImpl) stmt).argument();
51             }
52             if (stmt instanceof ErrorAppTagEffectiveStatementImpl) {
53                 errorAppTagInit = ((ErrorAppTagEffectiveStatementImpl) stmt).argument();
54             }
55             if (stmt instanceof ErrorMessageEffectiveStatementImpl) {
56                 errorMessageInit = ((ErrorMessageEffectiveStatementImpl) stmt).argument();
57             }
58             if (stmt instanceof ModifierEffectiveStatementImpl) {
59                 modifierInit = ((ModifierEffectiveStatementImpl) stmt).argument();
60             }
61         }
62
63         this.description = descriptionInit;
64         this.reference = referenceInit;
65         this.errorAppTag = errorAppTagInit;
66         this.errorMessage = errorMessageInit;
67         this.modifier = modifierInit;
68         this.constraints = constraintFactory.createConstraints(this, super.argument());
69     }
70
71     @Override
72     public final A argument() {
73         return constraints;
74     }
75
76     public final boolean isCustomizedStatement() {
77         return this.description != null || this.reference != null || this.errorAppTag != null
78                 || this.errorMessage != null || this.modifier != null;
79     }
80
81     public final String getDescription() {
82         return description;
83     }
84
85     public final ModifierKind getModifier() {
86         return modifier;
87     }
88
89     public final String getReference() {
90         return reference;
91     }
92
93     public final String getErrorAppTag() {
94         return errorAppTag;
95     }
96
97     public final String getErrorMessage() {
98         return errorMessage;
99     }
100 }
101
102 abstract class ConstraintFactory<A> {
103     abstract protected A createConstraints(AbstractConstraintEffectiveStatement<A, ?> stmt, A argument);
104 }
105
106 abstract class ListConstraintFactory<A> extends ConstraintFactory<List<A>> {
107     @Override
108     protected List<A> createConstraints(final AbstractConstraintEffectiveStatement<List<A>, ?> stmt,
109             final List<A> argument) {
110         if (!stmt.isCustomizedStatement()) {
111             return ImmutableList.copyOf(argument);
112         }
113
114         final List<A> customizedConstraints = new ArrayList<>(argument.size());
115         for (final A constraint : argument) {
116             customizedConstraints.add(createCustomizedConstraint(constraint, stmt));
117         }
118         return ImmutableList.copyOf(customizedConstraints);
119     }
120
121     abstract protected A createCustomizedConstraint(A constraint, AbstractConstraintEffectiveStatement<List<A>, ?> stmt);
122 }
123
124 final class LengthConstraintFactory extends ListConstraintFactory<LengthConstraint> {
125     @Override
126     protected LengthConstraint createCustomizedConstraint(final LengthConstraint lengthConstraint,
127             final AbstractConstraintEffectiveStatement<List<LengthConstraint>, ?> stmt) {
128         return new LengthConstraintEffectiveImpl(lengthConstraint.getMin(), lengthConstraint.getMax(),
129                 stmt.getDescription(), stmt.getReference(), stmt.getErrorAppTag(), stmt.getErrorMessage());
130     }
131 }
132
133 final class RangeConstraintFactory extends ListConstraintFactory<RangeConstraint> {
134     @Override
135     protected RangeConstraint createCustomizedConstraint(final RangeConstraint rangeConstraint,
136             final AbstractConstraintEffectiveStatement<List<RangeConstraint>, ?> stmt) {
137         return new RangeConstraintEffectiveImpl(rangeConstraint.getMin(), rangeConstraint.getMax(),
138                 stmt.getDescription(), stmt.getReference(), stmt.getErrorAppTag(), stmt.getErrorMessage());
139     }
140 }
141
142 final class PatternConstraintFactory extends ConstraintFactory<PatternConstraint> {
143     @Override
144     protected PatternConstraint createConstraints(
145             final AbstractConstraintEffectiveStatement<PatternConstraint, ?> stmt, final PatternConstraint argument) {
146         if (!stmt.isCustomizedStatement()) {
147             return argument;
148         }
149
150         return createCustomizedConstraint(argument, stmt);
151     }
152
153     private static PatternConstraint createCustomizedConstraint(final PatternConstraint patternConstraint,
154             final AbstractConstraintEffectiveStatement<?, ?> stmt) {
155         return new PatternConstraintEffectiveImpl(patternConstraint.getRegularExpression(),
156                 patternConstraint.getRawRegularExpression(), stmt.getDescription(), stmt.getReference(),
157                 stmt.getErrorAppTag(), stmt.getErrorMessage(), stmt.getModifier());
158     }
159 }