/// <summary> /// Creates a store object by name, as specified in the config file. /// </summary> /// <param name="name">The name for this store 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 store object.</param> public static ISwiffotronStore CreateStore(string name, string assembly, string classname, string init) { if (assembly == string.Empty) { /* Shortcut value to say "Look in the executing assembly" */ assembly = null; } ISwiffotronStore newStore = 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 */ newStore = (ISwiffotronStore)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); newStore = (ISwiffotronStore)oh.Unwrap(); } newStore.Initialise(init); return(newStore); }
/// <summary> /// Writes a block of data to the store specified in the fully qualified /// store key of the form [store name]:[store key] /// </summary> /// <param name="key">The store key</param> /// <param name="data">The data to store as a byte array.</param> /// <returns>Null if it was not saved (Saves disabled) or the relative /// store path from the store URL, e.g. "store://mystore/things/thing" returns /// "things/thing"</returns> public string Save(SwiffotronContext ctx, string key, byte[] data) { Uri storeURI = new Uri(key); if (storeURI.Scheme != "store") /* ISSUE 67: Constants, please. */ { throw new SwiffotronException( SwiffotronError.BadInputXML, ctx, @"Store paths should begin with store://"); } string storeId = storeURI.Host; key = storeURI.AbsolutePath.Substring(1); if (!conf.EnableStoreWrites) { /* Give up, but return the key we would have used. Used in debug * tests. */ return(key); } if (!stores.ContainsKey(storeId)) { throw new SwiffotronException( SwiffotronError.BadInputXML, ctx, @"Store '" + storeId + @"' not registered."); } ISwiffotronStore store = stores[storeId]; using (Stream s = store.OpenOutput(key)) { s.Write(data, 0, data.Length); } store.Commit(key); return(key); }
public void Register(string name, ISwiffotronStore store) { this.stores.Add(name, store); }
/// <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); }