1 /*
2 * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
3 *
4 * This software is open source.
5 * See the bottom of this file for the licence.
6 *
7 * $Id: TestEmbeddedHandler.java,v 1.6 2004/06/25 08:03:47 maartenc Exp $
8 */
9
10 package org.dom4j;
11
12 import java.io.File;
13
14 import junit.framework.Test;
15 import junit.framework.TestSuite;
16 import junit.textui.TestRunner;
17
18 import org.dom4j.io.SAXReader;
19
20 /***
21 * TestEmbeddedHandler
22 *
23 *
24 * Created: Thu Mar 21 15:45:59 2002
25 *
26 * @author <a href="mailto:franz.beil@temis-group.com">FB</a>
27 * @version
28 */
29 public class TestEmbeddedHandler extends AbstractTestCase {
30
31 protected String[] testDocuments = { "xml/test/FranzBeilMain.xml", };
32 final int MAIN_READER = 0;
33 final int ON_END_READER = 1;
34 private StringBuffer[] _results =
35 { new StringBuffer(), new StringBuffer()};
36 protected int _test;
37
38 public static void main(String[] args) {
39 TestRunner.run(suite());
40 }
41
42 public static Test suite() {
43 return new TestSuite(TestEmbeddedHandler.class);
44 }
45
46 /***
47 * Constructor for TestEmbeddedHandler.
48 * @param name
49 */
50 public TestEmbeddedHandler(String name) {
51 super(name);
52 }
53
54 //---------------------------------------------
55 // Handler classes
56 //---------------------------------------------
57
58 class MainHandler implements ElementHandler {
59
60 SAXReader _mainReader;
61 String _mainDir;
62
63 public MainHandler(String dir) {
64 _mainReader = new SAXReader();
65 _mainDir = dir;
66 _mainReader.addHandler("/import/stuff", new EmbeddedHandler());
67 }
68
69 public void onStart(ElementPath path) {}
70
71 public void onEnd(ElementPath path) {
72 String href = path.getCurrent().attribute("href").getValue();
73 Element importRef = path.getCurrent();
74 Element parentElement = importRef.getParent();
75 SAXReader onEndReader = new SAXReader();
76 onEndReader.addHandler("/import/stuff", new EmbeddedHandler());
77
78 File file = new File(_mainDir + File.separator + href);
79 Element importElement = null;
80 try {
81 if (_test == MAIN_READER)
82 importElement = _mainReader.read(file).getRootElement();
83 else if (_test == ON_END_READER)
84 importElement = onEndReader.read(file).getRootElement();
85 } catch (Exception e) {
86 // too bad that it's not possible to throw the exception at the caller
87 e.printStackTrace();
88 }
89
90 // prune and replace
91 importRef.detach();
92 parentElement.add(importElement);
93 }
94 }
95
96 public class EmbeddedHandler implements ElementHandler {
97 public void onStart(ElementPath path) {
98 _results[_test].append(
99 path.getCurrent().attribute("name").getValue() + "\n");
100 }
101 public void onEnd(ElementPath path) {}
102 }
103
104 //---------------------------------------------
105 // Test case(s)
106 //---------------------------------------------
107
108 public void testMainReader() throws Exception {
109 _test = MAIN_READER;
110 readDocuments();
111 // System.out.println("testMainReader()\n"+_results[_test].toString());
112 }
113
114 public void testOnEndReader() throws Exception {
115 _test = ON_END_READER;
116 readDocuments();
117 }
118
119 public void testBothReaders() throws Exception {
120 testMainReader();
121 testOnEndReader();
122 if (!_results[MAIN_READER]
123 .toString()
124 .equals(_results[ON_END_READER].toString())) {
125 StringBuffer msg = new StringBuffer();
126 msg.append("Results of tests should be equal!\n");
127 msg.append("Results testMainReader():\n" + _results[MAIN_READER].toString());
128 msg.append(
129 "Results testOnEndReader():\n" + _results[ON_END_READER].toString());
130 throw new Exception(msg.toString());
131 }
132 }
133
134
135 //---------------------------------------------
136 // Implementation methods
137 //---------------------------------------------
138
139 private void readDocuments() throws Exception {
140 for (int i = 0; i < testDocuments.length; i++) {
141 File testDoc = new File(testDocuments[i]);
142 String mainDir = testDoc.getParent();
143 SAXReader reader = new SAXReader();
144 ElementHandler mainHandler = new MainHandler(mainDir);
145 reader.addHandler("/main/import", mainHandler);
146 reader.read(testDoc);
147 }
148 }
149
150 }
151
152
153
154
155 /*
156 * Redistribution and use of this software and associated documentation
157 * ("Software"), with or without modification, are permitted provided
158 * that the following conditions are met:
159 *
160 * 1. Redistributions of source code must retain copyright
161 * statements and notices. Redistributions must also contain a
162 * copy of this document.
163 *
164 * 2. Redistributions in binary form must reproduce the
165 * above copyright notice, this list of conditions and the
166 * following disclaimer in the documentation and/or other
167 * materials provided with the distribution.
168 *
169 * 3. The name "DOM4J" must not be used to endorse or promote
170 * products derived from this Software without prior written
171 * permission of MetaStuff, Ltd. For written permission,
172 * please contact dom4j-info@metastuff.com.
173 *
174 * 4. Products derived from this Software may not be called "DOM4J"
175 * nor may "DOM4J" appear in their names without prior written
176 * permission of MetaStuff, Ltd. DOM4J is a registered
177 * trademark of MetaStuff, Ltd.
178 *
179 * 5. Due credit should be given to the DOM4J Project -
180 * http://www.dom4j.org
181 *
182 * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
183 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
184 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
185 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
186 * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
187 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
188 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
189 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
190 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
191 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
192 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
193 * OF THE POSSIBILITY OF SUCH DAMAGE.
194 *
195 * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
196 *
197 * $Id: TestEmbeddedHandler.java,v 1.6 2004/06/25 08:03:47 maartenc Exp $
198 */