1 package org.apache.torque.generator.merge;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import java.io.ByteArrayOutputStream;
23 import java.io.IOException;
24 import java.io.UnsupportedEncodingException;
25
26 import org.apache.torque.generator.GeneratorException;
27 import org.eclipse.jgit.diff.RawText;
28 import org.eclipse.jgit.diff.RawTextComparator;
29 import org.eclipse.jgit.merge.MergeAlgorithm;
30 import org.eclipse.jgit.merge.MergeFormatter;
31 import org.eclipse.jgit.merge.MergeResult;
32
33 /**
34 * Can execute a three-way merge. This class is thread safe.
35 *
36 * @version $Id: ThreeWayMerger.java 1331190 2012-04-27 02:41:35Z tfischer $
37 */
38 public class ThreeWayMerger
39 {
40 /**
41 * Performs a three-way merge.
42 *
43 * @param base the base from which the other two versions are derived,
44 * not null.
45 * @param generated the newly generated text, not null.
46 * @param edited the possibly edited text, not null.
47 * @param charsetName the name of the character set, not null.
48 * @return the merge result, not null.
49 *
50 * @throws GeneratorException if merging fails.
51 */
52 public String merge(
53 String base,
54 String generated,
55 String edited,
56 String charsetName)
57 throws GeneratorException
58 {
59 MergeAlgorithm mergeAlgorithm = new MergeAlgorithm();
60 MergeResult<RawText> mergeResult;
61 try
62 {
63 mergeResult = mergeAlgorithm.merge(
64 RawTextComparator.DEFAULT,
65 new RawText(base.getBytes(charsetName)),
66 new RawText(generated.getBytes(charsetName)),
67 new RawText(edited.getBytes(charsetName)));
68 }
69 catch (UnsupportedEncodingException e)
70 {
71 throw new GeneratorException(
72 "unknown character set " + charsetName,
73 e);
74 }
75 ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
76 try
77 {
78 new MergeFormatter().formatMerge(
79 outputStream,
80 mergeResult,
81 "base",
82 "generated",
83 "edited",
84 charsetName);
85 }
86 catch (IOException e)
87 {
88 throw new GeneratorException("could nor render merge result", e);
89 }
90 try
91 {
92 String result
93 = new String(outputStream.toByteArray(), charsetName);
94 return result;
95 }
96 catch (UnsupportedEncodingException e)
97 {
98 throw new GeneratorException(
99 "unknown character set " + charsetName,
100 e);
101 }
102 }
103 }