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

import Torello.Java.LV;
import Torello.Java.Additional.Ret2;

import java.util.Vector;
import java.util.stream.IntStream;
import java.util.stream.Stream;

class Retrieve
{
    static Ret2<int[], String[]> retrieve(
            final Vector<? super TagNode>   html,
            final int                       sPos,
            final int                       ePos,
            final String                    attribute
        )
    {
        InnerTagKeyException.check(attribute);

        // Use a Java Int-Stream.  Save matches here (vector-position)
        IntStream.Builder posB = IntStream.builder();

        // Use a Java Stream<String>.  Save attribute-values here
        Stream.Builder<String> strB = Stream.builder();

        // Temp Variables & Loop Variable
        LV      l = new LV(sPos, ePos, html);
        TagNode tn;
        String  attribValue;

        for (int i=l.start; i < l.end; i++)

            // Only Visit Open TagNode Elements with '.str' long enough to contain attributes
            if ((tn = ((HTMLNode) html.elementAt(i)).openTagPWA()) != null)

                // If the Open-Tag does not have the attribute, skip the node. If it does, save it.
                if ((attribValue = tn.AV(attribute)) != null)
                { 
                    posB.accept(i);             // Save the vector-index position of the TagNode
                    strB.accept(attribValue);   // Save the Attribute-Value inside that TagNode
                }

        // Java Stream's shall build the arrays.  Put them into an instance of Ret2, and return
        return new Ret2<>(posB.build().toArray(), strB.build().toArray(String[]::new));
    }

    static String[] retrieve(
            final Vector<? super TagNode>   html,
            final int[]                     posArr,
            final String                    attribute
        )
    {
        InnerTagKeyException.check(attribute);

        // Return Array, and its corresponding array-index pointer.
        String[]    ret = new String[posArr.length];
        int         i   = 0;


        // Minimum length of the TagNode.str to even have the specified attribute
        // '<', TOKEN, SPACE, INNERTAG, '=', '>'

        int MIN = 4 + attribute.length();

        for (int pos: posArr)
        {
            HTMLNode n = (HTMLNode) html.elementAt(pos);

            if (! n.isTagNode()) throw new TagNodeExpectedException(pos);

            TagNode tn = (TagNode) n;

            if (tn.isClosing) throw new OpeningTagNodeExpectedException(pos);

            ret[i++] = (tn.str.length() < (tn.tok.length() + MIN))

                ? null              // CASE-1: TagNode.str is too short to even have the attribute
                : tn.AV(attribute); // CASE-2: Possibly has it: Save the result of TagNode.AV(...)
        }

        return ret;
    }

}