/// <summary> /// Creates a cache object by name, as specified in the config file. /// </summary> /// <param name="name">The name for this cache that will be referenced in /// any swiffotron XML files.</param> /// <param name="assembly">The fully qualified assembly name. Pass an empty /// string or null to reference the currently executing assembly and use /// any default implementors.</param> /// <param name="classname">The fully qualified class name</param> /// <param name="init">An initialisation string passed in the call to Initialise /// on the new cache object.</param> public static ISwiffotronCache CreateCache(string name, string assembly, string classname, string init) { if (assembly == string.Empty) { /* Shortcut value to say "Look in the executing assembly" */ assembly = null; } ISwiffotronCache newCache = null; if (assembly != null && assembly.ToLower().EndsWith(".dll")) { /* Load from arbitrary DLL */ Type type = Assembly.LoadFrom(assembly).GetType(classname); /* Class cast problems just get thrown upwards and destroy the app */ newCache = (ISwiffotronCache)Activator.CreateInstance(type); } else { /* Load by named assembly */ /* Class cast problems just get thrown upwards and destroy the app */ ObjectHandle oh = Activator.CreateInstance(assembly, classname); newCache = (ISwiffotronCache)oh.Unwrap(); } newCache.Initialise(init); return(newCache); }
public void Register(string name, ISwiffotronCache cache) { this.caches.Add(name, cache); }
/// <summary> /// Loads a configuration file passed to the new Swiffotron and parses it, /// creating any implementing classes named in the configuration. /// </summary> /// <param name="configXml">A stream ready and primed with lovely XML /// configuration data.</param> private void LoadConfigXML(Stream configXml) { this.Xml = new XMLHelper(); this.Xml.SetContext(new SwiffotronContext("Swiffotron configuration")); Xml.LoadConfigurationXML(configXml); /* First, set up any caches: */ XmlAttribute attrib; foreach (XPathNavigator hit in Xml.Select(@"/con:config/con:cache")) { XmlAttributeCollection attribs = ((XmlElement)hit.UnderlyingObject).Attributes; /* The schema defines these attributes as mandatory, so they will exist: */ string name = attribs[@"name"].Value; string classname = attribs[@"classname"].Value; /* Optional parameters, which we default to null before we call Initialise on * any implementor. */ attrib = attribs[@"init"]; string init = (attrib == null) ? null : attrib.Value; attrib = attribs[@"assembly"]; string assembly = (attrib == null) ? null : attrib.Value; /* Create our named cache as specified by our config file. */ ISwiffotronCache newCache = Extern.CreateCache(name, assembly, classname, init); /* Use Add method here to ensure that the name is unique. Key errors get thrown * upwards and destroy the app. Hey, fix your config file, user. */ this.Caches.Register(name, newCache); } /* Now, set up any stores: */ foreach (XPathNavigator hit in Xml.Select(@"/con:config/con:store")) { XmlAttributeCollection attribs = ((XmlElement)hit.UnderlyingObject).Attributes; /* The schema defines these attributes as mandatory, so they will exist: */ string name = attribs[@"name"].Value; string classname = attribs[@"classname"].Value; /* Optional parameter, which we default to null before we call Initialise on * any implementor. */ attrib = attribs[@"init"]; string init = (attrib == null) ? null : attrib.Value; attrib = attribs[@"assembly"]; string assembly = (attrib == null) ? null : attrib.Value; /* Create our named store as specified by our config file. */ ISwiffotronStore newStore = Extern.CreateStore(name, assembly, classname, init); /* Use Add method here rather than the index operator to ensure that the * name is unique. Key errors get thrown upwards and destroy the app. * Hey, fix your config file, user. */ this.Stores.Register(name, newStore); } /* ISSUE 68: Staggeringly inefficient xpath queries that navigate from the root node every damned * time. Do we care? */ this.EnableStoreWrites = Xml.SelectBoolean(@"/con:config/con:swfprefs/con:storeWriteEnabled/text()"); this.swfReaderOptions = new SWFReaderOptions() { StrictTagLength = Xml.SelectBoolean(@"/con:config/con:swfprefs/con:stricttaglength/text()") }; this.swfWriterOptions = new SWFWriterOptions() { Compressed = Xml.SelectBoolean(@"/con:config/con:swfprefs/con:compression/text()"), EnableDebugger = Xml.SelectBoolean(@"/con:config/con:swfprefs/con:debugcode/text()") }; string htmlType = Xml.SelectString(@"/con:config/con:htmlType/text()", "JQuery"); this.HTMLType = (SWF2HTMLOptions.FrameworkType)Enum.Parse(typeof(SWF2HTMLOptions.FrameworkType), htmlType); this.HTMLStandalone = Xml.SelectBoolean(@"/con:config/con:htmlStandalone/text()", false); }