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.springframework;
18  
19  import java.io.IOException;
20  import java.io.InputStream;
21  import java.util.List;
22  import java.util.Map;
23  
24  import org.springframework.core.io.AbstractResource;
25  import org.springframework.core.io.Resource;
26  
27  import ch.elca.el4j.services.xmlmerge.AbstractXmlMergeException;
28  import ch.elca.el4j.services.xmlmerge.ConfigurationException;
29  import ch.elca.el4j.services.xmlmerge.XmlMerge;
30  import ch.elca.el4j.services.xmlmerge.config.ConfigurableXmlMerge;
31  import ch.elca.el4j.services.xmlmerge.config.PropertyXPathConfigurer;
32  import ch.elca.el4j.services.xmlmerge.merge.DefaultXmlMerge;
33  
34  /**
35   * A spring resource merging XML files read from other resources.
36   *
37   * @svnLink $Revision: 3884 $;$Date: 2009-08-04 15:48:31 +0200 (Di, 04. Aug 2009) $;$Author: swismer $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/xml_merge/common/src/main/java/ch/elca/el4j/services/xmlmerge/springframework/XmlMergeResource.java $
38   *
39   * @author Laurent Bovet (LBO)
40   * @author Alex Mathey (AMA)
41   */
42  public class XmlMergeResource extends AbstractResource {
43  	
44  	/**
45  	 * The list of resources to merge.
46  	 */
47  	List m_resources;
48  	
49  	/**
50  	 * An XmlMerge instance used to merge the resources.
51  	 */
52  	XmlMerge m_xmlMerge;
53  	
54  	/**
55  	 * Configuration properties.
56  	 */
57  	Map m_properties;
58  		
59  	/**
60  	 * {@inheritDoc}
61  	 */
62  	public String getDescription() {
63  		return "Resource merging other XML resources using XmlMerge";
64  	}
65  	
66  	/**
67  	 * Returns an InputStream containing the data of this merged resource.
68  	 *
69  	 * @return The InputStream containing the data of this merged resource.
70  	 * @see org.springframework.core.io.AbstractResource
71  	 */
72  	public InputStream getInputStream() throws IOException {
73  
74  		if (m_properties == null) {
75  			// Default configuration
76  			m_xmlMerge = new DefaultXmlMerge();
77  		} else {
78  			try {
79  				m_xmlMerge = new ConfigurableXmlMerge(
80  					new PropertyXPathConfigurer(m_properties));
81  			} catch (ConfigurationException e) {
82  				throw new RuntimeException(e);
83  			}
84  		}
85  
86  		if (m_resources == null) {
87  			throw new RuntimeException("Resources not set");
88  		}
89  
90  		InputStream[] sources = new InputStream[m_resources.size()];
91  
92  		for (int i = 0; i < sources.length; i++) {
93  			sources[i] = ((Resource) m_resources.get(i)).getInputStream();
94  		}
95  
96  		try {
97  			return m_xmlMerge.merge(sources);
98  		} catch (AbstractXmlMergeException e) {
99  			throw new RuntimeException(e);
100 		}
101 	}
102 	 
103 	/**
104 	 * Sets the list of resources to merge.
105 	 *
106 	 * @param resources
107 	 *            The list of resources to merge
108 	 */
109 	public void setResources(List resources) {
110 		this.m_resources = resources;
111 
112 	}
113 	 
114 	/**
115 	 * Sets the configuration properties.
116 	 *
117 	 * @param map
118 	 *            A map containing the configuration properties
119 	 */
120 	public void setProperties(Map map) {
121 		m_properties = map;
122 	}
123 
124 }