~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to xml/wsdlui/src/org/netbeans/modules/xml/wsdl/ui/search/AttributeNameSearchProvider.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
6
 * The contents of this file are subject to the terms of either the GNU
 
7
 * General Public License Version 2 only ("GPL") or the Common
 
8
 * Development and Distribution License("CDDL") (collectively, the
 
9
 * "License"). You may not use this file except in compliance with the
 
10
 * License. You can obtain a copy of the License at
 
11
 * http://www.netbeans.org/cddl-gplv2.html
 
12
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
 
13
 * specific language governing permissions and limitations under the
 
14
 * License.  When distributing the software, include this License Header
 
15
 * Notice in each file and include the License file at
 
16
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
 
17
 * particular file as subject to the "Classpath" exception as provided
 
18
 * by Sun in the GPL Version 2 section of the License file that
 
19
 * accompanied this code. If applicable, add the following below the
 
20
 * License Header, with the fields enclosed by brackets [] replaced by
 
21
 * your own identifying information:
 
22
 * "Portions Copyrighted [year] [name of copyright owner]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
26
 * The Original Software is NetBeans. The Initial Developer of the Original
 
27
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
 
28
 * Microsystems, Inc. All Rights Reserved.
 
29
 *
 
30
 * If you wish your version of this file to be governed by only the CDDL
 
31
 * or only the GPL Version 2, indicate your decision by adding
 
32
 * "[Contributor] elects to include this software in this distribution
 
33
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
 
34
 * single choice of license, a recipient has the option to distribute
 
35
 * your version of this file under either the CDDL, the GPL Version 2 or
 
36
 * to extend the choice of license to its licensees as provided above.
 
37
 * However, if you add GPL Version 2 code and therefore, elected the GPL
 
38
 * Version 2 license, then the option applies only if the new code is
 
39
 * made subject to such option by the copyright holder.
 
40
 */
 
41
 
 
42
package org.netbeans.modules.xml.wsdl.ui.search;
 
43
 
 
44
import java.util.ArrayList;
 
45
import java.util.List;
 
46
import java.util.regex.Matcher;
 
47
import java.util.regex.Pattern;
 
48
import java.util.regex.PatternSyntaxException;
 
49
import org.netbeans.modules.xml.wsdl.model.WSDLComponent;
 
50
import org.netbeans.modules.xml.wsdl.model.WSDLModel;
 
51
import org.netbeans.modules.xml.wsdl.model.visitor.ChildVisitor;
 
52
import org.netbeans.modules.xml.xam.ui.category.Category;
 
53
import org.netbeans.modules.xml.xam.ui.search.Query;
 
54
import org.netbeans.modules.xml.xam.ui.search.SearchException;
 
55
import org.netbeans.modules.xml.xam.ui.search.SearchProvider;
 
56
import org.netbeans.modules.xml.xam.ui.search.WildcardStringMatcher;
 
57
import org.openide.util.NbBundle;
 
58
import org.w3c.dom.NamedNodeMap;
 
59
import org.w3c.dom.Node;
 
60
 
 
61
/**
 
62
 * Implements a SearchProvider that compares the name of each attribute
 
63
 * with the query string, using a case-insensitive string comparison.
 
64
 *
 
65
 * @author Nathan Fiedler
 
66
 */
 
67
public class AttributeNameSearchProvider extends ChildVisitor
 
68
        implements SearchProvider {
 
69
    /** The last query submitted by the user, if any, lower-cased. */
 
70
    private String phrase;
 
71
    /** True if the phrase contains wildcards (e.g. * or ?). */
 
72
    private boolean wildcarded;
 
73
    /** Model in which to perform the search. */
 
74
    private WSDLModel model;
 
75
    /** List of matching schema components. */
 
76
    private List<Object> results;
 
77
    /** Provides the selected component, if needed. */
 
78
    private Category category;
 
79
    /** The compiled regular expression pattern, if provided. */
 
80
    private Pattern pattern;
 
81
 
 
82
    /**
 
83
     * Creates a new instance of AttributeNameSearchProvider.
 
84
     *
 
85
     * @param  model     schema model in which to perform search.
 
86
     * @param  category  provides the selected component.
 
87
     */
 
88
    public AttributeNameSearchProvider(WSDLModel model, Category category) {
 
89
        this.model = model;
 
90
        this.category = category;
 
91
    }
 
92
 
 
93
    public String getDisplayName() {
 
94
        return NbBundle.getMessage(AttributeNameSearchProvider.class,
 
95
                "LBL_SearchProvider_AttributeName");
 
96
    }
 
97
 
 
98
    public String getInputDescription() {
 
99
        return NbBundle.getMessage(AttributeNameSearchProvider.class,
 
100
                "HELP_SearchProvider_AttributeName");
 
101
    }
 
102
 
 
103
    public String getShortDescription() {
 
104
        return NbBundle.getMessage(AttributeNameSearchProvider.class,
 
105
                "HINT_SearchProvider_AttributeName");
 
106
    }
 
107
 
 
108
    public List<Object> search(Query query) throws SearchException {
 
109
        if (query.isRegularExpression()) {
 
110
            try {
 
111
                pattern = Pattern.compile(query.getQuery());
 
112
                phrase = null;
 
113
            } catch (PatternSyntaxException pse) {
 
114
                throw new SearchException(pse.getMessage(), pse);
 
115
            }
 
116
        } else {
 
117
            pattern = null;
 
118
            phrase = query.getQuery().toLowerCase();
 
119
            wildcarded = WildcardStringMatcher.containsWildcards(phrase);
 
120
        }
 
121
        results = new ArrayList<Object>();
 
122
        // Search for components with the given attribute name.
 
123
        if (query.useSelected()) {
 
124
            WSDLComponent component = Providers.getSelectedComponent(category);
 
125
            if (component != null) {
 
126
                component.accept(this);
 
127
            } else {
 
128
                // Maybe it is a category node that is selected.
 
129
                Class<? extends WSDLComponent> childType =
 
130
                        Providers.getSelectedChildType(category);
 
131
                if (childType != null) {
 
132
                    List<? extends WSDLComponent> components =
 
133
                            model.getDefinitions().getChildren(childType);
 
134
                    for (WSDLComponent comp : components) {
 
135
                        comp.accept(this);
 
136
                    }
 
137
                }
 
138
            }
 
139
        } else {
 
140
            model.getDefinitions().accept(this);
 
141
        }
 
142
        return results;
 
143
    }
 
144
 
 
145
    protected void visitComponent(WSDLComponent comp) {
 
146
        NamedNodeMap attrs = comp.getPeer().getAttributes();
 
147
        for (int ii = 0; ii < attrs.getLength(); ii++) {
 
148
            Node attr = attrs.item(ii);
 
149
            String name = attr.getNodeName();
 
150
            if (phrase != null) {
 
151
                name = name.toLowerCase();
 
152
                if (wildcarded) {
 
153
                    if (WildcardStringMatcher.match(name, phrase)) {
 
154
                        results.add(comp);
 
155
                    }
 
156
                } else if (name.indexOf(phrase) > -1) {
 
157
                    results.add(comp);
 
158
                    break;
 
159
                }
 
160
            } else if (pattern != null) {
 
161
                Matcher matcher = pattern.matcher(name);
 
162
                if (matcher.find()) {
 
163
                    results.add(comp);
 
164
                    break;
 
165
                }
 
166
            }
 
167
        }
 
168
        // Visit the children last, to get results in breadth-first order.
 
169
        super.visitComponent(comp);
 
170
    }
 
171
}