001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019: package org.netbeans.modules.xslt.tmap.model.impl;
020:
021: import java.io.File;
022: import java.util.ArrayList;
023: import java.util.Arrays;
024: import java.util.Collection;
025: import java.util.Collections;
026: import java.util.LinkedList;
027: import java.util.List;
028: import java.util.Map;
029: import java.util.Map.Entry;
030: import java.util.Set;
031: import org.netbeans.api.project.FileOwnerQuery;
032: import org.netbeans.api.project.Project;
033: import org.netbeans.api.project.ProjectUtils;
034: import org.netbeans.api.project.SourceGroup;
035: import org.netbeans.modules.xml.catalogsupport.DefaultProjectCatalogSupport;
036: import org.netbeans.modules.xml.catalogsupport.ProjectConstants;
037: import org.netbeans.modules.xml.retriever.catalog.Utilities;
038: import org.netbeans.modules.xml.retriever.catalog.Utilities.DocumentTypesEnum;
039: import org.netbeans.modules.xslt.tmap.model.spi.ExternalModelRetriever;
040: import org.netbeans.modules.xml.wsdl.model.WSDLModel;
041: import org.netbeans.modules.xml.wsdl.model.WSDLModelFactory;
042: import org.netbeans.modules.xml.xam.ModelSource;
043: import org.netbeans.modules.xslt.tmap.model.api.TMapModel;
044: import org.openide.filesystems.FileObject;
045: import org.openide.filesystems.FileUtil;
046:
047: /**
048: *
049: * @author Vitaly Bychkov
050: * @version 1.0
051: */
052: public class ExternalModelRetrieverImpl implements
053: ExternalModelRetriever {
054:
055: public ExternalModelRetrieverImpl() {
056: }
057:
058: public Collection<WSDLModel> getWSDLModels(TMapModel model,
059: String namespace) {
060: if (namespace == null) {
061: return Collections.emptyList();
062: }
063: List<WSDLModel> list = new LinkedList<WSDLModel>();
064:
065: collectWsdlModelsViaFS(model, namespace, list);
066:
067: return list;
068: }
069:
070: private static void collectWsdlModelsViaFS(TMapModel model,
071: String namespace, List<WSDLModel> list) {
072: FileObject[] files = getFilesByNamespace(
073: model.getModelSource(), namespace,
074: DocumentTypesEnum.wsdl);
075: for (FileObject file : files) {
076: WSDLModel wsdlModel;
077: ModelSource modelSource = Utilities.getModelSource(file,
078: true);
079: wsdlModel = WSDLModelFactory.getDefault().getModel(
080: modelSource);
081:
082: if (wsdlModel != null) {
083: list.add(wsdlModel);
084: }
085: }
086: }
087:
088: private static FileObject[] getFilesByNamespace(ModelSource source,
089: String namespace, DocumentTypesEnum type) {
090: assert namespace != null;
091: List<FileObject> list = new LinkedList<FileObject>();
092: FileObject modelFo = source.getLookup()
093: .lookup(FileObject.class);
094: // e.g. in case when this retriever is invoked from command line
095: if (modelFo == null) {
096: return new FileObject[0];
097: }
098:
099: Project project = FileOwnerQuery.getOwner(modelFo);
100: List<SourceGroup> sourceGroups = new ArrayList<SourceGroup>();
101: sourceGroups.addAll(Arrays.asList(ProjectUtils.getSources(
102: project).getSourceGroups(
103: ProjectConstants.SOURCES_TYPE_XML)));
104:
105: // List<String> sourceGroupTypeList = new ArrayList<String>();
106: // sourceGroupTypeList.add(ProjectConstants.SOURCES_TYPE_XML);
107:
108: DefaultProjectCatalogSupport support = project.getLookup()
109: .lookup(DefaultProjectCatalogSupport.class);
110: if (support != null) {
111: Set<Project> refProjects = support.getProjectReferences();
112: if (refProjects != null && refProjects.size() > 0) {
113: for (Project refProject : refProjects) {
114: sourceGroups
115: .addAll(Arrays
116: .asList(ProjectUtils
117: .getSources(refProject)
118: .getSourceGroups(
119: ProjectConstants.SOURCES_TYPE_XML)));
120: }
121: }
122: }
123:
124: /*
125: * We perform search only in sources folder of project for avoiding
126: * search in "build" folder.
127: * Otherwise we will have duplicate models for files wuth same contents.
128: * See bug description in #6423749 ( bugtruk ).
129: */
130: for (SourceGroup group : sourceGroups) {
131: File file = FileUtil.toFile(group.getRootFolder());
132: if (file == null) {
133: return null;
134: }
135: Map<FileObject, String> map = Utilities
136: .getFiles2NSMappingInProj(file, type);
137: for (Entry<FileObject, String> entry : map.entrySet()) {
138: String ns = entry.getValue();
139: if (namespace.equals(ns)) {
140: list.add(entry.getKey());
141: }
142: }
143: }
144: return list.toArray(new FileObject[list.size()]);
145: }
146: }
|