Introduce formatting methods for SourceException
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / RevisionDateStatementImpl.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 org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
13 import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping;
14 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
15 import org.opendaylight.yangtools.yang.model.api.stmt.RevisionDateStatement;
16 import org.opendaylight.yangtools.yang.parser.spi.SubstatementValidator;
17 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractDeclaredStatement;
18 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
19 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
20 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
21 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.RevisionDateEffectiveStatementImpl;
22
23 public class RevisionDateStatementImpl extends AbstractDeclaredStatement<Date> implements RevisionDateStatement {
24     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR =
25             SubstatementValidator.builder(Rfc6020Mapping.REVISION_DATE).build();
26
27     protected RevisionDateStatementImpl(final StmtContext<Date, RevisionDateStatement, ?> context) {
28         super(context);
29     }
30
31     public static class Definition
32             extends
33             AbstractStatementSupport<Date, RevisionDateStatement, EffectiveStatement<Date, RevisionDateStatement>> {
34
35         public Definition() {
36             super(Rfc6020Mapping.REVISION_DATE);
37         }
38
39         @Override
40         public Date parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) throws SourceException {
41             try {
42                 return SimpleDateFormatUtil.getRevisionFormat().parse(value);
43             } catch (ParseException e) {
44                 throw new SourceException(ctx.getStatementSourceReference(), e,
45                     "Revision value %s is not in required format yyyy-MM-dd", value);
46             }
47         }
48
49         @Override
50         public RevisionDateStatement createDeclared(final StmtContext<Date, RevisionDateStatement, ?> ctx) {
51             return new RevisionDateStatementImpl(ctx);
52         }
53
54         @Override
55         public EffectiveStatement<Date, RevisionDateStatement> createEffective(
56                 final StmtContext<Date, RevisionDateStatement, EffectiveStatement<Date, RevisionDateStatement>> ctx) {
57             return new RevisionDateEffectiveStatementImpl(ctx);
58         }
59
60         @Override
61         public void onFullDefinitionDeclared(
62                 final StmtContext.Mutable<Date, RevisionDateStatement, EffectiveStatement<Date, RevisionDateStatement>> stmt) {
63             super.onFullDefinitionDeclared(stmt);
64             SUBSTATEMENT_VALIDATOR.validate(stmt);
65         }
66     }
67
68     @Override
69     public Date getDate() {
70         return argument();
71     }
72 }