KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > schema > ui > basic > search > NameSpaceSearchProvider


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.schema.ui.basic.search;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.regex.Matcher JavaDoc;
25 import java.util.regex.Pattern JavaDoc;
26 import java.util.regex.PatternSyntaxException JavaDoc;
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 /**
42  * Implements a SearchProvider that looks for components belonging to a
43  * schema imported under a particular namespace, using a case-insensitive
44  * wildcard comparison.
45  *
46  * @author Nathan Fiedler
47  */

48 public class NameSpaceSearchProvider extends DeepSchemaVisitor
49         implements SearchProvider {
50     /** The last query submitted by the user, if any, lower-cased; may
51      * contain wildcards. */

52     private String JavaDoc phrase;
53     /** True if the phrase contains wildcards (e.g. * or ?). */
54     private boolean wildcarded;
55     /** Model in which to perform the search. */
56     private SchemaModel model;
57     /** List of matching schema components. */
58     private List JavaDoc<Object JavaDoc> results;
59     /** Provides the selected component, if needed. */
60     private Category category;
61     /** The compiled regular expression pattern, if provided. */
62     private Pattern JavaDoc pattern;
63
64     /**
65      * Creates a new instance of NameSpaceSearchProvider.
66      *
67      * @param model schema model in which to perform search.
68      * @param category provides the selected component.
69      */

70     public NameSpaceSearchProvider(SchemaModel model, Category category) {
71         this.model = model;
72         this.category = category;
73     }
74
75     public String JavaDoc getDisplayName() {
76         return NbBundle.getMessage(NameSpaceSearchProvider.class,
77                 "LBL_SearchProvider_NameSpace");
78     }
79
80     public String JavaDoc getInputDescription() {
81         return NbBundle.getMessage(NameSpaceSearchProvider.class,
82                 "HELP_SearchProvider_NameSpace");
83     }
84
85     public String JavaDoc getShortDescription() {
86         return NbBundle.getMessage(NameSpaceSearchProvider.class,
87                 "HINT_SearchProvider_NameSpace");
88     }
89
90     public List JavaDoc<Object JavaDoc> search(Query query) throws SearchException {
91         if (query.isRegularExpression()) {
92             try {
93                 pattern = Pattern.compile(query.getQuery());
94                 phrase = null;
95             } catch (PatternSyntaxException JavaDoc 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 JavaDoc<Object JavaDoc>();
105         // Search for named references with the given namespace.
106
if (query.useSelected()) {
107             SchemaComponent component = Providers.getSelectedComponent(category);
108             if (component != null) {
109                 component.accept(this);
110             } else {
111                 // Maybe it is a category node that is selected.
112
Class JavaDoc<? extends SchemaComponent> childType =
113                         Providers.getSelectedChildType(category);
114                 if (childType != null) {
115                     List JavaDoc<? 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     /**
129      * Compares the given namespace to the query phrase provided by the user.
130      *
131      * @param comp component to add to result set, if matched.
132      * @param namespace the name space of the given component.
133      */

134     private void match(SchemaComponent comp, String JavaDoc 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 JavaDoc 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