c054f90d0eb84f3382ef53e698ac1f2e2707083c
[xmlrouter] / impl / src / main / java / org / wamblee / xmlrouter / impl / TransformationPath.java
1 package org.wamblee.xmlrouter.impl;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import org.wamblee.xmlrouter.config.Transformation;
7
8 public class TransformationPath {
9
10     private List<Transformation> transformations;
11
12     public TransformationPath() {
13         transformations = new ArrayList<Transformation>();
14     }
15
16     public TransformationPath(Transformation aTransformation) {
17         this();
18         transformations.add(aTransformation);
19     }
20
21     public TransformationPath(List<Transformation> aTransformations) {
22         this();
23         transformations.addAll(aTransformations);
24     }
25
26     public TransformationPath appendPath(TransformationPath aSequence) {
27         if (transformations.isEmpty()) {
28             return new TransformationPath(aSequence.transformations);
29         }
30         if (aSequence.transformations.isEmpty()) {
31             // nothing to append
32             return new TransformationPath(transformations);
33         }
34         // both are non-empty
35         if (!getToType().equals(aSequence.getFromType())) {
36             throw new RuntimeException(
37                 "error concatening sequence, destination of first differs from source of second");
38         }
39         List<Transformation> t = new ArrayList<Transformation>(transformations);
40         t.addAll(aSequence.transformations);
41         return new TransformationPath(t);
42     }
43
44     public int size() {
45         return transformations.size();
46     }
47
48     public String getFromType() {
49         if (transformations.isEmpty()) {
50             return null;
51         }
52         return transformations.get(0).getFromType();
53     }
54
55     public String getToType() {
56         if (transformations.isEmpty()) {
57             return null;
58         }
59         return transformations.get(transformations.size() - 1).getToType();
60     }
61
62     public List<Transformation> getTransformations() {
63         return transformations;
64     }
65
66     @Override
67     public String toString() {
68         StringBuffer buf = new StringBuffer();
69         buf.append("TransformationPath(");
70         for (int i = 0; i < transformations.size(); i++) {
71             if (i > 0) {
72                 buf.append(", ");
73             }
74             buf.append(transformations.get(i).toString());
75         }
76         buf.append(")");
77         return buf.toString();
78     }
79 }