cf0210d3094045b85e5a9c4d36b1b0ce4475bf52
[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.config.Transformation;
25
26 /**
27  * This class provides robustness towards externally provided transformations.
28  * 
29  * @author Erik Brakkee
30  * 
31  */
32 public class RobustTransformation extends RobustIdentifiable<Transformation>
33     implements Transformation {
34
35     private static final Logger LOGGER = Logger
36         .getLogger(RobustTransformation.class.getName());
37
38     private Transformation transformation;
39
40     /**
41      * Constructs the wrapper.
42      * 
43      * @param aId
44      *            Unique id.
45      * @param aTransformation
46      *            Wrapped transformation.
47      */
48     public RobustTransformation(String aPrefix, Transformation aTransformation) {
49         super(aPrefix, aTransformation);
50         transformation = aTransformation;
51     }
52
53     @Override
54     public String getFromType() {
55         try {
56             String from = transformation.getFromType();
57             if (from == null) {
58                 logTypeReturnedNull("from");
59                 return Constants.UNKNOWN_DOCUMENT_TYPE.toString();
60             }
61             return from;
62         } catch (Exception e) {
63             logTypeThrewException("from", e);
64             return Constants.UNKNOWN_DOCUMENT_TYPE.toString();
65         }
66     }
67
68     @Override
69     public String getToType() {
70         try {
71             String to = transformation.getToType();
72             if (to == null) {
73                 logTypeReturnedNull("to");
74                 return Constants.UNKNOWN_DOCUMENT_TYPE.toString();
75             }
76             return to;
77         } catch (Exception e) {
78             logTypeThrewException("to", e);
79             return Constants.UNKNOWN_DOCUMENT_TYPE.toString();
80         }
81     }
82
83     @Override
84     public DOMSource transform(DOMSource aDocument) {
85         try {
86             DOMSource res = transformation.transform(aDocument);
87             if (res == null) {
88                 logTransformationReturnedNull(aDocument);
89                 return null;
90             }
91             return res;
92         } catch (Exception e) {
93             logTranformationThrewException(aDocument, e);
94             return null;
95         }
96     }
97
98     private void logNameThrewException(Exception aE) {
99         LOGGER.log(Level.WARNING, "getName" +
100             " threw exception, returning default value " +
101             Constants.UNKNOWN_NAME, aE);
102     }
103
104     private void logTypeThrewException(String aFromTo, Exception aE) {
105         LOGGER.log(Level.WARNING, "get" + aFromTo +
106             " threw exception, returning default value " +
107             Constants.UNKNOWN_DOCUMENT_TYPE, aE);
108     }
109
110     private void logTypeReturnedNull(String aFromTo) {
111         LOGGER.log(Level.WARNING, "get" + aFromTo +
112             " returned null, returning default value " +
113             Constants.UNKNOWN_DOCUMENT_TYPE);
114     }
115
116     private void logTranformationThrewException(DOMSource aEvent, Exception aE) {
117         LOGGER.log(Level.WARNING,
118             "transformation " + getId() + " threw exception for event " +
119                 new XMLDocument(aEvent).print(true), aE);
120     }
121
122     private void logTransformationReturnedNull(DOMSource aEvent) {
123         LOGGER.log(Level.WARNING, "transformation " + getId() +
124             " returned null for event " + new XMLDocument(aEvent).print(true));
125     }
126
127 }