View Javadoc

1   /*
2    * Copyright 2008-2010 Digital Enterprise Research Institute (DERI)
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *          http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * This rule detects the issue of missing Open Graph namespace.
30   *
31   * @see org.deri.any23.validator.rule.OpenGraphNamespaceFix
32   * @author Michele Mostarda (mostarda@fbk.eu)
33   * @author Davide Palmisano (palmisano@fbk.eu)
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  }