KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > schema > refactoring > SchemaRefactoringEngine


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.schema.refactoring;
20
21 import java.io.IOException JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.List JavaDoc;
25 import org.netbeans.modules.xml.refactoring.DeleteRequest;
26 import org.netbeans.modules.xml.refactoring.FileRenameRequest;
27 import org.netbeans.modules.xml.refactoring.RefactorRequest;
28 import org.netbeans.modules.xml.refactoring.RenameRequest;
29 import org.netbeans.modules.xml.refactoring.Usage;
30 import org.netbeans.modules.xml.refactoring.UsageGroup;
31 import org.netbeans.modules.xml.refactoring.spi.RefactoringEngine;
32 import org.netbeans.modules.xml.refactoring.spi.SharedUtils;
33 import org.netbeans.modules.xml.refactoring.spi.UIHelper;
34 import org.netbeans.modules.xml.schema.model.ReferenceableSchemaComponent;
35 import org.netbeans.modules.xml.schema.model.Schema;
36 import org.netbeans.modules.xml.schema.model.SchemaComponent;
37 import org.netbeans.modules.xml.schema.model.SchemaModel;
38 import org.netbeans.modules.xml.schema.model.SchemaModelReference;
39 import org.netbeans.modules.xml.schema.model.visitor.FindUsageVisitor;
40 import org.netbeans.modules.xml.schema.model.visitor.Preview;
41 import org.netbeans.modules.xml.xam.Component;
42 import org.netbeans.modules.xml.xam.Model;
43 import org.netbeans.modules.xml.xam.dom.DocumentComponent;
44 import org.netbeans.modules.xml.xam.locator.CatalogModelException;
45 import org.openide.filesystems.FileObject;
46
47 /**
48  *
49  * @author Nam Nguyen
50  */

51 public class SchemaRefactoringEngine extends RefactoringEngine {
52     
53     /** Creates a new instance of SchemaRefactoringEngine */
54     public SchemaRefactoringEngine() {
55     }
56
57     public List JavaDoc<String JavaDoc> getSearchMimeTypes() {
58         return Collections.singletonList(RefactoringUtil.XSD_MIME_TYPE);
59     }
60
61     public Component getSearchRoot(FileObject file) throws IOException JavaDoc {
62         return RefactoringUtil.getSchema(file);
63     }
64
65     public List JavaDoc<UsageGroup> findUsages(Model target, Component searchRoot) {
66         if (! (target instanceof SchemaModel) ||
67             ! (searchRoot instanceof Schema)) {
68             return null;
69         }
70         SchemaModel targetModel = (SchemaModel) target;
71         Schema schema = (Schema) searchRoot;
72         UsageGroup ug = new UsageGroup(this, schema.getModel(), targetModel);
73         List JavaDoc<SchemaModelReference> refs = new ArrayList JavaDoc<SchemaModelReference>();
74         refs.addAll(schema.getImports());
75         refs.addAll(schema.getIncludes());
76         refs.addAll(schema.getRedefines());
77         for (SchemaModelReference ref : refs) {
78             SchemaModel importedModel = null;
79             try {
80                 importedModel = ref.resolveReferencedModel();
81             } catch (CatalogModelException e) {
82                 ug.addError(searchRoot, e.getMessage());
83             }
84             if (targetModel.equals(importedModel)) {
85                 ug.addItem(ref);
86                 return Collections.singletonList(ug);
87             }
88         }
89         return Collections.emptyList();
90     }
91
92     public List JavaDoc<UsageGroup> findUsages(Component target, Component searchRoot) {
93         if (! (target instanceof ReferenceableSchemaComponent) ||
94             ! (searchRoot instanceof Schema)) {
95             return Collections.emptyList();
96         }
97         
98         ReferenceableSchemaComponent referenceable =
99                 (ReferenceableSchemaComponent) target;
100         Schema schema = (Schema) searchRoot;
101         UsageGroup u = null;
102         Preview p = new FindUsageVisitor().findUsages(Collections.singleton(schema), referenceable);
103         if (! p.getUsages().keySet().isEmpty()) {
104             u = new UsageGroup(this, schema.getModel(), referenceable);
105             for (SchemaComponent c : p.getUsages().keySet()) {
106                 u.addItem(c);
107             }
108         }
109         if (u == null) {
110             return Collections.emptyList();
111         } else {
112             return Collections.singletonList(u);
113         }
114     }
115
116     public UIHelper getUIHelper() {
117         return new SchemaUIHelper();
118     }
119
120     public void refactorUsages(RefactorRequest request) throws IOException JavaDoc {
121         for (UsageGroup usage : request.getUsages().getUsages()) {
122             if (usage.getEngine() instanceof SchemaRefactoringEngine) {
123                 if (request instanceof RenameRequest) {
124                     new RenameReferenceVisitor().rename((RenameRequest)request, usage);
125                 } else if (request instanceof DeleteRequest) {
126                     // no supports for cascade delete or reset reference at this time.
127
} else if (request instanceof FileRenameRequest) {
128                     _refactorUsages((FileRenameRequest)request, usage);
129                 }
130             }
131         }
132     }
133     
134     void _refactorUsages(FileRenameRequest request, UsageGroup usage) {
135         if (request == null || usage == null || usage.getModel() == null) return;
136         if (! (usage.getModel() instanceof SchemaModel)) return;
137         SchemaModel model = (SchemaModel) usage.getModel();
138         boolean startTransaction = ! model.isIntransaction();
139         try {
140             if (startTransaction) {
141                 model.startTransaction();
142             }
143             for (Usage u : usage.getItems()) {
144                 if (u.getComponent() instanceof SchemaModelReference) {
145                     SchemaModelReference ref = (SchemaModelReference) u.getComponent();
146                     String JavaDoc newLocation = request.calculateNewLocationString(ref.getSchemaLocation());
147                     ref.setSchemaLocation(newLocation);
148                 }
149             }
150         } finally {
151             if (startTransaction && model.isIntransaction()) {
152                 model.endTransaction();
153             }
154         }
155     }
156     
157     public <T extends RefactorRequest> boolean supportsRefactorType(Class JavaDoc<T> type) {
158         return (type == RenameRequest.class ||
159                 type == DeleteRequest.class ||
160                 type == FileRenameRequest.class);
161     }
162
163     public void precheck(RefactorRequest request) {
164         if (request.getTarget() instanceof SchemaComponent) {
165             if (request instanceof RenameRequest) {
166                 RefactoringUtil.prepareDescription((RenameRequest)request, SchemaModel.class);
167             } else if (request instanceof DeleteRequest) {
168                 SharedUtils.addCascadeDeleteErrors((DeleteRequest)request, SchemaModel.class);
169             } else if (request instanceof FileRenameRequest) {
170                 RefactoringUtil.prepareDescription((FileRenameRequest)request, SchemaModel.class);
171             }
172         }
173     }
174     
175     @Override JavaDoc
176     public String JavaDoc getModelReference(Component component) {
177         if (component instanceof SchemaModelReference) {
178             return ((SchemaModelReference)component).getSchemaLocation();
179         }
180         return null;
181     }
182 }
183
Popular Tags