source: trunk/eraser/Eraser.DefaultPlugins/ErasureTargets/ErasureTargetBase.cs @ 2613

Revision 2613, 4.7 KB checked in by lowjoel, 3 years ago (diff)

We should error out if we call the ErasureTargetBase?.ReadXml? class directly.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Rev URL
Line 
1/*
2 * $Id$
3 * Copyright 2008-2012 The Eraser Project
4 * Original Author: Joel Low <lowjoel@users.sourceforge.net>
5 * Modified By:
6 *
7 * This file is part of Eraser.
8 *
9 * Eraser is free software: you can redistribute it and/or modify it under the
10 * terms of the GNU General Public License as published by the Free Software
11 * Foundation, either version 3 of the License, or (at your option) any later
12 * version.
13 *
14 * Eraser is distributed in the hope that it will be useful, but WITHOUT ANY
15 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 *
18 * A copy of the GNU General Public License can be found at
19 * <http://www.gnu.org/licenses/>.
20 */
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using System.Runtime.Serialization;
27using System.Xml;
28using System.Xml.Serialization;
29using System.Security.Permissions;
30
31using Eraser.Util;
32using Eraser.Plugins;
33using Eraser.Plugins.Registrars;
34using Eraser.Plugins.ExtensionPoints;
35
36namespace Eraser.DefaultPlugins
37{
38    public abstract class ErasureTargetBase : IErasureTarget
39    {
40        #region IErasureTarget Members
41
42        public abstract string Name
43        {
44            get;
45        }
46
47        public ITask Task
48        {
49            get;
50            set;
51        }
52
53        public virtual bool SupportsMethod(IErasureMethod method)
54        {
55            return true;
56        }
57
58        public abstract IErasureTargetConfigurer Configurer
59        {
60            get;
61        }
62
63        public abstract void Execute();
64
65        public SteppedProgressManager Progress
66        {
67            get;
68            protected set;
69        }
70        #endregion
71
72        #region Serialization code
73        protected ErasureTargetBase(SerializationInfo info, StreamingContext context)
74        {
75            Guid methodGuid = (Guid)info.GetValue("Method", typeof(Guid));
76            if (methodGuid == Guid.Empty)
77                Method = ErasureMethodRegistrar.Default;
78            else
79                Method = Host.Instance.ErasureMethods[methodGuid];
80        }
81
82        [SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]
83        public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
84        {
85            info.AddValue("Method", Method.Guid);
86        }
87
88        public System.Xml.Schema.XmlSchema GetSchema()
89        {
90            return null;
91        }
92
93        /// <summary>
94        /// Reads the XML for the current Erasure Target.
95        /// </summary>
96        /// <param name="reader">The XML Reader containing the element to deserialise.</param>
97        /// <param name="advance">Whether to advance the element pointer. Set to false if
98        /// a derived class will be doing further processing on the element.</param>
99        /// <remarks>When inheriting and overriding this method, call the base method with
100        /// <paramref name="advance"/> set to false.</remarks>
101        protected virtual void ReadXml(XmlReader reader, bool advance)
102        {
103            Guid methodGuid = Guid.Empty;
104            if (reader.HasAttributes)
105            {
106                string method = reader.GetAttribute("method");
107                if (method != null)
108                    methodGuid = new Guid(method);
109            }
110
111            if (methodGuid == Guid.Empty)
112                Method = ErasureMethodRegistrar.Default;
113            else
114                Method = Host.Instance.ErasureMethods[methodGuid];
115
116            if (advance)
117                throw new NotImplementedException("ErasureTargetBase.ReadXml must be " +
118                    "implemented by a derived class.");
119        }
120
121        public void ReadXml(XmlReader reader)
122        {
123            ReadXml(reader, true);
124        }
125
126        public virtual void WriteXml(XmlWriter writer)
127        {
128            if (method != ErasureMethodRegistrar.Default)
129                writer.WriteAttributeString("method", method.Guid.ToString());
130        }
131        #endregion
132
133        #region IRegisterable Members
134
135        public abstract Guid Guid
136        {
137            get;
138        }
139
140        #endregion
141
142        /// <summary>
143        /// Constructor.
144        /// </summary>
145        protected ErasureTargetBase()
146        {
147            Method = ErasureMethodRegistrar.Default;
148        }
149
150        public IErasureMethod Method
151        {
152            get{
153                return method;
154            }
155            set
156            {
157                if (!SupportsMethod(value))
158                    throw new ArgumentException(S._("The selected erasure method is not " +
159                        "supported for this erasure target."));
160                method = value;
161            }
162        }
163
164        /// <summary>
165        /// Gets the effective erasure method for the current target (i.e., returns
166        /// the correct erasure method for cases where the <see cref="Method"/>
167        /// property is <see cref="ErasureMethodRegistrar.Default"/>
168        /// </summary>
169        /// <returns>The Erasure method which the target should be erased with.
170        /// This function will never return <see cref="ErasureMethodRegistrar.Default"/></returns>
171        public virtual IErasureMethod EffectiveMethod
172        {
173            get
174            {
175                if (Method != ErasureMethodRegistrar.Default)
176                    return Method;
177
178                throw new InvalidOperationException("The effective method of the erasure " +
179                    "target cannot be ErasureMethodRegistrar.Default");
180            }
181        }
182       
183        /// <summary>
184        /// The backing variable for the <see cref="Method"/> property.
185        /// </summary>
186        private IErasureMethod method;
187    }
188}
Note: See TracBrowser for help on using the repository browser.