2
* Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
4
* This software is open source.
5
* See the bottom of this file for the licence.
10
import junit.textui.TestRunner;
14
import org.dom4j.io.SAXReader;
19
* @author <a href="mailto:franz.beil@temis-group.com">FB </a>
21
public class EmbeddedHandlerTest extends AbstractTestCase {
22
private static final int MAIN_READER = 0;
24
private static final int ON_END_READER = 1;
26
protected String[] testDocuments = {"xml/test/FranzBeilMain.xml"};
28
private StringBuffer[] results = {new StringBuffer(), new StringBuffer()};
32
public static void main(String[] args) {
33
TestRunner.run(EmbeddedHandlerTest.class);
36
// ---------------------------------------------
38
// ---------------------------------------------
39
public void testMainReader() throws Exception {
44
public void testOnEndReader() throws Exception {
49
public void testBothReaders() throws Exception {
53
if (!results[MAIN_READER].toString().equals(
54
results[ON_END_READER].toString())) {
55
StringBuffer msg = new StringBuffer();
56
msg.append("Results of tests should be equal!\n");
57
msg.append("Results testMainReader():\n"
58
+ results[MAIN_READER].toString());
59
msg.append("Results testOnEndReader():\n"
60
+ results[ON_END_READER].toString());
61
throw new Exception(msg.toString());
65
// ---------------------------------------------
66
// Implementation methods
67
// ---------------------------------------------
68
private void readDocuments() throws Exception {
69
for (int i = 0; i < testDocuments.length; i++) {
70
File testDoc = getFile(testDocuments[i]);
71
String mainDir = testDoc.getParent();
72
SAXReader reader = new SAXReader();
73
ElementHandler mainHandler = new MainHandler(mainDir);
74
reader.addHandler("/main/import", mainHandler);
75
getDocument(testDocuments[i], reader);
80
// ---------------------------------------------
81
class MainHandler implements ElementHandler {
82
private SAXReader mainReader;
84
private String mainDir;
86
public MainHandler(String dir) {
87
mainReader = new SAXReader();
89
mainReader.addHandler("/import/stuff", new EmbeddedHandler());
92
public void onStart(ElementPath path) {
95
public void onEnd(ElementPath path) {
96
String href = path.getCurrent().attribute("href").getValue();
97
Element importRef = path.getCurrent();
98
Element parentElement = importRef.getParent();
99
SAXReader onEndReader = new SAXReader();
100
onEndReader.addHandler("/import/stuff", new EmbeddedHandler());
102
File file = new File(mainDir + File.separator + href);
103
Element importElement = null;
106
if (test == MAIN_READER) {
107
importElement = mainReader.read(file).getRootElement();
108
} else if (test == ON_END_READER) {
109
importElement = onEndReader.read(file).getRootElement();
111
} catch (Exception e) {
112
// too bad that it's not possible to throw the exception at the
119
parentElement.add(importElement);
123
public class EmbeddedHandler implements ElementHandler {
124
public void onStart(ElementPath path) {
125
results[test].append(path.getCurrent().attribute("name").getValue()
129
public void onEnd(ElementPath path) {
135
* Redistribution and use of this software and associated documentation
136
* ("Software"), with or without modification, are permitted provided that the
137
* following conditions are met:
139
* 1. Redistributions of source code must retain copyright statements and
140
* notices. Redistributions must also contain a copy of this document.
142
* 2. Redistributions in binary form must reproduce the above copyright notice,
143
* this list of conditions and the following disclaimer in the documentation
144
* and/or other materials provided with the distribution.
146
* 3. The name "DOM4J" must not be used to endorse or promote products derived
147
* from this Software without prior written permission of MetaStuff, Ltd. For
148
* written permission, please contact dom4j-info@metastuff.com.
150
* 4. Products derived from this Software may not be called "DOM4J" nor may
151
* "DOM4J" appear in their names without prior written permission of MetaStuff,
152
* Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
154
* 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
156
* THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
157
* ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
158
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
159
* ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
160
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
161
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
162
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
163
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
164
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
165
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
166
* POSSIBILITY OF SUCH DAMAGE.
168
* Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.