001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.xml.wsdl.ui.search;
043:
044: import java.util.ArrayList;
045: import java.util.List;
046: import java.util.regex.Matcher;
047: import java.util.regex.Pattern;
048: import java.util.regex.PatternSyntaxException;
049: import org.netbeans.modules.xml.wsdl.model.WSDLComponent;
050: import org.netbeans.modules.xml.wsdl.model.WSDLModel;
051: import org.netbeans.modules.xml.wsdl.model.visitor.ChildVisitor;
052: import org.netbeans.modules.xml.xam.ui.category.Category;
053: import org.netbeans.modules.xml.xam.ui.search.Query;
054: import org.netbeans.modules.xml.xam.ui.search.SearchException;
055: import org.netbeans.modules.xml.xam.ui.search.SearchProvider;
056: import org.netbeans.modules.xml.xam.ui.search.WildcardStringMatcher;
057: import org.openide.util.NbBundle;
058: import org.w3c.dom.NamedNodeMap;
059: import org.w3c.dom.Node;
060:
061: /**
062: * Implements a SearchProvider that compares the name of each attribute
063: * with the query string, using a case-insensitive string comparison.
064: *
065: * @author Nathan Fiedler
066: */
067: public class AttributeNameSearchProvider extends ChildVisitor implements
068: SearchProvider {
069: /** The last query submitted by the user, if any, lower-cased. */
070: private String phrase;
071: /** True if the phrase contains wildcards (e.g. * or ?). */
072: private boolean wildcarded;
073: /** Model in which to perform the search. */
074: private WSDLModel model;
075: /** List of matching schema components. */
076: private List<Object> results;
077: /** Provides the selected component, if needed. */
078: private Category category;
079: /** The compiled regular expression pattern, if provided. */
080: private Pattern pattern;
081:
082: /**
083: * Creates a new instance of AttributeNameSearchProvider.
084: *
085: * @param model schema model in which to perform search.
086: * @param category provides the selected component.
087: */
088: public AttributeNameSearchProvider(WSDLModel model,
089: Category category) {
090: this .model = model;
091: this .category = category;
092: }
093:
094: public String getDisplayName() {
095: return NbBundle.getMessage(AttributeNameSearchProvider.class,
096: "LBL_SearchProvider_AttributeName");
097: }
098:
099: public String getInputDescription() {
100: return NbBundle.getMessage(AttributeNameSearchProvider.class,
101: "HELP_SearchProvider_AttributeName");
102: }
103:
104: public String getShortDescription() {
105: return NbBundle.getMessage(AttributeNameSearchProvider.class,
106: "HINT_SearchProvider_AttributeName");
107: }
108:
109: public List<Object> search(Query query) throws SearchException {
110: if (query.isRegularExpression()) {
111: try {
112: pattern = Pattern.compile(query.getQuery());
113: phrase = null;
114: } catch (PatternSyntaxException pse) {
115: throw new SearchException(pse.getMessage(), pse);
116: }
117: } else {
118: pattern = null;
119: phrase = query.getQuery().toLowerCase();
120: wildcarded = WildcardStringMatcher
121: .containsWildcards(phrase);
122: }
123: results = new ArrayList<Object>();
124: // Search for components with the given attribute name.
125: if (query.useSelected()) {
126: WSDLComponent component = Providers
127: .getSelectedComponent(category);
128: if (component != null) {
129: component.accept(this );
130: } else {
131: // Maybe it is a category node that is selected.
132: Class<? extends WSDLComponent> childType = Providers
133: .getSelectedChildType(category);
134: if (childType != null) {
135: List<? extends WSDLComponent> components = model
136: .getDefinitions().getChildren(childType);
137: for (WSDLComponent comp : components) {
138: comp.accept(this );
139: }
140: }
141: }
142: } else {
143: model.getDefinitions().accept(this );
144: }
145: return results;
146: }
147:
148: protected void visitComponent(WSDLComponent comp) {
149: NamedNodeMap attrs = comp.getPeer().getAttributes();
150: for (int ii = 0; ii < attrs.getLength(); ii++) {
151: Node attr = attrs.item(ii);
152: String name = attr.getNodeName();
153: if (phrase != null) {
154: name = name.toLowerCase();
155: if (wildcarded) {
156: if (WildcardStringMatcher.match(name, phrase)) {
157: results.add(comp);
158: }
159: } else if (name.indexOf(phrase) > -1) {
160: results.add(comp);
161: break;
162: }
163: } else if (pattern != null) {
164: Matcher matcher = pattern.matcher(name);
165: if (matcher.find()) {
166: results.add(comp);
167: break;
168: }
169: }
170: }
171: // Visit the children last, to get results in breadth-first order.
172: super.visitComponent(comp);
173: }
174: }
|