View Javadoc
1   /*
2    * Copyright 2012-2017 CodeLibs Project and the Others.
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,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.codelibs.fess.util;
17  
18  import org.apache.commons.lang3.StringUtils;
19  import org.codelibs.core.lang.StringUtil;
20  import org.codelibs.core.stream.StreamUtil;
21  import org.w3c.dom.Node;
22  
23  public class PrunedTag {
24      private final String tag;
25      private String id;
26      private String css;
27      private String attrName;
28      private String attrValue;
29  
30      public PrunedTag(final String tag) {
31          this.tag = tag;
32      }
33  
34      public boolean matches(final Node node) {
35          if (tag.equalsIgnoreCase(node.getNodeName())) {
36              if (attrName != null) {
37                  final Node attr = node.getAttributes().getNamedItem(attrName);
38                  if (attr == null || !attrValue.equals(attr.getNodeValue())) {
39                      return false;
40                  }
41              }
42              if (id == null) {
43                  if (css == null) {
44                      return true;
45                  } else {
46                      final Node classAttr = node.getAttributes().getNamedItem("class");
47                      if (classAttr != null) {
48                          final String value = classAttr.getNodeValue();
49                          if (StringUtil.isNotBlank(value)) {
50                              return StreamUtil.split(value, " ").get(stream -> stream.anyMatch(s -> css.equals(s)));
51                          }
52                      }
53                  }
54              } else {
55                  final Node idAttr = node.getAttributes().getNamedItem("id");
56                  if (idAttr != null) {
57                      final String value = idAttr.getNodeValue();
58                      return id.equals(value);
59                  }
60              }
61          }
62          return false;
63      }
64  
65      @Override
66      public int hashCode() {
67          final int prime = 31;
68          int result = 1;
69          result = prime * result + ((css == null) ? 0 : css.hashCode());
70          result = prime * result + ((id == null) ? 0 : id.hashCode());
71          result = prime * result + ((tag == null) ? 0 : tag.hashCode());
72          return result;
73      }
74  
75      @Override
76      public boolean equals(final Object obj) {
77          if (this == obj) {
78              return true;
79          }
80          if (obj == null) {
81              return false;
82          }
83          if (getClass() != obj.getClass()) {
84              return false;
85          }
86          final PrunedTag other = (PrunedTag) obj;
87          return StringUtils.compare(tag, other.tag) == 0 //
88                  && StringUtils.compare(css, other.css) == 0 //
89                  && StringUtils.compare(id, other.id) == 0 //
90                  && StringUtils.compare(attrName, other.attrName) == 0 //
91                  && StringUtils.compare(attrValue, other.attrValue) == 0;
92      }
93  
94      public void setId(final String id) {
95          this.id = id;
96      }
97  
98      public void setCss(final String css) {
99          this.css = css;
100     }
101 
102     public void setAttr(final String name, final String value) {
103         this.attrName = name;
104         this.attrValue = value;
105     }
106 
107     @Override
108     public String toString() {
109         return "PrunedTag [tag=" + tag + ", id=" + id + ", css=" + css + ", attrName=" + attrName + ", attrValue=" + attrValue + "]";
110     }
111 }