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