From 3be50df7b6212e48f13a132d8abcd14a8dbd3255 Mon Sep 17 00:00:00 2001 From: Chris Hostetter Date: Thu, 26 Oct 2017 10:37:29 -0700 Subject: [PATCH] SOLR-11552: ref-guide tools should fail build if any page exists with #parents != 1 --- .../tools/BuildNavAndPDFBody.java | 42 ++++++++++++++++--- 1 file changed, 36 insertions(+), 6 deletions(-) diff --git a/solr/solr-ref-guide/tools/BuildNavAndPDFBody.java b/solr/solr-ref-guide/tools/BuildNavAndPDFBody.java index cb2361ae700..d62f538707d 100644 --- a/solr/solr-ref-guide/tools/BuildNavAndPDFBody.java +++ b/solr/solr-ref-guide/tools/BuildNavAndPDFBody.java @@ -67,9 +67,21 @@ public class BuildNavAndPDFBody { if (null == mainPage) { throw new RuntimeException("no main-page found with shortname: " + mainPageShortname); } - mainPage.buildKidsRecursive(allPages); + // NOTE: mainPage claims to be it's own parent to prevent anyone decendent from introducing a loop + mainPage.buildPageTreeRecursive(mainPage, allPages); - // TODO: use depthFirstWalk to prune allPages to validate that we don't have any loops or orphan pages + { // validate that there are no orphan pages + int orphans = 0; + for (Page p : allPages.values()) { + if (null == p.getParent()) { + orphans++; + System.err.println("ERROR: Orphan page: " + p.file); + } + } + if (0 != orphans) { + throw new RuntimeException("Found " + orphans + " orphan pages (which are not in the 'page-children' attribute of any other pages)"); + } + } // Build up the PDF file, @@ -213,7 +225,14 @@ public class BuildNavAndPDFBody { public final String permalink; public final List kidShortnames; /** NOTE: not populated on construction - * @see #buildKidsRecursive + * @see #buildPageTreeRecursive + */ + private Page parent; + public Page getParent() { + return parent; + } + /** NOTE: not populated on construction + * @see #buildPageTreeRecursive */ public final List kids; private final List mutableKids; @@ -258,15 +277,26 @@ public class BuildNavAndPDFBody { this.kids = Collections.unmodifiableList(mutableKids); } - /** Recursively populates {@link #kids} from {@link #kidShortnames} via the allPages Map */ - public void buildKidsRecursive(Map allPages) { + /** + * Recursively sets {@link #getParent} and populates {@link #kids} from {@link #kidShortnames} + * via the allPages Map + */ + public void buildPageTreeRecursive(Page parent, Map allPages) { + if (null != parent) { + if (null != this.parent) { + // as long as we also check (later) that every page has a parent, this check (prior to recusion) + // also ensures we never have any loops + throw new RuntimeException(file.getName() + " is listed as the child of (at least) 2 pages: '" + parent.shortname + "' and '" + this.parent.shortname + "'"); + } + this.parent = parent; + } for (String kidShortname : kidShortnames) { Page kid = allPages.get(kidShortname); if (null == kid) { throw new RuntimeException("Unable to locate " + kidShortname + "; child of " + shortname + "("+file.toString()); } mutableKids.add(kid); - kid.buildKidsRecursive(allPages); + kid.buildPageTreeRecursive(this, allPages); } }