Introduce formatting methods for SourceException
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / RevisionStatementImpl.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 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020;
9
10 import java.text.ParseException;
11 import java.util.Date;
12 import javax.annotation.Nullable;
13 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
14 import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping;
15 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
16 import org.opendaylight.yangtools.yang.model.api.stmt.DescriptionStatement;
17 import org.opendaylight.yangtools.yang.model.api.stmt.ReferenceStatement;
18 import org.opendaylight.yangtools.yang.model.api.stmt.RevisionStatement;
19 import org.opendaylight.yangtools.yang.parser.spi.SubstatementValidator;
20 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractDeclaredStatement;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
23 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
24 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.RevisionEffectiveStatementImpl;
25
26 public class RevisionStatementImpl extends AbstractDeclaredStatement<Date>
27         implements RevisionStatement {
28     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(
29         Rfc6020Mapping.REVISION)
30             .add(Rfc6020Mapping.DESCRIPTION, 0, 1).add(Rfc6020Mapping.REFERENCE, 0, 1).build();
31
32     protected RevisionStatementImpl(final StmtContext<Date, RevisionStatement, ?> context) {
33         super(context);
34     }
35
36     public static class Definition
37             extends
38             AbstractStatementSupport<Date, RevisionStatement, EffectiveStatement<Date, RevisionStatement>> {
39
40         public Definition() {
41             super(Rfc6020Mapping.REVISION);
42         }
43
44         @Override
45         public Date parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
46             try {
47                 return SimpleDateFormatUtil.getRevisionFormat().parse(value);
48             } catch (ParseException e) {
49                 throw new SourceException(ctx.getStatementSourceReference(), e,
50                     "Revision value %s is not in required format yyyy-MM-dd", value);
51             }
52         }
53
54         @Override
55         public RevisionStatement createDeclared(final StmtContext<Date, RevisionStatement, ?> ctx) {
56             return new RevisionStatementImpl(ctx);
57         }
58
59         @Override
60         public EffectiveStatement<Date, RevisionStatement> createEffective(
61                 final StmtContext<Date, RevisionStatement, EffectiveStatement<Date, RevisionStatement>> ctx) {
62             return new RevisionEffectiveStatementImpl(ctx);
63         }
64
65         @Override
66         public void onFullDefinitionDeclared(
67                 final StmtContext.Mutable<Date, RevisionStatement, EffectiveStatement<Date, RevisionStatement>> stmt) {
68             super.onFullDefinitionDeclared(stmt);
69             SUBSTATEMENT_VALIDATOR.validate(stmt);
70         }
71     }
72
73     @Override
74     public Date getDate() {
75         return argument();
76     }
77
78     @Nullable
79     @Override
80     public DescriptionStatement getDescription() {
81         return firstDeclared(DescriptionStatement.class);
82     }
83
84     @Nullable
85     @Override
86     public ReferenceStatement getReference() {
87         return firstDeclared(ReferenceStatement.class);
88     }
89 }