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  import java.util.Locale;
    21
    22  /**
    23   * Describes a <b>Microdata item property</b>.
    24   *
    25   * @author Michele Mostarda (mostarda@fbk.eu)
    26   */
    27  public class ItemProp extends Item {
    28
    29      /**
    30       * Property name.
    31       */
    32      private final String name;
    33
    34      /**
    35       * Property value.
    36       */
    37      private final ItemPropValue value;
    38
    39      /**
    40       * Constructor.
    41       *
    42       * @param xpath
    43       *            item location in container document.
    44       * @param name
    45       *            item property name.
    46       * @param value
    47       *            item property value.
    48       */
    49      public ItemProp(String xpath, String name, ItemPropValue value) {
    50          this(xpath, name, value, false);
    51      }
    52
    53      final boolean reverse;
    54
    55      ItemProp(String xpath, String name, ItemPropValue value, boolean reverse) {
    56          super(xpath);
    57
    58          if (name == null) {
    59              throw new NullPointerException("name cannot be null.");
    60          }
    61          if (name.trim().length() == 0) {
    62              throw new IllegalArgumentException("invalid property name '" + name + "'");
    63          }
    64          if (value == null) {
    65              throw new NullPointerException("value cannot be null.");
    66          }
    67          this.name = name;
    68          this.value = value;
    69          this.reverse = reverse;
    70      }
    71
    72      /**
    73       * @return the item property name.
    74       */
    75      public String getName() {
    76          return name;
    77      }
    78
    79      /**
    80       * @return the item property value.
    81       */
    82      public ItemPropValue getValue() {
    83          return value;
    84      }
    85
    86      @Override
    87      public String toJSON() {
    88          return String.format(Locale.ROOT, "{ \"xpath\" : \"%s\", \"name\" : \"%s\", \"value\" : %s }", getXpath(), name,
    89                  value.toJSON());
    90      }
    91
    92      @Override
    93      public String toString() {
    94          return toJSON();
    95      }
    96
    97      @Override
    98      public int hashCode() {
    99          return name.hashCode() * value.hashCode() * 3;
    100     }
    101
    102     @Override
    103     public boolean equals(Object obj) {
    104         if (obj == null) {
    105             return false;
    106         }
    107         if (obj == this) {
    108             return true;
    109         }
    110         if (obj instanceof ItemProp) {
    111             final ItemProp other = (ItemProp) obj;
    112             return name.equals(other.name) && value.equals(other.value);
    113         }
    114         return false;
    115     }
    116 }