2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.parser.rfc7950.stmt.type;
10 import org.eclipse.jdt.annotation.NonNull;
11 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
12 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
13 import org.opendaylight.yangtools.yang.model.api.stmt.LengthEffectiveStatement;
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.rfc7950.stmt.DeclaredEffectiveStatementBase;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
24 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 final class StringTypeEffectiveStatementImpl extends DeclaredEffectiveStatementBase<String, TypeStatement>
29 implements TypeEffectiveStatement<TypeStatement> {
30 private static final Logger LOG = LoggerFactory.getLogger(StringTypeEffectiveStatementImpl.class);
32 private final @NonNull StringTypeDefinition typeDefinition;
34 StringTypeEffectiveStatementImpl(
35 final StmtContext<String, TypeStatement, EffectiveStatement<String, TypeStatement>> ctx,
36 final StringTypeDefinition baseType) {
39 final StringTypeBuilder builder = RestrictedTypes.newStringBuilder(baseType,
40 AbstractTypeStatementSupport.typeEffectiveSchemaPath(ctx));
42 for (EffectiveStatement<?, ?> stmt : effectiveSubstatements()) {
43 if (stmt instanceof LengthEffectiveStatement) {
44 final LengthEffectiveStatement length = (LengthEffectiveStatement)stmt;
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",
56 if (stmt instanceof PatternEffectiveStatement) {
57 final PatternConstraint pattern = ((PatternEffectiveStatement)stmt).argument();
58 if (pattern != null) {
59 builder.addPatternConstraint(pattern);
61 LOG.debug("Ignoring empty pattern statement {}", stmt);
64 if (stmt instanceof UnknownSchemaNode) {
65 builder.addUnknownSchemaNode((UnknownSchemaNode)stmt);
69 typeDefinition = builder.build();
73 public StringTypeDefinition getTypeDefinition() {
74 return typeDefinition;