5 // Jonathan Pryor <jpryor@novell.com>
7 // Copyright (c) 2010 Novell, Inc. (http://www.novell.com)
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System.Collections.Generic;
35 using System.Xml.Linq;
38 using Mono.Documentation;
43 namespace Mono.Documentation
45 public class MDocUpdateEcmaXml : MDocCommand
47 string file = "CLILibraryTypes.xml";
48 List<string> directories;
49 Dictionary<string, HashSet<string>> libraries = new Dictionary<string, HashSet<string>>();
51 public override void Run (IEnumerable<string> args)
53 string current_library = "";
55 var options = new OptionSet () {
57 "{FILE} to generate/update documentation within.\n" +
58 "If not specified, will process " + file + ".\n" +
59 "Set to '-' to skip updates and write to standard output.",
62 "The {LIBRARY} that the following --type=TYPE types should be a part of.",
63 v => current_library = v },
65 "The full {TYPE} name of a type to copy into the output file.",
66 v => AddTypeToLibrary (current_library, v) },
68 directories = Parse (options, args, "export-ecma-xml",
69 "[OPTIONS]+ DIRECTORIES",
70 "Export mdoc documentation within DIRECTORIES into ECMA-format XML.\n\n" +
71 "DIRECTORIES are mdoc(5) directories as produced by 'mdoc update'.");
72 if (directories == null || directories.Count == 0)
78 void AddTypeToLibrary (string library, string type)
80 HashSet<string> types;
81 if (!libraries.TryGetValue (library, out types))
82 libraries.Add (library, types = new HashSet<string> ());
83 types.Add (type.Replace ('/', '.').Replace ('+', '.'));
88 XDocument input = LoadFile (this.file);
90 var seenLibraries = new HashSet<string> ();
92 Action<string> creator = file => {
93 using (var output = CreateWriter (file)) {
94 // spit out header comments, DTD, etc.
95 foreach (var node in input.Nodes ()) {
96 if (node.NodeType == XmlNodeType.Element || node.NodeType == XmlNodeType.Text)
98 node.WriteTo (output);
101 using (var librariesElement = new Element (output, o => o.WriteStartElement ("Libraries"))) {
102 UpdateExistingLibraries (input, output, seenLibraries);
103 GenerateMissingLibraries (input, output, seenLibraries);
105 output.WriteWhitespace ("\r\n");
108 MdocFile.UpdateFile (this.file, creator);
111 static XDocument LoadFile (string file)
113 if (file == "-" || !File.Exists (file))
114 return CreateDefaultDocument ();
116 var settings = new XmlReaderSettings {
119 using (var reader = XmlReader.Create (file, settings))
120 return XDocument.Load (reader, LoadOptions.PreserveWhitespace);
123 static XDocument CreateDefaultDocument ()
125 return new XDocument (
126 new XComment (" ====================================================================== "),
127 new XComment (" This XML is a description of the Common Language Infrastructure (CLI) library. "),
128 new XComment (" This file is a normative part of Partition IV of the following standards: ISO/IEC 23271 and ECMA 335 "),
129 new XComment (" ====================================================================== "),
130 new XDocumentType ("Libraries", null, "CLILibraryTypes.dtd", null),
131 new XElement ("Libraries"));
134 static XmlWriter CreateWriter (string file)
136 var settings = new XmlWriterSettings {
139 NewLineChars = "\r\n",
140 OmitXmlDeclaration = true,
144 return XmlWriter.Create (Console.Out, settings);
146 settings.Encoding = new UTF8Encoding (false);
147 return XmlWriter.Create (file, settings);
150 struct Element : IDisposable {
153 public Element (XmlWriter output, Action<XmlWriter> action)
155 this.output = output;
159 public void Dispose ()
161 output.WriteEndElement ();
165 void UpdateExistingLibraries (XDocument input, XmlWriter output, HashSet<string> seenLibraries)
167 foreach (XElement types in input.Root.Elements ()) {
168 XAttribute library = types.Attribute ("Library");
169 HashSet<string> libraryTypes;
170 if (library == null || !libraries.TryGetValue (library.Value, out libraryTypes)) {
171 types.WriteTo (output);
174 seenLibraries.Add (library.Value);
175 var seenTypes = new HashSet<string> ();
176 using (Element typesElement = CreateTypesElement (output, library.Value)) {
177 foreach (XElement type in types.Elements ()) {
178 XAttribute fullName = type.Attribute ("FullName");
179 string typeName = fullName == null
181 : XmlDocUtils.ToEscapedTypeName (fullName.Value);
182 if (typeName == null || !libraryTypes.Contains (typeName)) {
183 type.WriteTo (output);
186 seenTypes.Add (typeName);
187 WriteType (output, typeName);
189 foreach (string type in libraryTypes.Except (seenTypes))
190 WriteType (output, type);
195 static Element CreateTypesElement (XmlWriter output, string library)
200 {o.WriteStartElement ("Types");
201 o.WriteAttributeString ("Library", library);});
204 void WriteType (XmlWriter output, string typeName)
206 XElement type = LoadType (typeName);
208 XAttribute fullName = type.Attribute ("FullName");
209 XAttribute fullNameSp = type.Attribute ("FullNameSP");
210 if (fullNameSp == null && fullName != null) {
211 type.Add (new XAttribute ("FullNameSP", fullName.Value.Replace ('.', '_')));
214 type.WriteTo (output);
217 void GenerateMissingLibraries (XDocument input, XmlWriter output, HashSet<string> seenLibraries)
219 foreach (KeyValuePair<string, HashSet<string>> lib in libraries) {
220 if (seenLibraries.Contains (lib.Key))
222 seenLibraries.Add (lib.Key);
223 using (var typesElement = CreateTypesElement (output, lib.Key)) {
224 foreach (string type in lib.Value) {
225 LoadType (type).WriteTo (output);
231 XElement LoadType (string type)
233 foreach (KeyValuePair<string, string> permutation in GetTypeDirectoryFilePermutations (type)) {
234 foreach (string root in directories) {
235 string path = Path.Combine (root, Path.Combine (permutation.Key, permutation.Value + ".xml"));
236 if (File.Exists (path))
237 return XElement.Load (path);
240 throw new FileNotFoundException ("Unable to find documentation file for type: " + type + ".");
243 // type has been "normalized", which (alas) means we have ~no clue which
244 // part is the namespace and which is the type name, particularly
245 // problematic as types may be nested to any level.
246 // Try ~all permutations. :-)
247 static IEnumerable<KeyValuePair<string, string>> GetTypeDirectoryFilePermutations (string type)
249 int end = type.Length;
251 while ((dot = type.LastIndexOf ('.', end-1)) >= 0) {
252 yield return new KeyValuePair<string, string> (
253 type.Substring (0, dot),
254 type.Substring (dot+1).Replace ('.', '+'));
257 yield return new KeyValuePair<string, string> ("", type.Replace ('.', '+'));