neo_doxygen: Isolate language-specific code.
authorJean-Christophe Beaupré <jcbrinfo@users.noreply.github.com>
Sun, 2 Nov 2014 21:13:38 +0000 (16:13 -0500)
committerJean-Christophe Beaupré <jcbrinfo@users.noreply.github.com>
Tue, 4 Nov 2014 17:16:08 +0000 (12:16 -0500)
Signed-off-by: Jean-Christophe Beaupré <jcbrinfo@users.noreply.github.com>

contrib/neo_doxygen/src/doxml/doxml.nit
contrib/neo_doxygen/src/doxml/language_specific.nit [new file with mode: 0644]
contrib/neo_doxygen/src/doxml/listener.nit
contrib/neo_doxygen/src/doxml/memberdef.nit
contrib/neo_doxygen/src/neo_doxygen.nit

index 431d514..280334a 100644 (file)
@@ -24,6 +24,9 @@ class CompoundFileReader
        # The project graph.
        var model: ProjectGraph
 
+       # The language-specific strategies to use.
+       redef var source_language: SourceLanguage
+
        private var reader: XMLReader = new XophonReader
        private var compounddef: CompoundDefListener is noinit
        private var noop: NoopListener is noinit
diff --git a/contrib/neo_doxygen/src/doxml/language_specific.nit b/contrib/neo_doxygen/src/doxml/language_specific.nit
new file mode 100644 (file)
index 0000000..b5afa8e
--- /dev/null
@@ -0,0 +1,104 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Handle language-specific parts of the importation.
+module doxml::language_specific
+
+import model
+
+# Various importation logics that depend on the project’s language.
+abstract class SourceLanguage
+
+       # Apply the information deduced from `type_text` to `member`.
+       #
+       # `type_text` is the content of the `<type>` element.
+       fun apply_member_type(member: Member, type_text: RawType) do
+               if type_text["text"] != null then
+                       member.static_type = type_text
+               end
+       end
+
+       # Extract the specified keyword at the beginning of the specified text.
+       #
+       # If the keyword is at the beginning of the specified text, return `true`
+       # and remove the keyword. Else, return false.
+       #
+       # Used to extract some keywords that Doxygen puts in the type.
+       #
+       #     class DummySource
+       #       super JavaSource
+       #     #
+       #       fun test(text: LinkedText, keyword: String): Bool do
+       #               return extract_keyword(text, keyword)
+       #       end
+       #     end
+       #     #
+       #     var text = new RawType(new ProjectGraph(""))
+       #     var dummy = new DummySource
+       #     var res: Bool
+       #     #
+       #     text.add_part("abstract final", "")
+       #     res = dummy.test(text, "static")
+       #     assert not res
+       #     res = dummy.test(text, "abstract")
+       #     assert res
+       #     assert "final" == text["text"].as(JsonArray).first
+       #     res = dummy.test(text, "final")
+       #     assert res
+       #     assert text["text"] == null
+       #     res = dummy.test(text, "abstract")
+       #     assert not res
+       protected fun extract_keyword(text: LinkedText, keyword: String): Bool do
+               var text_array = text["text"]
+               if text_array == null then return false
+               assert text_array isa JsonArray
+               if text_array.is_empty then return false
+
+               var content = text_array.first.as(String).l_trim
+               var link = text.links.first
+               var found = false
+
+               if link == null and content.has_prefix(keyword) then
+                       if keyword.length == content.length then
+                               content = ""
+                               found = true
+                       else if content.chars[keyword.length] <= ' ' then
+                               content = content.substring_from(keyword.length).l_trim
+                               found = true
+                       end
+                       if "" == content then
+                               text.shift_part
+                       else if found then
+                               text.set_part(0, content, "")
+                       end
+               end
+               return found
+       end
+end
+
+# Importation logics for Java.
+class JavaSource
+       super SourceLanguage
+
+       redef fun apply_member_type(member, type_text) do
+               # For abstract members, Doxygen put `abstract` at the beginning of the type.
+               # We assume that Doxygen do not put annotations in the type (it seems to
+               # be the case).
+               member.is_abstract = extract_keyword(type_text, "abstract")
+               # TODO final
+               # TODO void
+               # TODO Avoid using `RawType` when possible. Only use `RawType` as a fallback.
+               super
+       end
+end
index ccdd8f5..26de0d2 100644 (file)
@@ -17,6 +17,7 @@ module doxml::listener
 
 import saxophonit
 import model
+import language_specific
 
 # Common abstractions for SAX listeners reading XML documents generated by Doxygen.
 abstract class DoxmlListener
@@ -28,6 +29,9 @@ abstract class DoxmlListener
        # The project graph.
        fun graph: ProjectGraph is abstract
 
+       # The language-specific strategies to use.
+       fun source_language: SourceLanguage is abstract
+
        redef fun document_locator=(locator: SAXLocator) do
                self.locator = locator
        end
@@ -130,13 +134,18 @@ abstract class StackableListener
        # The project graph.
        private var p_graph: ProjectGraph is noinit
 
+       # The language-specific strategies to use.
+       private var p_source: SourceLanguage is noinit
+
 
        init do
                super
                p_graph = parent.graph
+               p_source = parent.source_language
        end
 
        redef fun graph do return p_graph
+       redef fun source_language do return p_source
 
        # Temporary redirect events to itself until the end of the specified element.
        fun listen_until(uri: String, local_name: String) do
index dec4264..3a5100b 100644 (file)
@@ -50,7 +50,7 @@ class MemberDefListener
                else if "name" == local_name then
                        member.name = text.to_s
                else if "type" == local_name then
-                       member.static_type = type_listener.linked_text
+                       source_language.apply_member_type(member, type_listener.linked_text)
                else
                        super
                end
index 4ce124b..7738a4a 100644 (file)
@@ -54,7 +54,8 @@ class NeoDoxygen
        # * `dir`: Doxygen XML output directory path.
        fun put_project(name: String, dir: String) do
                model = new ProjectGraph(name)
-               var reader = new CompoundFileReader(model)
+               # TODO Let the user select the language.
+               var reader = new CompoundFileReader(model, new JavaSource)
                # Queue for sub-directories.
                var directories = new Array[String]