KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.netbeans.modules.xml.schema.model.Schema;
25 import org.netbeans.modules.xml.schema.model.SchemaComponent;
26 import org.netbeans.modules.xml.schema.model.SchemaModel;
27 import org.netbeans.modules.xml.schema.model.visitor.FindSchemaComponentFromDOM;
28 import org.netbeans.modules.xml.xdm.nodes.Document;
29 import org.netbeans.modules.xml.xdm.nodes.Element;
30 import org.netbeans.modules.xml.xdm.nodes.Node;
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.xdm.visitor.XPathFinder;
35 import org.openide.util.NbBundle;
36
37 /**
38  * Implements a SearchProvider that finds components matching an XML Path.
39  *
40  * @author Nathan Fiedler
41  */

42 public class XPathSearchProvider implements SearchProvider {
43     /** Model in which to perform the search. */
44     private SchemaModel model;
45     /** List of matching schema components. */
46     private List JavaDoc<Object JavaDoc> results;
47
48     /**
49      * Creates a new instance of XPathSearchProvider.
50      *
51      * @param model schema model in which to perform search.
52      */

53     public XPathSearchProvider(SchemaModel model) {
54         this.model = model;
55     }
56
57     public String JavaDoc getDisplayName() {
58         return NbBundle.getMessage(XPathSearchProvider.class,
59                 "LBL_SearchProvider_XPath");
60     }
61
62     public String JavaDoc getInputDescription() {
63         return NbBundle.getMessage(XPathSearchProvider.class,
64                 "HELP_SearchProvider_XPath");
65     }
66
67     public String JavaDoc getShortDescription() {
68         return NbBundle.getMessage(XPathSearchProvider.class,
69                 "HINT_SearchProvider_XPath");
70     }
71
72     public List JavaDoc<Object JavaDoc> search(Query query) throws SearchException {
73         results = new ArrayList JavaDoc<Object JavaDoc>();
74         Schema schema = model.getSchema();
75         org.w3c.dom.Document JavaDoc document = model.getDocument();
76         if (document instanceof Document) {
77             XPathFinder xfinder = new XPathFinder();
78             try {
79                 List JavaDoc<Node> nodes = xfinder.findNodes((Document) document,
80                         query.getQuery());
81                 FindSchemaComponentFromDOM dfinder = new FindSchemaComponentFromDOM();
82                 for (Node node : nodes) {
83                     if (node instanceof Element) {
84                         SchemaComponent comp = dfinder.findComponent(schema,
85                                 (Element) node);
86                         results.add(comp);
87                     }
88                     // Else, Attr is not connected to the DOM tree, so no
89
// way to find the path from the root.
90
}
91             } catch (RuntimeException JavaDoc re) {
92                 // Some expressions (e.g. /) lead to an exception.
93
// Ignore them and indicate a failed search.
94
}
95         }
96         return results;
97     }
98 }
99
Popular Tags