neo_doxygen: Introduce a tool to import a Doxygen model.
[nit.git] / contrib / neo_doxygen / src / doxml / doxml.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # Doxygen’s XML documents reading.
16 module doxml
17
18 import compounddef
19
20 # Reader for XML documents whose the schema is `compound.xsd`.
21 class CompoundFileReader
22 super DoxmlListener
23
24 # The project graph.
25 var model: ProjectGraph
26
27 private var reader: XMLReader = new XophonReader
28 private var compounddef: CompoundDefListener is noinit
29 private var noop: NoopListener is noinit
30
31 init do
32 compounddef = new CompoundDefListener(reader, self)
33 noop = new NoopListener(reader, self)
34 end
35
36 redef fun graph do return model
37
38 # Read the document at the specified path.
39 fun read(path: String) do
40 reader.content_handler = self
41 reader.parse_file(path)
42 compounddef.compound = new UnknownCompound(model)
43 end
44
45 redef fun start_dox_element(local_name: String, atts: Attributes) do
46 if local_name == "compounddef" then
47 read_compound(atts)
48 else if "doxygen" != local_name then
49 noop.listen_until(dox_uri, local_name)
50 end
51 end
52
53 private fun read_compound(atts: Attributes) do
54 var kind = get_required(atts, "kind")
55
56 create_compound(kind)
57 # TODO Make all values of `kind` and `visibility` compatible with the Nit meta-model.
58 if get_bool(atts, "final") then
59 kind = "final {kind}"
60 end
61 if get_bool(atts, "sealed") then
62 kind = "sealed {kind}"
63 end
64 if get_bool(atts, "abstract") then
65 kind = "abstract {kind}"
66 end
67 compounddef.compound.kind = kind
68 compounddef.compound.model_id = get_required(atts, "id")
69 compounddef.compound.visibility = get_optional(atts, "prot", "")
70 end
71
72 private fun create_compound(kind: String) do
73 if kind == "file" then
74 compounddef.compound = new FileCompound(model)
75 else if kind == "namespace" then
76 compounddef.compound = new Namespace(model)
77 else if kind == "class" or kind == "interface" or kind == "enum" then
78 compounddef.compound = new ClassCompound(model)
79 else
80 compounddef.compound = new UnknownCompound(model)
81 noop.listen_until(dox_uri, "compounddef")
82 return
83 end
84 compounddef.listen_until(dox_uri, "compounddef")
85 end
86 end