• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

knowledgepixels / nanodash / 28239341232

26 Jun 2026 12:54PM UTC coverage: 28.047% (+0.4%) from 27.648%
28239341232

Pull #516

github

web-flow
Merge 6f3af466b into dbca2aa96
Pull Request #516: Strip parent/child word overlap in breadcrumb labels

1720 of 6991 branches covered (24.6%)

Branch coverage included in aggregate %.

3605 of 11995 relevant lines covered (30.05%)

4.45 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

76.13
src/main/java/com/knowledgepixels/nanodash/component/TitleBar.java
1
package com.knowledgepixels.nanodash.component;
2

3
import java.io.Serializable;
4
import java.util.ArrayList;
5
import java.util.List;
6

7
import org.apache.wicket.behavior.AttributeAppender;
8
import org.apache.wicket.markup.html.WebMarkupContainer;
9
import org.apache.wicket.markup.html.WebPage;
10
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
11
import org.apache.wicket.markup.html.panel.EmptyPanel;
12
import org.apache.wicket.markup.html.panel.Panel;
13
import org.apache.wicket.markup.repeater.Item;
14
import org.apache.wicket.markup.repeater.data.DataView;
15
import org.apache.wicket.markup.repeater.data.ListDataProvider;
16

17
import com.knowledgepixels.nanodash.NanodashPageRef;
18
import com.knowledgepixels.nanodash.NanodashPreferences;
19
import com.knowledgepixels.nanodash.page.NanodashPage;
20
import com.knowledgepixels.nanodash.page.PublishPage;
21
import com.knowledgepixels.nanodash.page.QueryListPage;
22
import com.knowledgepixels.nanodash.page.SpaceListPage;
23
import com.knowledgepixels.nanodash.page.UserListPage;
24

25
/**
26
 * TitleBar is the top bar of the Nanodash application, which contains
27
 * navigation elements such as profile, my channel, users, connectors,
28
 * publish, query, and breadcrumb navigation.
29
 */
30
public class TitleBar extends Panel {
31

32
    private String highlight;
33

34
    /**
35
     * The breadcrumb/tab strip row (the grey band just below the nav bar). Holds
36
     * the breadcrumb links on the left and, optionally, the resource tab strip on
37
     * the right (see {@link #setTabs(ResourceTabs)}).
38
     */
39
    private WebMarkupContainer breadcrumbPath;
40

41
    /**
42
     * Constructs a TitleBar with the specified id, page, highlight element,
43
     * and an array of path references for breadcrumb navigation.
44
     *
45
     * @param id        the component id
46
     * @param page      the current Nanodash page
47
     * @param highlight the id of the element to highlight
48
     * @param pathRefs  an array of NanodashPageRef for breadcrumb navigation
49
     */
50
    public TitleBar(String id, NanodashPage page, String highlight, NanodashPageRef... pathRefs) {
51
        super(id);
9✔
52
        this.highlight = highlight;
9✔
53
        add(new ProfileItem("profile", page));
39✔
54
        // Centered title-bar message: the post-publish confirmation and/or the
55
        // always-on "you haven't published an introduction yet" warning.
56
        add(new JustPublishedMessagePanel("justPublishedMessage", page.getPageParameters()));
42✔
57

58
        createNavLink("users", UserListPage.class);
15✔
59
        createNavLink("connectors", SpaceListPage.class);
15✔
60
        createNavLink("publish", PublishPage.class).setVisible(!NanodashPreferences.get().isReadOnlyMode());
33!
61
        createNavLink("query", QueryListPage.class);
15✔
62

63
        breadcrumbPath = new WebMarkupContainer("breadcrumbpath");
18✔
64
        WebMarkupContainer breadcrumbLinks = new WebMarkupContainer("breadcrumblinks");
15✔
65
        List<CrumbPart> crumbParts = buildCrumbParts(pathRefs);
9✔
66
        if (!crumbParts.isEmpty()) {
9!
67
            CrumbPart first = crumbParts.get(0);
×
68
            breadcrumbLinks.add(first.ref().createComponent("firstpathelement", first.label()));
×
69
            // Getting serialization exception if not using 'new ArrayList<...>(...)' here:
70
            List<CrumbPart> moreParts = new ArrayList<CrumbPart>(crumbParts.subList(1, crumbParts.size()));
×
71
            breadcrumbLinks.add(new DataView<CrumbPart>("morepathelements", new ListDataProvider<CrumbPart>(moreParts)) {
×
72

73
                @Override
74
                protected void populateItem(Item<CrumbPart> item) {
75
                    CrumbPart part = item.getModelObject();
×
76
                    item.add(part.ref().createComponent("furtherpathelement", part.label()));
×
77
                }
×
78

79
            });
80
        } else {
×
81
            breadcrumbLinks.setVisible(false);
12✔
82
        }
83
        breadcrumbPath.add(breadcrumbLinks);
30✔
84
        // Tab strip placeholder (right side of the strip); replaced via setTabs().
85
        breadcrumbPath.add(new EmptyPanel("tabs").setVisible(false));
45✔
86
        // The strip shows when there is a breadcrumb to display; setTabs() also
87
        // forces it visible so a tab strip shows even without a breadcrumb.
88
        breadcrumbPath.setVisible(pathRefs.length > 0);
24!
89
        add(breadcrumbPath);
30✔
90
    }
3✔
91

92
    /**
93
     * One breadcrumb segment: the {@link NanodashPageRef} it links to and the
94
     * (possibly split) label text to show for it.
95
     */
96
    public record CrumbPart(NanodashPageRef ref, String label) implements Serializable {
27✔
97
    }
98

99
    /**
100
     * Flattens a breadcrumb path into the segments to render. Three transforms are
101
     * applied to each ref's label:
102
     * <ul>
103
     *   <li>For each non-root crumb, the part it shares with its parent label
104
     *       is stripped (e.g. parent "Knowledge Pixels", child "Knowledge
105
     *       Pixels Incubator" renders as "Incubator"). The shared part is the
106
     *       longest common character prefix, but only stripped when it covers
107
     *       (almost) all of the parent and ends on a non-letter/digit boundary
108
     *       in the child — this also catches singular/plural variations like
109
     *       parent "Nano Sessions", child "Nano Session #30" → "#30".</li>
110
     *   <li>For each non-root crumb, a word-level overlap where the child's
111
     *       leading word(s) restate the parent's trailing word(s) is stripped
112
     *       (e.g. parent "Abc Def Ghi", child "Ghi Jkl" renders as "Jkl"). The
113
     *       longest such parent-suffix/child-prefix overlap wins, matched on
114
     *       whole words case-insensitively, and never strips the whole child
115
     *       away.</li>
116
     *   <li>Only the leading segment of the remaining label is kept — the part
117
     *       before the first list/title separator ({@code ,}, {@code :},
118
     *       {@code ;}, {@code |}, or a spaced {@code -}) — so each path level
119
     *       renders as one concise crumb. E.g. "FIP.38.T.8 | FAIR Implementation
120
     *       Profile Training Session 8" renders as "FIP.38.T.8" and "3PFF: the
121
     *       Three Point FAIRification Framework" as "3PFF".</li>
122
     * </ul>
123
     * The parent-prefix comparison uses the original (un-simplified) labels.
124
     */
125
    static List<CrumbPart> buildCrumbParts(NanodashPageRef[] pathRefs) {
126
        List<CrumbPart> parts = new ArrayList<>();
12✔
127
        for (int i = 0; i < pathRefs.length; i++) {
24✔
128
            String label = pathRefs[i].getLabel();
15✔
129
            if (label == null) {
6!
130
                parts.add(new CrumbPart(pathRefs[i], null));
×
131
                continue;
×
132
            }
133
            if (i > 0) {
6✔
134
                String parentLabel = pathRefs[i - 1].getLabel();
21✔
135
                label = stripParentPrefix(parentLabel, label);
12✔
136
                label = stripParentOverlap(parentLabel, label);
12✔
137
            }
138
            // Show only the leading segment (before the first list/title separator)
139
            // so each path level renders as one concise crumb — e.g.
140
            // "FIP.38.T.8 | FAIR Implementation Profile Training Session 8" → "FIP.38.T.8",
141
            // "3PFF: the Three Point FAIRification Framework" → "3PFF".
142
            List<String> segments = splitLabel(label);
9✔
143
            if (!segments.isEmpty()) {
9!
144
                parts.add(new CrumbPart(pathRefs[i], segments.get(0)));
39✔
145
            }
146
        }
147
        return parts;
6✔
148
    }
149

150
    /**
151
     * Splits a label on list/title separators — comma, colon, semicolon, pipe
152
     * (with or without surrounding spaces), or a space-surrounded hyphen — into
153
     * trimmed, non-empty segments. Returns the trimmed whole label if there is
154
     * no separator (so the result always has at least one element).
155
     */
156
    static List<String> splitLabel(String label) {
157
        List<String> segments = new ArrayList<>();
12✔
158
        if (label == null) return segments;
6!
159
        for (String s : label.split("\\s*[,;:]\\s+|\\s*\\|\\s*|\\s+-\\s+")) {
54✔
160
            String trimmed = s.trim();
9✔
161
            if (!trimmed.isEmpty()) segments.add(trimmed);
21!
162
        }
163
        if (segments.isEmpty()) {
9!
164
            String trimmed = label.trim();
×
165
            if (!trimmed.isEmpty()) segments.add(trimmed);
×
166
        }
167
        return segments;
6✔
168
    }
169

170
    /**
171
     * If the child label appears to restate the parent's "topic" at the start,
172
     * strips that shared prefix and returns the remainder. Otherwise returns
173
     * the child label unchanged.
174
     */
175
    private static String stripParentPrefix(String parent, String child) {
176
        if (parent == null || parent.isEmpty() || child == null) return child;
21!
177
        int lcp = 0;
6✔
178
        int max = Math.min(parent.length(), child.length());
18✔
179
        while (lcp < max && parent.charAt(lcp) == child.charAt(lcp)) lcp++;
36✔
180
        // Require a substantial match: at least 3 chars, and within 2 chars of
181
        // the full parent length (so things like "Sessions" vs "Session" still
182
        // match, but "Nanopublication Sessions" vs "Nano Session #30" doesn't).
183
        if (lcp < 3 || lcp < parent.length() - 2) return child;
33!
184
        // The boundary in the child must not fall mid-word, otherwise we'd be
185
        // chopping off a partial word like "Foo Bar" -> "Foo Baz Quux" -> "z Quux".
186
        if (lcp >= child.length() || Character.isLetterOrDigit(child.charAt(lcp))) return child;
27!
187
        String remainder = child.substring(lcp).replaceAll("^\\s+", "");
21✔
188
        if (remainder.isEmpty()) return child;
9!
189
        return remainder;
6✔
190
    }
191

192
    /**
193
     * If the child label's leading word(s) restate the parent label's trailing
194
     * word(s), strips that overlap and returns the remainder. E.g. parent
195
     * "Abc Def Ghi", child "Ghi Jkl" → "Jkl", or parent "Knowledge Pixels
196
     * Incubator Office Hours", child "Office Hour 24 June 2026" → "24 June 2026".
197
     * The overlap is matched on whole words, case-insensitively and tolerating
198
     * singular/plural variation ("Hours" ≈ "Hour"); the longest parent-suffix that
199
     * matches a child-prefix wins. Returns the child unchanged when there is no
200
     * overlap, or when the overlap would consume the whole child label (at least
201
     * one word is always kept).
202
     */
203
    static String stripParentOverlap(String parent, String child) {
204
        if (parent == null || parent.isEmpty() || child == null || child.isEmpty()) return child;
36!
205
        String[] parentWords = parent.trim().split("\\s+");
15✔
206
        String[] childWords = child.trim().split("\\s+");
15✔
207
        // Keep at least one child word, so the overlap can cover at most all but
208
        // the last child word.
209
        int maxK = Math.min(parentWords.length, childWords.length - 1);
24✔
210
        for (int k = maxK; k >= 1; k--) {
21✔
211
            boolean match = true;
6✔
212
            for (int j = 0; j < k; j++) {
21✔
213
                if (!wordsEquivalent(parentWords[parentWords.length - k + j], childWords[j])) {
39✔
214
                    match = false;
6✔
215
                    break;
3✔
216
                }
217
            }
218
            if (match) {
6✔
219
                return String.join(" ", java.util.Arrays.asList(childWords).subList(k, childWords.length));
27✔
220
            }
221
        }
222
        return child;
6✔
223
    }
224

225
    /**
226
     * Whether two breadcrumb words count as the same topic word: equal ignoring
227
     * case, or equal once a single trailing plural "s" is dropped from each (so
228
     * "Hours" matches "Hour", "Sessions" matches "Session").
229
     */
230
    private static boolean wordsEquivalent(String a, String b) {
231
        if (a.equalsIgnoreCase(b)) return true;
18✔
232
        return depluralize(a).equalsIgnoreCase(depluralize(b));
18✔
233
    }
234

235
    private static String depluralize(String word) {
236
        return word.length() > 1 && (word.endsWith("s") || word.endsWith("S"))
39!
237
                ? word.substring(0, word.length() - 1)
24✔
238
                : word;
3✔
239
    }
240

241
    /**
242
     * Places a resource tab strip (Content | About | Raw) on the right side of
243
     * the breadcrumb strip, and forces the strip visible so the tabs show even on
244
     * pages without a breadcrumb (e.g. user pages). Returns {@code this} for
245
     * fluent use at the {@code add(...)} call site.
246
     *
247
     * @param tabs the tab strip (its markup id must be {@code "tabs"})
248
     * @return this TitleBar
249
     */
250
    public TitleBar setTabs(ResourceTabs tabs) {
251
        breadcrumbPath.addOrReplace(tabs);
×
252
        breadcrumbPath.setVisible(true);
×
253
        return this;
×
254
    }
255

256
    private BookmarkablePageLink<Void> createNavLink(String id, Class<? extends WebPage> pageClass) {
257
        BookmarkablePageLink<Void> link = new BookmarkablePageLink<>(id, pageClass);
18✔
258
        if (id.equals(highlight)) {
15!
259
            link.add(new AttributeAppender("class", "selected"));
×
260
        }
261
        add(link);
27✔
262
        return link;
6✔
263
    }
264

265
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc