1
2
3
4
5
6
7
8 package org.dom4j;
9
10 import junit.textui.TestRunner;
11
12 import java.io.File;
13
14 import org.dom4j.io.SAXReader;
15
16 /***
17 * TestEmbeddedHandler
18 *
19 * @author <a href="mailto:franz.beil@temis-group.com">FB </a>
20 */
21 public class EmbeddedHandlerTest extends AbstractTestCase {
22 private static final int MAIN_READER = 0;
23
24 private static final int ON_END_READER = 1;
25
26 protected String[] testDocuments = {"xml/test/FranzBeilMain.xml"};
27
28 private StringBuffer[] results = {new StringBuffer(), new StringBuffer()};
29
30 protected int test;
31
32 public static void main(String[] args) {
33 TestRunner.run(EmbeddedHandlerTest.class);
34 }
35
36
37
38
39 public void testMainReader() throws Exception {
40 test = MAIN_READER;
41 readDocuments();
42 }
43
44 public void testOnEndReader() throws Exception {
45 test = ON_END_READER;
46 readDocuments();
47 }
48
49 public void testBothReaders() throws Exception {
50 testMainReader();
51 testOnEndReader();
52
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());
62 }
63 }
64
65
66
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);
76 }
77 }
78
79
80
81 class MainHandler implements ElementHandler {
82 private SAXReader mainReader;
83
84 private String mainDir;
85
86 public MainHandler(String dir) {
87 mainReader = new SAXReader();
88 mainDir = dir;
89 mainReader.addHandler("/import/stuff", new EmbeddedHandler());
90 }
91
92 public void onStart(ElementPath path) {
93 }
94
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());
101
102 File file = new File(mainDir + File.separator + href);
103 Element importElement = null;
104
105 try {
106 if (test == MAIN_READER) {
107 importElement = mainReader.read(file).getRootElement();
108 } else if (test == ON_END_READER) {
109 importElement = onEndReader.read(file).getRootElement();
110 }
111 } catch (Exception e) {
112
113
114 e.printStackTrace();
115 }
116
117
118 importRef.detach();
119 parentElement.add(importElement);
120 }
121 }
122
123 public class EmbeddedHandler implements ElementHandler {
124 public void onStart(ElementPath path) {
125 results[test].append(path.getCurrent().attribute("name").getValue()
126 + "\n");
127 }
128
129 public void onEnd(ElementPath path) {
130 }
131 }
132 }
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169