Introduce formatting methods for SourceException
[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 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;
27
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;
32
33     public StringTypeEffectiveStatementImpl(
34             final StmtContext<String, TypeStatement, EffectiveStatement<String, TypeStatement>> ctx,
35             final StringTypeDefinition baseType) {
36         super(ctx);
37
38         final StringTypeBuilder builder = RestrictedTypes.newStringBuilder(baseType,
39             TypeUtils.typeEffectiveSchemaPath(ctx));
40
41         for (EffectiveStatement<?, ?> stmt : effectiveSubstatements()) {
42             if (stmt instanceof LengthEffectiveStatementImpl) {
43                 builder.setLengthAlternatives(((LengthEffectiveStatementImpl)stmt).argument());
44             }
45             if (stmt instanceof PatternEffectiveStatementImpl) {
46                 final PatternConstraint pattern = ((PatternEffectiveStatementImpl)stmt).argument();
47                 if (pattern != null) {
48                     builder.addPatternConstraint(pattern);
49                 } else {
50                     LOG.debug("Ignoring empty pattern statement {}", stmt);
51                 }
52             }
53             if (stmt instanceof UnknownEffectiveStatementImpl) {
54                 builder.addUnknownSchemaNode((UnknownEffectiveStatementImpl)stmt);
55             }
56         }
57
58         try {
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());
64         }
65     }
66
67     @Override
68     public StringTypeDefinition getTypeDefinition() {
69         return typeDefinition;
70     }
71 }