in util/src/main/java/org/apache/aries/util/manifest/ManifestHeaderProcessor.java [523:633]
public static String generateFilter(Map<String, String> attribs) {
StringBuilder filter = new StringBuilder("(&");
boolean realAttrib = false;
StringBuffer realAttribs = new StringBuffer();
if (attribs == null) {
attribs = new HashMap<String, String>();
}
for (Map.Entry<String, String> attrib : attribs.entrySet()) {
String attribName = attrib.getKey();
if (attribName.endsWith(":")) {
// skip all directives. It is used to affect the attribs on the
// filter xml.
} else if ((Constants.VERSION_ATTRIBUTE.equals(attribName))
|| (Constants.BUNDLE_VERSION_ATTRIBUTE.equals(attribName))) {
// version and bundle-version attrib requires special
// conversion.
realAttrib = true;
VersionRange vr = ManifestHeaderProcessor
.parseVersionRange(attrib.getValue());
filter.append("(" + attribName + ">=" + vr.getMinimumVersion());
if (vr.getMaximumVersion() != null) {
filter.append(")(" + attribName + "<=");
filter.append(vr.getMaximumVersion());
}
if (vr.getMaximumVersion() != null && vr.isMinimumExclusive()) {
filter.append(")(!(" + attribName + "=");
filter.append(vr.getMinimumVersion());
filter.append(")");
}
if (vr.getMaximumVersion() != null && vr.isMaximumExclusive()) {
filter.append(")(!(" + attribName + "=");
filter.append(vr.getMaximumVersion());
filter.append(")");
}
filter.append(")");
} else if (NESTED_FILTER_ATTRIBUTE_TO_USE_WITHOUT_FORMATTING.equals(attribName)) {
// Filters go in whole, no formatting needed
realAttrib = true;
filter.append(attrib.getValue());
} else if (Constants.OBJECTCLASS.equals(attribName)) {
realAttrib = true;
// objectClass has a "," separated list of interfaces
String[] values = attrib.getValue().split(",");
for (String s : values)
filter.append("(" + Constants.OBJECTCLASS + "=" + s + ")");
} else {
// attribName was not version..
realAttrib = true;
filter.append("(" + attribName + "=" + attrib.getValue() + ")");
// store all attributes in order to build up the mandatory
// filter and separate them with ", "
// skip bundle-symbolic-name in the mandatory directive query
if (!Constants.BUNDLE_SYMBOLICNAME_ATTRIBUTE
.equals(attribName)) {
realAttribs.append(attribName);
realAttribs.append(", ");
}
}
}
/*
* The following is how OBR makes mandatory attributes work, we require
* that the set of mandatory attributes on the export is a subset of (or
* equal to) the set of the attributes we supply.
*/
if (realAttribs.length() > 0) {
String attribStr = (realAttribs.toString()).trim();
// remove the final ,
if ((attribStr.length() > 0) && (attribStr.endsWith(","))) {
attribStr = attribStr.substring(0, attribStr.length() - 1);
}
// build the mandatory filter, e.g.(mandatory:<*company, local)
filter.append("(" + Constants.MANDATORY_DIRECTIVE + ":" + "<*"
+ attribStr + ")");
}
// Prune (& off the front and ) off end
String filterString = filter.toString();
int openBraces = 0;
for (int i = 0; openBraces < 3; i++) {
i = filterString.indexOf('(', i);
if (i == -1) {
break;
} else {
openBraces++;
}
}
if (openBraces < 3 && filterString.length() > 2) {
filter.delete(0, 2);
} else {
filter.append(")");
}
String result = "";
if (realAttrib != false) {
result = filter.toString();
}
return result;
}