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
package Torello.JavaDoc;

import java.io.File;
import java.io.IOException;
import java.util.Objects;
import java.util.Vector;

import Torello.HTML.HTMLNode;
import Torello.JavaDoc.SyntaxHiLite.HiLiteCache;
import Torello.JavaDoc.SyntaxHiLite.Pygmentize;

/**
 * A simple Functional-Interface allowing a user to swap in any customized, alternate or
 * proprietary Syntax HiLiter to replace the default HiLiter used by this Upgrader Tool.
 * 
 * <BR /><BR /><EMBED CLASS='external-html' DATA-FILE-ID=HILITER>
 */
@FunctionalInterface
public interface HiLiter
{
    /**
     * This {@code Functional Interface} expects this method to be implemented.  Any HiLiter that
     * would be "plugged into" this Documentation Tool must be able to <B>Pretty-Print HiLite</B>
     * input Source-Code (passed as a {@code java.lang.String}) <I>and return HTML</I>.
     * 
     * <BR /><BR /><B>NOTE:</B> Any Code HiLiter that can operate with this input may be plugged
     * in here.  In order to "Vectorize" HTML, just use the
     * {@code HTMLPage.getPageTokens(String, false)} method.
     *
     * @param code This is the Source-Code as a {@code java.lang.String}
     * 
     * @param codeType This is a short descriptor of what kind of code is being passed.  The most
     * frequent values for this parameter are: {@code 'java', 'html', 'js'} etc...
     * 
     * @param snippetOrComplete This is a {@code boolean} that when {@code TRUE}, indicates that
     * a <B>code-snippet</B> has been passed, and when {@code FALSE} indicates that a <B>complete
     * source code file</B> has been passed.
     * 
     * @return The intention is to return a Vectorized-HTML page (or sub-page) that contains a
     * <B>HiLited &AMP; Pretty-Printed</B> version of the source-code.
     * 
     * @throws IOException This method is not defined, but it is permitted to throw 
     * {@code IOException} - if there have been I/O problems when attempting to transform the
     * source-code.
     *
     * @throws HiLiteException This exception may be used if other problems occur during the
     * transformation process.
     */
    public Vector<HTMLNode> hiLite
        (String code, String codeType, boolean snippetOrComplete)
        throws IOException, HiLiteException;

    /**
     * The Default <B>Code HiLiter</B> uses the package {@code Torello.JavaDoc.SyntaxHiLite}.
     * 
     * @param cache This is the HiLiter-Cache that <I>may or may not be used</I>.  
     * @return An instance of {@code HiLiter} that can be used by the {@code Upgrade} Tool
     */
    public static HiLiter getDefault(final HiLiteCache cache)
    {
        Objects.requireNonNull(cache, "HiLiteCache 'cache' Parameter has been passed null.");
        return new JDU_HL(cache);
    }
}

class JDU_HL implements HiLiter
{
    private final Pygmentize    pygmentize = new Pygmentize();
    private final HiLiteCache   cache;

    JDU_HL(HiLiteCache cache) { this.cache = cache; }

    public Vector<HTMLNode> hiLite
        (String code, String codeType, boolean snippetOrComplete)
        throws IOException
    {
        final byte      cssPrefix       = snippetOrComplete ? (byte) 1 : (byte) 2;
        final boolean   useLineNumbers  = ! snippetOrComplete;

        return pygmentize.hiLite(code, codeType, useLineNumbers, cssPrefix, cache);
    }
}