This is an automated email from the ASF dual-hosted git repository.

sunlan pushed a commit to branch refine-groovydoc
in repository https://gitbox.apache.org/repos/asf/groovy.git


The following commit(s) were added to refs/heads/refine-groovydoc by this push:
     new 5ed536e  Try to extract javadoc content and its relevant node
5ed536e is described below

commit 5ed536eb03cc4fe20d7c587d82eb9bb3ff4c2c3d
Author: Daniel Sun <sun...@apache.org>
AuthorDate: Sun Dec 16 01:46:54 2018 +0800

    Try to extract javadoc content and its relevant node
---
 .../org/codehaus/groovy/antlr/SourceBuffer.java    |   4 +
 .../tools/groovydoc/SimpleGroovyClassDoc.java      | 390 +++++++++++++++++++++
 .../groovydoc/SimpleJavaClassDocAssembler.java     | 133 +++++++
 .../SimpleJavaClassDocAssemblerTest.groovy         |  27 ++
 .../SimpleJavaClassDocAssemblerTest1.java          |  39 +++
 5 files changed, 593 insertions(+)

diff --git a/src/main/java/org/codehaus/groovy/antlr/SourceBuffer.java 
b/src/main/java/org/codehaus/groovy/antlr/SourceBuffer.java
index 558370f..87add9c 100644
--- a/src/main/java/org/codehaus/groovy/antlr/SourceBuffer.java
+++ b/src/main/java/org/codehaus/groovy/antlr/SourceBuffer.java
@@ -108,4 +108,8 @@ public class SourceBuffer {
             lines.add(current);
         }
     }
+
+    public String getContent() {
+        return String.join("", lines);
+    }
 }
diff --git 
a/subprojects/groovy-groovydoc/src/main/java/org/apache/groovy/tools/groovydoc/SimpleGroovyClassDoc.java
 
b/subprojects/groovy-groovydoc/src/main/java/org/apache/groovy/tools/groovydoc/SimpleGroovyClassDoc.java
new file mode 100644
index 0000000..85c5cf6
--- /dev/null
+++ 
b/subprojects/groovy-groovydoc/src/main/java/org/apache/groovy/tools/groovydoc/SimpleGroovyClassDoc.java
@@ -0,0 +1,390 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you 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.
+ */
+package org.apache.groovy.tools.groovydoc;
+
+import org.codehaus.groovy.groovydoc.GroovyAnnotationRef;
+import org.codehaus.groovy.groovydoc.GroovyClassDoc;
+import org.codehaus.groovy.groovydoc.GroovyConstructorDoc;
+import org.codehaus.groovy.groovydoc.GroovyFieldDoc;
+import org.codehaus.groovy.groovydoc.GroovyMethodDoc;
+import org.codehaus.groovy.groovydoc.GroovyPackageDoc;
+import org.codehaus.groovy.groovydoc.GroovyType;
+
+public class SimpleGroovyClassDoc implements GroovyClassDoc {
+    @Override
+    public GroovyConstructorDoc[] constructors() {
+        return new GroovyConstructorDoc[0];
+    }
+
+    @Override
+    public GroovyConstructorDoc[] constructors(boolean filter) {
+        return new GroovyConstructorDoc[0];
+    }
+
+    @Override
+    public boolean definesSerializableFields() {
+        return false;
+    }
+
+    @Override
+    public GroovyFieldDoc[] enumConstants() {
+        return new GroovyFieldDoc[0];
+    }
+
+    @Override
+    public GroovyFieldDoc[] fields() {
+        return new GroovyFieldDoc[0];
+    }
+
+    @Override
+    public GroovyFieldDoc[] properties() {
+        return new GroovyFieldDoc[0];
+    }
+
+    @Override
+    public GroovyFieldDoc[] fields(boolean filter) {
+        return new GroovyFieldDoc[0];
+    }
+
+    @Override
+    public GroovyClassDoc findClass(String className) {
+        return null;
+    }
+
+    @Override
+    public GroovyClassDoc[] importedClasses() {
+        return new GroovyClassDoc[0];
+    }
+
+    @Override
+    public GroovyPackageDoc[] importedPackages() {
+        return new GroovyPackageDoc[0];
+    }
+
+    @Override
+    public GroovyClassDoc[] innerClasses() {
+        return new GroovyClassDoc[0];
+    }
+
+    @Override
+    public GroovyClassDoc[] innerClasses(boolean filter) {
+        return new GroovyClassDoc[0];
+    }
+
+    @Override
+    public GroovyClassDoc[] interfaces() {
+        return new GroovyClassDoc[0];
+    }
+
+    @Override
+    public GroovyType[] interfaceTypes() {
+        return new GroovyType[0];
+    }
+
+    @Override
+    public boolean isAbstract() {
+        return false;
+    }
+
+    @Override
+    public boolean isExternalizable() {
+        return false;
+    }
+
+    @Override
+    public boolean isSerializable() {
+        return false;
+    }
+
+    @Override
+    public GroovyMethodDoc[] methods() {
+        return new GroovyMethodDoc[0];
+    }
+
+    @Override
+    public GroovyMethodDoc[] methods(boolean filter) {
+        return new GroovyMethodDoc[0];
+    }
+
+    @Override
+    public GroovyFieldDoc[] serializableFields() {
+        return new GroovyFieldDoc[0];
+    }
+
+    @Override
+    public GroovyMethodDoc[] serializationMethods() {
+        return new GroovyMethodDoc[0];
+    }
+
+    @Override
+    public boolean subclassOf(GroovyClassDoc gcd) {
+        return false;
+    }
+
+    @Override
+    public GroovyClassDoc superclass() {
+        return null;
+    }
+
+    @Override
+    public GroovyType superclassType() {
+        return null;
+    }
+
+    @Override
+    public String getFullPathName() {
+        return null;
+    }
+
+    @Override
+    public String getRelativeRootPath() {
+        return null;
+    }
+
+    @Override
+    public GroovyAnnotationRef[] annotations() {
+        return new GroovyAnnotationRef[0];
+    }
+
+    @Override
+    public GroovyClassDoc containingClass() {
+        return null;
+    }
+
+    @Override
+    public GroovyPackageDoc containingPackage() {
+        return null;
+    }
+
+    @Override
+    public boolean isFinal() {
+        return false;
+    }
+
+    @Override
+    public boolean isPackagePrivate() {
+        return false;
+    }
+
+    @Override
+    public boolean isPrivate() {
+        return false;
+    }
+
+    @Override
+    public boolean isProtected() {
+        return false;
+    }
+
+    @Override
+    public boolean isPublic() {
+        return false;
+    }
+
+    @Override
+    public boolean isStatic() {
+        return false;
+    }
+
+    @Override
+    public String modifiers() {
+        return null;
+    }
+
+    @Override
+    public int modifierSpecifier() {
+        return 0;
+    }
+
+    @Override
+    public String qualifiedName() {
+        return null;
+    }
+
+    @Override
+    public String commentText() {
+        return null;
+    }
+
+    @Override
+    public String getRawCommentText() {
+        return null;
+    }
+
+    @Override
+    public boolean isAnnotationType() {
+        return false;
+    }
+
+    @Override
+    public boolean isAnnotationTypeElement() {
+        return false;
+    }
+
+    @Override
+    public boolean isClass() {
+        return false;
+    }
+
+    @Override
+    public boolean isConstructor() {
+        return false;
+    }
+
+    @Override
+    public boolean isDeprecated() {
+        return false;
+    }
+
+    @Override
+    public boolean isEnum() {
+        return false;
+    }
+
+    @Override
+    public boolean isEnumConstant() {
+        return false;
+    }
+
+    @Override
+    public boolean isError() {
+        return false;
+    }
+
+    @Override
+    public boolean isException() {
+        return false;
+    }
+
+    @Override
+    public boolean isField() {
+        return false;
+    }
+
+    @Override
+    public boolean isIncluded() {
+        return false;
+    }
+
+    @Override
+    public boolean isInterface() {
+        return false;
+    }
+
+    @Override
+    public boolean isMethod() {
+        return false;
+    }
+
+    @Override
+    public boolean isOrdinaryClass() {
+        return false;
+    }
+
+    @Override
+    public String name() {
+        return null;
+    }
+
+    @Override
+    public void setRawCommentText(String arg0) {
+
+    }
+
+    @Override
+    public String firstSentenceCommentText() {
+        return null;
+    }
+
+    /**
+     * Compares this object with the specified object for order.  Returns a
+     * negative integer, zero, or a positive integer as this object is less
+     * than, equal to, or greater than the specified object.
+     *
+     * <p>The implementor must ensure
+     * {@code sgn(x.compareTo(y)) == -sgn(y.compareTo(x))}
+     * for all {@code x} and {@code y}.  (This
+     * implies that {@code x.compareTo(y)} must throw an exception iff
+     * {@code y.compareTo(x)} throws an exception.)
+     *
+     * <p>The implementor must also ensure that the relation is transitive:
+     * {@code (x.compareTo(y) > 0 && y.compareTo(z) > 0)} implies
+     * {@code x.compareTo(z) > 0}.
+     *
+     * <p>Finally, the implementor must ensure that {@code x.compareTo(y)==0}
+     * implies that {@code sgn(x.compareTo(z)) == sgn(y.compareTo(z))}, for
+     * all {@code z}.
+     *
+     * <p>It is strongly recommended, but <i>not</i> strictly required that
+     * {@code (x.compareTo(y)==0) == (x.equals(y))}.  Generally speaking, any
+     * class that implements the {@code Comparable} interface and violates
+     * this condition should clearly indicate this fact.  The recommended
+     * language is "Note: this class has a natural ordering that is
+     * inconsistent with equals."
+     *
+     * <p>In the foregoing description, the notation
+     * {@code sgn(}<i>expression</i>{@code )} designates the mathematical
+     * <i>signum</i> function, which is defined to return one of {@code -1},
+     * {@code 0}, or {@code 1} according to whether the value of
+     * <i>expression</i> is negative, zero, or positive, respectively.
+     *
+     * @param o the object to be compared.
+     * @return a negative integer, zero, or a positive integer as this object
+     * is less than, equal to, or greater than the specified object.
+     * @throws NullPointerException if the specified object is null
+     * @throws ClassCastException   if the specified object's type prevents it
+     *                              from being compared to this object.
+     */
+    @Override
+    public int compareTo(Object o) {
+        return 0;
+    }
+
+    @Override
+    public boolean isPrimitive() {
+        return false;
+    }
+
+    /**
+     * The qualified name of this type excluding any dimension information.
+     * For example, a two dimensional array of String returns 
"<code>java.lang.String</code>".
+     */
+    @Override
+    public String qualifiedTypeName() {
+        return null;
+    }
+
+    /**
+     * The unqualified name of this type excluding any dimension or nesting 
information.
+     * For example, the class <code>Outer.Inner</code> returns 
"<code>Inner</code>".
+     */
+    @Override
+    public String simpleTypeName() {
+        return null;
+    }
+
+    /**
+     * The unqualified name of this type excluding any dimension information.
+     * For example, a two dimensional array of String returns 
"<code>String</code>".
+     */
+    @Override
+    public String typeName() {
+        return null;
+    }
+}
diff --git 
a/subprojects/groovy-groovydoc/src/main/java/org/apache/groovy/tools/groovydoc/SimpleJavaClassDocAssembler.java
 
b/subprojects/groovy-groovydoc/src/main/java/org/apache/groovy/tools/groovydoc/SimpleJavaClassDocAssembler.java
new file mode 100644
index 0000000..1af0066
--- /dev/null
+++ 
b/subprojects/groovy-groovydoc/src/main/java/org/apache/groovy/tools/groovydoc/SimpleJavaClassDocAssembler.java
@@ -0,0 +1,133 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you 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.
+ */
+package org.apache.groovy.tools.groovydoc;
+
+import com.github.javaparser.JavaParser;
+import com.github.javaparser.ast.Node;
+import com.github.javaparser.ast.body.AnnotationDeclaration;
+import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
+import com.github.javaparser.ast.body.ConstructorDeclaration;
+import com.github.javaparser.ast.body.EnumConstantDeclaration;
+import com.github.javaparser.ast.body.EnumDeclaration;
+import com.github.javaparser.ast.body.FieldDeclaration;
+import com.github.javaparser.ast.body.MethodDeclaration;
+import com.github.javaparser.ast.comments.JavadocComment;
+import com.github.javaparser.ast.visitor.VoidVisitorAdapter;
+import org.codehaus.groovy.groovydoc.GroovyClassDoc;
+import org.codehaus.groovy.tools.groovydoc.LinkArgument;
+
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Properties;
+import java.util.stream.Collectors;
+
+public class SimpleJavaClassDocAssembler {
+    private final String packagePath;
+    private final String javaSourceContent;
+    private final String className;
+    private final List<LinkArgument> links;
+    private final Properties properties;
+    private final Map<String, GroovyClassDoc> classDocs = new 
LinkedHashMap<>();
+
+    public SimpleJavaClassDocAssembler(String packagePath, String file, String 
javaSourceContent, List<LinkArgument> links, Properties properties) {
+        this.packagePath = packagePath;
+        this.javaSourceContent = javaSourceContent;
+        this.links = links;
+        this.properties = properties;
+
+        if (file != null && file.contains(".")) {
+            // todo: replace this simple idea of default class name
+            int idx = file.lastIndexOf(".");
+            className = file.substring(0, idx);
+        } else {
+            className = file;
+        }
+    }
+
+    private Map<String, GroovyClassDoc> parse() {
+        new VoidVisitorAdapter<Object>() {
+            @Override
+            public void visit(JavadocComment comment, Object arg) {
+                super.visit(comment, arg);
+
+                System.out.println(collect(comment));
+//
+//                String title = String.format("%s (%s)", comment.getName(), 
packagePath);
+//                System.out.println(title);
+//                System.out.println(comment.get());
+//                System.out.println("--------------------------");
+//                Javadoc javadoc = comment.get().asJavadocComment().parse();
+//                System.out.println(javadoc.getDescription().toText());
+//                javadoc.getBlockTags().forEach(e -> 
System.out.println(e.getName() + ":" + e.getContent()));
+//                System.out.println("===========================");
+            }
+        }.visit(JavaParser.parse(javaSourceContent), null);
+
+        return classDocs;
+    }
+
+    private static String collect(JavadocComment comment) {
+        Optional<Node> commentedNode = comment.getCommentedNode();
+
+        if (!commentedNode.isPresent()) {
+            return null;
+        }
+
+        Node node = commentedNode.get();
+
+        if (node instanceof MethodDeclaration) {
+            MethodDeclaration methodDeclaration = (MethodDeclaration) node;
+            return "Method " + methodDeclaration.getDeclarationAsString();
+        }
+        if (node instanceof ConstructorDeclaration) {
+            ConstructorDeclaration constructorDeclaration = 
(ConstructorDeclaration) node;
+            return "Constructor " + 
constructorDeclaration.getDeclarationAsString();
+        }
+        if (node instanceof FieldDeclaration) {
+            FieldDeclaration fieldDeclaration = (FieldDeclaration) node;
+            List<String> varNames = 
fieldDeclaration.getVariables().stream().map(v -> 
v.getName().getId()).collect(Collectors.toList());
+            return "Field " + String.join(", ", varNames);
+        }
+        if (node instanceof EnumConstantDeclaration) {
+            EnumConstantDeclaration enumConstantDeclaration = 
(EnumConstantDeclaration) node;
+            return "Enum Constant" + enumConstantDeclaration.getName();
+        }
+        if (node instanceof ClassOrInterfaceDeclaration) {
+            ClassOrInterfaceDeclaration classOrInterfaceDeclaration = 
(ClassOrInterfaceDeclaration) node;
+
+            if (classOrInterfaceDeclaration.isInterface()) {
+                return "Interface " + classOrInterfaceDeclaration.getName() + 
": " + comment.parse().getDescription().toText();
+            } else {
+                return "Class " + classOrInterfaceDeclaration.getName() + ": " 
+ comment.parse().getDescription().toText();
+            }
+        }
+        if (node instanceof EnumDeclaration) {
+            EnumDeclaration enumDeclaration = (EnumDeclaration) node;
+            return "Enum " + enumDeclaration.getName();
+        }
+        if (node instanceof AnnotationDeclaration) {
+            AnnotationDeclaration annotationDeclaration = 
(AnnotationDeclaration) node;
+            return "Annotation " + annotationDeclaration.getName();
+        }
+
+        return node.toString();
+    }
+}
\ No newline at end of file
diff --git 
a/subprojects/groovy-groovydoc/src/test/groovy/org/apache/groovy/tools/groovydoc/SimpleJavaClassDocAssemblerTest.groovy
 
b/subprojects/groovy-groovydoc/src/test/groovy/org/apache/groovy/tools/groovydoc/SimpleJavaClassDocAssemblerTest.groovy
new file mode 100644
index 0000000..677dd4b
--- /dev/null
+++ 
b/subprojects/groovy-groovydoc/src/test/groovy/org/apache/groovy/tools/groovydoc/SimpleJavaClassDocAssemblerTest.groovy
@@ -0,0 +1,27 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you 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.
+ */
+package org.apache.groovy.tools.groovydoc
+
+class SimpleJavaClassDocAssemblerTest extends GroovyTestCase {
+    void testClassDoc() {
+        def src = 
SimpleJavaClassDocAssemblerTest.getResourceAsStream('/org/apache/groovy/tools/groovydoc/SimpleJavaClassDocAssemblerTest1.java').text
+        def sjcda = new 
SimpleJavaClassDocAssembler('org/apache/groovy/tools/groovydoc', 
'SimpleJavaClassDocAssemblerTest1.java', src, [], new Properties())
+        sjcda.parse()
+    }
+}
\ No newline at end of file
diff --git 
a/subprojects/groovy-groovydoc/src/test/resources/org/apache/groovy/tools/groovydoc/SimpleJavaClassDocAssemblerTest1.java
 
b/subprojects/groovy-groovydoc/src/test/resources/org/apache/groovy/tools/groovydoc/SimpleJavaClassDocAssemblerTest1.java
new file mode 100644
index 0000000..87d4668
--- /dev/null
+++ 
b/subprojects/groovy-groovydoc/src/test/resources/org/apache/groovy/tools/groovydoc/SimpleJavaClassDocAssemblerTest1.java
@@ -0,0 +1,39 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you 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.
+ */
+package org.apache.groovy.tools.groovydoc;
+
+/**
+ * This a class doc
+ */
+public class SimpleJavaClassDocAssemblerTest1 {
+    /**
+     * The greeting word
+     */
+    public static final String HELLO = "hello";
+
+    /**
+     * say hello
+     *
+     * @param name some name
+     * @return the greeting words
+     */
+    public String hello(String name) {
+        return HELLO + "," + name;
+    }
+}

Reply via email to