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;
27 public class RobustTransformation implements Transformation {
29 private static final Logger LOGGER = Logger
30 .getLogger(RobustTransformation.class.getName());
32 private Id<Transformation> id;
33 private Transformation transformation;
35 public RobustTransformation(Id<Transformation> aId,
36 Transformation aTransformation) {
38 transformation = aTransformation;
42 public String getName() {
44 String name = transformation.getName();
46 logTypeReturnedNull("from");
47 return Constants.UNKNOWN_DOCUMENT_TYPE.toString();
50 } catch (Exception e) {
51 logNameThrewException(e);
52 return Constants.UNKNOWN_DOCUMENT_TYPE.toString();
57 public String getFromType() {
59 String from = transformation.getFromType();
61 logTypeReturnedNull("from");
62 return Constants.UNKNOWN_DOCUMENT_TYPE.toString();
65 } catch (Exception e) {
66 logTypeThrewException("from", e);
67 return Constants.UNKNOWN_DOCUMENT_TYPE.toString();
72 public String getToType() {
74 String to = transformation.getToType();
76 logTypeReturnedNull("to");
77 return Constants.UNKNOWN_DOCUMENT_TYPE.toString();
80 } catch (Exception e) {
81 logTypeThrewException("to", e);
82 return Constants.UNKNOWN_DOCUMENT_TYPE.toString();
87 public DOMSource transform(DOMSource aDocument) {
89 DOMSource res = transformation.transform(aDocument);
91 logTransformationReturnedNull(aDocument);
95 } catch (Exception e) {
96 logTranformationThrewException(aDocument, e);
101 private void logNameThrewException(Exception aE) {
102 LOGGER.log(Level.WARNING, "getName" +
103 " threw exception, returning default value " +
104 Constants.UNKNOWN_NAME, aE);
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);
113 private void logTypeReturnedNull(String aFromTo) {
114 LOGGER.log(Level.WARNING, "get" + aFromTo +
115 " returned null, returning default value " +
116 Constants.UNKNOWN_DOCUMENT_TYPE);
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);
125 private void logTransformationReturnedNull(DOMSource aEvent) {
126 LOGGER.log(Level.WARNING, "transformation " + id +
127 " returned null for event " + new XMLDocument(aEvent).print(true));