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 * TidyMarkup data format is used for parsing HTML and return it as pretty well-formed HTML.
036 */
037@Metadata(firstVersion = "2.0.0", label = "dataformat,transformation", title = "TidyMarkup")
038@XmlRootElement(name = "tidyMarkup")
039@XmlAccessorType(XmlAccessType.FIELD)
040public class TidyMarkupDataFormat extends DataFormatDefinition {
041    @XmlAttribute(name = "dataObjectType") @Metadata(defaultValue = "org.w3c.dom.Node")
042    private String dataObjectTypeName;
043    @XmlAttribute
044    private Boolean omitXmlDeclaration;
045    @XmlTransient
046    private Class<?> dataObjectType;
047
048    public TidyMarkupDataFormat() {
049        super("tidyMarkup");
050        this.setDataObjectType(Node.class);
051    }
052
053    public TidyMarkupDataFormat(Class<?> dataObjectType) {
054        this();
055        if (!dataObjectType.isAssignableFrom(String.class) && !dataObjectType.isAssignableFrom(Node.class)) {
056            throw new IllegalArgumentException("TidyMarkupDataFormat only supports returning a String or a org.w3c.dom.Node object");
057        }
058        this.setDataObjectType(dataObjectType);
059    }
060
061    /**
062     * What data type to unmarshal as, can either be org.w3c.dom.Node or java.lang.String.
063     * <p/>
064     * Is by default org.w3c.dom.Node
065     */
066    public void setDataObjectType(Class<?> dataObjectType) {
067        this.dataObjectType = dataObjectType;
068    }
069
070    public Class<?> getDataObjectType() {
071        return dataObjectType;
072    }
073
074    public String getDataObjectTypeName() {
075        return dataObjectTypeName;
076    }
077
078    /**
079     * What data type to unmarshal as, can either be org.w3c.dom.Node or java.lang.String.
080     * <p/>
081     * Is by default org.w3c.dom.Node
082     */
083    public void setDataObjectTypeName(String dataObjectTypeName) {
084        this.dataObjectTypeName = dataObjectTypeName;
085    }
086
087    public Boolean getOmitXmlDeclaration() {
088        return omitXmlDeclaration;
089    }
090
091    /**
092     * When returning a String, do we omit the XML declaration in the top.
093     */
094    public void setOmitXmlDeclaration(Boolean omitXmlDeclaration) {
095        this.omitXmlDeclaration = omitXmlDeclaration;
096    }
097
098    @Override
099    protected DataFormat createDataFormat(RouteContext routeContext) {
100        if (dataObjectType == null && dataObjectTypeName != null) {
101            try {
102                dataObjectType = routeContext.getCamelContext().getClassResolver().resolveMandatoryClass(dataObjectTypeName);
103            } catch (ClassNotFoundException e) {
104                throw ObjectHelper.wrapRuntimeCamelException(e);
105            }
106        }
107
108        return super.createDataFormat(routeContext);
109    }
110
111    @Override
112    protected void configureDataFormat(DataFormat dataFormat, CamelContext camelContext) {
113        if (dataObjectType != null) {
114            setProperty(camelContext, dataFormat, "dataObjectType", dataObjectType);
115        }
116        if (omitXmlDeclaration != null) {
117            setProperty(camelContext, dataFormat, "omitXmlDeclaration", omitXmlDeclaration);
118        }
119    }
120
121}