Restructured the project creating a router directory for the router bundle implementa...
[xmlrouter] / router / 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 /**
24  * Transformation path from a source document to a destination.
25  * 
26  * @author Erik Brakkee
27  * 
28  */
29 public class TransformationPath {
30
31     private List<Transformation> transformations;
32
33     /**
34      * Constructs empty path.
35      */
36     public TransformationPath() {
37         transformations = new ArrayList<Transformation>();
38     }
39
40     /**
41      * Constructs path with single transformation.
42      * 
43      * @param aTransformation
44      *            Single transformation.
45      */
46     public TransformationPath(Transformation aTransformation) {
47         this();
48         transformations.add(aTransformation);
49     }
50
51     /**
52      * Constructs path with list of transformations.
53      * 
54      * @param aTransformations
55      *            List of transformations.
56      */
57     public TransformationPath(List<Transformation> aTransformations) {
58         this();
59         transformations.addAll(aTransformations);
60     }
61
62     /**
63      * Appends a transormation path to the current path.
64      * 
65      * @param aSequence
66      *            Transformation sequence to append.
67      * @return Appended transformations equence.
68      * @throws Runtime
69      *             exception if the appended transformation would not be valid.
70      */
71     public TransformationPath appendPath(TransformationPath aSequence) {
72         if (transformations.isEmpty()) {
73             return new TransformationPath(aSequence.transformations);
74         }
75         if (aSequence.transformations.isEmpty()) {
76             // nothing to append
77             return new TransformationPath(transformations);
78         }
79         // both are non-empty
80         if (!getToType().equals(aSequence.getFromType())) {
81             throw new RuntimeException(
82                 "error concatening sequence, destination of first differs from source of second");
83         }
84         List<Transformation> t = new ArrayList<Transformation>(transformations);
85         t.addAll(aSequence.transformations);
86         return new TransformationPath(t);
87     }
88
89     /**
90      * @return Number of transformations.
91      */
92     public int size() {
93         return transformations.size();
94     }
95
96     /**
97      * @return From type of the path or null if the sequence is empty.
98      */
99     public String getFromType() {
100         if (transformations.isEmpty()) {
101             return null;
102         }
103         return transformations.get(0).getFromType();
104     }
105
106     /**
107      * @return To type of the path or null if the sequence is empty.
108      */
109     public String getToType() {
110         if (transformations.isEmpty()) {
111             return null;
112         }
113         return transformations.get(transformations.size() - 1).getToType();
114     }
115
116     /**
117      * @return The transformations.
118      */
119     public List<Transformation> getTransformations() {
120         return transformations;
121     }
122
123     @Override
124     public String toString() {
125         StringBuffer buf = new StringBuffer();
126         buf.append("TransformationPath(");
127         for (int i = 0; i < transformations.size(); i++) {
128             if (i > 0) {
129                 buf.append(", ");
130             }
131             buf.append(transformations.get(i).toString());
132         }
133         buf.append(")");
134         return buf.toString();
135     }
136 }