1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package ch.elca.el4j.services.xmlmerge.web;
18
19 import java.util.HashMap;
20 import java.util.Map;
21
22 import javax.servlet.http.HttpServletRequest;
23 import javax.servlet.http.HttpServletResponse;
24
25 import org.springframework.web.servlet.ModelAndView;
26 import org.springframework.web.servlet.mvc.Controller;
27
28 import ch.elca.el4j.services.xmlmerge.XmlMerge;
29 import ch.elca.el4j.services.xmlmerge.config.ConfigurableXmlMerge;
30 import ch.elca.el4j.services.xmlmerge.config.PropertyXPathConfigurer;
31
32
33
34
35
36
37
38
39
40 public class DemoController implements Controller {
41
42
43
44
45 public static final String NL = System.getProperty("line.separator");
46
47
48
49
50 public ModelAndView handleRequest(HttpServletRequest request,
51 HttpServletResponse response) throws Exception {
52
53 String source1 = request.getParameter("source1");
54 String source2 = request.getParameter("source2");
55 String conf = request.getParameter("conf");
56
57 if (source1 == null) {
58
59 conf = "action.default=MERGE" + NL + NL
60 + "xpath.1=/root/d" + NL
61 + "action.1=REPLACE";
62
63 source1 = "<root attr1=\"1\">" + NL
64 + " some text " + NL
65 + " <a attr=\"old\">" + NL
66 + " <!-- this is a comment -->" + NL
67 + " <ab />" + NL
68 + " <xx>" + NL
69 + " <yy />" + NL
70 + " </xx>" + NL
71 + " <aa />" + NL
72 + " </a>" + NL
73 + " <b />" + NL
74 + " <c />" + NL
75 + " <d attr1=\"bye\"/>" + NL
76 + " <e>" + NL
77 + " <f />" + NL
78 + " </e>" + NL
79 + "</root>";
80
81 source2 = "<root attr2=\"2\">" + NL
82 + " , some other text " + NL
83 + " <a attr=\"new\">" + NL
84 + " <ab />" + NL
85 + " <!-- this is another comment -->" + NL
86 + " <xx>" + NL
87 + " <zz />" + NL
88 + " </xx>" + NL
89 + " <aa />" + NL
90 + " </a>" + NL
91 + " <c />" + NL
92 + " <b />" + NL
93 + " <d attr2=\"hello\"/>" + NL
94 + " <g />" + NL
95 + "</root>";
96 }
97
98 XmlMerge xmlMerge = new ConfigurableXmlMerge(
99 new PropertyXPathConfigurer(conf));
100
101 String result = xmlMerge.merge(new String[] {source1, source2});
102
103 Map model = new HashMap();
104
105 model.put("source1", source1.trim());
106 model.put("source2", source2.trim());
107 model.put("conf", conf.trim());
108 model.put("result", result.trim());
109
110 return new ModelAndView("demo", model);
111 }
112
113 }