KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > wsdl > refactoring > xsd > FindSchemaUsageVisitor


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 package org.netbeans.modules.xml.wsdl.refactoring.xsd;
20
21 import java.util.ArrayList JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.List JavaDoc;
24 import org.netbeans.modules.xml.refactoring.FindUsageResult;
25 import org.netbeans.modules.xml.refactoring.RefactoringManager;
26 import org.netbeans.modules.xml.refactoring.UsageGroup;
27 import org.netbeans.modules.xml.refactoring.UsageSet;
28 import org.netbeans.modules.xml.refactoring.spi.RefactoringEngine;
29 import org.netbeans.modules.xml.schema.model.GlobalElement;
30 import org.netbeans.modules.xml.schema.model.GlobalType;
31 import org.netbeans.modules.xml.schema.model.ReferenceableSchemaComponent;
32 import org.netbeans.modules.xml.schema.model.Schema;
33 import org.netbeans.modules.xml.wsdl.model.Definitions;
34 import org.netbeans.modules.xml.wsdl.model.ExtensibilityElement;
35 import org.netbeans.modules.xml.wsdl.model.Part;
36 import org.netbeans.modules.xml.wsdl.model.Types;
37 import org.netbeans.modules.xml.wsdl.model.WSDLComponent;
38 import org.netbeans.modules.xml.wsdl.model.visitor.ChildVisitor;
39 import org.netbeans.modules.xml.wsdl.model.visitor.WSDLVisitor;
40 import org.netbeans.modules.xml.xam.Component;
41 import org.netbeans.modules.xml.xam.NamedReferenceable;
42 import org.netbeans.modules.xml.xam.dom.NamedComponentReference;
43 import org.openide.ErrorManager;
44
45 /**
46  *
47  * @author Nam Nguyen
48  */

49 public class FindSchemaUsageVisitor extends ChildVisitor implements WSDLVisitor {
50     private ReferenceableSchemaComponent referenced;
51     private UsageGroup usage;
52     private List JavaDoc<UsageGroup> usages;
53     
54     /** Creates a new instance of FindUsageVisitor */
55     public FindSchemaUsageVisitor() {
56     }
57     
58     public List JavaDoc<UsageGroup> findUsages(ReferenceableSchemaComponent referenced,
59             Definitions wsdl, RefactoringEngine engine) {
60         this.referenced = referenced;
61         usage = new UsageGroup(engine, wsdl.getModel(), referenced);
62         usages = new ArrayList JavaDoc<UsageGroup>();
63         wsdl.accept(this);
64         if (!usage.isEmpty()) {
65             usages.add(usage);
66         }
67         return usages;
68     }
69     
70     public void visit(Types types) {
71         Collection JavaDoc<Schema> schemas = types.getSchemas();
72         if (schemas == null || schemas.size() == 0)
73             return;
74         for (Schema schema : schemas) {
75             collectUsage(schema);
76         }
77         super.visit(types);
78     }
79     
80     public void visit(Part part) {
81         try {
82             if (referenced instanceof GlobalElement && part.getElement() != null &&
83                     part.getElement().references((GlobalElement)referenced) ||
84                     referenced instanceof GlobalType && part.getType() != null &&
85                     part.getType().references((GlobalType)referenced))
86             {
87                 usage.addItem(part);
88             }
89         } catch(Exception JavaDoc e) {
90             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
91             usage.addError(part, e.getMessage());
92         }
93         super.visit(part);
94     }
95     
96     //extensibility element referencing schema component needs to have own engines
97
/*public void visit(ExtensibilityElement ee) {
98         collectUsage(ee);
99         super.visit(ee);
100     }*/

101     
102     private void collectUsage(Component searchRoot) {
103         FindUsageResult result = RefactoringManager.getInstance().findUsages(referenced, searchRoot);
104         try {
105             UsageSet usageSet = result.get();
106             for (UsageGroup u : usageSet.getUsages()) {
107                 if (! u.isEmpty()) {
108                     usages.add(u);
109                 }
110             }
111         } catch(Exception JavaDoc e) {
112             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
113             usage.addError(searchRoot, e.getMessage());
114         }
115     }
116 }
117
Popular Tags