KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > wsdl > ui > search > AttributeNameSearchProvider


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.wsdl.ui.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.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 JavaDoc;
37 import org.w3c.dom.Node JavaDoc;
38
39 /**
40  * Implements a SearchProvider that compares the name of each attribute
41  * with the query string, using a case-insensitive string comparison.
42  *
43  * @author Nathan Fiedler
44  */

45 public class AttributeNameSearchProvider extends ChildVisitor
46         implements SearchProvider {
47     /** The last query submitted by the user, if any, lower-cased. */
48     private String JavaDoc phrase;
49     /** True if the phrase contains wildcards (e.g. * or ?). */
50     private boolean wildcarded;
51     /** Model in which to perform the search. */
52     private WSDLModel model;
53     /** List of matching schema components. */
54     private List JavaDoc<Object JavaDoc> results;
55     /** Provides the selected component, if needed. */
56     private Category category;
57     /** The compiled regular expression pattern, if provided. */
58     private Pattern JavaDoc pattern;
59
60     /**
61      * Creates a new instance of AttributeNameSearchProvider.
62      *
63      * @param model schema model in which to perform search.
64      * @param category provides the selected component.
65      */

66     public AttributeNameSearchProvider(WSDLModel model, Category category) {
67         this.model = model;
68         this.category = category;
69     }
70
71     public String JavaDoc getDisplayName() {
72         return NbBundle.getMessage(AttributeNameSearchProvider.class,
73                 "LBL_SearchProvider_AttributeName");
74     }
75
76     public String JavaDoc getInputDescription() {
77         return NbBundle.getMessage(AttributeNameSearchProvider.class,
78                 "HELP_SearchProvider_AttributeName");
79     }
80
81     public String JavaDoc getShortDescription() {
82         return NbBundle.getMessage(AttributeNameSearchProvider.class,
83                 "HINT_SearchProvider_AttributeName");
84     }
85
86     public List JavaDoc<Object JavaDoc> search(Query query) throws SearchException {
87         if (query.isRegularExpression()) {
88             try {
89                 pattern = Pattern.compile(query.getQuery());
90                 phrase = null;
91             } catch (PatternSyntaxException JavaDoc 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 JavaDoc<Object JavaDoc>();
100         // Search for components with the given attribute name.
101
if (query.useSelected()) {
102             WSDLComponent component = Providers.getSelectedComponent(category);
103             if (component != null) {
104                 component.accept(this);
105             } else {
106                 // Maybe it is a category node that is selected.
107
Class JavaDoc<? extends WSDLComponent> childType =
108                         Providers.getSelectedChildType(category);
109                 if (childType != null) {
110                     List JavaDoc<? 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 JavaDoc attrs = comp.getPeer().getAttributes();
125         for (int ii = 0; ii < attrs.getLength(); ii++) {
126             Node JavaDoc attr = attrs.item(ii);
127             String JavaDoc 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 JavaDoc matcher = pattern.matcher(name);
140                 if (matcher.find()) {
141                     results.add(comp);
142                     break;
143                 }
144             }
145         }
146         // Visit the children last, to get results in breadth-first order.
147
super.visitComponent(comp);
148     }
149 }
150
Popular Tags