refactoring of the config interface towards more reuse in the implementation and...
[xmlrouter] / impl / src / main / java / org / wamblee / xmlrouter / impl / RobustTransformation.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.logging.Level;
19 import java.util.logging.Logger;
20
21 import javax.xml.transform.dom.DOMSource;
22
23 import org.wamblee.xml.XMLDocument;
24 import org.wamblee.xmlrouter.common.Id;
25 import org.wamblee.xmlrouter.config.Transformation;
26
27 /**
28  * This class provides robustness towards externally provided transformations.
29  * 
30  * @author Erik Brakkee
31  * 
32  */
33 public class RobustTransformation implements Transformation {
34
35     private static final Logger LOGGER = Logger
36         .getLogger(RobustTransformation.class.getName());
37
38     private Id<Transformation> id;
39     private Transformation transformation;
40
41     /**
42      * Constructs the wrapper.
43      * 
44      * @param aId
45      *            Unique id.
46      * @param aTransformation
47      *            Wrapped transformation.
48      */
49     public RobustTransformation(Id<Transformation> aId,
50         Transformation aTransformation) {
51         id = aId;
52         transformation = aTransformation;
53     }
54
55     @Override
56     public String getName() {
57         try {
58             String name = transformation.getName();
59             if (name == null) {
60                 logTypeReturnedNull("from");
61                 return Constants.UNKNOWN_DOCUMENT_TYPE.toString();
62             }
63             return name;
64         } catch (Exception e) {
65             logNameThrewException(e);
66             return Constants.UNKNOWN_DOCUMENT_TYPE.toString();
67         }
68     }
69
70     @Override
71     public String getFromType() {
72         try {
73             String from = transformation.getFromType();
74             if (from == null) {
75                 logTypeReturnedNull("from");
76                 return Constants.UNKNOWN_DOCUMENT_TYPE.toString();
77             }
78             return from;
79         } catch (Exception e) {
80             logTypeThrewException("from", e);
81             return Constants.UNKNOWN_DOCUMENT_TYPE.toString();
82         }
83     }
84
85     @Override
86     public String getToType() {
87         try {
88             String to = transformation.getToType();
89             if (to == null) {
90                 logTypeReturnedNull("to");
91                 return Constants.UNKNOWN_DOCUMENT_TYPE.toString();
92             }
93             return to;
94         } catch (Exception e) {
95             logTypeThrewException("to", e);
96             return Constants.UNKNOWN_DOCUMENT_TYPE.toString();
97         }
98     }
99
100     @Override
101     public DOMSource transform(DOMSource aDocument) {
102         try {
103             DOMSource res = transformation.transform(aDocument);
104             if (res == null) {
105                 logTransformationReturnedNull(aDocument);
106                 return null;
107             }
108             return res;
109         } catch (Exception e) {
110             logTranformationThrewException(aDocument, e);
111             return null;
112         }
113     }
114
115     private void logNameThrewException(Exception aE) {
116         LOGGER.log(Level.WARNING, "getName" +
117             " threw exception, returning default value " +
118             Constants.UNKNOWN_NAME, aE);
119     }
120
121     private void logTypeThrewException(String aFromTo, Exception aE) {
122         LOGGER.log(Level.WARNING, "get" + aFromTo +
123             " threw exception, returning default value " +
124             Constants.UNKNOWN_DOCUMENT_TYPE, aE);
125     }
126
127     private void logTypeReturnedNull(String aFromTo) {
128         LOGGER.log(Level.WARNING, "get" + aFromTo +
129             " returned null, returning default value " +
130             Constants.UNKNOWN_DOCUMENT_TYPE);
131     }
132
133     private void logTranformationThrewException(DOMSource aEvent, Exception aE) {
134         LOGGER.log(Level.WARNING,
135             "transformation " + id + " threw exception for event " +
136                 new XMLDocument(aEvent).print(true), aE);
137     }
138
139     private void logTransformationReturnedNull(DOMSource aEvent) {
140         LOGGER.log(Level.WARNING, "transformation " + id +
141             " returned null for event " + new XMLDocument(aEvent).print(true));
142     }
143
144 }