KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.IOException JavaDoc;
22 import java.util.Collection JavaDoc;
23 import org.netbeans.modules.xml.refactoring.RenameRequest;
24 import org.netbeans.modules.xml.refactoring.Usage;
25 import org.netbeans.modules.xml.refactoring.UsageGroup;
26 import org.netbeans.modules.xml.schema.model.GlobalElement;
27 import org.netbeans.modules.xml.schema.model.GlobalType;
28 import org.netbeans.modules.xml.schema.model.ReferenceableSchemaComponent;
29 import org.netbeans.modules.xml.wsdl.model.Part;
30 import org.netbeans.modules.xml.wsdl.model.WSDLComponent;
31 import org.netbeans.modules.xml.wsdl.model.WSDLModel;
32 import org.netbeans.modules.xml.wsdl.model.visitor.DefaultVisitor;
33 import org.netbeans.modules.xml.wsdl.model.visitor.WSDLVisitor;
34 import org.netbeans.modules.xml.xam.dom.NamedComponentReference;
35
36 /**
37  *
38  * @author Nam Nguyen
39  */

40 public class RenameSchemaReferenceVisitor extends DefaultVisitor implements WSDLVisitor {
41     private ReferenceableSchemaComponent renamedReferenced;
42     private String JavaDoc oldName;
43     
44     /** Creates a new instance of FindUsageVisitor */
45     public RenameSchemaReferenceVisitor() {
46     }
47     
48     public void rename(RenameRequest request, UsageGroup usage) throws IOException JavaDoc {
49         if (request == null || usage == null || usage.getModel() == null) return;
50         if (! (usage.getModel() instanceof WSDLModel)) return;
51         if (! (request.getTarget() instanceof ReferenceableSchemaComponent) ||
52                 request.getOldName() == null) {
53             return;
54         }
55
56         WSDLModel model = (WSDLModel) usage.getModel();
57         boolean startTransaction = ! model.isIntransaction();
58         renamedReferenced = (ReferenceableSchemaComponent) request.getRenamedTarget();
59         oldName = request.getOldName();
60         try {
61             if (startTransaction) {
62                 model.startTransaction();
63             }
64             Collection JavaDoc<Usage> items = usage.getItems();
65             for (Usage item : items) {
66                 if (item.isIncludedInRefactoring() &&
67                     item.getComponent() instanceof WSDLComponent) {
68                     WSDLComponent referencing = (WSDLComponent)item.getComponent();
69                     referencing.accept(this);
70                 }
71             }
72         } finally {
73             if (startTransaction && model.isIntransaction()) {
74                 model.endTransaction();
75             }
76         }
77     }
78     
79     private <T extends ReferenceableSchemaComponent> NamedComponentReference<T>
80             createReference(Class JavaDoc<T> type, WSDLComponent referencing) {
81         if (type.isAssignableFrom(renamedReferenced.getClass())) {
82             return referencing.createSchemaReference(type.cast(renamedReferenced), type);
83         } else {
84             assert false : type.getName()+" is not assignable from "+renamedReferenced.getClass().getName();
85             return null;
86         }
87     }
88     
89     public void visit(Part part) {
90         if (renamedReferenced instanceof GlobalElement &&
91             part.getElement().getQName().getLocalPart().equals(oldName))
92         {
93             NamedComponentReference<GlobalElement> ref = createReference(GlobalElement.class, part);
94             if (ref != null) {
95                 part.setElement(ref);
96                 part.setType(null);
97             }
98         } else if (renamedReferenced instanceof GlobalType &&
99                    part.getType().getQName().getLocalPart().equals(oldName))
100         {
101             NamedComponentReference<GlobalType> ref = createReference(GlobalType.class, part);
102             if (ref != null) {
103                 part.setType(ref);
104                 part.setElement(null);
105             }
106         }
107     }
108 }
Popular Tags