1
2
3
4
5
6
7
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
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
87 e.printStackTrace();
88 }
89
90
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
106
107
108 public void testMainReader() throws Exception {
109 _test = MAIN_READER;
110 readDocuments();
111
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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198