1 19 20 package org.netbeans.modules.xml.wsdl.ui.search; 21 22 import java.util.ArrayList ; 23 import java.util.List ; 24 import java.util.regex.Matcher ; 25 import java.util.regex.Pattern ; 26 import java.util.regex.PatternSyntaxException ; 27 import org.netbeans.modules.xml.wsdl.model.WSDLComponent; 28 import org.netbeans.modules.xml.wsdl.model.WSDLModel; 29 import org.netbeans.modules.xml.wsdl.model.visitor.ChildVisitor; 30 import org.netbeans.modules.xml.xam.ui.category.Category; 31 import org.netbeans.modules.xml.xam.ui.search.Query; 32 import org.netbeans.modules.xml.xam.ui.search.SearchException; 33 import org.netbeans.modules.xml.xam.ui.search.SearchProvider; 34 import org.netbeans.modules.xml.xam.ui.search.WildcardStringMatcher; 35 import org.openide.util.NbBundle; 36 import org.w3c.dom.NamedNodeMap ; 37 import org.w3c.dom.Node ; 38 39 45 public class AttributeNameSearchProvider extends ChildVisitor 46 implements SearchProvider { 47 48 private String phrase; 49 50 private boolean wildcarded; 51 52 private WSDLModel model; 53 54 private List <Object > results; 55 56 private Category category; 57 58 private Pattern pattern; 59 60 66 public AttributeNameSearchProvider(WSDLModel model, Category category) { 67 this.model = model; 68 this.category = category; 69 } 70 71 public String getDisplayName() { 72 return NbBundle.getMessage(AttributeNameSearchProvider.class, 73 "LBL_SearchProvider_AttributeName"); 74 } 75 76 public String getInputDescription() { 77 return NbBundle.getMessage(AttributeNameSearchProvider.class, 78 "HELP_SearchProvider_AttributeName"); 79 } 80 81 public String getShortDescription() { 82 return NbBundle.getMessage(AttributeNameSearchProvider.class, 83 "HINT_SearchProvider_AttributeName"); 84 } 85 86 public List <Object > search(Query query) throws SearchException { 87 if (query.isRegularExpression()) { 88 try { 89 pattern = Pattern.compile(query.getQuery()); 90 phrase = null; 91 } catch (PatternSyntaxException pse) { 92 throw new SearchException(pse.getMessage(), pse); 93 } 94 } else { 95 pattern = null; 96 phrase = query.getQuery().toLowerCase(); 97 wildcarded = WildcardStringMatcher.containsWildcards(phrase); 98 } 99 results = new ArrayList <Object >(); 100 if (query.useSelected()) { 102 WSDLComponent component = Providers.getSelectedComponent(category); 103 if (component != null) { 104 component.accept(this); 105 } else { 106 Class <? extends WSDLComponent> childType = 108 Providers.getSelectedChildType(category); 109 if (childType != null) { 110 List <? extends WSDLComponent> components = 111 model.getDefinitions().getChildren(childType); 112 for (WSDLComponent comp : components) { 113 comp.accept(this); 114 } 115 } 116 } 117 } else { 118 model.getDefinitions().accept(this); 119 } 120 return results; 121 } 122 123 protected void visitComponent(WSDLComponent comp) { 124 NamedNodeMap attrs = comp.getPeer().getAttributes(); 125 for (int ii = 0; ii < attrs.getLength(); ii++) { 126 Node attr = attrs.item(ii); 127 String name = attr.getNodeName(); 128 if (phrase != null) { 129 name = name.toLowerCase(); 130 if (wildcarded) { 131 if (WildcardStringMatcher.match(name, phrase)) { 132 results.add(comp); 133 } 134 } else if (name.indexOf(phrase) > -1) { 135 results.add(comp); 136 break; 137 } 138 } else if (pattern != null) { 139 Matcher matcher = pattern.matcher(name); 140 if (matcher.find()) { 141 results.add(comp); 142 break; 143 } 144 } 145 } 146 super.visitComponent(comp); 148 } 149 } 150 | Popular Tags |