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.config.Transformation;
27 * This class provides robustness towards externally provided transformations.
29 * @author Erik Brakkee
32 public class RobustTransformation extends RobustIdentifiable<Transformation>
33 implements Transformation {
35 private static final Logger LOGGER = Logger
36 .getLogger(RobustTransformation.class.getName());
38 private Transformation transformation;
41 * Constructs the wrapper.
45 * @param aTransformation
46 * Wrapped transformation.
48 public RobustTransformation(String aPrefix, Transformation aTransformation) {
49 super(aPrefix, aTransformation);
50 transformation = aTransformation;
54 public String getFromType() {
56 String from = transformation.getFromType();
58 logTypeReturnedNull("from");
59 return Constants.UNKNOWN_DOCUMENT_TYPE.toString();
62 } catch (Exception e) {
63 logTypeThrewException("from", e);
64 return Constants.UNKNOWN_DOCUMENT_TYPE.toString();
69 public String getToType() {
71 String to = transformation.getToType();
73 logTypeReturnedNull("to");
74 return Constants.UNKNOWN_DOCUMENT_TYPE.toString();
77 } catch (Exception e) {
78 logTypeThrewException("to", e);
79 return Constants.UNKNOWN_DOCUMENT_TYPE.toString();
84 public DOMSource transform(DOMSource aDocument) {
86 DOMSource res = transformation.transform(aDocument);
88 logTransformationReturnedNull(aDocument);
92 } catch (Exception e) {
93 logTranformationThrewException(aDocument, e);
98 private void logNameThrewException(Exception aE) {
99 LOGGER.log(Level.WARNING, "getName" +
100 " threw exception, returning default value " +
101 Constants.UNKNOWN_NAME, aE);
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);
110 private void logTypeReturnedNull(String aFromTo) {
111 LOGGER.log(Level.WARNING, "get" + aFromTo +
112 " returned null, returning default value " +
113 Constants.UNKNOWN_DOCUMENT_TYPE);
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);
122 private void logTransformationReturnedNull(DOMSource aEvent) {
123 LOGGER.log(Level.WARNING, "transformation " + getId() +
124 " returned null for event " + new XMLDocument(aEvent).print(true));