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.project;
020:
021: import java.io.FileFilter;
022: import java.io.File;
023: import java.util.ArrayList;
024: import java.util.Arrays;
025: import java.util.Collection;
026: import java.util.List;
027: import java.util.logging.Logger;
028: import org.netbeans.modules.xml.wsdl.model.Definitions;
029: import org.netbeans.modules.xml.wsdl.model.WSDLModel;
030: import org.netbeans.modules.xslt.tmap.model.spi.ExternalModelRetriever;
031: import org.netbeans.modules.xslt.tmap.model.api.TMapModel;
032: import org.openide.filesystems.FileUtil;
033:
034: /**
035: *
036: * @author Vitaly Bychkov
037: */
038: public class CommandlineWSDLModelRetriever implements
039: ExternalModelRetriever {
040:
041: private Logger logger = Logger
042: .getLogger(CommandlineWSDLModelRetriever.class.getName());
043: private List<File> myDependentProjectDirs;
044: private List<File> mySourceDirs;
045: private FileFilter myWsdlFilter;
046: private FileFilter myFolderFilter;
047: private boolean initialized = false;
048:
049: public CommandlineWSDLModelRetriever() {
050: }
051:
052: public void init(List<File> depedentProjectDirs,
053: List<File> sourceDirs) {
054: myDependentProjectDirs = depedentProjectDirs;
055: mySourceDirs = sourceDirs;
056: initialized = true;
057: }
058:
059: public boolean isInitialized() {
060: return initialized;
061: }
062:
063: public Collection<WSDLModel> getWSDLModels(TMapModel model,
064: String namespace) {
065: List<WSDLModel> filtredModels = new ArrayList<WSDLModel>();
066:
067: if (!isInitialized() || model == null || namespace == null) {
068: return filtredModels;
069: }
070: List<WSDLModel> allModels = getWsdlModels();
071: for (WSDLModel wSDLModel : allModels) {
072: assert wSDLModel != null;
073: Definitions def = wSDLModel.getDefinitions();
074: if (def == null) {
075: continue;
076: }
077: if (namespace.equals(def.getTargetNamespace())) {
078: filtredModels.add(wSDLModel);
079: }
080: }
081:
082: return filtredModels;
083: }
084:
085: protected WSDLModel getWsdlModel(File wsdlFile) {
086: if (wsdlFile == null) {
087: return null;
088: }
089: WSDLModel wsdlModel = null;
090: try {
091: wsdlModel = CommandlineTransformmapCatalogModel
092: .getDefault().getWsdlModel(wsdlFile.toURI());
093: } catch (Exception ex) {
094: this .logger.log(java.util.logging.Level.SEVERE,
095: "Error while getting WSDL Model ", ex);
096: throw new RuntimeException(
097: "Error while getting WSDL Model ", ex);
098: }
099:
100: if (wsdlModel != null
101: && !WSDLModel.State.VALID.equals(wsdlModel.getState())) {
102: return null;
103: }
104: return wsdlModel;
105: }
106:
107: private List<File> collectWSDLs(File folder) {
108: List<File> wsdls = new ArrayList<File>();
109: if (folder == null || !folder.isDirectory()) {
110: return wsdls;
111: }
112:
113: File[] childWsdls = folder.listFiles(getWsdlFilter());
114: if (childWsdls != null && childWsdls.length > 0) {
115: wsdls.addAll(Arrays.asList(childWsdls));
116: }
117:
118: File[] childFolders = folder.listFiles(getFolderFilter());
119: if (childFolders != null && childFolders.length > 0) {
120: for (File childFolder : childFolders) {
121: wsdls.addAll(collectWSDLs(childFolder));
122: }
123: }
124:
125: return wsdls;
126: }
127:
128: private List<WSDLModel> getWsdlModels() {
129: List<WSDLModel> models = new ArrayList<WSDLModel>();
130: List<File> sourceDirs = getSourceDirs();
131: if (sourceDirs == null || sourceDirs.size() < 1) {
132: return models;
133: }
134: List<File> wsdls = new ArrayList<File>();
135: for (File src : sourceDirs) {
136: wsdls.addAll(collectWSDLs(src));
137: }
138:
139: for (File file : wsdls) {
140: WSDLModel curWsdlModel = getWsdlModel(file);
141: if (curWsdlModel != null) {
142: models.add(curWsdlModel);
143: }
144: }
145:
146: return models;
147: }
148:
149: public List<File> getDepedentProjectDirs() {
150: return myDependentProjectDirs;
151: }
152:
153: public List<File> getSourceDirs() {
154: return mySourceDirs;
155: }
156:
157: /**
158: * Isn't thread safety
159: */
160: private FileFilter getFolderFilter() {
161: if (myFolderFilter == null) {
162: myFolderFilter = new FoldersFilter();
163: }
164: return myFolderFilter;
165: }
166:
167: /**
168: * Isn't thread safety
169: */
170: private FileFilter getWsdlFilter() {
171: if (myWsdlFilter == null) {
172: myWsdlFilter = new WsdlFileFilter();
173: }
174: return myWsdlFilter;
175: }
176:
177: private class FoldersFilter implements FileFilter {
178:
179: public boolean accept(File f) {
180: if (f == null) {
181: return false;
182: }
183:
184: return f.isDirectory();
185: }
186: }
187:
188: private class WsdlFileFilter implements FileFilter {
189:
190: public boolean accept(File f) {
191: if (f == null || f.isDirectory()) {
192: return false;
193: }
194:
195: return "wsdl".equals(FileUtil.getExtension(f.getName()));
196: }
197: }
198:
199: }
|