001/* 002 * Strategy.java July 2006 003 * 004 * Copyright (C) 2006, Niall Gallagher <niallg@users.sf.net> 005 * 006 * Licensed under the Apache License, Version 2.0 (the "License"); 007 * you may not use this file except in compliance with the License. 008 * You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 015 * implied. See the License for the specific language governing 016 * permissions and limitations under the License. 017 */ 018 019package org.simpleframework.xml.strategy; 020 021import java.util.Map; 022 023import org.simpleframework.xml.stream.InputNode; 024import org.simpleframework.xml.stream.NodeMap; 025import org.simpleframework.xml.stream.OutputNode; 026 027/** 028 * The <code>Strategy</code> interface represents a strategy that can be 029 * used to resolve and load the <code>Class</code> objects that compose 030 * a serializable object. A strategy implementation will make use of the 031 * provided attribute node map to extract details that can be used to 032 * determine what type of object must be used. 033 * <pre> 034 * 035 * <xml version="1.0"> 036 * <example class="some.example.Demo"> 037 * <integer>2</integer> 038 * </example> 039 * 040 * </pre> 041 * The above example shows how the default strategy augments elements 042 * with "class" attributes that describe the type that should be used 043 * to instantiate a field when an object is deserialized. So looking at 044 * the above example the root element would be a "some.example.Demo". 045 * <p> 046 * Custom <code>Strategy</code> implementations give the persister a 047 * chance to intercept the class loading and type resolution for XML 048 * documents. It also opens up the possibility for class versioning. 049 * To establish contextual information a <code>Map</code> object can be 050 * used. The map object is a transient object that is created and used 051 * for the duration of a single operation of the persister. 052 * 053 * @author Niall Gallagher 054 * 055 * @see org.simpleframework.xml.core.Persister 056 */ 057public interface Strategy { 058 059 /** 060 * This is used to resolve and load a class for the given element. 061 * The class should be of the same type or a subclass of the class 062 * specified. It can be resolved using the details within the 063 * provided XML node map, if the details used do not represent any 064 * serializable values they should be removed so as not to disrupt 065 * the deserialization process. For example the default strategy 066 * removes all "class" attributes from the given node map. 067 * 068 * @param type this is the type of the root element expected 069 * @param node this is the node map used to resolve an override 070 * @param map this is used to maintain contextual information 071 * 072 * @return the value that should be used to describe the instance 073 * 074 * @throws Exception thrown if the class cannot be resolved 075 */ 076 Value read(Type type, NodeMap<InputNode> node, Map map) throws Exception; 077 078 /** 079 * This is used to attach attribute values to the given node 080 * map during the serialization process. This method allows 081 * the strategy to augment the XML document so that it can be 082 * deserialized using a similar strategy. For example the 083 * default strategy adds a "class" attribute to the node map. 084 * 085 * @param type this is the declared class for the field used 086 * @param value this is the instance variable being serialized 087 * @param node this is the node map used to represent the value 088 * @param map this is used to maintain contextual information 089 * 090 * @return this returns true if serialization is complete 091 * 092 * @throws Exception thrown if the details cannot be set 093 */ 094 boolean write(Type type, Object value, NodeMap<OutputNode> node, Map map) throws Exception; 095 096}