1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.deri.any23.validator.rule;
18
19 import org.deri.any23.validator.DOMDocument;
20 import org.deri.any23.validator.Rule;
21 import org.deri.any23.validator.RuleContext;
22 import org.deri.any23.validator.ValidationReport;
23 import org.deri.any23.validator.ValidationReportBuilder;
24 import org.w3c.dom.Node;
25
26 import java.util.List;
27
28
29
30
31
32
33
34
35 public class MissingOpenGraphNamespaceRule implements Rule {
36
37 public String getHRName() {
38 return "missing-opengraph-namespace-rule";
39 }
40
41 public boolean applyOn(
42 DOMDocument document,
43 RuleContext context,
44 ValidationReportBuilder validationReportBuilder
45 ) {
46 List<Node> metas = document.getNodes("/HTML/HEAD/META");
47 boolean foundPrecondition = false;
48 for(Node meta : metas) {
49 Node propertyNode = meta.getAttributes().getNamedItem("property");
50 if( propertyNode != null && propertyNode.getTextContent().indexOf("og:") == 0) {
51 foundPrecondition = true;
52 break;
53 }
54 }
55 if(foundPrecondition) {
56 Node htmlNode = document.getNode("/HTML");
57 if( htmlNode.getAttributes().getNamedItem("xmlns:og") == null) {
58 validationReportBuilder.reportIssue(
59 ValidationReport.IssueLevel.error,
60 "Missing OpenGraph namespace declaration.",
61 htmlNode
62 );
63 return true;
64 }
65 }
66 return false;
67 }
68 }