Created
April 13, 2015 20:42
-
-
Save stdray/81be6540ef8581b33890 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public Prepare(content : XDocument) : XDocument | |
| { | |
| def praparedContent = XDocument.Load(content.CreateReader()); | |
| def fakeNamespaces = [ @"http://zakupki.gov.ru/oos/printform/1", @"http://zakupki.gov.ru/oos/integration/1"]; | |
| def clean(el) | |
| { | |
| def name = el.Name.LocalName; | |
| def parentName = el.Parent?.Name?.LocalName; | |
| def changes = List(); | |
| mutable nsChanged = false; | |
| //фиксим кривые префиксы неймспейсов | |
| foreach(attr in el.Attributes()) | |
| when(fakeNamespaces.Contains(attr.Value)) | |
| { | |
| changes.Add(() => | |
| { | |
| el.SetAttributeValue(attr.Name, @"http://zakupki.gov.ru/oos/types/1"); | |
| nsChanged = true; | |
| }); | |
| } | |
| when(!nsChanged && fakeNamespaces.Contains(el.Name.NamespaceName)) | |
| { | |
| changes.Add(() => | |
| { | |
| def preparedElBoby = | |
| if(el.HasElements) | |
| el.Elements().Select(e => { def (_, r) = clean(e); r} ).ToList() : object | |
| else | |
| el.Value; | |
| def preparedEl = XElement(el.Name.LocalName, preparedElBoby); | |
| preparedEl.ReplaceAttributes(el.Attributes()); | |
| when(el.Parent != null) | |
| el.ReplaceWith(preparedEl); | |
| }); | |
| } | |
| //удаляем пустые теги | |
| when((String.IsNullOrWhiteSpace(el.Value) && !el.Attributes().Any()) || | |
| name == "cryptoSigns" || | |
| (name == "signature" && parentName == "printForm") || | |
| name == "fcsClarification") | |
| { | |
| changes.Add(() => el.Remove()); | |
| } | |
| //фиксим значения представленые в экспоненциальной форме (XmlSerializer не умеет с ними работать) | |
| when(name == "value" && parentName == "quantity") | |
| safe el.Value = decimal.Parse(el.Value, NumberStyles.Any).ToString(); | |
| (changes, el); | |
| } | |
| try | |
| { | |
| def changes = praparedContent | |
| .Descendants() | |
| .Select(clean) | |
| .SelectMany((cs, _) => cs) | |
| .Reverse() | |
| .ToList(); | |
| changes.Iter(c => c()); | |
| praparedContent; | |
| } | |
| catch | |
| { | |
| | ex => | |
| Trace.WriteLine(ex.Message); | |
| throw; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment