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.factory;
18  
19  import java.util.HashMap;
20  import java.util.Iterator;
21  import java.util.Map;
22  
23  import org.jaxen.JaxenException;
24  import org.jaxen.jdom.JDOMXPath;
25  import org.jdom.Element;
26  
27  import ch.elca.el4j.services.xmlmerge.AbstractXmlMergeException;
28  import ch.elca.el4j.services.xmlmerge.MatchException;
29  import ch.elca.el4j.services.xmlmerge.Operation;
30  import ch.elca.el4j.services.xmlmerge.OperationFactory;
31  
32  /**
33   * An operation factory that resolves operations given a map { xpath (as
34   * String), Operation }. The order in the map is relevant if several XPath
35   * matches.
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/factory/XPathOperationFactory.java $
38   *
39   * @author Laurent Bovet (LBO)
40   * @author Alex Mathey (AMA)
41   */
42  public class XPathOperationFactory implements OperationFactory {
43  
44  	/**
45  	 * A map containing configuration properties.
46  	 */
47  	Map m_map = new HashMap();
48  
49  	/**
50  	 * The default operation returned by this factory.
51  	 */
52  	Operation m_defaultOperation;
53  
54  	/**
55  	 * Sets the factory's map containing configuration properties.
56  	 *
57  	 * @param map
58  	 *            A map containing configuration properties.
59  	 */
60  	public void setOperationMap(Map map) {
61  		this.m_map = map;
62  	}
63  
64  	/**
65  	 * Sets the default operation returned by this factory.
66  	 * @param operation The default operation returned by this factory.
67  	 */
68  	public void setDefaultOperation(Operation operation) {
69  		this.m_defaultOperation = operation;
70  	}
71  
72  	/**
73  	 * {@inheritDoc}
74  	 */
75  	public Operation getOperation(Element originalElement, Element patchElement)
76  		throws AbstractXmlMergeException {
77  		Iterator it = m_map.keySet().iterator();
78  		while (it.hasNext()) {
79  			String xPath = (String) it.next();
80  			if (matches(originalElement, xPath) || matches(patchElement,
81  				xPath)) {
82  				return (Operation) m_map.get(xPath);
83  			}
84  		}
85  		return m_defaultOperation;
86  	}
87  
88  	/**
89  	 * Detects whether the given element matches the given XPath string.
90  	 *
91  	 * @param element
92  	 *            The element which will be checked
93  	 * @param xPathString
94  	 *            The XPath expression the element will be checked against
95  	 * @return True if the given element matches the given XPath string
96  	 * @throws AbstractXmlMergeException
97  	 *             If an error occurred during the matching process
98  	 */
99  	private boolean matches(Element element, String xPathString)
100 		throws AbstractXmlMergeException {
101 
102 		if (element == null) {
103 			return false;
104 		}
105 
106 		try {
107 			JDOMXPath xPath = new JDOMXPath(xPathString);
108 
109 			boolean result = xPath.selectNodes(element.getParent()).contains(
110 				element);
111 
112 			return result;
113 
114 		} catch (JaxenException e) {
115 			throw new MatchException(element, e);
116 		}
117 	}
118 
119 }