001: /*
002: ******************************************************************
003: Copyright (c) 2001, Jeff Martin, Tim Bacon
004: All rights reserved.
005:
006: Redistribution and use in source and binary forms, with or without
007: modification, are permitted provided that the following conditions
008: are met:
009:
010: * Redistributions of source code must retain the above copyright
011: notice, this list of conditions and the following disclaimer.
012: * Redistributions in binary form must reproduce the above
013: copyright notice, this list of conditions and the following
014: disclaimer in the documentation and/or other materials provided
015: with the distribution.
016: * Neither the name of the xmlunit.sourceforge.net nor the names
017: of its contributors may be used to endorse or promote products
018: derived from this software without specific prior written
019: permission.
020:
021: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
022: "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
023: LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
024: FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
025: COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
026: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
027: BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
028: LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
029: CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
030: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
031: ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
032: POSSIBILITY OF SUCH DAMAGE.
033:
034: ******************************************************************
035: */
036:
037: package org.custommonkey.xmlunit;
038:
039: import org.w3c.dom.Attr;
040: import org.w3c.dom.CDATASection;
041: import org.w3c.dom.Comment;
042: import org.w3c.dom.DocumentType;
043: import org.w3c.dom.Element;
044: import org.w3c.dom.Entity;
045: import org.w3c.dom.EntityReference;
046: import org.w3c.dom.Node;
047: import org.w3c.dom.Notation;
048: import org.w3c.dom.ProcessingInstruction;
049: import org.w3c.dom.Text;
050:
051: /**
052: * Helper class.
053: * Abstract interface implementation that performs Node-type checks and
054: * delegates testNode() processing to subclass.
055: * <br />Examples and more at <a href="http://xmlunit.sourceforge.net"/>xmlunit.sourceforge.net</a>
056: * @see NodeTest
057: */
058: public abstract class AbstractNodeTester implements NodeTester {
059: /**
060: * Validate a single Node by delegating to node type specific methods.
061: * @see #testAttribute(Attr)
062: * @see #testCDATASection(CDATASection)
063: * @see #testComment(Comment)
064: * @see #testDocumentType(DocumentType)
065: * @see #testElement(Element)
066: * @see #testEntity(Entity)
067: * @see #testEntityReference(EntityReference)
068: * @see #testNotation(Notation)
069: * @see #testProcessingInstruction(ProcessingInstruction)
070: * @see #testText(Text)
071: */
072: public void testNode(Node aNode, NodeTest forTest)
073: throws NodeTestException {
074: switch (aNode.getNodeType()) {
075: case Node.ATTRIBUTE_NODE:
076: // should not happen as attributes are not exposed by DOM traversal
077: testAttribute((Attr) aNode);
078: break;
079: case Node.CDATA_SECTION_NODE:
080: testCDATASection((CDATASection) aNode);
081: break;
082: case Node.COMMENT_NODE:
083: testComment((Comment) aNode);
084: break;
085: case Node.DOCUMENT_TYPE_NODE:
086: testDocumentType((DocumentType) aNode);
087: break;
088: case Node.ELEMENT_NODE:
089: testElement((Element) aNode);
090: break;
091: case Node.ENTITY_NODE:
092: testEntity((Entity) aNode);
093: break;
094: case Node.ENTITY_REFERENCE_NODE:
095: testEntityReference((EntityReference) aNode);
096: break;
097: case Node.NOTATION_NODE:
098: testNotation((Notation) aNode);
099: break;
100: case Node.PROCESSING_INSTRUCTION_NODE:
101: testProcessingInstruction((ProcessingInstruction) aNode);
102: break;
103: case Node.TEXT_NODE:
104: testText((Text) aNode);
105: break;
106: default:
107: throw new NodeTestException(
108: "No delegate method for Node type", aNode);
109: }
110: }
111:
112: /**
113: * Template delegator for testNode() method. OVERRIDE to add custom logic
114: * @param attribute
115: * @exception NodeTestException always: override if required in subclass
116: */
117: public void testAttribute(Attr attribute) throws NodeTestException {
118: unhandled(attribute);
119: }
120:
121: /**
122: * Template delegator for testNode() method. OVERRIDE to add custom logic
123: * @param cdata
124: * @exception NodeTestException always: override if required in subclass
125: */
126: public void testCDATASection(CDATASection cdata)
127: throws NodeTestException {
128: unhandled(cdata);
129: }
130:
131: /**
132: * Template delegator for testNode() method. OVERRIDE to add custom logic
133: * @param comment
134: * @exception NodeTestException always: override if required in subclass
135: */
136: public void testComment(Comment comment) throws NodeTestException {
137: unhandled(comment);
138: }
139:
140: /**
141: * Template delegator for testNode() method. OVERRIDE to add custom logic
142: * @param doctype
143: * @exception NodeTestException always: override if required in subclass
144: */
145: public void testDocumentType(DocumentType doctype)
146: throws NodeTestException {
147: unhandled(doctype);
148: }
149:
150: /**
151: * Template delegator for testNode() method. OVERRIDE to add custom logic
152: * @param element
153: * @exception NodeTestException always: override if required in subclass
154: */
155: public void testElement(Element element) throws NodeTestException {
156: unhandled(element);
157: }
158:
159: /**
160: * Template delegator for testNode() method. OVERRIDE to add custom logic
161: * @param entity
162: * @exception NodeTestException always: override if required in subclass
163: */
164: public void testEntity(Entity entity) throws NodeTestException {
165: unhandled(entity);
166: }
167:
168: /**
169: * Template delegator for testNode() method. OVERRIDE to add custom logic
170: * @param reference
171: * @exception NodeTestException always: override if required in subclass
172: */
173: public void testEntityReference(EntityReference reference)
174: throws NodeTestException {
175: unhandled(reference);
176: }
177:
178: /**
179: * Template delegator for testNode() method. OVERRIDE to add custom logic
180: * @param notation
181: * @exception NodeTestException always: override if required in subclass
182: */
183: public void testNotation(Notation notation)
184: throws NodeTestException {
185: unhandled(notation);
186: }
187:
188: /**
189: * Template delegator for testNode() method. OVERRIDE to add custom logic
190: * @param instr
191: * @exception NodeTestException always: override if required in subclass
192: */
193: public void testProcessingInstruction(ProcessingInstruction instr)
194: throws NodeTestException {
195: unhandled(instr);
196: }
197:
198: /**
199: * Template delegator for testNode() method. OVERRIDE to add custom logic
200: * @param text
201: * @exception NodeTestException always: override if required in subclass
202: */
203: public void testText(Text text) throws NodeTestException {
204: unhandled(text);
205: }
206:
207: private void unhandled(Node aNode) throws NodeTestException {
208: throw new NodeTestException(
209: "Test fails by default in AbstractNodeTester", aNode);
210: }
211:
212: /**
213: * Validate that the Nodes validated one-by-one in the <code>isValid</code>
214: * method were all the Nodes expected. By default do nothing:
215: * can override to add custom logic
216: * @exception NodeTestException if mode Nodes were expected
217: */
218: public void noMoreNodes(NodeTest forTest) throws NodeTestException {
219: //by default do nothing
220: }
221: }
|