Author: rwesten
Date: Wed Nov 27 13:29:34 2013
New Revision: 1546028
URL: http://svn.apache.org/r1546028
Log:
STANBOL-1222: Updates to the Dereferece core module: Added two properties for
configuring dereferenced fields and ldpath statements; also an Utility class
for prasing those from an OSGI config
Added:
stanbol/trunk/enhancement-engines/dereference/core/src/main/java/org/apache/stanbol/enhancer/engines/dereference/DereferenceConstants.java
stanbol/trunk/enhancement-engines/dereference/core/src/main/java/org/apache/stanbol/enhancer/engines/dereference/DereferenceUtils.java
Modified:
stanbol/trunk/enhancement-engines/dereference/core/src/main/java/org/apache/stanbol/enhancer/engines/dereference/DereferenceException.java
stanbol/trunk/enhancement-engines/dereference/core/src/main/java/org/apache/stanbol/enhancer/engines/dereference/EntityDereferenceEngine.java
Added:
stanbol/trunk/enhancement-engines/dereference/core/src/main/java/org/apache/stanbol/enhancer/engines/dereference/DereferenceConstants.java
URL:
http://svn.apache.org/viewvc/stanbol/trunk/enhancement-engines/dereference/core/src/main/java/org/apache/stanbol/enhancer/engines/dereference/DereferenceConstants.java?rev=1546028&view=auto
==============================================================================
---
stanbol/trunk/enhancement-engines/dereference/core/src/main/java/org/apache/stanbol/enhancer/engines/dereference/DereferenceConstants.java
(added)
+++
stanbol/trunk/enhancement-engines/dereference/core/src/main/java/org/apache/stanbol/enhancer/engines/dereference/DereferenceConstants.java
Wed Nov 27 13:29:34 2013
@@ -0,0 +1,29 @@
+package org.apache.stanbol.enhancer.engines.dereference;
+
+/**
+ * Define configuration parameters for Dereference engines
+ * @author Rupert Westenthaler
+ *
+ */
+public interface DereferenceConstants {
+ /**
+ * Property used to configure the fields that should be dereferenced.<p>
+ * DereferenceEngines need to support a list of URIs but may also support
more
+ * complex syntax (such as the Entityhub FiedMapping). However parsing a
+ * list of properties URIs MUST BE still valid.<p>
+ * Support for Namespace prefixes via the Stanbol Namespace Prefix Service
+ * is optional. If unknown prefixes are used or prefixes are not supported
+ * the Engine is expected to throw a
+ * {@link org.osgi.service.cm.ConfigurationException} during activation
+ */
+ String DEREFERENCE_ENTITIES_FIELDS = "enhancer.engines.dereference.fields";
+ /**
+ * Property used to configure LDPath statements. Those are applied using
+ * each referenced Entity as Context.<p>
+ * DereferenceEngines that can not support LDPath are expected to throw a
+ * {@link org.osgi.service.cm.ConfigurationException} if values are set
+ * for this property.
+ */
+ String DEREFERENCE_ENTITIES_LDPATH = "enhancer.engines.dereference.ldpath";
+
+}
Modified:
stanbol/trunk/enhancement-engines/dereference/core/src/main/java/org/apache/stanbol/enhancer/engines/dereference/DereferenceException.java
URL:
http://svn.apache.org/viewvc/stanbol/trunk/enhancement-engines/dereference/core/src/main/java/org/apache/stanbol/enhancer/engines/dereference/DereferenceException.java?rev=1546028&r1=1546027&r2=1546028&view=diff
==============================================================================
---
stanbol/trunk/enhancement-engines/dereference/core/src/main/java/org/apache/stanbol/enhancer/engines/dereference/DereferenceException.java
(original)
+++
stanbol/trunk/enhancement-engines/dereference/core/src/main/java/org/apache/stanbol/enhancer/engines/dereference/DereferenceException.java
Wed Nov 27 13:29:34 2013
@@ -25,5 +25,9 @@ public class DereferenceException extend
public DereferenceException(UriRef entity, Throwable t){
super("Unable to dereference Entity " + entity+ "!", t);
}
-
+ public DereferenceException(UriRef entity, String reason){
+ super("Unable to dereference Entity " + entity +
+ (reason != null ? ": "+ reason : "") + "!");
+ }
+
}
Added:
stanbol/trunk/enhancement-engines/dereference/core/src/main/java/org/apache/stanbol/enhancer/engines/dereference/DereferenceUtils.java
URL:
http://svn.apache.org/viewvc/stanbol/trunk/enhancement-engines/dereference/core/src/main/java/org/apache/stanbol/enhancer/engines/dereference/DereferenceUtils.java?rev=1546028&view=auto
==============================================================================
---
stanbol/trunk/enhancement-engines/dereference/core/src/main/java/org/apache/stanbol/enhancer/engines/dereference/DereferenceUtils.java
(added)
+++
stanbol/trunk/enhancement-engines/dereference/core/src/main/java/org/apache/stanbol/enhancer/engines/dereference/DereferenceUtils.java
Wed Nov 27 13:29:34 2013
@@ -0,0 +1,115 @@
+package org.apache.stanbol.enhancer.engines.dereference;
+
+import static
org.apache.stanbol.enhancer.engines.dereference.DereferenceConstants.DEREFERENCE_ENTITIES_FIELDS;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Dictionary;
+import java.util.List;
+
+import org.apache.commons.lang.StringUtils;
+import org.osgi.service.cm.ConfigurationException;
+
+public class DereferenceUtils {
+
+
+ /**
+ * Parsed the {@link DereferenceConstants#DEREFERENCE_ENTITIES_FIELDS}
+ * config from the parsed Dictionary regardless if it is defined as
+ * <code>String[]</code>, <code>Collection<String></code> or
+ * <code>String</code> (single value).<p>
+ * This returns the fields as parsed by the configuration.<p>
+ * <b>NOTE:</b> This does not check/convert
<code>{prefix}:{localname}</code>
+ * configurations to URIs. The receiver of the list is responsible for
+ * that
+ * @param conf the configuration as parsed to the OSGI component
+ * @return the {@link List} with the unprocessed dereference fields
+ * @throws ConfigurationException if the value is not any of the supported
+ * types
+ */
+ public static List<String>
parseDereferencedFieldsConfig(Dictionary<String,Object> conf) throws
ConfigurationException {
+ Object value = conf.get(DEREFERENCE_ENTITIES_FIELDS);
+ final List<String> fields;
+ if(value instanceof String[]){
+ fields = Arrays.asList((String[])value);
+ } else if(value instanceof Collection<?>){
+ fields = new ArrayList<String>(((Collection<?>)value).size());
+ for(Object field : (Collection<?>)value){
+ if(field == null){
+ fields.add(null);
+ } else {
+ fields.add(field.toString());
+ }
+ }
+ } else if(value instanceof String){
+ fields = Collections.singletonList((String)value);
+ } else if(value != null){
+ throw new ConfigurationException(DEREFERENCE_ENTITIES_FIELDS,
+ "Dereference Entities Fields MUST BE parsed as String[],
Collection<String> or "
+ + "String (single value). The actual value '"+value+"'(type:
'"+value.getClass()
+ + "') is NOT supported");
+ } else { //value == null
+ fields = Collections.emptyList();
+ }
+ return fields;
+ }
+
+ /**
+ * Parses the LdPath program from the value of the
+ * {@link DereferenceConstants#DEREFERENCE_ENTITIES_LDPATH} property. <p>
+ * This supports <code>String</code> (the program as a single String),
+ * <code>String[]</code> and <code>Collection<String></code> (one
+ * statement per line).<p>
+ * <b>NOTE:</b> This does not parse the LDPath program as this can only be
+ * done by the LdPath repository used by the dereferencer.
+ * @param conf the configuration as parsed to the OSGI component
+ * @return the unparsed LDPath program as String
+ * @throws ConfigurationException if the value is not any of the supported
+ * types
+ */
+ public static String parseLdPathConfig(Dictionary<String,Object> conf)
throws ConfigurationException {
+ Object value =
conf.get(DereferenceConstants.DEREFERENCE_ENTITIES_LDPATH);
+ if(value == null){
+ return null;
+ } else if(value instanceof String){
+ return StringUtils.isBlank((String) value) ? null : (String) value;
+ }
+ StringBuilder sb = new StringBuilder();
+ boolean first = true;
+ if(value instanceof Collection<?>){
+ for(Object line : (Collection<?>)value){
+ if(line != null && !StringUtils.isBlank(line.toString())){
+ if(first){
+ first = false;
+ } else {
+ sb.append('\n');
+ }
+ sb.append(line.toString());
+ }
+ }
+ } else if(value instanceof String[]){
+ for(String line : (String[])value){
+ if(line != null && !StringUtils.isBlank(line)){
+ if(first){
+ first = false;
+ } else {
+ sb.append('\n');
+ }
+ sb.append(line);
+ }
+ }
+ } else {
+ throw new ConfigurationException(DEREFERENCE_ENTITIES_FIELDS,
+ "Dereference LDPath statements MUST BE parsed as String,
String[] or "
+ + "Collection<String>. The actual value '"+value+"'(type:
'"+value.getClass()
+ + "') is NOT supported");
+ }
+ //we we have not found non blank lines return null!
+ return !first ? sb.toString() : null;
+
+
+ }
+
+}
Modified:
stanbol/trunk/enhancement-engines/dereference/core/src/main/java/org/apache/stanbol/enhancer/engines/dereference/EntityDereferenceEngine.java
URL:
http://svn.apache.org/viewvc/stanbol/trunk/enhancement-engines/dereference/core/src/main/java/org/apache/stanbol/enhancer/engines/dereference/EntityDereferenceEngine.java?rev=1546028&r1=1546027&r2=1546028&view=diff
==============================================================================
---
stanbol/trunk/enhancement-engines/dereference/core/src/main/java/org/apache/stanbol/enhancer/engines/dereference/EntityDereferenceEngine.java
(original)
+++
stanbol/trunk/enhancement-engines/dereference/core/src/main/java/org/apache/stanbol/enhancer/engines/dereference/EntityDereferenceEngine.java
Wed Nov 27 13:29:34 2013
@@ -51,7 +51,7 @@ import org.apache.stanbol.enhancer.servi
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-public class EntityDereferenceEngine implements EnhancementEngine,
ServiceProperties {
+public class EntityDereferenceEngine implements EnhancementEngine,
ServiceProperties, DereferenceConstants {
private final Logger log =
LoggerFactory.getLogger(EntityDereferenceEngine.class);
@@ -88,6 +88,8 @@ public class EntityDereferenceEngine imp
throw new IllegalArgumentException("The parsed EntityDereferencer
MUST NOT be NULL!");
}
this.dereferencer = dereferencer;
+ //init the defautl ordering
+ setEngineOrdering(DEFAULT_ENGINE_ORDERING);
}
/**