001/*
002 * Copyright 2002-2016 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.oxm;
018
019import java.io.IOException;
020
021import javax.xml.transform.Result;
022
023/**
024 * Defines the contract for Object XML Mapping Marshallers. Implementations of this interface
025 * can serialize a given Object to an XML Stream.
026 *
027 * <p>Although the {@code marshal} method accepts a {@code java.lang.Object} as its
028 * first parameter, most {@code Marshaller} implementations cannot handle arbitrary
029 * {@code Object}s. Instead, a object class must be registered with the marshaller,
030 * or have a common base class.
031 *
032 * @author Arjen Poutsma
033 * @since 3.0
034 * @see Unmarshaller
035 */
036public interface Marshaller {
037
038        /**
039         * Indicate whether this marshaller can marshal instances of the supplied type.
040         * @param clazz the class that this marshaller is being asked if it can marshal
041         * @return {@code true} if this marshaller can indeed marshal instances of the supplied class;
042         * {@code false} otherwise
043         */
044        boolean supports(Class<?> clazz);
045
046        /**
047         * Marshal the object graph with the given root into the provided {@link Result}.
048         * @param graph the root of the object graph to marshal
049         * @param result the result to marshal to
050         * @throws IOException if an I/O error occurs
051         * @throws XmlMappingException if the given object cannot be marshalled to the result
052         */
053        void marshal(Object graph, Result result) throws IOException, XmlMappingException;
054
055}