YANGTOOLS-706: Split up base utility classes into rfc6020.util
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / type / StringTypeEffectiveStatementImpl.java
1 /*
2  * Copyright (c) 2015 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
9 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type;
10
11 import javax.annotation.Nonnull;
12 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
13 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
14 import org.opendaylight.yangtools.yang.model.api.stmt.PatternEffectiveStatement;
15 import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement;
16 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement;
17 import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
18 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
19 import org.opendaylight.yangtools.yang.model.util.type.InvalidLengthConstraintException;
20 import org.opendaylight.yangtools.yang.model.util.type.RestrictedTypes;
21 import org.opendaylight.yangtools.yang.model.util.type.StringTypeBuilder;
22 import org.opendaylight.yangtools.yang.parser.rfc6020.util.DeclaredEffectiveStatementBase;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
24 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
25 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.TypeUtils;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 public final class StringTypeEffectiveStatementImpl extends DeclaredEffectiveStatementBase<String, TypeStatement>
30         implements TypeEffectiveStatement<TypeStatement> {
31     private static final Logger LOG = LoggerFactory.getLogger(StringTypeEffectiveStatementImpl.class);
32     private final StringTypeDefinition typeDefinition;
33
34     public StringTypeEffectiveStatementImpl(
35             final StmtContext<String, TypeStatement, EffectiveStatement<String, TypeStatement>> ctx,
36             final StringTypeDefinition baseType) {
37         super(ctx);
38
39         final StringTypeBuilder builder = RestrictedTypes.newStringBuilder(baseType,
40             TypeUtils.typeEffectiveSchemaPath(ctx));
41
42         for (EffectiveStatement<?, ?> stmt : effectiveSubstatements()) {
43             if (stmt instanceof LengthEffectiveStatementImpl) {
44                 final LengthEffectiveStatementImpl length = (LengthEffectiveStatementImpl)stmt;
45
46                 try {
47                     builder.setLengthConstraint(length, length.argument());
48                 } catch (IllegalStateException e) {
49                     throw new SourceException(ctx.getStatementSourceReference(), e,
50                             "Multiple length constraints encountered");
51                 } catch (InvalidLengthConstraintException e) {
52                     throw new SourceException(ctx.getStatementSourceReference(), e, "Invalid length constraint %s",
53                         length.argument());
54                 }
55             }
56             if (stmt instanceof PatternEffectiveStatement) {
57                 final PatternConstraint pattern = ((PatternEffectiveStatement)stmt).argument();
58                 if (pattern != null) {
59                     builder.addPatternConstraint(pattern);
60                 } else {
61                     LOG.debug("Ignoring empty pattern statement {}", stmt);
62                 }
63             }
64             if (stmt instanceof UnknownSchemaNode) {
65                 builder.addUnknownSchemaNode((UnknownSchemaNode)stmt);
66             }
67         }
68
69         typeDefinition = builder.build();
70     }
71
72     @Nonnull
73     @Override
74     public StringTypeDefinition getTypeDefinition() {
75         return typeDefinition;
76     }
77 }