Debian Codegraph @

external/api-doc-tools/mdoc/Mono.Documentation/Updater/XmlSyncer.cs

raw
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Xml;
5 using Mono.Cecil;
6 using Mono.Documentation.Updater.Frameworks;
7
8 namespace Mono.Documentation.Updater
9 {
10 public static class XmlSyncer
11 {
12 public static void MakeParameters (XmlElement root, MemberReference member, IList<ParameterDefinition> parameters, FrameworkTypeEntry typeEntry, ref bool fxAlternateTriggered)
13 {
14 XmlElement e = DocUtils.WriteElement (root, "Parameters");
15
16 /// addParameter does the work of adding the actual parameter to the XML
17 Action<ParameterDefinition, XmlElement, string, int, bool, string, bool> addParameter = (ParameterDefinition param, XmlElement nextTo, string paramType, int index, bool addIndex, string fx, bool addfx) =>
18 {
19 var pe = root.OwnerDocument.CreateElement ("Parameter");
20
21 if (nextTo == null)
22 e.AppendChild (pe);
23 else
24 e.InsertAfter (pe, nextTo);
25
26 pe.SetAttribute ("Name", param.Name);
27 pe.SetAttribute ("Type", paramType);
28 if (param.ParameterType is ByReferenceType)
29 {
30 if (param.IsOut)
31 pe.SetAttribute ("RefType", "out");
32 else
33 pe.SetAttribute ("RefType", "ref");
34 }
35 if (addIndex)
36 pe.SetAttribute ("Index", index.ToString ());
37 if (addfx)
38 pe.SetAttribute (Consts.FrameworkAlternate, fx);
39
40 MakeAttributes (pe, GetCustomAttributes (param.CustomAttributes, ""));
41 };
42
43 /// addFXAttributes, adds the index attribute to all existing elements.
44 /// Used when we first detect the scenario which requires this.
45 Action<XmlNodeList> addFXAttributes = nodes =>
46 {
47 var i = 0;
48 foreach (var node in nodes.Cast<XmlElement> ())
49 {
50 node.SetAttribute ("Index", i.ToString ());
51 i++;
52 }
53 };
54
55 int parameterIndex = 0;
56 int parameterIndexOffset = 0;
57
58 var paramNodes = e.GetElementsByTagName ("Parameter");
59 bool inFXMode = frameworksCache.Frameworks.Count () > 1;
60
61 foreach (ParameterDefinition p in parameters)
62 {
63 var ptype = GetDocParameterType (p.ParameterType);
64 if (parameterIndex >= paramNodes.Count)
65 {
66 // this parameter hasn't been added yet
67 bool hasParameterName = string.IsNullOrWhiteSpace (p.Name);
68 addParameter (p, null, ptype, parameterIndex, false, "", false);
69 }
70 else // there's enough nodes, see if it truly exists
71 {
72 //look for < parameter > that matches position
73 XmlElement parameterNode = e.ChildNodes[parameterIndex + parameterIndexOffset] as XmlElement;
74
75
76 if (parameterNode != null)
77 {
78 //Assert Type Matches (if not, throw?)
79 if (parameterNode.HasAttribute ("Name") && parameterNode.Attributes["Name"].Value == p.Name)
80 {
81 // we're good, continue on.
82 }
83 else
84 { // name doesn't match
85 if (parameterNode.HasAttribute ("Index"))
86 {
87 // TODO: add a FrameworkAlternate check, and set offset correctly
88 int pindex;
89 if (int.TryParse (parameterNode.GetAttribute ("Index"), out pindex) && pindex < parameterIndex)
90 {
91 parameterIndexOffset++;
92
93 continue;
94 }
95 }
96 else
97 {
98 if (!inFXMode) throw new Exception ("shit");
99 addFXAttributes (paramNodes);
100 //-find type in previous frameworks
101
102
103 string fxList = FXUtils.PreviouslyProcessedFXString (typeEntry);
104
105 //-find < parameter where index = currentIndex >
106 var currentNode = paramNodes[parameterIndex] as XmlElement;
107 currentNode.SetAttribute (Consts.FrameworkAlternate, fxList);
108
109 addParameter (p, parameterNode, ptype, parameterIndex - parameterIndexOffset, true, typeEntry.Framework.Name, true);
110 parameterIndexOffset++;
111 fxAlternateTriggered = true;
112 }
113 }
114
115 }
116 else
117 { // no element at this index
118 // TODO: does this ever happen?
119 throw new Exception ("This wasn't supposed to happen");
120 //addParameter (p);
121 }
122 /*
123 - If found
124 - Assert Type Matches (if not, throw?)
125 -If Name Matches …
126 - if “FrameworkAlternate”
127 -Add typeEntry.Framework.Name to list
128 - done!
129 -Else (exists, but name doesn’t match … FrameworkAlternate path)
130 - check if inFXMode if not, throw
131 -AddFXParameters
132 - adds Index to all existing<parameters
133 -find type in previous frameworks
134 -find < parameter where index = currentIndex >
135 -Add FrameworkAlternate = allPreviousFrameworks and Index = currentIndex
136 - Add new node with Index = currentIndex
137 - else not found
138 -add
139 */
140 }
141 parameterIndex++;
142 }
143 //-purge `typeEntry.Framework` from any<parameter> that
144 // has FrameworkAlternate, and “name” doesn’t match any
145 // `parameters`
146 var alternates = paramNodes
147 .Cast<XmlElement> ()
148 .Select (p => new
149 {
150 Element = p,
151 Name = p.GetAttribute ("Name"),
152 FrameworkAlternate = p.GetAttribute (Consts.FrameworkAlternate)
153 })
154 .Where (p =>
155 !string.IsNullOrWhiteSpace (p.FrameworkAlternate) &&
156 p.FrameworkAlternate.Contains (typeEntry.Framework.Name) &&
157 !parameters.Any (param => param.Name == p.Name))
158 .ToArray ();
159 if (alternates.Any ())
160 {
161 foreach (var a in alternates)
162 {
163 string newValue = FXUtils.RemoveFXFromList (a.FrameworkAlternate, typeEntry.Framework.Name);
164 if (string.IsNullOrWhiteSpace (newValue))
165 {
166 a.Element.RemoveAttribute (Consts.FrameworkAlternate);
167 }
168 else
169 {
170 a.Element.SetAttribute (Consts.FrameworkAlternate, newValue);
171 }
172 }
173 }
174
175 return;
176 /*
177 // old code
178 foreach (ParameterDefinition p in parameters)
179 {
180 XmlElement pe;
181
182 // param info
183 var ptype = GetDocParameterType (p.ParameterType);
184 var newPType = ptype;
185
186 if (MDocUpdater.SwitchingToMagicTypes)
187 {
188 newPType = NativeTypeManager.ConvertFromNativeType (ptype);
189 }
190
191 // now find the existing node, if it's there so we can reuse it.
192 var nodes = root.SelectSingleNode ("Parameters").SelectNodes ("Parameter")
193 .Cast<XmlElement> ().Where (x => x.GetAttribute ("Name") == p.Name)
194 .ToArray ();
195
196 // FYI: Exists? No?
197 if (nodes.Count () == 0)
198 {
199 // TODO: instead of this. Needs to be replaced with a better
200 // check for Parameter index ... should I add parameter index?
201
202 // are we in frameworks mode?
203 // add Index to all existing parameter nodes if they don't have them
204 // match existing to position and type
205 bool _inFXMode = typeEntry.Framework.Frameworks.Count () > 1;
206
207 // when I find the one, name won't match ...
208
209 // find all "previous" frameworks
210 // Add FrameworkAlternate with previous frameworks to found/pre-existing node
211 var allPreviousTypes_ = typeEntry.Framework.Frameworks
212 .Where (f => f.index < typeEntry.Framework.index)
213 .Select (f => f.FindTypeEntry (typeEntry))
214 .ToArray ();
215
216 var allPreviousFrameworks = allPreviousTypes.Value.Select (previous => previous.Framework.Name).ToArray ();
217 string fxList = string.Join (";", allPreviousFrameworks);
218
219 // find the parameters in `root` that have an index == this parameter's index
220 // if they don't match, then we need to make a new one for this
221
222 // Create new "Parameter" node, with FrameworkAlternate = this
223
224 // Legacy: wasn't found, let's make sure it wasn't just cause the param name was changed
225 nodes = root.SelectSingleNode ("Parameters").SelectNodes ("Parameter")
226 .Cast<XmlElement> ()
227 .Skip (parameterIndex) // this makes sure we don't inadvertently "reuse" nodes when adding new ones
228 .Where (x => x.GetAttribute ("Name") != p.Name && (x.GetAttribute ("Type") == ptype || x.GetAttribute ("Type") == newPType))
229 .Take (1) // there might be more than one that meets this parameter ... only take the first.
230 .ToArray ();
231 }
232
233 AddXmlNode (nodes,
234 x => x.GetAttribute ("Type") == ptype,
235 x => x.SetAttribute ("Type", ptype),
236 () =>
237 {
238 pe = root.OwnerDocument.CreateElement ("Parameter");
239 e.AppendChild (pe);
240
241 pe.SetAttribute ("Name", p.Name);
242 pe.SetAttribute ("Type", ptype);
243 if (p.ParameterType is ByReferenceType)
244 {
245 if (p.IsOut)
246 pe.SetAttribute ("RefType", "out");
247 else
248 pe.SetAttribute ("RefType", "ref");
249 }
250
251 MakeAttributes (pe, GetCustomAttributes (p.CustomAttributes, ""));
252 return pe;
253 },
254 member);
255
256 parameterIndex++;
257 }
258
259 // TODO: was there a `Parameter` that we didn't process that has FrameworkAlternate?
260 // if yes, remove this framework from that FrameworkAlternate
261 // if that makes the list empty, remove the node and corresponding /Docs/parameter node
262 */
263 }
264
265
266
267
268 }
269 }