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 org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
12 import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement;
13 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement;
14 import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint;
15 import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
16 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
17 import org.opendaylight.yangtools.yang.model.util.type.InvalidLengthConstraintException;
18 import org.opendaylight.yangtools.yang.model.util.type.RestrictedTypes;
19 import org.opendaylight.yangtools.yang.model.util.type.StringTypeBuilder;
20 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
21 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
22 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.TypeUtils;
23 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.DeclaredEffectiveStatementBase;
24 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.UnknownEffectiveStatementImpl;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 public final class StringTypeEffectiveStatementImpl extends DeclaredEffectiveStatementBase<String, TypeStatement> implements
29 TypeEffectiveStatement<TypeStatement> {
30 private static final Logger LOG = LoggerFactory.getLogger(StringTypeEffectiveStatementImpl.class);
31 private final StringTypeDefinition typeDefinition;
33 public StringTypeEffectiveStatementImpl(
34 final StmtContext<String, TypeStatement, EffectiveStatement<String, TypeStatement>> ctx,
35 final StringTypeDefinition baseType) {
38 final StringTypeBuilder builder = RestrictedTypes.newStringBuilder(baseType,
39 TypeUtils.typeEffectiveSchemaPath(ctx));
41 for (EffectiveStatement<?, ?> stmt : effectiveSubstatements()) {
42 if (stmt instanceof LengthEffectiveStatementImpl) {
43 builder.setLengthAlternatives(((LengthEffectiveStatementImpl)stmt).argument());
45 if (stmt instanceof PatternEffectiveStatementImpl) {
46 final PatternConstraint pattern = ((PatternEffectiveStatementImpl)stmt).argument();
47 if (pattern != null) {
48 builder.addPatternConstraint(pattern);
50 LOG.debug("Ignoring empty pattern statement {}", stmt);
53 if (stmt instanceof UnknownEffectiveStatementImpl) {
54 builder.addUnknownSchemaNode((UnknownEffectiveStatementImpl)stmt);
59 typeDefinition = builder.build();
60 } catch (InvalidLengthConstraintException e) {
61 final LengthConstraint c = e.getOffendingConstraint();
62 throw new SourceException(ctx.getStatementSourceReference(), e, "Invalid length constraint: <%s, %s>",
63 c.getMin(), c.getMax());
68 public StringTypeDefinition getTypeDefinition() {
69 return typeDefinition;