initial versions.
[xmlrouter] / impl / src / main / java / org / wamblee / xmlrouter / impl / RobustTransformation.java
diff --git a/impl/src/main/java/org/wamblee/xmlrouter/impl/RobustTransformation.java b/impl/src/main/java/org/wamblee/xmlrouter/impl/RobustTransformation.java
new file mode 100644 (file)
index 0000000..fb028ae
--- /dev/null
@@ -0,0 +1,94 @@
+package org.wamblee.xmlrouter.impl;
+
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import javax.xml.transform.dom.DOMSource;
+
+import org.wamblee.xml.XMLDocument;
+import org.wamblee.xmlrouter.common.Id;
+import org.wamblee.xmlrouter.config.Transformation;
+
+public class RobustTransformation implements Transformation {
+
+    private static final Logger LOGGER = Logger
+        .getLogger(RobustTransformation.class.getName());
+
+    private Id<Transformation> id;
+    private Transformation transformation;
+
+    public RobustTransformation(Id<Transformation> aId,
+        Transformation aTransformation) {
+        id = aId;
+        transformation = aTransformation;
+    }
+
+    @Override
+    public String getFromType() {
+        try {
+            String from = transformation.getFromType();
+            if (from == null) {
+                logTypeReturnedNull("from");
+                return Constants.UNKNOWN_DOCUMENT_TYPE.toString();
+            }
+            return from;
+        } catch (Exception e) {
+            logTypeThrewException("from", e);
+            return Constants.UNKNOWN_DOCUMENT_TYPE.toString();
+        }
+    }
+
+    @Override
+    public String getToType() {
+        try {
+            String to = transformation.getToType();
+            if (to == null) {
+                logTypeReturnedNull("to");
+                return Constants.UNKNOWN_DOCUMENT_TYPE.toString();
+            }
+            return to;
+        } catch (Exception e) {
+            logTypeThrewException("to", e);
+            return Constants.UNKNOWN_DOCUMENT_TYPE.toString();
+        }
+    }
+
+    @Override
+    public DOMSource transform(DOMSource aDocument) {
+        try {
+            DOMSource res = transformation.transform(aDocument);
+            if (res == null) {
+                logTransformationReturnedNull(aDocument);
+                return null;
+            }
+            return res;
+        } catch (Exception e) {
+            logTranformationThrewException(aDocument, e);
+            return null;
+        }
+    }
+
+    private void logTypeThrewException(String aFromTo, Exception aE) {
+        LOGGER.log(Level.WARNING, "get" + aFromTo +
+            " threw exception, returning default value " +
+            Constants.UNKNOWN_DOCUMENT_TYPE, aE);
+    }
+
+    private void logTypeReturnedNull(String aFromTo) {
+        LOGGER.log(Level.WARNING, "get" + aFromTo +
+            " returned null, returning default value " +
+            Constants.UNKNOWN_DOCUMENT_TYPE);
+    }
+
+    private void logTranformationThrewException(DOMSource aEvent, Exception aE) {
+        LOGGER.log(Level.WARNING,
+            "transformation " + id + " threw exception for event " +
+                new XMLDocument(aEvent).print(true), aE);
+    }
+
+    private void logTransformationReturnedNull(DOMSource aEvent) {
+        LOGGER.log(Level.WARNING, "transformation " + id +
+            " returned null for event " + new XMLDocument(aEvent).print(true));
+    }
+
+}