1 32 33 package websphinx.workbench; 34 35 import java.awt.*; 36 import websphinx.*; 37 import rcm.awt.Constrain; 38 39 public class PagePredicateEditor extends Panel { 41 PageFeatureChoice choice; 42 43 46 public PagePredicateEditor () { 47 setLayout (new GridBagLayout ()); 48 choice = new PageFeatureChoice (); 49 Constrain.add (this, choice, Constrain.labelLike (0, 0)); 50 Constrain.add (this, choice.getArgs(), Constrain.areaLike (0, 1)); 51 setPagePredicate (null); 52 } 53 54 public void setPagePredicate (PagePredicate pred) { 55 choice.setPagePredicate (pred); 57 } 58 59 public PagePredicate getPagePredicate () { 60 return choice.getPagePredicate (); 61 } 62 63 } 64 65 class PageFeatureChoice extends FeatureChoice { 66 PageFeatureArgs args = new PageFeatureArgs (); 67 68 final static String NULL_FEATURE = "all pages"; 69 final static String URL_FEATURE = "URL"; 70 final static String HTML_FEATURE = "HTML"; 71 final static String TEXT_FEATURE = "text"; 72 final static String LABEL_FEATURE = "labels"; 73 final static String TITLE_FEATURE = "title"; 74 final static String SCRIPT_FEATURE = "script"; 75 76 public PageFeatureChoice () { 77 addItem (NULL_FEATURE); 78 addItem (LABEL_FEATURE); 79 addItem (TITLE_FEATURE); 80 addItem (URL_FEATURE); 81 addItem (TEXT_FEATURE); 82 addItem (HTML_FEATURE); 83 addItem (SCRIPT_FEATURE); 84 } 85 86 public void setPagePredicate (PagePredicate pred) { 87 PagePredicate neg = null; 88 if (pred instanceof DualPredicate) { 89 neg = (PagePredicate)((DualPredicate)pred).getNegativePredicate (); 90 pred = (PagePredicate)((DualPredicate)pred).getPositivePredicate (); 91 } 92 93 if (pred == null) { 94 select (NULL_FEATURE); 95 } 96 else if (pred instanceof URLPredicate) { 97 URLPredicate urlpred = (URLPredicate)pred; 98 URLPredicate urlneg = (URLPredicate)neg; 99 select (URL_FEATURE); 100 args.setURLPattern (urlpred.getPattern ().toString()); 101 args.setURLNegPattern (urlneg != null 102 ? urlneg.getPattern ().toString () 103 : ""); 104 } 105 else if (pred instanceof ContentPredicate) { 106 ContentPredicate contpred = (ContentPredicate)pred; 107 ContentPredicate contneg = (ContentPredicate)neg; 108 if (contpred.getOverHTML()) { 109 select (HTML_FEATURE); 110 args.setHTMLPattern (contpred.getPattern ().toString()); 111 args.setHTMLNegPattern (contneg != null 112 ? contneg.getPattern ().toString () 113 : ""); 114 } 115 else { 116 select (TEXT_FEATURE); 117 args.setTextPattern (contpred.getPattern ().toString()); 118 args.setTextNegPattern (contneg != null 119 ? contneg.getPattern ().toString () 120 : ""); 121 } 122 } 123 else if (pred instanceof TitlePredicate) { 124 TitlePredicate titlepred = (TitlePredicate)pred; 125 TitlePredicate titleneg = (TitlePredicate)neg; 126 select (TITLE_FEATURE); 127 args.setTitlePattern (titlepred.getPattern ().toString()); 128 args.setTitleNegPattern (titleneg != null 129 ? titleneg.getPattern ().toString () 130 : ""); 131 } 132 else if (pred instanceof LabelPredicate) { 133 LabelPredicate labelpred = (LabelPredicate)pred; 134 select (LABEL_FEATURE); 135 args.setOrTerms (labelpred.getOrTerms()); 136 args.setLabels (labelpred.getLabels()); 137 } 138 else if (pred instanceof Script) { 139 Script script = (Script)pred; 140 select (SCRIPT_FEATURE); 141 args.setScript (script.getScript ()); 142 } 143 else { 144 select (NULL_FEATURE); 145 } 146 } 147 148 public Panel getArgs () { 149 return args; 150 } 151 152 public PagePredicate getPagePredicate () { 153 String feat = getSelectedItem (); 154 if (feat.equals (URL_FEATURE)) 155 return makeSingleOrDual (new URLPredicate (new Wildcard (args.getURLPattern())), 156 args.getURLNegPattern().length() == 0 157 ? null 158 : new URLPredicate (new Wildcard (args.getURLNegPattern()))); 159 else if (feat.equals (HTML_FEATURE)) 160 return makeSingleOrDual (new ContentPredicate (new Tagexp (args.getHTMLPattern()), true), 161 args.getHTMLNegPattern().length() == 0 162 ? null 163 : new ContentPredicate (new Tagexp (args.getHTMLNegPattern()), true)); 164 else if (feat.equals (TEXT_FEATURE)) 165 return makeSingleOrDual (new ContentPredicate (new Regexp (args.getTextPattern()), false), 166 args.getTextNegPattern().length() == 0 167 ? null 168 : new ContentPredicate (new Regexp (args.getTextNegPattern()), false)); 169 else if (feat.equals (TITLE_FEATURE)) 170 return makeSingleOrDual (new TitlePredicate (new Regexp (args.getTitlePattern())), 171 args.getTitleNegPattern().length() == 0 172 ? null 173 : new TitlePredicate (new Regexp (args.getTitleNegPattern()))); 174 else if (feat.equals (LABEL_FEATURE)) 175 return new LabelPredicate (args.getLabels(), args.getOrTerms()); 176 else if (feat.equals (SCRIPT_FEATURE)) 177 return new Script (args.getScript (), false); 178 else 179 return null; 180 } 181 182 private static PagePredicate makeSingleOrDual (PagePredicate positive, 183 PagePredicate negative) { 184 return negative == null 185 ? positive 186 : new DualPredicate (positive, negative); 187 } 188 } 189 190 class PageFeatureArgs extends Panel { 191 192 TextField urlPattern; 193 TextField urlNegPattern; 194 TextField textPattern; 195 TextField textNegPattern; 196 TextField htmlPattern; 197 TextField htmlNegPattern; 198 TextField titlePattern; 199 TextField titleNegPattern; 200 TextField labels; 201 Choice orTerms; 202 TextArea script; 203 204 final static String ANY_TERMS = "any"; 205 final static String ALL_TERMS = "all"; 206 207 public PageFeatureArgs () { 208 Panel panel; 209 210 setLayout (new CardLayout ()); 211 212 add (PageFeatureChoice.NULL_FEATURE, panel = new Panel ()); 213 214 add (PageFeatureChoice.URL_FEATURE, panel = Constrain.makeConstrainedPanel (1, 4)); 215 Constrain.add (panel, new Label (" matches the wildcard expression "), Constrain.labelLike (0, 0)); 216 Constrain.add (panel, urlPattern = new TextField (), Constrain.fieldLike (0, 1)); 217 Constrain.add (panel, new Label (" but not the expression "), Constrain.labelLike (0, 2)); 218 Constrain.add (panel, urlNegPattern = new TextField (), Constrain.fieldLike (0, 3)); 219 220 add (PageFeatureChoice.HTML_FEATURE, panel = Constrain.makeConstrainedPanel (1, 4)); 221 Constrain.add (panel, new Label (" matches the HTML tag expression "), Constrain.labelLike (0, 0)); 222 Constrain.add (panel, htmlPattern = new TextField (), Constrain.fieldLike (0, 1)); 223 Constrain.add (panel, new Label (" but not the expression "), Constrain.labelLike (0, 2)); 224 Constrain.add (panel, htmlNegPattern = new TextField (), Constrain.fieldLike (0, 3)); 225 226 add (PageFeatureChoice.TEXT_FEATURE, panel = Constrain.makeConstrainedPanel (1, 4)); 227 Constrain.add (panel, new Label (" matches the regular expression "), Constrain.labelLike (0, 0)); 228 Constrain.add (panel, textPattern = new TextField (), Constrain.fieldLike (0, 1)); 229 Constrain.add (panel, new Label (" but not the expression "), Constrain.labelLike (0, 2)); 230 Constrain.add (panel, textNegPattern = new TextField (), Constrain.fieldLike (0, 3)); 231 232 add (PageFeatureChoice.TITLE_FEATURE, panel = Constrain.makeConstrainedPanel (1, 4)); 233 Constrain.add (panel, new Label (" matches the regular expression "), Constrain.labelLike (0, 0)); 234 Constrain.add (panel, titlePattern = new TextField (), Constrain.fieldLike (0, 1)); 235 Constrain.add (panel, new Label (" but not the expression "), Constrain.labelLike (0, 2)); 236 Constrain.add (panel, titleNegPattern = new TextField (), Constrain.fieldLike (0, 3)); 237 238 add (PageFeatureChoice.LABEL_FEATURE, panel = Constrain.makeConstrainedPanel (3, 2)); 239 Constrain.add (panel, new Label (" include "), Constrain.labelLike (0, 0)); 240 Constrain.add (panel, orTerms = new Choice (), Constrain.labelLike (1, 0)); 241 orTerms.addItem (ANY_TERMS); 242 orTerms.addItem (ALL_TERMS); 243 orTerms.select (ANY_TERMS); 244 Constrain.add (panel, new Label (" of the labels "), Constrain.labelLike (2, 0)); 245 Constrain.add (panel, labels = new TextField (), Constrain.fieldLike (0, 1, 3)); 246 247 ScriptInterpreter interp = Context.getScriptInterpreter (); 248 if (interp != null) { 249 add (PageFeatureChoice.SCRIPT_FEATURE, 250 panel = Constrain.makeConstrainedPanel (1, 2)); 251 Constrain.add (panel, new Label (interp.getLanguage() + " Function (crawler, page)"), 252 Constrain.labelLike (0, 0)); 253 Constrain.add (panel, script = new TextArea ("return true;\n"), 254 Constrain.areaLike (0, 1)); 255 } 256 else { 257 add (PageFeatureChoice.SCRIPT_FEATURE, 258 panel = Constrain.makeConstrainedPanel (1, 1)); 259 Constrain.add (panel, new Label ("No scripting language is available."), 260 Constrain.labelLike (0, 0)); 261 } 262 } 263 264 public void setURLPattern (String pattern) { 265 urlPattern.setText (pattern); 266 } 267 268 public String getURLPattern () { 269 return urlPattern.getText (); 270 } 271 272 public void setURLNegPattern (String pattern) { 273 urlNegPattern.setText (pattern); 274 } 275 276 public String getURLNegPattern () { 277 return urlNegPattern.getText (); 278 } 279 280 public void setTextPattern (String pattern) { 281 textPattern.setText (pattern); 282 } 283 284 public String getTextPattern () { 285 return textPattern.getText (); 286 } 287 288 public void setTextNegPattern (String pattern) { 289 textNegPattern.setText (pattern); 290 } 291 292 public String getTextNegPattern () { 293 return textNegPattern.getText (); 294 } 295 296 public void setHTMLPattern (String pattern) { 297 htmlPattern.setText (pattern); 298 } 299 300 public String getHTMLPattern () { 301 return htmlPattern.getText (); 302 } 303 304 public void setHTMLNegPattern (String pattern) { 305 htmlNegPattern.setText (pattern); 306 } 307 308 public String getHTMLNegPattern () { 309 return htmlNegPattern.getText (); 310 } 311 312 public void setTitlePattern (String pattern) { 313 titlePattern.setText (pattern); 314 } 315 316 public String getTitlePattern () { 317 return titlePattern.getText (); 318 } 319 320 public void setTitleNegPattern (String pattern) { 321 titleNegPattern.setText (pattern); 322 } 323 324 public String getTitleNegPattern () { 325 return titleNegPattern.getText (); 326 } 327 328 public void setLabels (String pattern) { 329 labels.setText (pattern); 330 } 331 332 public String getLabels () { 333 return labels.getText (); 334 } 335 336 public void setOrTerms (boolean orTerms) { 337 this.orTerms.select (orTerms ? ANY_TERMS : ALL_TERMS); 338 } 339 340 public boolean getOrTerms () { 341 return orTerms.getSelectedItem ().equals (ANY_TERMS); 342 } 343 344 public void setScript (String script) { 345 this.script.setText (script); 346 } 347 348 public String getScript () { 349 return script != null ? script.getText () : null; 350 } 351 352 } 353 | Popular Tags |