001/** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.camel.model.dataformat; 018 019import javax.xml.bind.annotation.XmlAccessType; 020import javax.xml.bind.annotation.XmlAccessorType; 021import javax.xml.bind.annotation.XmlAttribute; 022import javax.xml.bind.annotation.XmlRootElement; 023import javax.xml.bind.annotation.XmlTransient; 024 025import org.w3c.dom.Node; 026 027import org.apache.camel.CamelContext; 028import org.apache.camel.model.DataFormatDefinition; 029import org.apache.camel.spi.DataFormat; 030import org.apache.camel.spi.Metadata; 031import org.apache.camel.spi.RouteContext; 032import org.apache.camel.util.ObjectHelper; 033 034/** 035 * Tidymark (wellformed HTML) data format 036 */ 037@Metadata(label = "dataformat,transformation", title = "TidyMarkup") 038@XmlRootElement(name = "tidyMarkup") 039@XmlAccessorType(XmlAccessType.FIELD) 040public class TidyMarkupDataFormat extends DataFormatDefinition { 041 @XmlAttribute(name = "dataObjectType") 042 private String dataObjectTypeName; 043 @XmlTransient 044 private Class<?> dataObjectType; 045 046 public TidyMarkupDataFormat() { 047 super("tidyMarkup"); 048 this.setDataObjectType(Node.class); 049 } 050 051 public TidyMarkupDataFormat(Class<?> dataObjectType) { 052 this(); 053 if (!dataObjectType.isAssignableFrom(String.class) && !dataObjectType.isAssignableFrom(Node.class)) { 054 throw new IllegalArgumentException("TidyMarkupDataFormat only supports returning a String or a org.w3c.dom.Node object"); 055 } 056 this.setDataObjectType(dataObjectType); 057 } 058 059 /** 060 * What data type to unmarshal as, can either be org.w3c.dom.Node or java.lang.String. 061 * <p/> 062 * Is by default org.w3c.dom.Node 063 */ 064 public void setDataObjectType(Class<?> dataObjectType) { 065 this.dataObjectType = dataObjectType; 066 } 067 068 public Class<?> getDataObjectType() { 069 return dataObjectType; 070 } 071 072 public String getDataObjectTypeName() { 073 return dataObjectTypeName; 074 } 075 076 /** 077 * What data type to unmarshal as, can either be org.w3c.dom.Node or java.lang.String. 078 * <p/> 079 * Is by default org.w3c.dom.Node 080 */ 081 public void setDataObjectTypeName(String dataObjectTypeName) { 082 this.dataObjectTypeName = dataObjectTypeName; 083 } 084 085 @Override 086 protected DataFormat createDataFormat(RouteContext routeContext) { 087 if (dataObjectType == null && dataObjectTypeName != null) { 088 try { 089 dataObjectType = routeContext.getCamelContext().getClassResolver().resolveMandatoryClass(dataObjectTypeName); 090 } catch (ClassNotFoundException e) { 091 throw ObjectHelper.wrapRuntimeCamelException(e); 092 } 093 } 094 095 return super.createDataFormat(routeContext); 096 } 097 098 @Override 099 protected void configureDataFormat(DataFormat dataFormat, CamelContext camelContext) { 100 if (dataObjectType != null) { 101 setProperty(camelContext, dataFormat, "dataObjectType", dataObjectType); 102 } 103 } 104 105}