Index: AbstractMergeSet.java =================================================================== --- AbstractMergeSet.java (revision 0) +++ AbstractMergeSet.java (revision 0) @@ -0,0 +1,93 @@ +/* + * ======================================================================== + * + * Copyright 2004-2005 Vincent Massol. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ======================================================================== + */ +package org.codehaus.cargo.module.merge; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import org.w3c.dom.Element; + +/** + * Abstract class containing a set of items that need to be merged, split up into items + * that just appear in the left-hand side, those in the right-hand side, and + * those that are in both (potential conflicts). + * + * @version $Id $ + * + */ +public abstract class AbstractMergeSet +{ + /** + * Items just in the left hand side + */ + public List inLeftOnly = new ArrayList(); + + /** + * Items just in the right hand side + */ + public List inRightOnly = new ArrayList(); + + /** + * Items in both sides (conflicts) + */ + public List inBoth = new ArrayList(); + + /** + * Perform the merge using the passed strategy + * @param action in the strategy to use when merging + * @return the count of merges performed + */ + public int merge(MergeStrategy action) + { + int count = 0; + + for (Iterator i = this.inLeftOnly.iterator(); i.hasNext();) + { + Element element = (Element) i.next(); + count += action.inLeft(this, element); + } + for (Iterator i = this.inRightOnly.iterator(); i.hasNext();) + { + Element element = (Element) i.next(); + count += action.inRight(this, element); + } + // Merge common items by overwriting them + for (Iterator i = this.inBoth.iterator(); i.hasNext();) + { + MergePair pair = (MergePair) i.next(); + count += action.inBoth(this, pair); + } + return count; + } + + /** + * As a result of the merge, add an element to the output + * @param e the element to be added + */ + public abstract void add(Element e); + + /** + * As a result of the merge, remove an element from the output + * @param e the element to be removed + */ + public abstract void remove(Element e); + +} Index: MergeProcessor.java =================================================================== --- MergeProcessor.java (revision 0) +++ MergeProcessor.java (revision 0) @@ -0,0 +1,43 @@ +/* + * ======================================================================== + * + * Copyright 2004-2005 Vincent Massol. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ======================================================================== + */ +package org.codehaus.cargo.module.merge; + + +/** + * Interface implemented by merger classes. + * @version $Id $ + * + */ +public interface MergeProcessor +{ + /** + * Add an item to be merged + * @param mergeItem in the item to merge. + * @throws MergeException on exceptions + */ + void addMergeItem(Object mergeItem) throws MergeException; + + /** + * Perform the merge + * @return the merged artifact + * @throws MergeException if there is a problem + */ + Object performMerge() throws MergeException; +} Index: MergeNodeList.java =================================================================== --- MergeNodeList.java (revision 0) +++ MergeNodeList.java (revision 0) @@ -0,0 +1,116 @@ +/* + * ======================================================================== + * + * Copyright 2004-2005 Vincent Massol. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ======================================================================== + */ +package org.codehaus.cargo.module.merge; + +import java.util.Iterator; + +import org.codehaus.cargo.util.xml.AbstractElement; +import org.codehaus.cargo.util.xml.AbstractNodeList; +import org.w3c.dom.Element; + +/** + * A MergedNodeList is a MergeSet that applies to AbstractNodeLists + * + * @version $Id $ + */ +public class MergeNodeList extends AbstractMergeSet +{ + /** + * The set of everything from the left hand side + */ + private AbstractNodeList leftSet; + + /** + * The set of everything from the right hand side + */ + private AbstractNodeList rightSet; + + /** + * Constructor + * @param leftSet in the left hand set + * @param rightSet in the right hand set + */ + protected MergeNodeList(AbstractNodeList leftSet, AbstractNodeList rightSet) + { + this.leftSet = leftSet; + this.rightSet = rightSet; + } + + /** + * Static constructor. Make a merge set from the two node sets, using the + * elements 'name' in order to determine whether it is in conflict + * + * @param leftSet in the left hand set + * @param rightSet in the right hand set + * @return the generated merge set + */ + public static final AbstractMergeSet createFromNames(AbstractNodeList leftSet, + AbstractNodeList rightSet) + { + AbstractMergeSet results = new MergeNodeList(leftSet, rightSet); + + Iterator i = leftSet.iterator(); + while (i.hasNext()) + { + AbstractElement left = (AbstractElement) i.next(); + AbstractElement right = rightSet.getByElementId(left.getElementId()); + + if (right == null) + { + results.inLeftOnly.add(left); + } + else + { + results.inBoth.add(new MergePair(left, right)); + } + } + + i = rightSet.iterator(); + + while (i.hasNext()) + { + AbstractElement right = (AbstractElement) i.next(); + AbstractElement left = leftSet.getByElementId(right.getElementId()); + + if (left == null) + { + results.inRightOnly.add(right); + } + } + return results; + } + + + /** + * @see org.codehaus.cargo.module.merge.AbstractMergeSet#add(org.w3c.dom.Element) + */ + public void add(Element e) + { + this.leftSet.add(e); + } + + /** + * @see org.codehaus.cargo.module.merge.AbstractMergeSet#remove(org.w3c.dom.Element) + */ + public void remove(Element e) + { + this.leftSet.remove(e); + } +} Index: MergeStrategy.java =================================================================== --- MergeStrategy.java (revision 0) +++ MergeStrategy.java (revision 0) @@ -0,0 +1,99 @@ +/* + * ======================================================================== + * + * Copyright 2004-2005 Vincent Massol. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ======================================================================== + */ +package org.codehaus.cargo.module.merge; + +import org.w3c.dom.Element; + +/** + * A merge strategy is a vistor/stratey defining some way in which potential + * merge conflicts can be resolved in the set. + * + * @version $Id $ + * + */ +public class MergeStrategy +{ + /** + * Preserve strategy only adds items from the right + */ + public static final MergeStrategy PRESERVE = new MergeStrategy() + { + public int inRight(AbstractMergeSet set, Element element) + { + set.add(element); + return 1; + } + }; + + /** + * Overwrite strategy adds items from the right, and overwrites + * duplicates. + */ + public static final MergeStrategy OVERWRITE = new MergeStrategy() + { + public int inRight(AbstractMergeSet set, Element element) + { + set.add(element); + return 1; + } + + int inBoth(AbstractMergeSet set, MergePair pair) + { + // Merge common items by overwriting them + set.remove(pair.left); + set.add(pair.right); + return 1; + } + }; + + /** + * Deal with merging an element that only appears in the left set + * @param set in the calling MergeSet + * @param element in the item present only in the left set + * @return count of merged items + */ + int inLeft(AbstractMergeSet set, Element element) + { + return 0; + } + + /** + * Deal with merging an element that only appears in the right set + * @param set in the calling MergeSet + * @param element in the item present only in the left set + * @return count of merged items + */ + int inRight(AbstractMergeSet set, Element element) + { + return 0; + } + + /** + * Deal with merging an element appears in both sets + * @param set in the calling MergeSet + * @param pair the pair of items + * @return count of merged items + */ + int inBoth(AbstractMergeSet set, MergePair pair) + { + return 0; + } + +} Index: MergeElement.java =================================================================== --- MergeElement.java (revision 0) +++ MergeElement.java (revision 0) @@ -0,0 +1,85 @@ +/* + * ======================================================================== + * + * Copyright 2004-2005 Vincent Massol. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ======================================================================== + */ +package org.codehaus.cargo.module.merge; + +import org.w3c.dom.Element; +import org.w3c.dom.Node; + +/** + * MergeElement - a MergeSet that only contains one element from left or right + * + * @version $Id $ + */ +public class MergeElement extends AbstractMergeSet +{ + /** + * The parent of the left-hand element + */ + private Element leftParent; + + /** + * Constructor. + * + * NB: leftParent is needed as it is allowable for left to be null, in + * the event that it does not exist (And so could not be determined otherwise) + * + * @param leftParent in the left hand elements parent + * @param left in the left hand element + * @param right in the right hand element + */ + public MergeElement(Element leftParent, Element left, Element right) + { + super(); + + this.leftParent = leftParent; + + if (left == null) + { + this.inRightOnly.add(right); + } + else if (right == null) + { + this.inLeftOnly.add(left); + } + else + { + this.inBoth.add(new MergePair(left, right)); + } + + } + + /** + * @see org.codehaus.cargo.module.merge.AbstractMergeSet#add(org.w3c.dom.Element) + */ + public void add(Element e) + { + Node n = this.leftParent.getOwnerDocument().importNode(e, true); + this.leftParent.appendChild(n); + } + + /** + * @see org.codehaus.cargo.module.merge.AbstractMergeSet#remove(org.w3c.dom.Element) + */ + public void remove(Element e) + { + this.leftParent.removeChild(e); + } + +} Index: MergePair.java =================================================================== --- MergePair.java (revision 0) +++ MergePair.java (revision 0) @@ -0,0 +1,52 @@ +/* + * ======================================================================== + * + * Copyright 2004-2005 Vincent Massol. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ======================================================================== + */ +package org.codehaus.cargo.module.merge; + +import org.w3c.dom.Element; + +/** + * Class containing a pair of items that need to be merged. + * + * @version $Id $ + * + */ +public class MergePair +{ + /** + * Left and right hand elements + */ + public Element left; + + /** + * right + */ + public Element right; + + /** + * Constructor + * @param left left element + * @param right right element + */ + MergePair(Element left, Element right) + { + this.left = left; + this.right = right; + } +} Index: MergeException.java =================================================================== --- MergeException.java (revision 0) +++ MergeException.java (revision 0) @@ -0,0 +1,54 @@ +/* + * ======================================================================== + * + * Copyright 2004-2005 Vincent Massol. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ======================================================================== + */ +package org.codehaus.cargo.module.merge; + +/** + * Class for various exceptions happening within merges + * + * @version $Id $ + */ +public class MergeException extends Exception +{ + /** + * Constructor + * @param e in cause of the exception + */ + public MergeException(Throwable e) + { + super(e); + } + + /** + * @param string in string to describe the exception + */ + public MergeException(String string) + { + super(string); + } + + /** + * @param string in string to describe the exception + * @param e in causing exception + */ + public MergeException(String string, Exception e) + { + super(string, e); + } +} Index: AbstractMergeSet.java =================================================================== --- AbstractMergeSet.java (revision 0) +++ AbstractMergeSet.java (revision 0) @@ -0,0 +1,93 @@ +/* + * ======================================================================== + * + * Copyright 2004-2005 Vincent Massol. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ======================================================================== + */ +package org.codehaus.cargo.module.merge; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import org.w3c.dom.Element; + +/** + * Abstract class containing a set of items that need to be merged, split up into items + * that just appear in the left-hand side, those in the right-hand side, and + * those that are in both (potential conflicts). + * + * @version $Id $ + * + */ +public abstract class AbstractMergeSet +{ + /** + * Items just in the left hand side + */ + public List inLeftOnly = new ArrayList(); + + /** + * Items just in the right hand side + */ + public List inRightOnly = new ArrayList(); + + /** + * Items in both sides (conflicts) + */ + public List inBoth = new ArrayList(); + + /** + * Perform the merge using the passed strategy + * @param action in the strategy to use when merging + * @return the count of merges performed + */ + public int merge(MergeStrategy action) + { + int count = 0; + + for (Iterator i = this.inLeftOnly.iterator(); i.hasNext();) + { + Element element = (Element) i.next(); + count += action.inLeft(this, element); + } + for (Iterator i = this.inRightOnly.iterator(); i.hasNext();) + { + Element element = (Element) i.next(); + count += action.inRight(this, element); + } + // Merge common items by overwriting them + for (Iterator i = this.inBoth.iterator(); i.hasNext();) + { + MergePair pair = (MergePair) i.next(); + count += action.inBoth(this, pair); + } + return count; + } + + /** + * As a result of the merge, add an element to the output + * @param e the element to be added + */ + public abstract void add(Element e); + + /** + * As a result of the merge, remove an element from the output + * @param e the element to be removed + */ + public abstract void remove(Element e); + +} Index: MergeElement.java =================================================================== --- MergeElement.java (revision 0) +++ MergeElement.java (revision 0) @@ -0,0 +1,85 @@ +/* + * ======================================================================== + * + * Copyright 2004-2005 Vincent Massol. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ======================================================================== + */ +package org.codehaus.cargo.module.merge; + +import org.w3c.dom.Element; +import org.w3c.dom.Node; + +/** + * MergeElement - a MergeSet that only contains one element from left or right + * + * @version $Id $ + */ +public class MergeElement extends AbstractMergeSet +{ + /** + * The parent of the left-hand element + */ + private Element leftParent; + + /** + * Constructor. + * + * NB: leftParent is needed as it is allowable for left to be null, in + * the event that it does not exist (And so could not be determined otherwise) + * + * @param leftParent in the left hand elements parent + * @param left in the left hand element + * @param right in the right hand element + */ + public MergeElement(Element leftParent, Element left, Element right) + { + super(); + + this.leftParent = leftParent; + + if (left == null) + { + this.inRightOnly.add(right); + } + else if (right == null) + { + this.inLeftOnly.add(left); + } + else + { + this.inBoth.add(new MergePair(left, right)); + } + + } + + /** + * @see org.codehaus.cargo.module.merge.AbstractMergeSet#add(org.w3c.dom.Element) + */ + public void add(Element e) + { + Node n = this.leftParent.getOwnerDocument().importNode(e, true); + this.leftParent.appendChild(n); + } + + /** + * @see org.codehaus.cargo.module.merge.AbstractMergeSet#remove(org.w3c.dom.Element) + */ + public void remove(Element e) + { + this.leftParent.removeChild(e); + } + +} Index: MergeException.java =================================================================== --- MergeException.java (revision 0) +++ MergeException.java (revision 0) @@ -0,0 +1,54 @@ +/* + * ======================================================================== + * + * Copyright 2004-2005 Vincent Massol. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ======================================================================== + */ +package org.codehaus.cargo.module.merge; + +/** + * Class for various exceptions happening within merges + * + * @version $Id $ + */ +public class MergeException extends Exception +{ + /** + * Constructor + * @param e in cause of the exception + */ + public MergeException(Throwable e) + { + super(e); + } + + /** + * @param string in string to describe the exception + */ + public MergeException(String string) + { + super(string); + } + + /** + * @param string in string to describe the exception + * @param e in causing exception + */ + public MergeException(String string, Exception e) + { + super(string, e); + } +} Index: MergeNodeList.java =================================================================== --- MergeNodeList.java (revision 0) +++ MergeNodeList.java (revision 0) @@ -0,0 +1,116 @@ +/* + * ======================================================================== + * + * Copyright 2004-2005 Vincent Massol. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ======================================================================== + */ +package org.codehaus.cargo.module.merge; + +import java.util.Iterator; + +import org.codehaus.cargo.util.xml.AbstractElement; +import org.codehaus.cargo.util.xml.AbstractNodeList; +import org.w3c.dom.Element; + +/** + * A MergedNodeList is a MergeSet that applies to AbstractNodeLists + * + * @version $Id $ + */ +public class MergeNodeList extends AbstractMergeSet +{ + /** + * The set of everything from the left hand side + */ + private AbstractNodeList leftSet; + + /** + * The set of everything from the right hand side + */ + private AbstractNodeList rightSet; + + /** + * Constructor + * @param leftSet in the left hand set + * @param rightSet in the right hand set + */ + protected MergeNodeList(AbstractNodeList leftSet, AbstractNodeList rightSet) + { + this.leftSet = leftSet; + this.rightSet = rightSet; + } + + /** + * Static constructor. Make a merge set from the two node sets, using the + * elements 'name' in order to determine whether it is in conflict + * + * @param leftSet in the left hand set + * @param rightSet in the right hand set + * @return the generated merge set + */ + public static final AbstractMergeSet createFromNames(AbstractNodeList leftSet, + AbstractNodeList rightSet) + { + AbstractMergeSet results = new MergeNodeList(leftSet, rightSet); + + Iterator i = leftSet.iterator(); + while (i.hasNext()) + { + AbstractElement left = (AbstractElement) i.next(); + AbstractElement right = rightSet.getByElementId(left.getElementId()); + + if (right == null) + { + results.inLeftOnly.add(left); + } + else + { + results.inBoth.add(new MergePair(left, right)); + } + } + + i = rightSet.iterator(); + + while (i.hasNext()) + { + AbstractElement right = (AbstractElement) i.next(); + AbstractElement left = leftSet.getByElementId(right.getElementId()); + + if (left == null) + { + results.inRightOnly.add(right); + } + } + return results; + } + + + /** + * @see org.codehaus.cargo.module.merge.AbstractMergeSet#add(org.w3c.dom.Element) + */ + public void add(Element e) + { + this.leftSet.add(e); + } + + /** + * @see org.codehaus.cargo.module.merge.AbstractMergeSet#remove(org.w3c.dom.Element) + */ + public void remove(Element e) + { + this.leftSet.remove(e); + } +} Index: MergePair.java =================================================================== --- MergePair.java (revision 0) +++ MergePair.java (revision 0) @@ -0,0 +1,52 @@ +/* + * ======================================================================== + * + * Copyright 2004-2005 Vincent Massol. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ======================================================================== + */ +package org.codehaus.cargo.module.merge; + +import org.w3c.dom.Element; + +/** + * Class containing a pair of items that need to be merged. + * + * @version $Id $ + * + */ +public class MergePair +{ + /** + * Left and right hand elements + */ + public Element left; + + /** + * right + */ + public Element right; + + /** + * Constructor + * @param left left element + * @param right right element + */ + MergePair(Element left, Element right) + { + this.left = left; + this.right = right; + } +} Index: MergeProcessor.java =================================================================== --- MergeProcessor.java (revision 0) +++ MergeProcessor.java (revision 0) @@ -0,0 +1,43 @@ +/* + * ======================================================================== + * + * Copyright 2004-2005 Vincent Massol. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ======================================================================== + */ +package org.codehaus.cargo.module.merge; + + +/** + * Interface implemented by merger classes. + * @version $Id $ + * + */ +public interface MergeProcessor +{ + /** + * Add an item to be merged + * @param mergeItem in the item to merge. + * @throws MergeException on exceptions + */ + void addMergeItem(Object mergeItem) throws MergeException; + + /** + * Perform the merge + * @return the merged artifact + * @throws MergeException if there is a problem + */ + Object performMerge() throws MergeException; +} Index: MergeStrategy.java =================================================================== --- MergeStrategy.java (revision 0) +++ MergeStrategy.java (revision 0) @@ -0,0 +1,99 @@ +/* + * ======================================================================== + * + * Copyright 2004-2005 Vincent Massol. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ======================================================================== + */ +package org.codehaus.cargo.module.merge; + +import org.w3c.dom.Element; + +/** + * A merge strategy is a vistor/stratey defining some way in which potential + * merge conflicts can be resolved in the set. + * + * @version $Id $ + * + */ +public class MergeStrategy +{ + /** + * Preserve strategy only adds items from the right + */ + public static final MergeStrategy PRESERVE = new MergeStrategy() + { + public int inRight(AbstractMergeSet set, Element element) + { + set.add(element); + return 1; + } + }; + + /** + * Overwrite strategy adds items from the right, and overwrites + * duplicates. + */ + public static final MergeStrategy OVERWRITE = new MergeStrategy() + { + public int inRight(AbstractMergeSet set, Element element) + { + set.add(element); + return 1; + } + + int inBoth(AbstractMergeSet set, MergePair pair) + { + // Merge common items by overwriting them + set.remove(pair.left); + set.add(pair.right); + return 1; + } + }; + + /** + * Deal with merging an element that only appears in the left set + * @param set in the calling MergeSet + * @param element in the item present only in the left set + * @return count of merged items + */ + int inLeft(AbstractMergeSet set, Element element) + { + return 0; + } + + /** + * Deal with merging an element that only appears in the right set + * @param set in the calling MergeSet + * @param element in the item present only in the left set + * @return count of merged items + */ + int inRight(AbstractMergeSet set, Element element) + { + return 0; + } + + /** + * Deal with merging an element appears in both sets + * @param set in the calling MergeSet + * @param pair the pair of items + * @return count of merged items + */ + int inBoth(AbstractMergeSet set, MergePair pair) + { + return 0; + } + +}