KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.netbeans.modules.xml.refactoring.FileRenameRequest;
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.Schema;
27 import org.netbeans.modules.xml.schema.model.SchemaModel;
28 import org.netbeans.modules.xml.schema.model.SchemaModelFactory;
29 import org.netbeans.modules.xml.xam.ModelSource;
30 import org.netbeans.modules.xml.retriever.catalog.Utilities;
31 import org.netbeans.modules.xml.wsdl.model.Import;
32 import org.netbeans.modules.xml.schema.model.SchemaComponent;
33 import org.netbeans.modules.xml.schema.model.SchemaModelReference;
34 import org.netbeans.modules.xml.xam.Component;
35 import org.netbeans.modules.xml.xam.Model;
36 import org.netbeans.modules.xml.xam.dom.AbstractDocumentComponent;
37 import org.openide.filesystems.FileObject;
38 import org.openide.filesystems.FileUtil;
39 import org.openide.util.NbBundle;
40
41 /**
42  *
43  * @author Nam Nguyen
44  */

45 public class RefactoringUtil {
46     public static final String JavaDoc XSD_MIME_TYPE = "application/x-schema+xml"; // NOI18N
47

48     public static Schema getSchema(FileObject fo) throws IOException JavaDoc {
49         if (! XSD_MIME_TYPE.equals(FileUtil.getMIMEType(fo))) {
50             return null;
51         }
52         Schema ret = null;
53         ModelSource modelSource = Utilities.getModelSource(fo, true);
54         SchemaModel sm = SchemaModelFactory.getDefault().getModel(modelSource);
55         assert sm != null : "Unexpected null model returned from factory";
56         if (sm.getState().equals(Model.State.VALID)) {
57             return sm.getSchema();
58         } else {
59             String JavaDoc msg = NbBundle.getMessage(RefactoringUtil.class, "MSG_SchemaModelNotWellFormed", fo.getPath());
60             throw new IOException JavaDoc(msg);
61         }
62     }
63
64     public static void prepareDescription(RenameRequest request, Class JavaDoc<? extends Model> referencingModelType) {
65         SchemaComponent target = (SchemaComponent) request.getTarget();
66         for (UsageGroup usage : request.getUsages().getUsages()) {
67             if (! (referencingModelType.isAssignableFrom(usage.getModel().getClass()))) {
68                 continue;
69             }
70             String JavaDoc ns = ((SchemaModel)target.getModel()).getEffectiveNamespace(target);
71             for (Object JavaDoc o : usage.getItems()) {
72                 Usage i = (Usage) o; //strange i have to do this
73
String JavaDoc prefix = ((AbstractDocumentComponent)i.getComponent()).lookupPrefix(ns);
74                 String JavaDoc refString = prefix + ":" + request.getNewName(); //NOI18N
75
//TODO a visitor to get the right attribute name from i.getComponent().
76
String JavaDoc refAttribute = "ref"; //NOI18N
77
String JavaDoc msg = NbBundle.getMessage(RefactoringUtil.class,
78                         "MSG_SetReferenceStringTo", refAttribute, refString);
79                 i.setRefactoringDescription(msg);
80             }
81         }
82     }
83
84     public static void prepareDescription(FileRenameRequest request, Class JavaDoc<? extends Model> referencingModelType) {
85         for (UsageGroup usage : request.getUsages().getUsages()) {
86             if (! (referencingModelType.isAssignableFrom(usage.getModel().getClass()))) {
87                 continue;
88             }
89             for (Usage i : usage.getItems()) {
90                 String JavaDoc refAttribute = getLocationReferenceAttributeName(i.getComponent());
91                 String JavaDoc msg = NbBundle.getMessage(RefactoringUtil.class,
92                         "MSG_SetLocationStringTo", refAttribute, getNewLocationValue(request, i.getComponent()));
93                 i.setRefactoringDescription(msg);
94             }
95         }
96     }
97     
98     public static String JavaDoc getLocationReferenceAttributeName(Component usageComponent) {
99         if (usageComponent instanceof org.netbeans.modules.xml.wsdl.model.Import) {
100             return "location"; //NOI18N
101
} else if (usageComponent instanceof SchemaModelReference) {
102             return "schemaLocation"; //NOI18N
103
} else {
104             return "ref"; //NO18N
105
}
106     }
107     
108     private static String JavaDoc getNewLocationValue(FileRenameRequest request, Component usageComponent) {
109         String JavaDoc current = ""; //NOI18N
110
if (usageComponent instanceof Import) {
111             current =((Import)usageComponent).getLocation();
112         } else if (usageComponent instanceof SchemaModelReference) {
113             current = ((SchemaModelReference)usageComponent).getSchemaLocation();
114         }
115
116         return request.calculateNewLocationString(current);
117     }
118 }
119
Popular Tags