359b4b4647ae4e4be02c439692b1d6c29917c59d
[xmlrouter] / impl / src / main / java / org / wamblee / xmlrouter / impl / TransformationPath.java
1 /*
2  * Copyright 2005-2011 the original author or authors.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */ 
16 package org.wamblee.xmlrouter.impl;
17
18 import java.util.ArrayList;
19 import java.util.List;
20
21 import org.wamblee.xmlrouter.config.Transformation;
22
23 public class TransformationPath {
24
25     private List<Transformation> transformations;
26
27     public TransformationPath() {
28         transformations = new ArrayList<Transformation>();
29     }
30
31     public TransformationPath(Transformation aTransformation) {
32         this();
33         transformations.add(aTransformation);
34     }
35
36     public TransformationPath(List<Transformation> aTransformations) {
37         this();
38         transformations.addAll(aTransformations);
39     }
40
41     public TransformationPath appendPath(TransformationPath aSequence) {
42         if (transformations.isEmpty()) {
43             return new TransformationPath(aSequence.transformations);
44         }
45         if (aSequence.transformations.isEmpty()) {
46             // nothing to append
47             return new TransformationPath(transformations);
48         }
49         // both are non-empty
50         if (!getToType().equals(aSequence.getFromType())) {
51             throw new RuntimeException(
52                 "error concatening sequence, destination of first differs from source of second");
53         }
54         List<Transformation> t = new ArrayList<Transformation>(transformations);
55         t.addAll(aSequence.transformations);
56         return new TransformationPath(t);
57     }
58
59     public int size() {
60         return transformations.size();
61     }
62
63     public String getFromType() {
64         if (transformations.isEmpty()) {
65             return null;
66         }
67         return transformations.get(0).getFromType();
68     }
69
70     public String getToType() {
71         if (transformations.isEmpty()) {
72             return null;
73         }
74         return transformations.get(transformations.size() - 1).getToType();
75     }
76
77     public List<Transformation> getTransformations() {
78         return transformations;
79     }
80
81     @Override
82     public String toString() {
83         StringBuffer buf = new StringBuffer();
84         buf.append("TransformationPath(");
85         for (int i = 0; i < transformations.size(); i++) {
86             if (i > 0) {
87                 buf.append(", ");
88             }
89             buf.append(transformations.get(i).toString());
90         }
91         buf.append(")");
92         return buf.toString();
93     }
94 }