2525import com .github .gumtreediff .matchers .MappingStore ;
2626import com .github .gumtreediff .tree .ITree ;
2727import com .github .gumtreediff .tree .TreeContext ;
28+ import com .google .gson .stream .JsonWriter ;
2829
2930import javax .xml .stream .XMLOutputFactory ;
3031import javax .xml .stream .XMLStreamException ;
3536
3637public final class ActionsIoUtils {
3738
38- private ActionsIoUtils () { }
39+ private ActionsIoUtils () {
40+ }
3941
4042 public static ActionSerializer toText (TreeContext sctx , List <Action > actions ,
41- MappingStore mappings ) throws IOException {
43+ MappingStore mappings ) throws IOException {
4244 return new ActionSerializer (sctx , mappings , actions ) {
4345
4446 @ Override
@@ -49,7 +51,7 @@ protected ActionFormatter newFormatter(TreeContext ctx, Writer writer) throws Ex
4951 }
5052
5153 public static ActionSerializer toXml (TreeContext sctx , List <Action > actions ,
52- MappingStore mappings ) throws IOException {
54+ MappingStore mappings ) throws IOException {
5355 return new ActionSerializer (sctx , mappings , actions ) {
5456
5557 @ Override
@@ -59,6 +61,17 @@ protected ActionFormatter newFormatter(TreeContext ctx, Writer writer) throws Ex
5961 };
6062 }
6163
64+ public static ActionSerializer toJson (TreeContext sctx , List <Action > actions ,
65+ MappingStore mappings ) throws IOException {
66+ return new ActionSerializer (sctx , mappings , actions ) {
67+
68+ @ Override
69+ protected ActionFormatter newFormatter (TreeContext ctx , Writer writer ) throws Exception {
70+ return new JsonFormatter (ctx , writer );
71+ }
72+ };
73+ }
74+
6275 public abstract static class ActionSerializer extends AbstractSerializer {
6376 final TreeContext context ;
6477 final MappingStore mappings ;
@@ -80,7 +93,7 @@ public void writeTo(Writer writer) throws Exception {
8093 ITree src = a .getNode ();
8194 if (a instanceof Move ) {
8295 ITree dst = mappings .getDst (src );
83- fmt .moveAction (src , dst );
96+ fmt .moveAction (src , dst , (( Move ) a ). getPosition () );
8497 break ;
8598 } else if (a instanceof Update ) {
8699 ITree dst = mappings .getDst (src );
@@ -102,11 +115,17 @@ public void writeTo(Writer writer) throws Exception {
102115
103116 interface ActionFormatter {
104117 void startActions () throws Exception ;
118+
105119 void insertRoot (ITree node ) throws Exception ;
120+
106121 void insertAction (ITree node , ITree parent , int index ) throws Exception ;
107- void moveAction (ITree src , ITree dst ) throws Exception ;
122+
123+ void moveAction (ITree src , ITree dst , int index ) throws Exception ;
124+
108125 void updateAction (ITree src , ITree dst ) throws Exception ;
126+
109127 void deleteAction (ITree node ) throws Exception ;
128+
110129 void endActions () throws Exception ;
111130 }
112131
@@ -142,16 +161,17 @@ public void insertAction(ITree node, ITree parent, int index) throws Exception {
142161 }
143162
144163 @ Override
145- public void moveAction (ITree src , ITree dst ) throws XMLStreamException {
164+ public void moveAction (ITree src , ITree dst , int index ) throws XMLStreamException {
146165 start (Move .class , src );
147- writer .writeAttribute ("to" , Integer .toString (dst .getId ()));
166+ writer .writeAttribute ("parent" , Integer .toString (dst .getId ()));
167+ writer .writeAttribute ("at" , Integer .toString (index ));
148168 end (src );
149169 }
150170
151171 @ Override
152172 public void updateAction (ITree src , ITree dst ) throws XMLStreamException {
153173 start (Update .class , src );
154- writer .writeAttribute ("to " , Integer . toString ( dst .getId () ));
174+ writer .writeAttribute ("label " , dst .getLabel ( ));
155175 end (src );
156176 }
157177
@@ -181,14 +201,16 @@ private void end(ITree node) throws XMLStreamException {
181201 static class TextFormatter implements ActionFormatter {
182202 final Writer writer ;
183203 final TreeContext context ;
204+
184205 public TextFormatter (TreeContext ctx , Writer writer ) {
185206 this .context = ctx ;
186207 this .writer = writer ;
187208 }
188209
189210
190211 @ Override
191- public void startActions () throws Exception { }
212+ public void startActions () throws Exception {
213+ }
192214
193215 @ Override
194216 public void insertRoot (ITree node ) throws Exception {
@@ -197,17 +219,17 @@ public void insertRoot(ITree node) throws Exception {
197219
198220 @ Override
199221 public void insertAction (ITree node , ITree parent , int index ) throws Exception {
200- write ("Insert %s -> %d %s" , _ (node ), index , _ (parent ));
222+ write ("Insert %s -> %d %s" , _ (node ), index , _ (parent ));
201223 }
202224
203225 @ Override
204- public void moveAction (ITree src , ITree dst ) throws Exception {
226+ public void moveAction (ITree src , ITree dst , int position ) throws Exception {
205227 write ("Move %s -> %s" , _ (src ), _ (dst ));
206228 }
207229
208230 @ Override
209231 public void updateAction (ITree src , ITree dst ) throws Exception {
210- write ("Move %s -> %s" , _ (src ), _ (dst ));
232+ write ("Move %s -> %s" , _ (src ), _ (dst ));
211233 }
212234
213235 @ Override
@@ -216,7 +238,8 @@ public void deleteAction(ITree node) throws Exception {
216238 }
217239
218240 @ Override
219- public void endActions () throws Exception { }
241+ public void endActions () throws Exception {
242+ }
220243
221244 private void write (String fmt , Object ... objs ) throws IOException {
222245 writer .append (String .format (fmt , objs ));
@@ -227,4 +250,67 @@ private String _(ITree node) {
227250 return String .format ("%s(%d)" , node .toPrettyString (context ), node .getId ());
228251 }
229252 }
253+
254+ static class JsonFormatter implements ActionFormatter {
255+ private final JsonWriter writer ;
256+
257+ JsonFormatter (TreeContext ctx , Writer writer ) {
258+ this .writer = new JsonWriter (writer );
259+ }
260+
261+ @ Override
262+ public void startActions () throws IOException {
263+ writer .beginArray ();
264+ }
265+
266+ @ Override
267+ public void insertRoot (ITree node ) throws IOException {
268+ start (Insert .class , node );
269+ end (node );
270+ }
271+
272+ @ Override
273+ public void insertAction (ITree node , ITree parent , int index ) throws IOException {
274+ start (Insert .class , node );
275+ writer .name ("parent" ).value (parent .getId ());
276+ writer .name ("at" ).value (index );
277+ end (node );
278+ }
279+
280+ @ Override
281+ public void moveAction (ITree src , ITree dst , int index ) throws IOException {
282+ start (Move .class , src );
283+ writer .name ("parent" ).value (dst .getId ());
284+ writer .name ("at" ).value (index );
285+ end (src );
286+ }
287+
288+ @ Override
289+ public void updateAction (ITree src , ITree dst ) throws IOException {
290+ start (Update .class , src );
291+ writer .name ("label" ).value (dst .getLabel ());
292+ end (src );
293+ }
294+
295+ @ Override
296+ public void deleteAction (ITree node ) throws IOException {
297+ start (Delete .class , node );
298+ end (node );
299+ }
300+
301+ private void start (Class <? extends Action > name , ITree src ) throws IOException {
302+ writer .beginObject ();
303+ writer .name ("action" ).value (name .getSimpleName ().toLowerCase ());
304+ writer .name ("tree" ).value (src .getId ());
305+ }
306+
307+ private void end (ITree node ) throws IOException {
308+ writer .endObject ();
309+ }
310+
311+ @ Override
312+ public void endActions () throws Exception {
313+ writer .endArray ();
314+ }
315+ }
230316}
0 commit comments