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.xpath;
19  
20  import org.eclipse.rdf4j.model.Value;
21  
22  import java.util.Map;
23  
24  /**
25   * Represents a generic template term.
26   *
27   * @author Michele Mostarda (mostarda@fbk.eu)
28   */
29  public abstract class Term<T extends Value> {
30  
31      /**
32       * Internal value.
33       */
34      private final String internalValue;
35  
36      /**
37       * if true the #internalValue is a variable name,
38       * otherwise is a constant.
39       */
40      private final boolean isVar;
41  
42      /**
43       * Constructor.
44       *
45       * @param internalValue internal term value.
46       * @param isVar if true the <code>internalValue</code> is a variable name,
47       *              otherwise is a constant.
48       */
49      protected Term(String internalValue, boolean isVar) {
50          this.internalValue = internalValue;
51          this.isVar = isVar;
52      }
53  
54      /**
55       * @return the internal value.
56       */
57      public String getInternalValue() {
58          return internalValue;
59      }
60  
61      /**
62       * @return the isVar flag value.
63       */
64      public boolean isVar() {
65          return isVar;
66      }
67  
68      /**
69       * Returns the value represented by this {@link Term}
70       * given the <code>varMapping</code>, the #isVar and #internalValue
71       * parameters.
72       *
73       * @param varMapping a map representing values of variables.
74       * @return the value for this term.
75       */
76      public T getValue(Map<String, String> varMapping) {
77          final String value;
78          if(isVar) {
79              value = varMapping.get(internalValue);
80              if(value == null) {
81                  throw new IllegalStateException(
82                          String.format("Cannot find a valid value for variable '%s'", internalValue)
83                  );
84              }
85          } else {
86              value = internalValue;
87          }
88          return getValueInternal(value);
89      }
90  
91      protected abstract T getValueInternal(String value);
92  
93      @Override
94      public String toString() {
95          return isVar ? ( "?" + internalValue ) : internalValue;
96      }
97  }