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
9 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type;
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;
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;
34 public StringTypeEffectiveStatementImpl(
35 final StmtContext<String, TypeStatement, EffectiveStatement<String, TypeStatement>> ctx,
36 final StringTypeDefinition baseType) {
39 final StringTypeBuilder builder = RestrictedTypes.newStringBuilder(baseType,
40 TypeUtils.typeEffectiveSchemaPath(ctx));
42 for (EffectiveStatement<?, ?> stmt : effectiveSubstatements()) {
43 if (stmt instanceof LengthEffectiveStatementImpl) {
44 final LengthEffectiveStatementImpl length = (LengthEffectiveStatementImpl)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();
74 public StringTypeDefinition getTypeDefinition() {
75 return typeDefinition;