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.action;
18
19 import java.util.ArrayList;
20 import java.util.Iterator;
21 import java.util.List;
22
23 import org.jdom.Content;
24 import org.jdom.Element;
25
26 import ch.elca.el4j.services.xmlmerge.Action;
27
28 /**
29 * Copies the patch element into the output by inserting it after already
30 * existing elements of the same name. Usually applied with the
31 * {@link ch.elca.el4j.services.xmlmerge.matcher.SkipMatcher}.
32 *
33 * @svnLink $Revision: 3878 $;$Date: 2009-08-04 15:06:35 +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/action/InsertAction.java $
34 *
35 * @author Laurent Bovet (LBO)
36 * @author Alex Mathey (AMA)
37 */
38 public class InsertAction implements Action {
39
40 /**
41 * {@inheritDoc}
42 */
43 public void perform(Element originalElement, Element patchElement,
44 Element outputParentElement) {
45
46 if (patchElement == null && originalElement != null) {
47 outputParentElement.addContent((Element) originalElement.clone());
48
49 } else {
50 List outputContent = outputParentElement.getContent();
51
52 Iterator it = outputContent.iterator();
53
54 int lastIndex = outputContent.size();
55
56 while (it.hasNext()) {
57 Content content = (Content) it.next();
58
59 if (content instanceof Element) {
60 Element element = (Element) content;
61
62 if (element.getQualifiedName().equals(
63 patchElement.getQualifiedName())) {
64 lastIndex = outputParentElement.indexOf(element);
65 }
66 }
67 }
68
69 List toAdd = new ArrayList();
70 toAdd.add(patchElement);
71 outputContent.addAll(Math.min(lastIndex + 1, outputContent.size()),
72 toAdd);
73 }
74 }
75
76 }