View Javadoc

1   package org.astrogrid.portal.utils.acting;
2   
3   import java.util.ArrayList;
4   import java.util.Iterator;
5   import java.util.List;
6   
7   import org.apache.log4j.Category;
8   import org.xml.sax.SAXException;
9   import org.xml.sax.SAXParseException;
10  import org.xml.sax.helpers.DefaultHandler;
11  
12  
13  public class ValidationHandler extends DefaultHandler {
14      private Category logger = Category.getInstance(getClass());
15      
16      private List errors = new ArrayList();
17      private List warnings = new ArrayList();
18      private List fatalErrors = new ArrayList();
19      
20      /***
21       * @see org.xml.sax.ErrorHandler#error(org.xml.sax.SAXParseException)
22       */
23      public void error(SAXParseException e) throws SAXException {
24        super.error(e);
25        
26        logger.debug("[error] " + e.getMessage());
27        
28        errors.add(e);
29      }
30  
31      /***
32       * @see org.xml.sax.ErrorHandler#fatalError(org.xml.sax.SAXParseException)
33       */
34      public void fatalError(SAXParseException e) throws SAXException {
35        super.fatalError(e);
36        
37        logger.debug("[fatalError] " + e.getMessage());
38        
39        fatalErrors.add(e);
40      }
41  
42      /***
43       * @see org.xml.sax.ErrorHandler#warning(org.xml.sax.SAXParseException)
44       */
45      public void warning(SAXParseException e) throws SAXException {
46        super.warning(e);
47  
48        logger.debug("[warning] " + e.getMessage());
49        
50        warnings.add(e);
51      }
52      
53      public void exception(Exception e) {
54        logger.debug("[exception] " + e.getMessage());
55        
56        errors.add(e);
57      }
58      
59      public boolean anyErrors() {
60        logger.debug("[anyErrors] errors: " + errors.size());
61  
62        return errors.size() != 0;
63      }
64      
65      public Iterator getErrorIterator() {
66        return errors.iterator();
67      }
68  
69      public List getErrorMessages() {
70        return convertToStrings(errors);
71      }
72  
73      public boolean anyWarnings() {
74        logger.debug("[anyWarnings] warnings: " + warnings.size());
75  
76        return warnings.size() != 0;
77      }
78  
79      public Iterator getWarningIterator() {
80        return warnings.iterator();
81      }
82  
83      public List getWarningMessages() {
84        return convertToStrings(warnings);
85      }
86  
87      public boolean anyFatalErrors() {
88        logger.debug("[anyFatalErrors] fatalErrors: " + fatalErrors.size());
89  
90        return fatalErrors.size() != 0;
91      }
92  
93      public Iterator getFatalErrorsIterator() {
94        return fatalErrors.iterator();
95      }
96  
97      public List getFatalErrorsMessages() {
98        return convertToStrings(fatalErrors);
99      }
100 
101     public boolean valid() {
102       return
103           (!anyErrors() &&
104            !anyWarnings() &&
105            !anyFatalErrors());
106     }
107 
108     private List convertToStrings(List exceptions) {
109       List result = new ArrayList(exceptions.size());
110     
111       Exception e = null;
112       Iterator exIt = exceptions.iterator();
113       while(exIt.hasNext()) {
114         e = (Exception) exIt.next();
115         result.add(e.getLocalizedMessage());
116       }
117     
118       return result;
119     }
120   }