View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *  http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.any23.extractor.microdata;
19  
20  /**
21   * Describes a <b>Microdata item property</b>.
22   *
23   * @author Michele Mostarda (mostarda@fbk.eu)
24   */
25  public class ItemProp extends Item {
26  
27      /**
28       * Property name.
29       */
30      private final String name;
31  
32      /**
33       * Property value.
34       */
35      private final ItemPropValue value;
36  
37      /**
38       * Constructor.
39       *
40       * @param xpath item location in container document.
41       * @param name item property name.
42       * @param value item property value.
43       */
44      public ItemProp(String xpath, String name, ItemPropValue value) {
45          super(xpath);
46  
47          if(name == null) {
48              throw new NullPointerException("name cannot be null.");
49          }
50          if(name.trim().length() == 0) {
51              throw new IllegalArgumentException("invalid property name '" + name + "'");
52          }
53          if(value == null) {
54              throw new NullPointerException("value cannot be null.");
55          }
56          this.name = name;
57          this.value = value;
58      }
59  
60      /**
61       * @return the item property name.
62       */
63      public String getName() {
64          return name;
65      }
66  
67      /**
68       * @return the item property value.
69       */
70      public ItemPropValue getValue() {
71          return value;
72      }
73  
74      @Override
75      public String toJSON() {
76          return String.format(
77                  "{ \"xpath\" : \"%s\", \"name\" : \"%s\", \"value\" : %s }",
78                  getXpath(),
79                  name,
80                  value.toJSON()
81          );
82      }
83  
84      @Override
85      public String toString() {
86          return toJSON();
87      }
88  
89      @Override
90      public int hashCode() {
91          return name.hashCode() * value.hashCode() * 3;
92      }
93  
94      @Override
95      public boolean equals(Object obj) {
96          if( obj == null ) {
97              return false;
98          }
99          if(obj == this) {
100             return true;
101         }
102         if(obj instanceof ItemProp) {
103             final ItemProp other = (ItemProp) obj;
104             return name.equals(other.name) && value.equals( other.value );
105         }
106         return false;
107     }
108 }