1 19 20 package org.netbeans.modules.xml.schema.ui.basic.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.schema.model.AttributeGroupReference; 28 import org.netbeans.modules.xml.schema.model.AttributeReference; 29 import org.netbeans.modules.xml.schema.model.ElementReference; 30 import org.netbeans.modules.xml.schema.model.GroupReference; 31 import org.netbeans.modules.xml.schema.model.SchemaComponent; 32 import org.netbeans.modules.xml.schema.model.SchemaModel; 33 import org.netbeans.modules.xml.schema.model.visitor.DeepSchemaVisitor; 34 import org.netbeans.modules.xml.xam.ui.category.Category; 35 import org.netbeans.modules.xml.xam.ui.search.Query; 36 import org.netbeans.modules.xml.xam.ui.search.SearchException; 37 import org.netbeans.modules.xml.xam.ui.search.SearchProvider; 38 import org.netbeans.modules.xml.xam.ui.search.WildcardStringMatcher; 39 import org.openide.util.NbBundle; 40 41 48 public class NameSpaceSearchProvider extends DeepSchemaVisitor 49 implements SearchProvider { 50 52 private String phrase; 53 54 private boolean wildcarded; 55 56 private SchemaModel model; 57 58 private List <Object > results; 59 60 private Category category; 61 62 private Pattern pattern; 63 64 70 public NameSpaceSearchProvider(SchemaModel model, Category category) { 71 this.model = model; 72 this.category = category; 73 } 74 75 public String getDisplayName() { 76 return NbBundle.getMessage(NameSpaceSearchProvider.class, 77 "LBL_SearchProvider_NameSpace"); 78 } 79 80 public String getInputDescription() { 81 return NbBundle.getMessage(NameSpaceSearchProvider.class, 82 "HELP_SearchProvider_NameSpace"); 83 } 84 85 public String getShortDescription() { 86 return NbBundle.getMessage(NameSpaceSearchProvider.class, 87 "HINT_SearchProvider_NameSpace"); 88 } 89 90 public List <Object > search(Query query) throws SearchException { 91 if (query.isRegularExpression()) { 92 try { 93 pattern = Pattern.compile(query.getQuery()); 94 phrase = null; 95 } catch (PatternSyntaxException pse) { 96 throw new SearchException(pse.getMessage(), pse); 97 } 98 99 } else { 100 pattern = null; 101 phrase = query.getQuery().toLowerCase(); 102 wildcarded = WildcardStringMatcher.containsWildcards(phrase); 103 } 104 results = new ArrayList <Object >(); 105 if (query.useSelected()) { 107 SchemaComponent component = Providers.getSelectedComponent(category); 108 if (component != null) { 109 component.accept(this); 110 } else { 111 Class <? extends SchemaComponent> childType = 113 Providers.getSelectedChildType(category); 114 if (childType != null) { 115 List <? extends SchemaComponent> components = 116 model.getSchema().getChildren(childType); 117 for (SchemaComponent comp : components) { 118 comp.accept(this); 119 } 120 } 121 } 122 } else { 123 model.getSchema().accept(this); 124 } 125 return results; 126 } 127 128 134 private void match(SchemaComponent comp, String namespace) { 135 if (namespace != null) { 136 if (phrase != null) { 137 namespace = namespace.toLowerCase(); 138 if (wildcarded) { 139 if (WildcardStringMatcher.match(namespace, phrase)) { 140 results.add(comp); 141 } 142 } else if (namespace.indexOf(phrase) > -1) { 143 results.add(comp); 144 } 145 } else if (pattern != null) { 146 Matcher matcher = pattern.matcher(namespace); 147 if (matcher.find()) { 148 results.add(comp); 149 } 150 } 151 } 152 } 153 154 public void visit(AttributeReference ar) { 155 super.visit(ar); 156 match(ar, ar.getRef().getEffectiveNamespace()); 157 } 158 159 public void visit(AttributeGroupReference agr) { 160 super.visit(agr); 161 match(agr, agr.getGroup().getEffectiveNamespace()); 162 } 163 164 public void visit(ElementReference er) { 165 super.visit(er); 166 match(er, er.getRef().getEffectiveNamespace()); 167 } 168 169 public void visit(GroupReference gr) { 170 super.visit(gr); 171 match(gr, gr.getRef().getEffectiveNamespace()); 172 } 173 } 174 | Popular Tags |