Move utility method for creating SimpleFilters from BundleRequirementImpl
to SimpleFilter. (FELIX-2950)
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1148886 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/framework/src/main/java/org/apache/felix/framework/capabilityset/SimpleFilter.java b/framework/src/main/java/org/apache/felix/framework/capabilityset/SimpleFilter.java
index 18e7adb..92c6254 100644
--- a/framework/src/main/java/org/apache/felix/framework/capabilityset/SimpleFilter.java
+++ b/framework/src/main/java/org/apache/felix/framework/capabilityset/SimpleFilter.java
@@ -20,6 +20,9 @@
import java.util.ArrayList;
import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import org.apache.felix.framework.util.VersionRange;
public class SimpleFilter
{
@@ -545,4 +548,109 @@
}
return startIdx;
}
+
+ /**
+ * Converts a attribute map to a filter. The filter is created by iterating
+ * over the map's entry set. If ordering of attributes is important (e.g.,
+ * for hitting attribute indices), then the map's entry set should iterate
+ * in the desired order. Equality testing is assumed for all attribute types
+ * other than version ranges, which are handled appropriated. If the attribute
+ * map is empty, then a filter that matches anything is returned.
+ * @param attrs Map of attributes to convert to a filter.
+ * @return A filter corresponding to the attributes.
+ */
+ public static SimpleFilter convert(Map<String, Object> attrs)
+ {
+ // Rather than building a filter string to be parsed into a SimpleFilter,
+ // we will just create the parsed SimpleFilter directly.
+
+ List<SimpleFilter> filters = new ArrayList<SimpleFilter>();
+
+ for (Entry<String, Object> entry : attrs.entrySet())
+ {
+ if (entry.getValue() instanceof VersionRange)
+ {
+ VersionRange vr = (VersionRange) entry.getValue();
+ if (vr.isFloorInclusive())
+ {
+ filters.add(
+ new SimpleFilter(
+ entry.getKey(),
+ vr.getFloor().toString(),
+ SimpleFilter.GTE));
+ }
+ else
+ {
+ SimpleFilter not =
+ new SimpleFilter(null, new ArrayList(), SimpleFilter.NOT);
+ ((List) not.getValue()).add(
+ new SimpleFilter(
+ entry.getKey(),
+ vr.getFloor().toString(),
+ SimpleFilter.LTE));
+ filters.add(not);
+ }
+
+ if (vr.getCeiling() != null)
+ {
+ if (vr.isCeilingInclusive())
+ {
+ filters.add(
+ new SimpleFilter(
+ entry.getKey(),
+ vr.getCeiling().toString(),
+ SimpleFilter.LTE));
+ }
+ else
+ {
+ SimpleFilter not =
+ new SimpleFilter(null, new ArrayList(), SimpleFilter.NOT);
+ ((List) not.getValue()).add(
+ new SimpleFilter(
+ entry.getKey(),
+ vr.getCeiling().toString(),
+ SimpleFilter.GTE));
+ filters.add(not);
+ }
+ }
+ }
+ else
+ {
+ List<String> values = SimpleFilter.parseSubstring(entry.getValue().toString());
+ if (values.size() > 1)
+ {
+ filters.add(
+ new SimpleFilter(
+ entry.getKey(),
+ values,
+ SimpleFilter.SUBSTRING));
+ }
+ else
+ {
+ filters.add(
+ new SimpleFilter(
+ entry.getKey(),
+ values.get(0),
+ SimpleFilter.EQ));
+ }
+ }
+ }
+
+ SimpleFilter sf = null;
+
+ if (filters.size() == 1)
+ {
+ sf = filters.get(0);
+ }
+ else if (attrs.size() > 1)
+ {
+ sf = new SimpleFilter(null, filters, SimpleFilter.AND);
+ }
+ else if (filters.isEmpty())
+ {
+ sf = new SimpleFilter(null, null, SimpleFilter.MATCH_ALL);
+ }
+
+ return sf;
+ }
}
\ No newline at end of file
diff --git a/framework/src/main/java/org/apache/felix/framework/util/manifestparser/ManifestParser.java b/framework/src/main/java/org/apache/felix/framework/util/manifestparser/ManifestParser.java
index 55bcb3d..60118f8 100644
--- a/framework/src/main/java/org/apache/felix/framework/util/manifestparser/ManifestParser.java
+++ b/framework/src/main/java/org/apache/felix/framework/util/manifestparser/ManifestParser.java
@@ -441,7 +441,7 @@
newAttrs.putAll(attrs);
// Create filter now so we can inject filter directive.
- SimpleFilter sf = BundleRequirementImpl.convertToFilter(newAttrs);
+ SimpleFilter sf = SimpleFilter.convert(newAttrs);
// Inject filter directive.
// TODO: OSGi R4.3 - Can we insert this on demand somehow?
@@ -1352,7 +1352,7 @@
clauses.get(0).m_paths.get(0));
// Create filter now so we can inject filter directive.
- SimpleFilter sf = BundleRequirementImpl.convertToFilter(newAttrs);
+ SimpleFilter sf = SimpleFilter.convert(newAttrs);
// Inject filter directive.
// TODO: OSGi R4.3 - Can we insert this on demand somehow?
@@ -1450,7 +1450,7 @@
newAttrs.putAll(attrs);
// Create filter now so we can inject filter directive.
- SimpleFilter sf = BundleRequirementImpl.convertToFilter(newAttrs);
+ SimpleFilter sf = SimpleFilter.convert(newAttrs);
// Inject filter directive.
// TODO: OSGi R4.3 - Can we insert this on demand somehow?
diff --git a/framework/src/main/java/org/apache/felix/framework/wiring/BundleRequirementImpl.java b/framework/src/main/java/org/apache/felix/framework/wiring/BundleRequirementImpl.java
index 300cd4d..eabd5bc 100644
--- a/framework/src/main/java/org/apache/felix/framework/wiring/BundleRequirementImpl.java
+++ b/framework/src/main/java/org/apache/felix/framework/wiring/BundleRequirementImpl.java
@@ -64,7 +64,7 @@
BundleRevision revision, String namespace,
Map<String, String> dirs, Map<String, Object> attrs)
{
- this(revision, namespace, dirs, Collections.EMPTY_MAP, convertToFilter(attrs));
+ this(revision, namespace, dirs, Collections.EMPTY_MAP, SimpleFilter.convert(attrs));
}
public String getNamespace()
@@ -106,99 +106,4 @@
{
return "[" + m_revision + "] " + m_namespace + "; " + getFilter().toString();
}
-
- private static SimpleFilter convertToFilter(Map<String, Object> attrs)
- {
- // Rather than building a filter string to be parsed into a SimpleFilter,
- // we will just create the parsed SimpleFilter directly.
-
- List<SimpleFilter> filters = new ArrayList<SimpleFilter>();
-
- for (Entry<String, Object> entry : attrs.entrySet())
- {
- if (entry.getValue() instanceof VersionRange)
- {
- VersionRange vr = (VersionRange) entry.getValue();
- if (vr.isFloorInclusive())
- {
- filters.add(
- new SimpleFilter(
- entry.getKey(),
- vr.getFloor().toString(),
- SimpleFilter.GTE));
- }
- else
- {
- SimpleFilter not =
- new SimpleFilter(null, new ArrayList(), SimpleFilter.NOT);
- ((List) not.getValue()).add(
- new SimpleFilter(
- entry.getKey(),
- vr.getFloor().toString(),
- SimpleFilter.LTE));
- filters.add(not);
- }
-
- if (vr.getCeiling() != null)
- {
- if (vr.isCeilingInclusive())
- {
- filters.add(
- new SimpleFilter(
- entry.getKey(),
- vr.getCeiling().toString(),
- SimpleFilter.LTE));
- }
- else
- {
- SimpleFilter not =
- new SimpleFilter(null, new ArrayList(), SimpleFilter.NOT);
- ((List) not.getValue()).add(
- new SimpleFilter(
- entry.getKey(),
- vr.getCeiling().toString(),
- SimpleFilter.GTE));
- filters.add(not);
- }
- }
- }
- else
- {
- List<String> values = SimpleFilter.parseSubstring(entry.getValue().toString());
- if (values.size() > 1)
- {
- filters.add(
- new SimpleFilter(
- entry.getKey(),
- values,
- SimpleFilter.SUBSTRING));
- }
- else
- {
- filters.add(
- new SimpleFilter(
- entry.getKey(),
- values.get(0),
- SimpleFilter.EQ));
- }
- }
- }
-
- SimpleFilter sf = null;
-
- if (filters.size() == 1)
- {
- sf = filters.get(0);
- }
- else if (attrs.size() > 1)
- {
- sf = new SimpleFilter(null, filters, SimpleFilter.AND);
- }
- else if (filters.isEmpty())
- {
- sf = new SimpleFilter(null, null, SimpleFilter.MATCH_ALL);
- }
-
- return sf;
- }
}
\ No newline at end of file