View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package javax.faces.validator;
20  
21  import java.util.Collection;
22  
23  import javax.faces.FacesException;
24  import javax.faces.application.FacesMessage;
25  
26  /**
27   * see Javadoc of <a href="http://java.sun.com/javaee/javaserverfaces/1.2/docs/api/index.html">JSF Specification</a>
28   */
29  public class ValidatorException
30          extends FacesException
31  {
32      private static final long serialVersionUID = 5965885122446047949L;
33      private FacesMessage _facesMessage;
34      private Collection<FacesMessage> _facesMessages;
35  
36      public ValidatorException(Collection<FacesMessage> messages)
37      {
38          super(facesMessagesToString(messages));
39          _facesMessages = messages;
40      }
41      
42      public ValidatorException(Collection<FacesMessage> messages, Throwable cause)
43      {
44          super(facesMessagesToString(messages),cause);
45          _facesMessages = messages;
46      }
47      
48      public ValidatorException(FacesMessage message)
49      {
50          super(facesMessageToString(message));
51          _facesMessage = message;
52      }
53  
54      public ValidatorException(FacesMessage message,
55                                Throwable cause)
56      {
57          super(facesMessageToString(message), cause);
58          _facesMessage = message;
59      }
60  
61      public FacesMessage getFacesMessage()
62      {
63          return _facesMessage;
64      }
65      
66      public Collection<FacesMessage> getFacesMessages()
67      {
68          return _facesMessages;
69      }
70  
71      private static String facesMessageToString(FacesMessage message)
72      {
73          String summary = message.getSummary();
74          String detail = message.getDetail();
75          
76          if (summary != null)
77          {
78              if (detail != null)
79              {
80                  return summary + ": " + detail;
81              }
82              
83              return summary;
84          }
85          
86          return detail != null ? detail : "";
87      }
88      
89      private static String facesMessagesToString(Collection<FacesMessage> messages)
90      {
91          if (messages == null || messages.isEmpty())
92          {
93              return "";
94          }
95          StringBuilder sb = new StringBuilder();
96  
97          String separator = "";
98          for (FacesMessage message : messages)
99          {
100             if (message != null)
101             {
102                 String summary = message.getSummary();
103                 String detail = message.getDetail();
104                 
105                 if (summary != null)
106                 {
107                     sb.append(separator);
108                     sb.append(summary);
109                     if (detail != null)
110                     {
111                         sb.append(": ");
112                         sb.append(detail);
113                     }
114                     separator = ", ";
115                 }
116             }
117         }
118         return sb.toString();
119     }
120 }