View Javadoc

1   /* This file is part of COPAL (COntext Provisioning for All).
2    *
3    * COPAL is a part of SM4All (Smart hoMes for All) project.
4    *
5    * COPAL is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU Lesser General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * COPAL is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU Lesser General Public License for more details.
14   *
15   * You should have received a copy of the GNU Lesser General Public License
16   * along with COPAL. If not, see <http://www.gnu.org/licenses/>.
17   */
18  package at.ac.tuwien.infosys.sm4all.copal.api.util;
19  
20  import java.text.MessageFormat;
21  import org.w3c.dom.Document;
22  import org.w3c.dom.Node;
23  
24  /**
25   * Exception for when an unmarshaled {@link Document} is missing a required
26   * field. Field can be any {@link Node} in the {@link Document}.
27   * 
28   * @author sanjin
29   */
30  public class MissingFieldException extends FailedUnmarshallingException {
31  
32      private static final long serialVersionUID = -3870300417820475062L;
33  
34      private static final int BUFFER_SIZE = 20;
35      private static final MessageFormat DETAIL_MESSAGE = new MessageFormat(
36              "{0} is missing");
37  
38      private final String fieldName;
39  
40      /**
41       * Creates missing a required field exception with specified name of missing
42       * field. The cause is not initialized, and may subsequently be initialized
43       * by a call to {@link #initCause(Throwable)}.
44       * 
45       * @param fieldName the name of missing field (which is saved for later
46       *        retrieval by the {@link #getFieldName()} method).
47       */
48      public MissingFieldException(final String fieldName) {
49          super(DETAIL_MESSAGE.format(new Object[]{ fieldName },
50                  new StringBuffer(BUFFER_SIZE), null).toString());
51  
52          this.fieldName = fieldName;
53      }
54  
55      /**
56       * Creates missing a required field exception with specified name of missing
57       * field and cause.
58       * 
59       * @param fieldName the name of missing field (which is saved for later
60       *        retrieval by the {@link #getFieldName()} method).
61       * @param cause the cause (which is saved for later retrieval by the
62       *        {@link #getCause()} method). A <code>null</code> value is
63       *        permitted, and indicates that the cause is nonexistent or unknown.
64       */
65      public MissingFieldException(final String fieldName, final Throwable cause) {
66          super(DETAIL_MESSAGE.format(new Object[]{ fieldName },
67                  new StringBuffer(BUFFER_SIZE), null).toString(), cause);
68  
69          this.fieldName = fieldName;
70      }
71  
72      /**
73       * Returns the name of missing field.
74       * 
75       * @return the name of missing field.
76       */
77      public String getFieldName() {
78          return this.fieldName;
79      }
80  }