ManifestHeaderProcessor { 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(":")) { } else if ((Constants.VERSION_ATTRIBUTE.equals(attribName)) || (Constants.BUNDLE_VERSION_ATTRIBUTE.equals(attribName))) { 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.equals(attribName)) { realAttrib = true; filter.append(attrib.getValue()); } else if (Constants.OBJECTCLASS.equals(attribName)) { realAttrib = true; String[] values = attrib.getValue().split(","); for (String s : values) filter.append("(" + Constants.OBJECTCLASS + "=" + s + ")"); } else { realAttrib = true; filter.append("(" + attribName + "=" + attrib.getValue() + ")"); if (!!!Constants.BUNDLE_SYMBOLICNAME_ATTRIBUTE .equals(attribName)) { realAttribs.append(attribName); realAttribs.append(", "); } } } if (realAttribs.length() > 0) { String attribStr = (realAttribs.toString()).trim(); if ((attribStr.length() > 0) && (attribStr.endsWith(","))) { attribStr = attribStr.substring(0, attribStr.length() - 1); } filter.append("(" + Constants.MANDATORY_DIRECTIVE + ":" + "<*" + attribStr + ")"); } 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; }  static List<String> split(String value, String delimiter); static List<NameValuePair> parseExportString(String s); static List<NameValuePair> parseExportList(List<String> list); static Map<String, Map<String, String>> parseImportString(String s); static List<GenericMetadata> parseCapabilityString(String s); static List<GenericMetadata> parseRequirementString(String s); static NameValuePair parseBundleSymbolicName(String s); static VersionRange parseVersionRange(String s); static VersionRange parseVersionRange(String s, boolean exactVersion); static String generateFilter(Map<String, String> attribs); static String generateFilter(String type, String name,
      Map<String, String> attribs); static Map<String,String> parseFilter(String filter); static final String NESTED_FILTER_ATTRIBUTE; }
@Test public void testGenerateFilter() throws Exception { Map<String, String> valueMap = new HashMap<String, String>(); valueMap.put("version", "[1.2, 2.3]"); valueMap.put("resulution:", "mandatory"); valueMap.put("company", "com"); String filter = ManifestHeaderProcessor.generateFilter("symbolic-name", "com.ibm.foo", valueMap); String expected = createExpectedFilter(valueMap, "(&(symbolic-name=com.ibm.foo)", "(company=com)", "(version>=1.2.0)(version<=2.3.0)", "(mandatory:<*company))"); assertEquals("The filter is wrong.", expected, filter ); valueMap.clear(); valueMap.put("version", "(1.2, 2.3]"); valueMap.put("resulution:", "mandatory"); valueMap.put("company", "com"); filter = ManifestHeaderProcessor.generateFilter("symbolic-name", "com.ibm.foo", valueMap); expected = createExpectedFilter(valueMap, "(&(symbolic-name=com.ibm.foo)", "(company=com)", "(version>=1.2.0)(version<=2.3.0)(!(version=1.2.0))", "(mandatory:<*company))"); assertEquals("The filter is wrong.", expected, filter ); valueMap.clear(); valueMap.put("version", "(1.2, 2.3)"); valueMap.put("resulution:", "mandatory"); valueMap.put("company", "com"); filter = ManifestHeaderProcessor.generateFilter("symbolic-name", "com.ibm.foo", valueMap); expected = createExpectedFilter(valueMap, "(&(symbolic-name=com.ibm.foo)", "(company=com)", "(version>=1.2.0)(version<=2.3.0)(!(version=1.2.0))(!(version=2.3.0))", "(mandatory:<*company))"); assertEquals("The filter is wrong.", expected, filter ); valueMap.clear(); valueMap.put("version", "1.2"); valueMap.put("resulution:", "mandatory"); valueMap.put("company", "com"); filter = ManifestHeaderProcessor.generateFilter("symbolic-name", "com.ibm.foo", valueMap); expected = createExpectedFilter(valueMap, "(&(symbolic-name=com.ibm.foo)", "(company=com)", "(version>=1.2.0)", "(mandatory:<*company))"); assertEquals("The filter is wrong.", expected, filter ); valueMap.clear(); valueMap.put("resulution:", "mandatory"); valueMap.put("company", "com"); filter = ManifestHeaderProcessor.generateFilter("symbolic-name", "com.ibm.foo", valueMap); expected = createExpectedFilter(valueMap, "(&(symbolic-name=com.ibm.foo)", "(company=com)", "", "(mandatory:<*company))"); assertEquals("The filter is wrong.", expected, filter ); }