2 * Copyright 2005-2011 the original author or authors.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.wamblee.xmlrouter.impl;
18 import java.util.logging.Level;
19 import java.util.logging.Logger;
21 import javax.xml.transform.dom.DOMSource;
23 import org.wamblee.xml.XMLDocument;
24 import org.wamblee.xmlrouter.common.Id;
25 import org.wamblee.xmlrouter.config.Transformation;
28 * This class provides robustness towards externally provided transformations.
30 * @author Erik Brakkee
33 public class RobustTransformation implements Transformation {
35 private static final Logger LOGGER = Logger
36 .getLogger(RobustTransformation.class.getName());
38 private Id<Transformation> id;
39 private Transformation transformation;
42 * Constructs the wrapper.
46 * @param aTransformation
47 * Wrapped transformation.
49 public RobustTransformation(Id<Transformation> aId,
50 Transformation aTransformation) {
52 transformation = aTransformation;
56 public String getName() {
58 String name = transformation.getName();
60 logTypeReturnedNull("from");
61 return Constants.UNKNOWN_DOCUMENT_TYPE.toString();
64 } catch (Exception e) {
65 logNameThrewException(e);
66 return Constants.UNKNOWN_DOCUMENT_TYPE.toString();
71 public String getFromType() {
73 String from = transformation.getFromType();
75 logTypeReturnedNull("from");
76 return Constants.UNKNOWN_DOCUMENT_TYPE.toString();
79 } catch (Exception e) {
80 logTypeThrewException("from", e);
81 return Constants.UNKNOWN_DOCUMENT_TYPE.toString();
86 public String getToType() {
88 String to = transformation.getToType();
90 logTypeReturnedNull("to");
91 return Constants.UNKNOWN_DOCUMENT_TYPE.toString();
94 } catch (Exception e) {
95 logTypeThrewException("to", e);
96 return Constants.UNKNOWN_DOCUMENT_TYPE.toString();
101 public DOMSource transform(DOMSource aDocument) {
103 DOMSource res = transformation.transform(aDocument);
105 logTransformationReturnedNull(aDocument);
109 } catch (Exception e) {
110 logTranformationThrewException(aDocument, e);
115 private void logNameThrewException(Exception aE) {
116 LOGGER.log(Level.WARNING, "getName" +
117 " threw exception, returning default value " +
118 Constants.UNKNOWN_NAME, aE);
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);
127 private void logTypeReturnedNull(String aFromTo) {
128 LOGGER.log(Level.WARNING, "get" + aFromTo +
129 " returned null, returning default value " +
130 Constants.UNKNOWN_DOCUMENT_TYPE);
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);
139 private void logTransformationReturnedNull(DOMSource aEvent) {
140 LOGGER.log(Level.WARNING, "transformation " + id +
141 " returned null for event " + new XMLDocument(aEvent).print(true));