All Downloads are FREE. Search and download functionalities are using the official Maven repository.

sk.antons.sb.rest.doclet.wrap.ElementWrap Maven / Gradle / Ivy

/*
 * Copyright 2020 Anton Straka
 *
 * 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.
 */
package sk.antons.sb.rest.doclet.wrap;

import com.sun.source.doctree.DocCommentTree;
import com.sun.source.doctree.DocTree;
import com.sun.source.util.TreePath;
import java.util.List;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.Element;
import sk.antons.jaul.Is;
import sk.antons.jaul.binary.Unicode;
import sk.antons.jaul.xml.Xml;

/**
 *
 * @author antons
 */
public abstract class ElementWrap {
    Element element;
    WrapEnv env;
    public ElementWrap(Element element, WrapEnv env) {
        this.element = element;
        this.env = env;
    }
    
    public String simpleName() { return element.getSimpleName().toString(); }
    
    DocCommentTree dcTree = null;
    protected DocCommentTree dcTree(){
        if(dcTree != null) return dcTree;
        if(element == null) return null;
        try {
            //TreePath dct = env.treeUtils.getPath(element);
            dcTree = env.treeUtils.getDocCommentTree(element);
        } catch(Throwable e) {
        }
        return dcTree;

    }
    
    public String javadoc() {
        if(element == null) return "";
        if(dcTree() == null) return "";

        StringBuilder html = new StringBuilder();
        html.append("
\n"); String value = toString(dcTree.getFirstSentence()); String value2 = toString(dcTree.getBody()); if((value2 != null) || (value != null)) { html.append("
"); if(value != null) { html.append(""); html.append(value); html.append(""); } if(value2 != null) { html.append(" "); html.append(value2); html.append("\n"); } html.append("
\n"); } List list = dcTree.getBlockTags(); if(!Is.empty(list)) { for(DocTree docTree : list) { html.append("
"); html.append(docTree); html.append("
\n"); } } html.append("
\n"); return html.toString(); } public String fixannotations(String text) { if(element == null) return ""; StringBuilder sb = new StringBuilder(); int lastpos = 0; int atpos = text.indexOf('@', lastpos); while(atpos > -1) { sb.append(text.substring(lastpos, atpos+1)); lastpos = atpos + 1; int pos = lastpos; char c = text.charAt(pos++); while( (pos < text.length()) && ( (c == '.') || (c == '_') || (c == '@') || (('a'<=c) && (c<='z')) || (('A'<=c) && (c<='Z')) || (('0'<=c) && (c<='9')) )) { if(c == '.') lastpos = pos; c = text.charAt(pos++); } atpos = text.indexOf('@', lastpos); } sb.append(text.substring(lastpos)); return sb.toString(); } public String annotations() { if(element == null) return ""; List list = element.getAnnotationMirrors(); if(Is.empty(list)) return ""; StringBuilder html = new StringBuilder(); html.append("
\n"); for(AnnotationMirror annotationMirror : list) { String text = annotationMirror.toString(); text = fixannotations(text); html.append("
"); html.append(text); html.append("
\n"); } html.append("
\n"); return html.toString(); } public String javadocFirst() { if(element == null) return ""; if(dcTree() == null) return ""; String value = toString(dcTree.getFirstSentence()); return value; } private String toString(List list) { if(Is.empty(list)) return ""; StringBuilder sb = new StringBuilder(); for(DocTree docTree : list) { sb.append(" ").append(docTree); } if(sb.length() == 0 ) return null; return Unicode.unescape(sb.toString()); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy