View Javadoc
1   
2   package eu.simuline.util.sgml;
3   
4   import java.util.List;
5   import java.util.ArrayList;
6   
7   import org.xml.sax.ContentHandler;
8   import org.xml.sax.Locator;
9   import org.xml.sax.SAXException;
10  import org.xml.sax.Attributes;
11  
12  /**
13   * Saves all events in a list to be returned by {@link #getEvents}. 
14   *
15   * @author <a href="mailto:ernst.reissner@simuline.eu">Ernst Reissner</a>
16   * @version 1.0
17   */
18  public final class SavingHandler 
19      implements ContentHandler, ParseExceptionHandler {
20  
21      /* --------------------------------------------------------------------- *
22       * class constants                                                       *
23       * --------------------------------------------------------------------- */
24  
25      public static final String START_OF_DOCUMENT = "start of document";
26      public static final String   END_OF_DOCUMENT =   "end of document";
27  
28      /* --------------------------------------------------------------------- *
29       * fields                                                                *
30       * --------------------------------------------------------------------- */
31  
32      /**
33       * The sequence of events taken place so far. 
34       */
35      private List<String> events;
36  
37      /**
38       * Whether to save things like processing instructions as well. 
39       */
40      private boolean strict;
41  
42      /* --------------------------------------------------------------------- *
43       * constructors                                                          *
44       * --------------------------------------------------------------------- */
45  
46      /**
47       * Creates a new <code>SavingHandler</code> instance.
48       *
49       * @param strict 
50       *    a <code>boolean</code> value signifying 
51       *    whether to save things like processing instructions as well. 
52       */
53      public SavingHandler(boolean strict) {
54  	this.events = new ArrayList<String>();
55  	this.strict = strict;
56      }
57  
58      /**
59       * Creates a new <code>SavingHandler</code>. 
60       */
61      public SavingHandler() {
62  	this(true);
63      }
64  
65  
66      /* --------------------------------------------------------------------- *
67       * methods                                                               *
68       * --------------------------------------------------------------------- */
69  
70      /**
71       * Returns the sequence of events taken place so far. 
72       *
73       * @return 
74       *    {@link #events}. 
75       */
76      public List<String> getEvents() {
77  	return this.events;
78      }
79  
80      /* --------------------------------------------------------------------- *
81       * methods implementing ContentHandler                                   *
82       * --------------------------------------------------------------------- */
83  
84      public void setDocumentLocator(Locator locator) {
85  	// is empty. 
86      }
87  
88      public void startDocument() throws SAXException {
89  	this.events.add(START_OF_DOCUMENT);
90      }
91  
92      public void endDocument() throws SAXException {
93  	this.events.add(END_OF_DOCUMENT);
94      }
95  
96      public void startPrefixMapping(String prefix,
97  				   String uri)
98  	throws SAXException {
99  	// is empty. 
100     }
101 
102     public void endPrefixMapping(String prefix)
103 	throws SAXException {
104 	// is empty. 
105     }
106 
107     public void startElement(String namespaceURI,
108 			     String localName,
109 			     String qName,
110 			     Attributes atts)
111 	throws SAXException {
112 	this.events.add("TS<" + qName + ">");
113     }
114 
115     public void endElement(String namespaceURI,
116 			   String localName,
117 			   String qName)
118 	throws SAXException {
119 	this.events.add("TE</" + qName + ">");
120     }
121 
122     public void characters(char[] chr,
123 			   int start,
124 			   int length)
125 	throws SAXException {
126 	// is empty. 
127     }
128 
129     public void ignorableWhitespace(char[] chr,
130 				    int start,
131 				    int length)
132 	throws SAXException {
133 	// is empty. 
134     }
135 
136     public void processingInstruction(String target,
137 				      String data)
138 	throws SAXException {
139 	if (this.strict) {
140 	    this.events.add("PI<!" + target + ", " + data + ">");
141 	}
142 	//System.out.println("P<" + target + ">");
143     }
144 
145     public void skippedEntity(String name)
146 	throws SAXException {
147 	// is empty. 
148     }
149 
150     /* --------------------------------------------------------------------- *
151      * methods implementing ParseExceptionHandler                            *
152      * --------------------------------------------------------------------- */
153 
154     public void foundMultipleAttribute(String attrName,
155 				       Object oldAttrValue) {
156 	StringBuilder buf = new StringBuilder();
157 	buf.append("Found second value for attribute \"");
158 	buf.append(attrName);
159 	buf.append("\"; overwritten ");
160 	if (oldAttrValue == AttributesImpl.NO_VALUE) {
161 	    buf.append("no value. ");
162 	} else {
163 	    buf.append("old value \"");
164 	    buf.append(oldAttrValue);
165 	    buf.append('\"');
166 	}
167 
168 	this.events.add(buf.toString());
169     }
170 
171 
172     public void foundIllegalCharInTag(char chr) {
173 	this.events.add("exc: ill letter in tag: " + chr);
174     }
175 
176     public void foundCharAfterEndOfEndTag(char chr) {
177 	this.events.add("exc: etter after eo EndTag: " + chr);
178     }
179 
180     public void foundUnexpectedEndOfDocument() {
181 	// is empty. 
182     }
183 } // class SavingHandler 
184