//////////////////////////////////////////////////////// public static void ReadKeywordMappings () { property_table = new Hashtable (); // FIXME: No need for a SerializerFactory here, since we need the serializer // only once XmlSerializerFactory xsf = new XmlSerializerFactory(); XmlSerializer xs = xsf.CreateSerializer (typeof (QueryMapping), new Type[] { typeof (QueryKeywordMapping)}); QueryMapping query_mapping = null; // <keyword name, can override> Dictionary<string, bool> mapping_override = new Dictionary<string, bool> (); using (Stream s = File.OpenRead (Path.Combine (PathFinder.ConfigDataDir, "query-mapping.xml"))) { try { query_mapping = (QueryMapping) xs.Deserialize (s); foreach (QueryKeywordMapping mapping in query_mapping.Mappings) { PropertyKeywordFu.RegisterMapping (mapping); mapping_override [mapping.Keyword] = true; } } catch (XmlException e) { Logger.Log.Error (e, "Unable to parse global query-mapping.xml"); } } // Override global mappings by local mappings if (! File.Exists (Path.Combine (PathFinder.StorageDir, "query-mapping.xml"))) return; using (Stream s = File.OpenRead (Path.Combine (PathFinder.StorageDir, "query-mapping.xml"))) { try { query_mapping = (QueryMapping) xs.Deserialize (s); foreach (QueryKeywordMapping mapping in query_mapping.Mappings) { if (mapping_override.ContainsKey (mapping.Keyword)) { property_table.Remove (mapping.Keyword); mapping_override [mapping.Keyword] = false; } PropertyKeywordFu.RegisterMapping (mapping); } } catch (XmlException e) { Logger.Log.Error (e, "Unable to parse local query-mapping.xml"); } } }
public override void Transform(Engine engine, Package package) { this.Package = package; this.Engine = engine; XmlSerializer serializer; Dynamic.Component component; bool hasOutput = HasPackageValue(package, "Output"); if (hasOutput) { GeneralUtils.TimedLog("start retrieving previous Output from package"); String inputValue = package.GetValue("Output"); GeneralUtils.TimedLog("start deserializing"); TextReader tr = new StringReader(inputValue); GeneralUtils.TimedLog("start creating serializer"); serializer = new XmlSerializerFactory().CreateSerializer(typeof(Dynamic.Component)); GeneralUtils.TimedLog("finished creating serializer"); component = (Dynamic.Component)serializer.Deserialize(tr); GeneralUtils.TimedLog("finished deserializing from package"); } else { GeneralUtils.ResetLogTimer(); GeneralUtils.TimedLog("Could not find 'Output' in the package"); GeneralUtils.TimedLog("Start creating dynamic component from current component in the package"); GeneralUtils.TimedLog("start creating serializer"); serializer = new XmlSerializerFactory().CreateSerializer(typeof(Dynamic.Component)); GeneralUtils.TimedLog("finished creating serializer"); component = GetDynamicComponent(manager); GeneralUtils.TimedLog("Finished creating dynamic component with title " + component.Title); } try { GeneralUtils.TimedLog("starting transformComponent"); TransformComponent(component); GeneralUtils.TimedLog("finished transformComponent"); } catch (StopChainException) { GeneralUtils.TimedLog("caught stopchainexception, will not write current component back to the package"); return; } var sw = new StringWriter(); var ms = new MemoryStream(); XmlWriter writer = new XmlTextWriterFormattedNoDeclaration(ms, Encoding.UTF8); string outputValue; //Create our own namespaces for the output var ns = new XmlSerializerNamespaces(); //Add an empty namespace and empty value ns.Add("", ""); serializer.Serialize(writer, component, ns); outputValue = Encoding.UTF8.GetString(ms.ToArray()); // for some reason, the .NET serializer leaves an invalid character at the start of the string // we will remove everything up to the first < so that the XML can be deserialized later! Regex re = new Regex("^[^<]+"); outputValue = re.Replace(outputValue, ""); if (hasOutput) { Item outputItem = package.GetByName("Output"); package.Remove(outputItem); outputItem.SetAsString(outputValue); package.PushItem("Output", outputItem); } else { package.PushItem(Package.OutputName, package.CreateStringItem(ContentType.Text, outputValue)); } GeneralUtils.TimedLog("finished Transform"); }
public static void Main(string[] args) { XmlSerializerFactory factory = new XmlSerializerFactory(); XmlSerializer serializer = factory.CreateSerializer(typeof(UploadState)); UploadState us; if (args.Length == 2) { us = new UploadState(args[1], args[0]); } else if (args.Length == 0 && new FileInfo("upload.dat").Exists) { using (FileStream fs = new FileStream("upload.dat", FileMode.Open)) { us = (UploadState)serializer.Deserialize(fs); } } else { Console.Error.WriteLine("Usage: bucket filename"); return; } try { NameValueCollection appConfig = ConfigurationManager.AppSettings; // Print the number of Amazon S3 Buckets. AmazonS3 s3Client = AWSClientFactory.CreateAmazonS3Client( appConfig["AWSAccessKey"], appConfig["AWSSecretKey"] ); if (string.IsNullOrEmpty(us.UploadId)) { InitiateMultipartUploadRequest req = new InitiateMultipartUploadRequest() .WithBucketName(us.BucketName) .WithKey(us.Key) ; us.UploadId = s3Client.InitiateMultipartUpload(req).UploadId; using (FileStream fs = new FileStream("upload.dat", FileMode.OpenOrCreate)) { serializer.Serialize(fs, us); } } while (us.FilePosition < us.FileLength) { try { Console.WriteLine("Uploading part {0} of {1}", us.PartNumber, us.NumChunks); UploadPartRequest ureq = new UploadPartRequest() .WithBucketName(us.BucketName) .WithFilePath(us.FileName) .WithFilePosition(us.FilePosition) .WithPartNumber(us.PartNumber) .WithPartSize(us.FileLength - us.FilePosition > us.ChunkSize ? us.ChunkSize : us.FileLength - us.FilePosition) .WithGenerateChecksum(true) .WithKey(us.Key) .WithUploadId(us.UploadId) .WithSubscriber(new EventHandler<UploadPartProgressArgs>(ShowProgress)) ; if (us.Responses.Count > us.PartNumber - 1) { us.Responses[us.PartNumber - 1] = new PartETag(us.PartNumber, s3Client.UploadPart(ureq).ETag); } else { us.Responses.Insert(us.PartNumber - 1, new PartETag(us.PartNumber, s3Client.UploadPart(ureq).ETag)); } us.PartNumber++; us.FilePosition += us.ChunkSize; using (FileStream fs = new FileStream("upload.dat", FileMode.OpenOrCreate)) { serializer.Serialize(fs, us); } } catch (System.Net.WebException) { } } CompleteMultipartUploadRequest creq = new CompleteMultipartUploadRequest() .WithPartETags(us.Responses) .WithBucketName(us.BucketName) .WithUploadId(us.UploadId) .WithKey(us.Key) ; CompleteMultipartUploadResponse cresp = s3Client.CompleteMultipartUpload(creq); System.Console.WriteLine("File available at {0}", cresp.Location); File.Delete("upload.dat"); } catch (AmazonS3Exception e) { Console.Error.WriteLine(e); } //Console.Write(GetServiceOutput()); //Console.Read(); }
void WriteSimpleType(Type type, object val) { var serializer = new XmlSerializerFactory().CreateSerializer(type); XmlSerializerNamespaces xmlnsEmpty = new XmlSerializerNamespaces(); xmlnsEmpty.Add("", ""); serializer.Serialize(writer, val, xmlnsEmpty); }
void WriteDynamicType(Type type, object val) { writer.WriteElementString("Type", type.FullName); writer.WriteStartElement("Value"); var serializer = new XmlSerializerFactory().CreateSerializer(type); XmlSerializerNamespaces xmlnsEmpty = new XmlSerializerNamespaces(); xmlnsEmpty.Add("", ""); serializer.Serialize(writer, val, xmlnsEmpty); writer.WriteEndElement(); }