Refactored simple SAX ElementHandler to be an interface.
[debian/openrocket] / core / src / net / sf / openrocket / file / simplesax / AbstractElementHandler.java
1 package net.sf.openrocket.file.simplesax;
2
3 import java.util.HashMap;
4
5 import net.sf.openrocket.aerodynamics.Warning;
6 import net.sf.openrocket.aerodynamics.WarningSet;
7
8 import org.xml.sax.SAXException;
9
10
11 /**
12  * An abstract base class for creating an ElementHandler.  This implements the close
13  * methods so that warnings are generated for spurious content.
14  * 
15  * @author Sampo Niskanen <sampo.niskanen@iki.fi>
16  */
17 public abstract class AbstractElementHandler implements ElementHandler {
18         
19         @Override
20         public abstract ElementHandler openElement(String element,
21                         HashMap<String, String> attributes, WarningSet warnings) throws SAXException;
22         
23         /**
24          * {@inheritDoc}
25          * <p>
26          * The default implementation is to add warnings for any textual content or attributes.
27          * This is useful for generating warnings for unknown XML attributes.
28          */
29         @Override
30         public void closeElement(String element, HashMap<String, String> attributes,
31                         String content, WarningSet warnings) throws SAXException {
32                 
33                 if (!content.trim().equals("")) {
34                         warnings.add(Warning.fromString("Unknown text in element '" + element
35                                         + "', ignoring."));
36                 }
37                 if (!attributes.isEmpty()) {
38                         warnings.add(Warning.fromString("Unknown attributes in element '" + element
39                                         + "', ignoring."));
40                 }
41         }
42         
43         /**
44          * {@inheritDoc}
45          * <p>
46          * The default implementation is a no-op.
47          */
48         @Override
49         public void endHandler(String element, HashMap<String, String> attributes,
50                         String content, WarningSet warnings) throws SAXException {
51                 // No-op
52         }
53         
54 }