xml.wsdl.refactoring/src/org/netbeans/modules/xml/wsdl/refactoring/WSDLRefactoringPlugin.java
author Milutin Kristofic <mkristofic@netbeans.org>
Mon, 30 Jan 2017 14:30:54 +0100
changeset 1583 fe20f672a61a
parent 1281 436be789153e
permissions -rw-r--r--
Added Missing copyright information in source files
mkristofic@1583
     1
/*
mkristofic@1583
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
mkristofic@1583
     3
 *
mkristofic@1583
     4
 * Copyright 2009-2017 Oracle and/or its affiliates. All rights reserved.
mkristofic@1583
     5
 *
mkristofic@1583
     6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
mkristofic@1583
     7
 * Other names may be trademarks of their respective owners.
mkristofic@1583
     8
 *
mkristofic@1583
     9
 * The contents of this file are subject to the terms of either the GNU
mkristofic@1583
    10
 * General Public License Version 2 only ("GPL") or the Common
mkristofic@1583
    11
 * Development and Distribution License("CDDL") (collectively, the
mkristofic@1583
    12
 * "License"). You may not use this file except in compliance with the
mkristofic@1583
    13
 * License. You can obtain a copy of the License at
mkristofic@1583
    14
 * http://www.netbeans.org/cddl-gplv2.html
mkristofic@1583
    15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
mkristofic@1583
    16
 * specific language governing permissions and limitations under the
mkristofic@1583
    17
 * License.  When distributing the software, include this License Header
mkristofic@1583
    18
 * Notice in each file and include the License file at
mkristofic@1583
    19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
mkristofic@1583
    20
 * particular file as subject to the "Classpath" exception as provided
mkristofic@1583
    21
 * by Oracle in the GPL Version 2 section of the License file that
mkristofic@1583
    22
 * accompanied this code. If applicable, add the following below the
mkristofic@1583
    23
 * License Header, with the fields enclosed by brackets [] replaced by
mkristofic@1583
    24
 * your own identifying information:
mkristofic@1583
    25
 * "Portions Copyrighted [year] [name of copyright owner]"
mkristofic@1583
    26
 *
mkristofic@1583
    27
 * Contributor(s):
mkristofic@1583
    28
 *
mkristofic@1583
    29
 * The Original Software is NetBeans. The Initial Developer of the Original
mkristofic@1583
    30
 * Software is Sun Microsystems, Inc. Portions Copyright 2009-2010 Sun
mkristofic@1583
    31
 * Microsystems, Inc. All Rights Reserved.
mkristofic@1583
    32
 *
mkristofic@1583
    33
 * If you wish your version of this file to be governed by only the CDDL
mkristofic@1583
    34
 * or only the GPL Version 2, indicate your decision by adding
mkristofic@1583
    35
 * "[Contributor] elects to include this software in this distribution
mkristofic@1583
    36
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
mkristofic@1583
    37
 * single choice of license, a recipient has the option to distribute
mkristofic@1583
    38
 * your version of this file under either the CDDL, the GPL Version 2 or
mkristofic@1583
    39
 * to extend the choice of license to its licensees as provided above.
mkristofic@1583
    40
 * However, if you add GPL Version 2 code and therefore, elected the GPL
mkristofic@1583
    41
 * Version 2 license, then the option applies only if the new code is
mkristofic@1583
    42
 * made subject to such option by the copyright holder.
mkristofic@1583
    43
 */
sonali@175
    44
package org.netbeans.modules.xml.wsdl.refactoring;
sonali@175
    45
sonali@175
    46
import java.io.IOException;
sonali@175
    47
import java.util.ArrayList;
sonali@379
    48
import java.util.Collection;
sonali@379
    49
import java.util.Collections;
sonali@175
    50
import java.util.Collections;
sonali@175
    51
import java.util.HashMap;
sonali@175
    52
import java.util.HashSet;
sonali@352
    53
import java.util.Iterator;
sonali@175
    54
import java.util.List;
sonali@175
    55
import java.util.Map;
sonali@175
    56
import java.util.Set;
sonali@352
    57
import org.netbeans.modules.refactoring.api.Problem;
sonali@175
    58
import org.netbeans.modules.refactoring.api.RefactoringSession;
sonali@175
    59
import org.netbeans.modules.refactoring.spi.ProgressProviderAdapter;
sonali@175
    60
import org.netbeans.modules.refactoring.spi.RefactoringElementImplementation;
sonali@175
    61
import org.netbeans.modules.refactoring.spi.RefactoringPlugin;
sonali@175
    62
import org.netbeans.modules.xml.refactoring.ErrorItem;
sonali@379
    63
import org.netbeans.modules.xml.refactoring.XMLRefactoringPlugin;
sonali@175
    64
import org.netbeans.modules.xml.refactoring.XMLRefactoringTransaction;
sonali@175
    65
import org.netbeans.modules.xml.refactoring.spi.SharedUtils;
sonali@175
    66
import org.netbeans.modules.xml.schema.model.ReferenceableSchemaComponent;
sonali@175
    67
import org.netbeans.modules.xml.schema.model.SchemaModel;
sonali@175
    68
import org.netbeans.modules.xml.wsdl.model.Definitions;
sonali@175
    69
import org.netbeans.modules.xml.wsdl.model.Import;
sonali@175
    70
import org.netbeans.modules.xml.wsdl.model.ReferenceableWSDLComponent;
sonali@175
    71
import org.netbeans.modules.xml.wsdl.model.WSDLModel;
sonali@379
    72
import org.netbeans.modules.xml.wsdl.model.WSDLModelFactory;
sonali@175
    73
import org.netbeans.modules.xml.wsdl.refactoring.xsd.FindSchemaUsageVisitor;
sonali@175
    74
import org.netbeans.modules.xml.wsdl.refactoring.xsd.SchemaUsageRefactoringEngine;
sonali@175
    75
import org.netbeans.modules.xml.xam.Component;
sonali@175
    76
import org.netbeans.modules.xml.xam.Model;
sonali@379
    77
import org.netbeans.modules.xml.xam.ModelSource;
sonali@175
    78
import org.netbeans.modules.xml.xam.Referenceable;
sonali@175
    79
import org.netbeans.modules.xml.xam.locator.CatalogModelException;
yaroslavskiy@1278
    80
import org.netbeans.modules.xml.schema.model.Schema;
yaroslavskiy@1278
    81
import org.netbeans.modules.xml.schema.model.SchemaModelReference;
sonali@175
    82
import org.openide.ErrorManager;
sonali@175
    83
import org.openide.filesystems.FileObject;
sonali@379
    84
import org.openide.filesystems.FileUtil;
sonali@175
    85
sonali@379
    86
public abstract class WSDLRefactoringPlugin extends ProgressProviderAdapter implements RefactoringPlugin, XMLRefactoringPlugin {
sonali@175
    87
    
sonali@175
    88
    List<ErrorItem> findErrors;
sonali@175
    89
    RefactoringSession session;
sonali@175
    90
    XMLRefactoringTransaction transaction;
sonali@371
    91
    public static final String WSDL_MIME_TYPE = "text/x-wsdl+xml";
sonali@175
    92
    
sonali@175
    93
    public WSDLRefactoringPlugin() {
sonali@175
    94
    }
sonali@175
    95
    
sonali@175
    96
    public List<WSDLRefactoringElement> find(Referenceable target, Component searchRoot){
yaroslavskiy@1278
    97
        if (searchRoot == null) {
yaroslavskiy@1278
    98
            return null;
yaroslavskiy@1278
    99
        }
sonali@175
   100
        if (target instanceof Model) {
sonali@175
   101
            return findUsages((Model) target, searchRoot);
sonali@175
   102
        } else if (target instanceof Component) {
sonali@175
   103
            return findUsages((Component) target, searchRoot);
sonali@175
   104
        } else {
sonali@175
   105
            return null;
sonali@175
   106
        }
sonali@175
   107
    }
sonali@175
   108
                    
sonali@175
   109
    public List<WSDLRefactoringElement> findUsages(Model target, Component searchRoot) {
yaroslavskiy@1278
   110
//System.out.println();
yaroslavskiy@1278
   111
//System.out.println("FIND");
yaroslavskiy@1278
   112
//System.out.println("    " + target.getModelSource().getLookup().lookup(FileObject.class));
yaroslavskiy@1278
   113
//System.out.println("    " + searchRoot.getModel().getModelSource().getLookup().lookup(FileObject.class));
yaroslavskiy@1278
   114
//System.out.println();
sonali@175
   115
        List<WSDLRefactoringElement> elements = new ArrayList<WSDLRefactoringElement>();
yaroslavskiy@1278
   116
sonali@175
   117
        if (target instanceof WSDLModel && searchRoot instanceof Definitions) {
sonali@175
   118
            Definitions definitions = (Definitions) searchRoot;
yaroslavskiy@1278
   119
            String namespace = ((WSDLModel) target).getDefinitions().getTargetNamespace();
yaroslavskiy@1278
   120
sonali@175
   121
            for (Import i : definitions.getImports()) {
sonali@175
   122
                Model imported = null;
sonali@175
   123
                if (namespace.equals(i.getNamespace())) { 
sonali@175
   124
                    try {
sonali@175
   125
                        imported = i.getImportedWSDLModel();
sonali@175
   126
                    } catch(CatalogModelException ex) {
sonali@175
   127
                        findErrors.add(new ErrorItem(searchRoot, ex.getMessage()));
sonali@175
   128
                    }
sonali@175
   129
                }
yaroslavskiy@1278
   130
                if (imported == target) {
sonali@175
   131
                    elements.add(new WSDLRefactoringElement(searchRoot.getModel(), target, i));
sonali@175
   132
                }
sonali@175
   133
            }
sonali@175
   134
        }
yaroslavskiy@1278
   135
        // # 172444
yaroslavskiy@1278
   136
        if (target instanceof WSDLModel && target == searchRoot.getModel()) {
yaroslavskiy@1278
   137
            Definitions definitions = ((WSDLModel) target).getDefinitions();
yaroslavskiy@1278
   138
yaroslavskiy@1278
   139
            // wsdl
yaroslavskiy@1278
   140
            for (Import _import : definitions.getImports()) {
yaroslavskiy@1278
   141
                elements.add(new WSDLRefactoringElement(target, target, _import));
yaroslavskiy@1278
   142
//System.out.println("   add wsdl: " + _import.getLocation());
yaroslavskiy@1278
   143
            }
yaroslavskiy@1278
   144
            // schema
yaroslavskiy@1278
   145
            Iterator<Schema> schemas = definitions.getTypes().getSchemas().iterator();
yaroslavskiy@1278
   146
yaroslavskiy@1278
   147
            while (schemas.hasNext()) {
yaroslavskiy@1278
   148
                Iterator<SchemaModelReference> references = schemas.next().getSchemaReferences().iterator();
yaroslavskiy@1278
   149
yaroslavskiy@1278
   150
                while (references.hasNext()) {
yaroslavskiy@1278
   151
                    SchemaModelReference reference = references.next();
yaroslavskiy@1278
   152
                    elements.add(new WSDLRefactoringElement(target, target, reference));
yaroslavskiy@1278
   153
//System.out.println("   add schema ref: " + reference.getSchemaLocation());
yaroslavskiy@1278
   154
                }
yaroslavskiy@1278
   155
            }
yaroslavskiy@1278
   156
        }
yaroslavskiy@1278
   157
//System.out.println();
yaroslavskiy@1278
   158
//System.out.println();
sonali@175
   159
        SchemaUsageRefactoringEngine engine = new SchemaUsageRefactoringEngine();
sonali@175
   160
        List<WSDLRefactoringElement> elem = engine.findUsages(target, searchRoot);
yaroslavskiy@1278
   161
sonali@175
   162
        if(elem != null)
sonali@175
   163
            elements.addAll(elem);
sonali@175
   164
        
sonali@175
   165
        if(elements.size() > 0 )
sonali@175
   166
            return elements;
sonali@175
   167
        else
sonali@175
   168
            return Collections.emptyList();
yaroslavskiy@1278
   169
     }
yaroslavskiy@1278
   170
sonali@175
   171
     public List<WSDLRefactoringElement> findUsages(Component target, Component searchRoot) {
sonali@175
   172
        List<WSDLRefactoringElement> elements = new ArrayList<WSDLRefactoringElement>();
sonali@175
   173
        List<WSDLRefactoringElement> temp = null;
sonali@175
   174
              
sonali@175
   175
        if (target instanceof ReferenceableWSDLComponent && searchRoot instanceof Definitions) {
sonali@175
   176
            temp = new FindWSDLUsageVisitor().findUsages((ReferenceableWSDLComponent)target, (Definitions)searchRoot) ;
sonali@175
   177
            if(temp != null && temp.size() > 0 )
sonali@175
   178
                elements.addAll(temp);
sonali@175
   179
        }
sonali@175
   180
       
sonali@175
   181
        if (target instanceof ReferenceableSchemaComponent && searchRoot instanceof Definitions) {
sonali@175
   182
            temp = new FindSchemaUsageVisitor().findUsages( (ReferenceableSchemaComponent)target, (Definitions)searchRoot, session, transaction);
sonali@175
   183
            if(temp != null && temp.size() > 0)
sonali@175
   184
                elements.addAll(temp);
sonali@175
   185
        }
sonali@175
   186
        
sonali@175
   187
        if (elements.size() == 0) {
sonali@175
   188
            return Collections.emptyList();
sonali@175
   189
        } else {
sonali@175
   190
            return elements;
sonali@175
   191
        }
sonali@175
   192
    }
sonali@175
   193
     
sonali@175
   194
     public List<Model> getModels(List<WSDLRefactoringElement> elements){
sonali@175
   195
         List<Model> models = new ArrayList<Model>();
sonali@175
   196
         for(WSDLRefactoringElement element:elements){
jbecicka@185
   197
             models.add( ((Component)element.getLookup().lookup(Component.class)).getModel());
sonali@175
   198
         }
sonali@175
   199
         return models;
sonali@175
   200
     }
sonali@175
   201
     
yaroslavskiy@1278
   202
     public Set<Component> getSearchRoots(Referenceable target) {
yaroslavskiy@1278
   203
//System.out.println();
yaroslavskiy@1278
   204
//System.out.println("get search root");
yaroslavskiy@1278
   205
         Set<Component> searchRoots = new HashSet<Component>();
yaroslavskiy@1278
   206
         Set<FileObject> files = SharedUtils.getSearchFiles(target);
yaroslavskiy@1278
   207
yaroslavskiy@1278
   208
         for (FileObject file : files) {
yaroslavskiy@1278
   209
//System.out.println("++ file: " + file);
sonali@175
   210
            WSDLRefactoringEngine engine = new WSDLRefactoringEngine();           
sonali@175
   211
            try {
sonali@175
   212
                Component root = engine.getSearchRoot(file);
yaroslavskiy@1278
   213
//System.out.println("       : " + root);
yaroslavskiy@1281
   214
                if (root != null) {
yaroslavskiy@1281
   215
                    searchRoots.add(root);
yaroslavskiy@1281
   216
                }
yaroslavskiy@1278
   217
            }
yaroslavskiy@1278
   218
            catch (IOException ex) {
sonali@175
   219
                ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, ex.getMessage());
sonali@175
   220
            }  
yaroslavskiy@1278
   221
         }
yaroslavskiy@1278
   222
//System.out.println();
yaroslavskiy@1278
   223
         return searchRoots;
sonali@175
   224
     }
sonali@175
   225
    
sonali@175
   226
     public Map<Model, Set<RefactoringElementImplementation>> getModelMap(List<RefactoringElementImplementation> elements){
sonali@175
   227
        Map<Model, Set<RefactoringElementImplementation>> results = new HashMap<Model, Set<RefactoringElementImplementation>>();
sonali@175
   228
        for(RefactoringElementImplementation element:elements){
sonali@318
   229
           Component comp = element.getLookup().lookup(Component.class);
sonali@318
   230
           Model model = null;
sonali@318
   231
           if(comp instanceof org.netbeans.modules.xml.schema.model.Import){
sonali@318
   232
               //special case of embedded schema import statements in WSDLModel
sonali@318
   233
               //for embedded schema, group the RE impls by Foreign Model
sonali@318
   234
                  SchemaModel mod=  (SchemaModel)comp.getModel();
sonali@358
   235
                  if(mod != null) {
sonali@358
   236
                      Component wsdlImport =mod.getSchema().getForeignParent();
sonali@358
   237
                      if(wsdlImport != null) {
sonali@358
   238
                          model = wsdlImport.getModel();
sonali@358
   239
                      } 
sonali@358
   240
               }
sonali@318
   241
           } else 
sonali@318
   242
               model = comp.getModel();
sonali@358
   243
           if(model == null)
sonali@358
   244
               continue;
sonali@175
   245
           Set<RefactoringElementImplementation> elementsInModel = results.get(model);
sonali@175
   246
           if(elementsInModel == null){
sonali@175
   247
               elementsInModel = new HashSet<RefactoringElementImplementation>();
sonali@175
   248
               elementsInModel.add(element);
sonali@175
   249
               results.put(model, elementsInModel);
sonali@175
   250
           } else
sonali@175
   251
               elementsInModel.add(element);
sonali@175
   252
        }
sonali@175
   253
        return results;
sonali@175
   254
    }
sonali@175
   255
     
sonali@175
   256
     public boolean isFatal(ErrorItem error){
sonali@175
   257
        if(error.getLevel() == ErrorItem.Level.FATAL)
sonali@175
   258
            return true;
sonali@175
   259
        else
sonali@175
   260
            return false;
yaroslavskiy@1278
   261
     } 
yaroslavskiy@1278
   262
sonali@352
   263
     public Problem processErrors(List<ErrorItem> errorItems){
sonali@352
   264
        
sonali@352
   265
        if (errorItems == null || errorItems.size()== 0){
sonali@352
   266
            return null;
sonali@352
   267
        }
sonali@352
   268
        Problem parent = null;
sonali@352
   269
        Problem child = null;
sonali@352
   270
        Problem head = null;
sonali@352
   271
        Iterator<ErrorItem> iterator = errorItems.iterator();
sonali@352
   272
                
sonali@352
   273
        while(iterator.hasNext()) {
sonali@352
   274
            ErrorItem error = iterator.next();
sonali@352
   275
            if(parent == null ){
sonali@352
   276
                parent = new Problem(isFatal(error), error.getMessage());
sonali@352
   277
                child = parent;
sonali@352
   278
                head = parent;
sonali@352
   279
                continue;
sonali@352
   280
            }
sonali@352
   281
            child = new Problem(isFatal(error), error.getMessage());
sonali@352
   282
            parent.setNext(child);
sonali@352
   283
            parent = child;
sonali@352
   284
            
sonali@352
   285
        }
sonali@352
   286
        return head;
sonali@352
   287
    }
yaroslavskiy@1278
   288
sonali@379
   289
    public String getModelReference(Component component) {
sonali@379
   290
        if (component instanceof Import) {
sonali@379
   291
            return ((Import)component).getLocation();
sonali@379
   292
        }
sonali@379
   293
        return null;
sonali@379
   294
    }
sonali@379
   295
sonali@379
   296
    public void setModelReference(Component component, String location) {
sonali@379
   297
        //do nothing
sonali@379
   298
    }
sonali@379
   299
      
sonali@379
   300
    public Collection<Component> getExternalReferences(Model model) {
sonali@379
   301
        Collection<Component> refs = new ArrayList<Component>();
sonali@379
   302
        if(model instanceof WSDLModel){
sonali@379
   303
            refs.addAll(((WSDLModel)model).getDefinitions().getImports());
sonali@379
   304
        }
sonali@379
   305
        return refs;
sonali@379
   306
    }
sonali@379
   307
    
sonali@379
   308
    public Model getModel(ModelSource source) {
sonali@379
   309
       FileObject fo = source.getLookup().lookup(FileObject.class);
sonali@379
   310
       if ( WSDL_MIME_TYPE.equals(FileUtil.getMIMEType(fo))) {
sonali@379
   311
           WSDLModel model = WSDLModelFactory.getDefault().getModel(source);
sonali@379
   312
           return model;
sonali@379
   313
       }
sonali@379
   314
       return null;
sonali@379
   315
    }
sonali@175
   316
}