1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package Torello.JDUInternal.Annotations.EntityAnnotations.Processor;

import Torello.Java.StrSource;
import Torello.Java.StrCh;
import Torello.Java.StringParse;

import Torello.Java.Additional.EffectivelyFinal;

import Torello.JavaDoc.LinkJavaSource;
import Torello.JavaDoc.hidden.LJSRepeatable;

import Torello.JDUInternal.Annotations.HELPER;

import javax.annotation.processing.Messager;

import javax.lang.model.element.Element;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.AnnotationValue;

import javax.tools.Diagnostic;

import java.util.List;
import java.util.Arrays;

import java.util.stream.Stream;

// @IntoHTMLTable Annotation:
// 
// EXPORTS:
//      public Background background() default Background.Blue;
//      public String title() default "";
//      public String divCSSClass() default "";
//      public String tableCSSClass() default "";
// 
// 
// The following files are relatedf to this 'javac' Annotation-Processor:
// 
// * Torello.JDUInternal.SimpleFeatures.IntoHTMLTableProc
//      This is the primary or "Work Horse" Method for Generating the HTML-Table that is inserted
//      into the Detail-Entry "description()" section.
// 
// * User-Annotations.css
//      For the CSS Color Choices/Options/Definitions
//      Since there are a suite of Color-Choices which have been presented to the user
// 
// * Torello.JavaDoc.IntoHTMLTable
//      For the actual annotation definition.
//      This is the actual @interface for the @IntoHTMLTable Annotation
// 
// * Torello.JDUInternal.Annotations.EntityAnnotations.Mirror.IHTMirror
//      The Data-Contents of Annotation that has been placed by the User on the Entity
//      (Entity: Method, Field, Constructor, Enum-Constant, Annotation-Element)
// 
// * Torello.JDUInternal.Annotations.EntityAnnotations.Processor.IntoHTMLTableProcessor
//      The Annotation-Processor that is invoked by 'javac' when compiling a class that uses the
//      @IntoHTMLTable Annotation.  It's a very minimalist processor that actually does very little
//      There is no way to do very much meaningful Error-Checking when invoked by 'javac'
//      Until the Java-Doc Upgrader is running, you cannot even identify errors.

public class IntoHTMLTableProcessor
{
    // This is used by the Error-Message Printer
    private static final String IHT_NAME =
        Torello.JavaDoc.IntoHTMLTable.class.getSimpleName();

    // @IntoHTMLTable Annotation:
    // 
    // EXPORTS:
    //      public Background background() default Background.Blue;
    //      public String title() default "";
    //      public String divCSSClass() default "";
    //      public String tableCSSClass() default "";

    public static boolean process(
            Element             ciet,
            AnnotationMirror    am,
            javax.annotation.processing.Messager messager
        )
    {
        EffectivelyFinal<Boolean> errors = new EffectivelyFinal<>(false);

        am.getElementValues().forEach((ExecutableElement ee, AnnotationValue av) ->
        {
            // Understanding the 'terminology' in Annotations and Annotation-Processor's is half of
            // the work in using it.

            final Object val  = av.getValue();

            switch (ee.toString())
            {
                case "background()" :
                    break;

                case "title()" :
                    errors.f |= checkTitleAE(messager, ciet, (String) val);
                    break;

                case "divCSSClass()" :
                    errors.f |= checkCSSClassAE(messager, ciet, "divCSSClass()", (String) val);
                    break;

                case "tableCSSClass()" :
                    errors.f |= checkCSSClassAE(messager, ciet, "divCSSClass()", (String) val);
                    break;

                default: 

                    // This should be UNREACHABLE-CODE.  The Java-Compiler, itself, should do the
                    // complaining that the user supplied an Annotation-Element name that is not
                    // among those listed above.  Unless 'javac' changes, this cannot execute.

                    throw new InternalError(
                        "There was an annotation parameter whose name wasn't recognized: " +
                        ee.toString() + "\n" +
                        "The only parameter's that may be passed to @IntoHTMLTable are:\n" +
                        "'background()', 'title()', 'divCSSClass()' and 'tableCSSClass()'"
                    );
            }
        });        

        return errors.f;
    }


    // ********************************************************************************************
    // ********************************************************************************************
    // Checks both the "name()" and "handle()" Annotation-Elements
    // ********************************************************************************************
    // ********************************************************************************************


    private static boolean checkTitleAE(
            javax.annotation.processing.Messager messager,
            Element ciet,
            String  aeValAsStr
        )
    {
        // THIS TEST IS BLOCK-COPIED IDENTICAL TO THE TEST THAT IS LOCATED INSIDE
        // "Torello.JDUInternal.Annotations.EntityAnnotations.Mirror.IHTErrorCheck"

        final boolean test =
                (! StrCh.containsOR(aeValAsStr, '\t', '\n', '\r'))
            &&  (aeValAsStr.length() <= 300);

        if (test) return true;

        // NOTE: Torello.JDUInternal.Messager **DOES NOT** ALLOW '\t'
        messager.printMessage(
            Diagnostic.Kind.ERROR,
            HELPER.LOCATION(ciet, IHT_NAME) +
            "\tString Annotation-Parameter 'title()' was passed " +
                "[" + aeValAsStr + "]\n" +
            "\tHowever, this String may not contain the characters: '\\t', '\\n', '\\r'\n" +
            "\tNor may it be more than 300 Characters long."
        );

        return false;
    }


    // ********************************************************************************************
    // ********************************************************************************************
    // Checks the "typeName()" Annotation-Elements
    // ********************************************************************************************
    // ********************************************************************************************


    private static boolean checkCSSClassAE(
            final javax.annotation.processing.Messager messager,
            final Element   ciet,
            final String    aeName,
            final String    aeValAsStr
        )
    {
        if (aeValAsStr.length() == 0) return true;

        final String[] cssClasses = aeValAsStr.split("\\s");

        final Stream.Builder<String> b = Stream.builder();

        boolean errors = false;

        for (final String cssClass : cssClasses)

            if (! StrSource.isCSSClassName(cssClass))
            {
                b.accept(cssClass);
                errors = true;
            }

        if (! errors) return true;

        messager.printMessage(
            Diagnostic.Kind.ERROR,
            HELPER.LOCATION(ciet, IHT_NAME) +

            // NOTE: Torello.JDUInternal.Messager **DOES NOT** ALLOW '\t'
            "\tString Annotation-Parameter " + aeName + " was passed " +
                "[" + aeValAsStr + "]\n" +
            "\tHowever, the following CSS-Classes are invalid CSS Class-Names:\n" +
            Arrays.toString(b.build().toArray())
        );

        return false;
    }
}