Skip to content

Commit cefea77

Browse files
authored
Xref Validations (#576)
* CCS-3775 Update view_uri to match the ideal URL path * added support for copy url and assembly fixes * CCS-3790 Product name and version url fragment missing from the view on Portal URL intemittenly * CCS-3790 Product name and version url fragment missing from the view… * CCS-3860 Return the Portal URL from Publish API * indentation formatting correction for new changes * CCS-3861 Use the portal URL from the Publish API * CCS-3861 renaming function to onGetUrl from getUrl to indicate it is a event * UI fixes for review comments * (CCS-4273) Create validation result Node(s) in JCR * checkout missed files, in last commit * CCS-4273 Code review changes for Create validation result Node(s) in JCR * CCS-4273 Renamed created node from integer to string * match all xrefs including filename and url * Xref matching to include all anchors and relative filepaths * Xref Validations 1.) Delete xref nodes, if xref is reuploaded 2.) Update Anchor based xrefs validatation 3.) Clone validation nodes from dependent modules to assembly * Xref Validations 1.) Delete xref nodes, if xref is reuploaded 2.) Update Anchor based xrefs validatation 3.) Clone validation nodes from dependent modules to assembly
1 parent eac2e12 commit cefea77

File tree

8 files changed

+68
-49
lines changed

8 files changed

+68
-49
lines changed

pantheon-bundle/src/main/java/com/redhat/pantheon/asciidoctor/extension/PantheonXrefProcessor.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public String preprocess(String content) {
5757
content = preprocessWithPattern(content, XREF_PATTERN, urlList);
5858
content = preprocessWithPattern(content, TRIANGLE_PATTERN, urlList);
5959
}
60-
new XrefValidationHelper().setObjectsToValidate(documentVariant.uuid().get(), urlList);
60+
new XrefValidationHelper().setObjectsToValidate(urlList);
6161
return content;
6262
}
6363

@@ -68,8 +68,15 @@ private String preprocessWithPattern(String line, Pattern pattern, List<String>
6868
String filepath = Optional.ofNullable(matcher.group("filepath")).orElse("");
6969
String anchor = Optional.ofNullable(matcher.group("anchor")).orElse("");
7070
String label = Optional.ofNullable(matcher.group("label")).orElse("");
71+
String pathPrefix = "";
72+
for (int pos = line.indexOf(matcher.group(0))-1; pos != -1; pos -=1 ) {
73+
pathPrefix = line.charAt(pos)+pathPrefix;
74+
if(pathPrefix.contains("\n")){
75+
pathPrefix = pathPrefix.substring(1);
76+
break;
77+
}
78+
}
7179

72-
filePaths.add(filepath);
7380
// Decide whether this is an xref that we can resolve
7481
// Assume it's a relative path to a file in the same repo for now
7582
Resource containingFolder = documentVariant.getParentLocale().getParent().getParent();
@@ -83,8 +90,13 @@ private String preprocessWithPattern(String line, Pattern pattern, List<String>
8390
} else {
8491
// TODO - Once validation exists, might want to add a check here for "target exists but is not publishable"
8592
matcher.appendReplacement(sb, matcher.group(0)); // ".group(0)" is the special group that contains the
86-
} // entire content of what was matched. I.e., we leave
87-
} // this alone/unmodified.
93+
// entire content of what was matched. I.e., we leave
94+
// this alone/unmodified.
95+
if(!pathPrefix.matches("^(\\s*\\/\\/[^\\n\\r]+$)")){
96+
filePaths.add(filepath);
97+
}
98+
}
99+
}
88100
matcher.appendTail(sb);
89101
return sb.toString();
90102
}

pantheon-bundle/src/main/java/com/redhat/pantheon/model/CustomModelAdapterFactory.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
ADAPTER_CLASSES + "=com.redhat.pantheon.model.module.ModuleVariant",
4141
ADAPTER_CLASSES + "=com.redhat.pantheon.model.module.ModuleVariants",
4242
ADAPTER_CLASSES + "=com.redhat.pantheon.model.module.ModuleVersion",
43+
ADAPTER_CLASSES + "=com.redhat.pantheon.validation.model.Validation",
4344
ADAPTER_CLASSES + "=com.redhat.pantheon.model.workspace.Workspace"
4445
}
4546
)

pantheon-bundle/src/main/java/com/redhat/pantheon/servlet/DocumentPreviewFilter.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,23 @@
11
package com.redhat.pantheon.servlet;
22

3-
import com.redhat.pantheon.helper.Symlinks;
43
import com.redhat.pantheon.jcr.JcrQueryHelper;
5-
import com.redhat.pantheon.model.document.DocumentVariant;
6-
import com.redhat.pantheon.servlet.assets.ImageServletFilter;
4+
import com.redhat.pantheon.validation.helper.XrefValidationHelper;
75
import org.apache.sling.api.SlingHttpServletRequest;
8-
import org.apache.sling.api.SlingHttpServletResponse;
96
import org.apache.sling.api.resource.Resource;
107
import org.apache.sling.api.resource.ResourceResolver;
11-
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
128
import org.apache.sling.servlets.annotations.SlingServletFilter;
13-
import org.apache.sling.servlets.annotations.SlingServletPaths;
149
import org.jetbrains.annotations.NotNull;
15-
import org.osgi.framework.Constants;
1610
import org.osgi.service.component.annotations.Component;
1711

18-
import javax.jcr.Node;
1912
import javax.jcr.RepositoryException;
20-
import javax.jcr.Session;
2113
import javax.servlet.Filter;
2214
import javax.servlet.FilterChain;
2315
import javax.servlet.FilterConfig;
24-
import javax.servlet.Servlet;
2516
import javax.servlet.ServletException;
2617
import javax.servlet.ServletRequest;
2718
import javax.servlet.ServletResponse;
2819
import javax.servlet.http.HttpServletRequest;
2920
import java.io.IOException;
30-
import java.util.Base64;
3121
import java.util.Optional;
3222
import java.util.regex.Matcher;
3323
import java.util.stream.Stream;
@@ -69,6 +59,7 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
6959
}
7060
// FIXME - need to rework document preview servlets to support latest suffix (variant preview servlet already works)
7161
String forwardString = firstResource.get().getPath() + ".preview/" + mode;
62+
XrefValidationHelper.initList();
7263
request.getRequestDispatcher(forwardString).forward(request, response);
7364
} catch (RepositoryException e) {
7465
throw new ServletException(e);

pantheon-bundle/src/main/java/com/redhat/pantheon/servlet/util/VersionUploadOperation.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.redhat.pantheon.model.document.DocumentLocale;
1212
import com.redhat.pantheon.model.document.DocumentMetadata;
1313
import com.redhat.pantheon.servlet.ServletUtils;
14+
import com.redhat.pantheon.validation.helper.XrefValidationHelper;
1415
import org.apache.commons.lang3.LocaleUtils;
1516
import org.apache.sling.api.SlingHttpServletRequest;
1617
import org.apache.sling.api.resource.ModifiableValueMap;
@@ -126,6 +127,7 @@ protected void versionUpload(SlingHttpServletRequest request,
126127
resolver.commit();
127128

128129
Map<String, Object> context = asciidoctorService.buildContextFromRequest(request);
130+
XrefValidationHelper.initList();
129131
asciidoctorService.getDocumentHtml(document, localeObj, document.getWorkspace().getCanonicalVariantName(),
130132
true, context, true);
131133

pantheon-bundle/src/main/java/com/redhat/pantheon/validation/helper/ValidationHelper.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,35 @@
44
import com.redhat.pantheon.model.document.DocumentVersion;
55
import com.redhat.pantheon.validation.model.*;
66
import com.redhat.pantheon.validation.validators.XrefValidator;
7-
import org.apache.sling.api.resource.ResourceResolver;
7+
import org.apache.sling.api.resource.PersistenceException;
8+
import org.slf4j.Logger;
9+
import org.slf4j.LoggerFactory;
10+
11+
import java.util.Optional;
812

913
/**
1014
* Validation helper for mapping violations and validations and creating validation node
1115
*/
1216
public class ValidationHelper {
13-
public void createXrefValidationNode(DocumentVersion documentVersion, String content){
17+
Logger logger = LoggerFactory.getLogger(ValidationHelper.class);
18+
public void createXrefValidationNode(DocumentVersion documentVersion, String content) throws PersistenceException {
1419
Violations violations = new XrefValidator(documentVersion.getParent(), content).validate();
20+
Validations validations = documentVersion.validations().getOrCreate();
21+
if(null != validations.validationType(PantheonConstants.TYPE_XREF).get()){
22+
try {
23+
validations.validationType(PantheonConstants.TYPE_XREF).get().delete();
24+
} catch (Exception e) {
25+
logger.error("error while validation node creation",e);
26+
}
27+
}
28+
1529
if(violations.hasViolations()) {
16-
ValidationType validationType = documentVersion.validations().getOrCreate().validationType(PantheonConstants.TYPE_XREF).getOrCreate();
1730
Validation validation;
1831
ErrorDetails errorDetails = violations.get(PantheonConstants.TYPE_XREF);
19-
if(errorDetails == null){
32+
if(null == errorDetails || errorDetails.length() ==0){
2033
return;
2134
}
35+
ValidationType validationType = validations.validationType(PantheonConstants.TYPE_XREF).getOrCreate();
2236
for(int ind=0; ind< errorDetails.length();ind++) {
2337
validation = validationType.page(ind+1).getOrCreate();
2438
validation.setValidation(violations, ind);

pantheon-bundle/src/main/java/com/redhat/pantheon/validation/helper/XrefValidationHelper.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,19 @@
88

99
public class XrefValidationHelper {
1010

11-
private static Map<String, List<String>> xRefs = new HashMap<>();
11+
private static List<String> xRefs;
1212

13-
public static List<String> getObjectsToValidate(String uuid) {
14-
return xRefs.get(uuid);
13+
public static List<String> getObjectsToValidate() {
14+
return xRefs;
1515
}
1616

17-
public static void setObjectsToValidate(String uuid, List<String> objectsToValidate) {
18-
if(objectsToValidate.size()>0)
19-
xRefs.put(uuid, objectsToValidate);
17+
public static void initList() {
18+
xRefs = new ArrayList<>();
19+
}
20+
21+
public static void setObjectsToValidate(List<String> objectsToValidate) {
22+
if(objectsToValidate.size()>0){
23+
xRefs.addAll(objectsToValidate);
24+
}
2025
}
2126
}

pantheon-bundle/src/main/java/com/redhat/pantheon/validation/model/Validation.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import com.redhat.pantheon.model.api.Field;
55
import com.redhat.pantheon.model.api.annotation.JcrPrimaryType;
66
import com.redhat.pantheon.model.workspace.WorkspaceChild;
7+
import com.redhat.pantheon.validation.helper.XrefValidationHelper;
8+
import org.apache.sling.api.resource.ValueMap;
79

810
import javax.inject.Named;
911

@@ -31,4 +33,7 @@ default Validation setValidation (Violations violations, int index) {
3133
}
3234
return this;
3335
}
36+
default Validation getValidation(){
37+
return this;
38+
}
3439
}

pantheon-bundle/src/main/java/com/redhat/pantheon/validation/validators/XrefValidator.java

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,17 @@ private Violations checkIfXrefValid(Violations violations) {
6767
private ErrorDetails checkXref() {
6868
ErrorDetails errorDetails = new ErrorDetails();
6969
try {
70-
Document doc = Jsoup.parse(content);
71-
List<String> xrefTargets = XrefValidationHelper.getObjectsToValidate(this.documentVariant.uuid().get());
72-
Elements resultLinks = doc.select("a");
70+
List<String> xrefTargets = XrefValidationHelper.getObjectsToValidate();
7371
if(null == xrefTargets || xrefTargets.size()==0){
7472
return errorDetails;
7573
}
7674
for (String xref : xrefTargets) {
77-
String target = getInvalidXrefs(resultLinks, xref);
78-
if(null != target) {
79-
errorDetails.add(target);
75+
if(!xref.endsWith(".adoc")) {
76+
if(validateIfAnchor(content,xref)<1){ // If it is anchor, it has not yet validated,
77+
errorDetails.add(xref);
78+
}
79+
}else {
80+
errorDetails.add(xref); // if it is a adoc file, it's already processed and validated
8081
}
8182
}
8283
}
@@ -87,29 +88,17 @@ private ErrorDetails checkXref() {
8788
}
8889

8990
/**
90-
* Check if processed xrefs are invalid, return the target xpath if so.
91+
* if path is an anchor, validate
9192
*
92-
* @param resultLinks
93+
* @param content
9394
* @param xref
9495
* @return
9596
* @throws RepositoryException
9697
*/
97-
private String getInvalidXrefs(Elements resultLinks, String xref) throws RepositoryException {
98-
if(xref.endsWith(".adoc")){
99-
Resource resource = documentVariant.getParentLocale().getParent().getParent();
100-
String[] resourceFragment = xref.split("/");
101-
102-
for(String rf:resourceFragment){
103-
switch (rf){
104-
case "..":resource = resource.getParent(); break; // TODO: fails in case dependent document not yet uploaded
105-
default: resource = resource.getChild(rf); break;
106-
}
107-
}
108-
109-
return resource==null ? xref :null;
110-
} else { //if path is an anchor
111-
return (int) resultLinks.eachAttr("href").stream().filter(s->s.endsWith(xref)).count() > 0? null :xref;
112-
}
98+
private int validateIfAnchor(String content, String xref) throws RepositoryException {
99+
//if path is an anchor
100+
Document doc = Jsoup.parse(content);
101+
return doc.getElementsByAttributeValue("id",xref).size();
113102
}
114103

115104
/**

0 commit comments

Comments
 (0)