View Javadoc

1   /*
2    * EL4J, the Extension Library for the J2EE, adds incremental enhancements to
3    * the spring framework, http://el4j.sf.net
4    * Copyright (C) 2006 by ELCA Informatique SA, Av. de la Harpe 22-24,
5    * 1000 Lausanne, Switzerland, http://www.elca.ch
6    *
7    * EL4J is published under the GNU Lesser General Public License (LGPL)
8    * Version 2.1. See http://www.gnu.org/licenses/
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   * GNU Lesser General Public License for more details.
14   *
15   * For alternative licensing, please contact info@elca.ch
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   * Spring controller for the XmlMerge web application.
34   *
35   * @svnLink $Revision: 3874 $;$Date: 2009-08-04 14:25:40 +0200 (Di, 04. Aug 2009) $;$Author: swismer $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/xml_merge/web/jar/src/main/java/ch/elca/el4j/services/xmlmerge/web/DemoController.java $
36   *
37   * @author Laurent Bovet (LBO)
38   * @author Alex Mathey (AMA)
39   */
40  public class DemoController implements Controller {
41  
42  	/**
43  	 * New line.
44  	 */
45  	public static final String NL = System.getProperty("line.separator");
46  	
47  	/**
48  	 * {@inheritDoc}
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 }