1 11 package org.eclipse.help.internal.dynamic; 12 13 import org.eclipse.core.runtime.IProduct; 14 import org.eclipse.core.runtime.Platform; 15 import org.osgi.framework.Bundle; 16 17 21 public class FilterResolver { 22 23 private static final String NAME_OS = "os"; private static final String NAME_WS = "ws"; private static final String NAME_ARCH = "arch"; private static final String NAME_PRODUCT = "product"; private static final String NAME_PLUGIN = "plugin"; 29 private static Extension extension; 30 private static FilterResolver instance; 31 32 public static FilterResolver getInstance() { 33 if (instance == null) { 34 instance = new FilterResolver(); 35 } 36 return instance; 37 } 38 39 43 public boolean isFiltered(String expression) { 44 String name = null; 45 String value = null; 46 boolean not = false; 47 int index = expression.indexOf("!="); if (index != -1) { 49 name = expression.substring(0, index).trim(); 50 value = expression.substring(index + 2).trim(); 51 not = true; 52 } 53 else { 54 index = expression.indexOf('='); 55 if (index != -1) { 56 name = expression.substring(0, index).trim(); 57 value = expression.substring(index + 1).trim(); 58 not = false; 59 } 60 } 61 if (name != null && name.length() > 0 && value != null && value.length() > 0) { 62 return isFiltered(name, value, not); 63 } 64 return false; 66 } 67 68 72 public boolean isFiltered(String name, String value, boolean not) { 73 boolean filtered; 74 if (name.equals(NAME_OS)) { 75 filtered = filterByWS(value); 76 } 77 else if (name.equals(NAME_WS)) { 78 filtered = filterByOS(value); 79 } 80 else if (name.equals(NAME_ARCH)) { 81 filtered = filterByARCH(value); 82 } 83 else if (name.equals(NAME_PRODUCT)) { 84 filtered = filterByProduct(value); 85 } 86 else if (name.equals(NAME_PLUGIN)) { 87 filtered = filterByPlugin(value); 88 } 89 else if (extension != null && extension.isHandled(name)) { 90 filtered = extension.isFiltered(name, value); 91 } 92 else { 93 filtered = filterBySystemProperty(name, value); 94 } 95 return not ? !filtered : filtered; 96 } 97 98 103 public static void setExtension(Extension extension) { 104 FilterResolver.extension = extension; 105 } 106 107 110 private boolean filterByWS(String ws) { 111 return !ws.equals(Platform.getWS()); 112 } 113 114 117 private boolean filterByOS(String os) { 118 return !os.equals(Platform.getOS()); 119 } 120 121 124 private boolean filterByARCH(String arch) { 125 return !arch.equals(Platform.getOSArch()); 126 } 127 128 131 private boolean filterByProduct(String productId) { 132 IProduct product = Platform.getProduct(); 133 if (product != null) { 134 return !productId.equals(product.getId()); 135 } 136 return false; 137 } 138 139 142 private boolean filterByPlugin(String bundleId) { 143 Bundle bundle = Platform.getBundle(bundleId); 144 return bundle == null; 145 } 146 147 151 private boolean filterBySystemProperty(String property, String value) { 152 try { 153 String systemValue = System.getProperty(property); 154 if (systemValue != null) { 155 return !value.equals(systemValue); 156 } 157 } 158 catch (Throwable t) { 159 } 161 return true; 162 } 163 164 168 public static interface Extension { 169 170 173 public boolean isHandled(String name); 174 175 178 public boolean isFiltered(String name, String value); 179 } 180 } 181 | Popular Tags |