initial versions.
[xmlrouter] / impl / src / main / java / org / wamblee / xmlrouter / impl / RobustTransformation.java
1 package org.wamblee.xmlrouter.impl;
2
3 import java.util.logging.Level;
4 import java.util.logging.Logger;
5
6 import javax.xml.transform.dom.DOMSource;
7
8 import org.wamblee.xml.XMLDocument;
9 import org.wamblee.xmlrouter.common.Id;
10 import org.wamblee.xmlrouter.config.Transformation;
11
12 public class RobustTransformation implements Transformation {
13
14     private static final Logger LOGGER = Logger
15         .getLogger(RobustTransformation.class.getName());
16
17     private Id<Transformation> id;
18     private Transformation transformation;
19
20     public RobustTransformation(Id<Transformation> aId,
21         Transformation aTransformation) {
22         id = aId;
23         transformation = aTransformation;
24     }
25
26     @Override
27     public String getFromType() {
28         try {
29             String from = transformation.getFromType();
30             if (from == null) {
31                 logTypeReturnedNull("from");
32                 return Constants.UNKNOWN_DOCUMENT_TYPE.toString();
33             }
34             return from;
35         } catch (Exception e) {
36             logTypeThrewException("from", e);
37             return Constants.UNKNOWN_DOCUMENT_TYPE.toString();
38         }
39     }
40
41     @Override
42     public String getToType() {
43         try {
44             String to = transformation.getToType();
45             if (to == null) {
46                 logTypeReturnedNull("to");
47                 return Constants.UNKNOWN_DOCUMENT_TYPE.toString();
48             }
49             return to;
50         } catch (Exception e) {
51             logTypeThrewException("to", e);
52             return Constants.UNKNOWN_DOCUMENT_TYPE.toString();
53         }
54     }
55
56     @Override
57     public DOMSource transform(DOMSource aDocument) {
58         try {
59             DOMSource res = transformation.transform(aDocument);
60             if (res == null) {
61                 logTransformationReturnedNull(aDocument);
62                 return null;
63             }
64             return res;
65         } catch (Exception e) {
66             logTranformationThrewException(aDocument, e);
67             return null;
68         }
69     }
70
71     private void logTypeThrewException(String aFromTo, Exception aE) {
72         LOGGER.log(Level.WARNING, "get" + aFromTo +
73             " threw exception, returning default value " +
74             Constants.UNKNOWN_DOCUMENT_TYPE, aE);
75     }
76
77     private void logTypeReturnedNull(String aFromTo) {
78         LOGGER.log(Level.WARNING, "get" + aFromTo +
79             " returned null, returning default value " +
80             Constants.UNKNOWN_DOCUMENT_TYPE);
81     }
82
83     private void logTranformationThrewException(DOMSource aEvent, Exception aE) {
84         LOGGER.log(Level.WARNING,
85             "transformation " + id + " threw exception for event " +
86                 new XMLDocument(aEvent).print(true), aE);
87     }
88
89     private void logTransformationReturnedNull(DOMSource aEvent) {
90         LOGGER.log(Level.WARNING, "transformation " + id +
91             " returned null for event " + new XMLDocument(aEvent).print(true));
92     }
93
94 }