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.schema.ui.basic.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.schema.model.AttributeGroupReference;
050: import org.netbeans.modules.xml.schema.model.AttributeReference;
051: import org.netbeans.modules.xml.schema.model.ElementReference;
052: import org.netbeans.modules.xml.schema.model.GroupReference;
053: import org.netbeans.modules.xml.schema.model.SchemaComponent;
054: import org.netbeans.modules.xml.schema.model.SchemaModel;
055: import org.netbeans.modules.xml.schema.model.visitor.DeepSchemaVisitor;
056: import org.netbeans.modules.xml.xam.ui.category.Category;
057: import org.netbeans.modules.xml.xam.ui.search.Query;
058: import org.netbeans.modules.xml.xam.ui.search.SearchException;
059: import org.netbeans.modules.xml.xam.ui.search.SearchProvider;
060: import org.netbeans.modules.xml.xam.ui.search.WildcardStringMatcher;
061: import org.openide.util.NbBundle;
062:
063: /**
064: * Implements a SearchProvider that looks for components belonging to a
065: * schema imported under a particular namespace, using a case-insensitive
066: * wildcard comparison.
067: *
068: * @author Nathan Fiedler
069: */
070: public class NameSpaceSearchProvider extends DeepSchemaVisitor
071: implements SearchProvider {
072: /** The last query submitted by the user, if any, lower-cased; may
073: * contain wildcards. */
074: private String phrase;
075: /** True if the phrase contains wildcards (e.g. * or ?). */
076: private boolean wildcarded;
077: /** Model in which to perform the search. */
078: private SchemaModel model;
079: /** List of matching schema components. */
080: private List<Object> results;
081: /** Provides the selected component, if needed. */
082: private Category category;
083: /** The compiled regular expression pattern, if provided. */
084: private Pattern pattern;
085:
086: /**
087: * Creates a new instance of NameSpaceSearchProvider.
088: *
089: * @param model schema model in which to perform search.
090: * @param category provides the selected component.
091: */
092: public NameSpaceSearchProvider(SchemaModel model, Category category) {
093: this .model = model;
094: this .category = category;
095: }
096:
097: public String getDisplayName() {
098: return NbBundle.getMessage(NameSpaceSearchProvider.class,
099: "LBL_SearchProvider_NameSpace");
100: }
101:
102: public String getInputDescription() {
103: return NbBundle.getMessage(NameSpaceSearchProvider.class,
104: "HELP_SearchProvider_NameSpace");
105: }
106:
107: public String getShortDescription() {
108: return NbBundle.getMessage(NameSpaceSearchProvider.class,
109: "HINT_SearchProvider_NameSpace");
110: }
111:
112: public List<Object> search(Query query) throws SearchException {
113: if (query.isRegularExpression()) {
114: try {
115: pattern = Pattern.compile(query.getQuery());
116: phrase = null;
117: } catch (PatternSyntaxException pse) {
118: throw new SearchException(pse.getMessage(), pse);
119: }
120:
121: } else {
122: pattern = null;
123: phrase = query.getQuery().toLowerCase();
124: wildcarded = WildcardStringMatcher
125: .containsWildcards(phrase);
126: }
127: results = new ArrayList<Object>();
128: // Search for named references with the given namespace.
129: if (query.useSelected()) {
130: SchemaComponent component = Providers
131: .getSelectedComponent(category);
132: if (component != null) {
133: component.accept(this );
134: } else {
135: // Maybe it is a category node that is selected.
136: Class<? extends SchemaComponent> childType = Providers
137: .getSelectedChildType(category);
138: if (childType != null) {
139: List<? extends SchemaComponent> components = model
140: .getSchema().getChildren(childType);
141: for (SchemaComponent comp : components) {
142: comp.accept(this );
143: }
144: }
145: }
146: } else {
147: model.getSchema().accept(this );
148: }
149: return results;
150: }
151:
152: /**
153: * Compares the given namespace to the query phrase provided by the user.
154: *
155: * @param comp component to add to result set, if matched.
156: * @param namespace the name space of the given component.
157: */
158: private void match(SchemaComponent comp, String namespace) {
159: if (namespace != null) {
160: if (phrase != null) {
161: namespace = namespace.toLowerCase();
162: if (wildcarded) {
163: if (WildcardStringMatcher.match(namespace, phrase)) {
164: results.add(comp);
165: }
166: } else if (namespace.indexOf(phrase) > -1) {
167: results.add(comp);
168: }
169: } else if (pattern != null) {
170: Matcher matcher = pattern.matcher(namespace);
171: if (matcher.find()) {
172: results.add(comp);
173: }
174: }
175: }
176: }
177:
178: public void visit(AttributeReference ar) {
179: super .visit(ar);
180: match(ar, ar.getRef().getEffectiveNamespace());
181: }
182:
183: public void visit(AttributeGroupReference agr) {
184: super .visit(agr);
185: match(agr, agr.getGroup().getEffectiveNamespace());
186: }
187:
188: public void visit(ElementReference er) {
189: super .visit(er);
190: match(er, er.getRef().getEffectiveNamespace());
191: }
192:
193: public void visit(GroupReference gr) {
194: super.visit(gr);
195: match(gr, gr.getRef().getEffectiveNamespace());
196: }
197: }
|