Coverage Report - org.apache.any23.extractor.microdata.ItemPropValue
 
Classes in this File Line Coverage Branch Coverage Complexity
ItemPropValue
0%
0/57
0%
0/46
2.619
ItemPropValue$Type
0%
0/5
N/A
2.619
 
 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 org.apache.any23.util.StringUtils;
 21  
 
 22  
 import java.net.MalformedURLException;
 23  
 import java.net.URL;
 24  
 import java.text.ParseException;
 25  
 import java.text.SimpleDateFormat;
 26  
 import java.util.Date;
 27  
 
 28  
 /**
 29  
  * Describes a possible value for a <b>Microdata item property</b>.
 30  
  *
 31  
  * @author Michele Mostarda (mostarda@fbk.eu)
 32  
  */
 33  
 public class ItemPropValue {
 34  
 
 35  0
     private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
 36  
 
 37  
     /**
 38  
      * Supported types.
 39  
      */
 40  0
     public enum Type {
 41  0
         Plain,
 42  0
         Link,
 43  0
         Date,
 44  0
         Nested
 45  
     }
 46  
 
 47  
     public static Date parseDateTime(String dateStr) throws ParseException {
 48  0
         return sdf.parse(dateStr);
 49  
     }
 50  
 
 51  
     public static String formatDateTime(Date in) {
 52  0
         return sdf.format(in);
 53  
     }
 54  
 
 55  
     /**
 56  
      * Internal content value.
 57  
      */
 58  
     private final Object content;
 59  
 
 60  
     /**
 61  
      * Content type.
 62  
      */
 63  
     private final Type type;
 64  
 
 65  
     /**
 66  
      * Constructor.
 67  
      *
 68  
      * @param content content object.
 69  
      * @param type content type.
 70  
      */
 71  0
     public ItemPropValue(Object content, Type type) {
 72  0
         if(content == null) {
 73  0
             throw new NullPointerException("content cannot be null.");
 74  
         }
 75  0
         if(type == null) {
 76  0
             throw new NullPointerException("type cannot be null.");
 77  
         }
 78  0
         if(type == Type.Nested && ! (content instanceof ItemScope) ) {
 79  0
             throw new IllegalArgumentException(
 80  
                     "content must be an " + ItemScope.class + " when type is " + Type.Nested
 81  
             );
 82  
         }
 83  0
         if(type == Type.Date && !(content instanceof Date) ) {
 84  0
             throw new IllegalArgumentException(
 85  
                     "content must be a " + Date.class.getName() + " whe type is " + Type.Date
 86  
             );
 87  
         }
 88  0
         if(content instanceof String && ((String) content).trim().length() == 0) {
 89  0
             throw new IllegalArgumentException("Invalid content '" + content + "'");
 90  
         }
 91  0
         this.content = content;
 92  0
         this.type = type;
 93  0
     }
 94  
 
 95  
     /**
 96  
      * @return the content object.
 97  
      */
 98  
     public Object getContent() {
 99  0
         return content;
 100  
     }
 101  
 
 102  
     /**
 103  
      * @return the content type.
 104  
      */
 105  
     public Type getType() {
 106  0
         return type;
 107  
     }
 108  
 
 109  
    /**
 110  
      * @return <code>true</code> if type is plain text.
 111  
      */
 112  
     public boolean isPlain() {
 113  0
         return type == Type.Plain;
 114  
     }
 115  
 
 116  
     /**
 117  
      * @return <code>true</code> if type is a link.
 118  
      */
 119  
     public boolean isLink() {
 120  0
         return type == Type.Link;
 121  
     }
 122  
 
 123  
     /**
 124  
      * @return <code>true</code> if type is a date.
 125  
      */
 126  
     public boolean isDate() {
 127  0
         return type == Type.Date;
 128  
     }
 129  
 
 130  
     /**
 131  
      * @return <code>true</code> if type is a nested {@link ItemScope}.
 132  
      */
 133  
     public boolean isNested() {
 134  0
         return type == Type.Nested;
 135  
     }
 136  
 
 137  
     /**
 138  
      * @return <code>true</code> if type is an integer.
 139  
      */
 140  
     public boolean isInteger() {
 141  0
         if(type != Type.Plain) return false;
 142  
          try {
 143  0
              Integer.parseInt((String) content);
 144  0
              return true;
 145  0
          } catch (Exception e) {
 146  0
              return false;
 147  
          }
 148  
      }
 149  
 
 150  
     /**
 151  
      * @return <code>true</code> if type is a float.
 152  
      */
 153  
      public boolean isFloat() {
 154  0
          if(type != Type.Plain) return false;
 155  
          try {
 156  0
              Float.parseFloat((String) content);
 157  0
              return true;
 158  0
          } catch (Exception e) {
 159  0
              return false;
 160  
          }
 161  
      }
 162  
 
 163  
     /**
 164  
      * @return <code>true</code> if type is a number.
 165  
      */
 166  
      public boolean isNumber() {
 167  0
          return isInteger() || isFloat();
 168  
      }
 169  
 
 170  
     /**
 171  
      * @return the content value as integer, or raises an exception.
 172  
      * @throws NumberFormatException if the content is not an integer.
 173  
      * @throws ClassCastException if content is not plain.
 174  
      */
 175  
      public int getAsInteger() {
 176  0
          return Integer.parseInt((String) content);
 177  
      }
 178  
 
 179  
     /**
 180  
      * @return the content value as float, or raises an exception.
 181  
      * @throws NumberFormatException if the content is not an float.
 182  
      * @throws ClassCastException if content is not plain.
 183  
      */
 184  
      public float getAsFloat() {
 185  0
          return Float.parseFloat((String) content);
 186  
      }
 187  
 
 188  
 
 189  
     /**
 190  
      * @return the content as {@link Date}
 191  
      *         if <code>type == Type.DateTime</code>,
 192  
      * @throws ClassCastException if content is not a valid date.
 193  
      */
 194  
     public Date getAsDate() {
 195  0
         return (Date) content;
 196  
     }
 197  
 
 198  
     /**
 199  
      * @return the content value as URL, or raises an exception.
 200  
      * @throws MalformedURLException if the content is not a valid URL.
 201  
      * @throws ClassCastException if content is not a link.
 202  
      */
 203  
     public URL getAsLink() {
 204  
         try {
 205  0
             return new URL((String) content);
 206  0
         } catch (MalformedURLException murle) {
 207  0
             throw new IllegalStateException("Error while parsing URI.", murle);
 208  
         }
 209  
     }
 210  
 
 211  
     /**
 212  
      * @return the content value as {@link ItemScope}.
 213  
      * @throws ClassCastException if the content is not a valid nested item.
 214  
      */
 215  
     public ItemScope getAsNested() {
 216  0
         return (ItemScope) content;
 217  
     }
 218  
 
 219  
     public String toJSON() {
 220  
         String contentStr;
 221  0
         if(content instanceof String) {
 222  0
             contentStr = "\"" + StringUtils.escapeAsJSONString((String) content) + "\"";
 223  0
         } else if(content instanceof Date) {
 224  0
             contentStr = "\"" + sdf.format((Date) content) + "\"";
 225  
         } else {
 226  0
             contentStr = content.toString();
 227  
         }
 228  
 
 229  0
         return String.format( "{ \"content\" : %s, \"type\" : \"%s\" }", contentStr, type );
 230  
     }
 231  
 
 232  
     @Override
 233  
     public String toString() {
 234  0
         return toJSON();
 235  
     }
 236  
 
 237  
     @Override
 238  
     public int hashCode() {
 239  0
         return content.hashCode() * type.hashCode() * 2;
 240  
     }
 241  
 
 242  
     @Override
 243  
     public boolean equals(Object obj) {
 244  0
         if(obj == null) {
 245  0
             return false;
 246  
         }
 247  0
         if(obj == this) {
 248  0
             return true;
 249  
         }
 250  0
         if(obj instanceof ItemPropValue) {
 251  0
             final ItemPropValue other = (ItemPropValue) obj;
 252  0
             return content.equals(other.content) && type.equals(other.type);
 253  
         }
 254  0
         return false;
 255  
     }
 256  
 
 257  
 }