KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > retriever > InfoCollector


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-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 /*
21  * InfoCollector.java
22  *
23  * Created on January 26, 2006, 3:15 PM
24  *
25  * To change this template, choose Tools | Template Manager
26  * and open the template in the editor.
27  */

28
29 package org.netbeans.modules.xml.retriever;
30
31 import java.io.File JavaDoc;
32 import java.net.URI JavaDoc;
33 import java.net.URISyntaxException JavaDoc;
34 import java.util.ArrayList JavaDoc;
35 import java.util.Collections JavaDoc;
36 import java.util.HashMap JavaDoc;
37 import java.util.List JavaDoc;
38 import java.util.Map JavaDoc;
39 import org.netbeans.modules.xml.retriever.catalog.Utilities.DocumentTypesEnum;
40 import org.netbeans.modules.xml.retriever.catalog.Utilities;
41
42 /**
43  *
44  * @author girix
45  */

46 public class InfoCollector {
47     enum InfoType{
48         url, // warning condition
49
relative_ok, //pass condition
50
relative_bad_found, // error condition
51
relative_bad_not_found, // error condition
52
relative_notfound, // warning condition
53
absolute_ok, // warning condition
54
absolute_bad, // warning condition
55
bad_uri // warning condition
56
};
57     
58     Map JavaDoc<File JavaDoc, List JavaDoc<InfoEntry>> allFiles2Info = new HashMap JavaDoc<File JavaDoc,List JavaDoc<InfoEntry>>();
59     
60     Map JavaDoc<File JavaDoc, List JavaDoc<InfoEntry>> errorFiles2Info = new HashMap JavaDoc<File JavaDoc,List JavaDoc<InfoEntry>>();
61     
62     Map JavaDoc<File JavaDoc, List JavaDoc<InfoEntry>> warningFiles2Info = new HashMap JavaDoc<File JavaDoc,List JavaDoc<InfoEntry>>();
63     
64     private Map JavaDoc<File JavaDoc, List JavaDoc<InfoEntry>> absURL2Info = new HashMap JavaDoc<File JavaDoc,List JavaDoc<InfoEntry>>();
65     
66     List JavaDoc<File JavaDoc> goodFileList = new ArrayList JavaDoc<File JavaDoc>();
67     
68     File JavaDoc root = null;
69     /** Creates a new instance of InfoCollector */
70     public InfoCollector(File JavaDoc root) {
71         this.root = root;
72         goCollect();
73     }
74     
75     public void goCollect(){
76         //get all xsd files starting from root dir
77
List JavaDoc<File JavaDoc> xsdFiles = Utilities.getFilesWithExtension(root, DocumentTypesEnum.schema.toString(), null);
78         //for each schema gather all external refs
79
Map JavaDoc<File JavaDoc,List JavaDoc<String JavaDoc>> xsdFile2Refs = getAllExternalRefs(xsdFiles, DocumentTypesEnum.schema);
80         
81         //get all wsdl files starting from root dir
82
List JavaDoc<File JavaDoc> wsdlFiles = Utilities.getFilesWithExtension(root, DocumentTypesEnum.wsdl.toString(), null);
83         //for each wsdl gather all external refs
84
Map JavaDoc<File JavaDoc,List JavaDoc<String JavaDoc>> wsdlFile2Refs = getAllExternalRefs(wsdlFiles, DocumentTypesEnum.wsdl);
85         
86         //merg 2 results
87
Map JavaDoc<File JavaDoc,List JavaDoc<String JavaDoc>> file2Refs = new HashMap JavaDoc<File JavaDoc,List JavaDoc<String JavaDoc>>();
88         file2Refs.putAll(xsdFile2Refs);
89         file2Refs.putAll(wsdlFile2Refs);
90         
91         //analyse the result and retain
92
analyzeResult(file2Refs);
93         makeGoodFileList();
94     }
95     
96     private Map JavaDoc<File JavaDoc,List JavaDoc<String JavaDoc>> getAllExternalRefs(List JavaDoc<File JavaDoc> files, DocumentTypesEnum docType){
97         Map JavaDoc<File JavaDoc,List JavaDoc<String JavaDoc>> file2Refs = new HashMap JavaDoc<File JavaDoc,List JavaDoc<String JavaDoc>>();
98         for(File JavaDoc file: files){
99             DocumentTypeParser schParser = DocumentParserFactory.getParser(docType);
100             List JavaDoc<String JavaDoc> externalRefList = null;
101             try {
102                 externalRefList = schParser.getAllLocationOfReferencedEntities(file);
103             } catch (Exception JavaDoc ex) {
104                 externalRefList = Collections.emptyList();
105             }
106             file2Refs.put(file, externalRefList);
107         }
108         return file2Refs;
109     }
110     
111     private void analyzeResult(Map JavaDoc<File JavaDoc,List JavaDoc<String JavaDoc>> file2Refs) {
112         for(File JavaDoc file : file2Refs.keySet()){
113             List JavaDoc<String JavaDoc> extRefList = file2Refs.get(file);
114             if(extRefList.size() <= 0){
115                 //this is for the files that do not have any external refs.
116
placeInProperBucket(file, new InfoEntry(null, InfoType.relative_ok));
117                 continue;
118             }
119             for(String JavaDoc refStr : extRefList){
120                 InfoEntry infEnt = analyze(file, refStr);
121                 placeInProperBucket(file, infEnt);
122             }
123         }
124     }
125     
126     private void placeInProperBucket(File JavaDoc file, InfoEntry infEnt) {
127         InfoType infoType = infEnt.getInfoType();
128         placeIn(allFiles2Info, file, infEnt);
129         switch(infoType){
130             case url:
131                 placeIn(absURL2Info, file, infEnt);
132                 break;
133             case absolute_ok:
134             case absolute_bad:
135             case bad_uri:
136             case relative_notfound:
137                 //this is a warning condition
138
placeIn(warningFiles2Info, file, infEnt);
139                 break;
140                 
141             case relative_bad_found:
142             case relative_bad_not_found:
143                 placeIn(errorFiles2Info, file, infEnt);
144                 break;
145                 
146             case relative_ok:
147                 //nothing. Filter laters
148
break;
149         }
150     }
151     
152     private void placeIn(Map JavaDoc<File JavaDoc, List JavaDoc<InfoCollector.InfoEntry>> files2Info, File JavaDoc file, InfoEntry infEnt) {
153         List JavaDoc<InfoEntry> infEntList = files2Info.get(file);
154         if(infEntList == null){
155             List JavaDoc<InfoEntry> newEntList = new ArrayList JavaDoc<InfoEntry>();
156             newEntList.add(infEnt);
157             files2Info.put(file, newEntList);
158         }else{
159             infEntList.add(infEnt);
160         }
161     }
162     
163     private void makeGoodFileList() {
164         for(File JavaDoc file : allFiles2Info.keySet()){
165             //good files are files that do not belong to errors list
166
if(!errorFiles2Info.containsKey(file))
167                 goodFileList.add(file);
168         }
169     }
170     
171     private InfoEntry analyze(File JavaDoc file, String JavaDoc refStr) {
172         String JavaDoc rootURIStr = root.toURI().toString();
173         URI JavaDoc fileURI = file.toURI();
174         URI JavaDoc refURI = null;
175         try {
176             refURI = new URI JavaDoc(refStr);
177         } catch (URISyntaxException JavaDoc ex) {
178             return new InfoEntry(refStr, InfoType.bad_uri);
179         } catch(NullPointerException JavaDoc npe){
180             return new InfoEntry(refStr, InfoType.bad_uri);
181         }
182         if(refURI.isAbsolute()){
183             if(refURI.getScheme().equalsIgnoreCase("http")) //NOI18N
184
return new InfoEntry(refStr, InfoType.url);
185             return new InfoEntry(refStr, InfoType.absolute_ok);
186         }
187         
188         URI JavaDoc finalRes = fileURI.resolve(refURI);
189         if(finalRes.toString().startsWith(rootURIStr)){
190             File JavaDoc childFile = new File JavaDoc(finalRes);
191             if(childFile.isFile())
192                 return new InfoEntry(refStr, InfoType.relative_ok);
193             else
194                 return new InfoEntry(refStr, InfoType.relative_notfound);
195         } else{
196             File JavaDoc childfile = new File JavaDoc(finalRes);
197             if(childfile.isFile())
198                 return new InfoEntry(refStr, InfoType.relative_bad_found);
199             else
200                 return new InfoEntry(refStr, InfoType.relative_bad_not_found);
201         }
202     }
203     
204     public List JavaDoc<File JavaDoc> getCopyableFileList(){
205         return goodFileList;
206     }
207     
208     
209     public Map JavaDoc<File JavaDoc, List JavaDoc<InfoEntry>> getWarnings(){
210         return warningFiles2Info;
211     }
212     
213     public Map JavaDoc<File JavaDoc, List JavaDoc<InfoEntry>> getErrors(){
214         return errorFiles2Info;
215     }
216     
217     Map JavaDoc<File JavaDoc, List JavaDoc<InfoEntry>> getAllEntries(){
218         return allFiles2Info;
219     }
220     
221     public boolean hasErrors(){
222         if(errorFiles2Info.size() > 0)
223             return true;
224         return false;
225     }
226     
227     public boolean hasWarnings(){
228         if(warningFiles2Info.size() > 0)
229             return true;
230         return false;
231     }
232     
233     public boolean hasReports(){
234         if(hasErrors() || hasWarnings())
235             return true;
236         return false;
237     }
238     
239     static class InfoEntry{
240         String JavaDoc childStr;
241         InfoType infoType;
242         public InfoEntry(String JavaDoc childStr, InfoType infoType){
243             this.childStr = childStr;
244             this.infoType = infoType;
245         }
246         
247         public String JavaDoc getChildStr(){
248             return childStr;
249         }
250         public InfoType getInfoType(){
251             return infoType;
252         }
253         
254         public String JavaDoc toString(){
255             return "[Ref:"+childStr+", InfoType:"+infoType.toString()+"]"; //NOI18N
256
}
257     }
258
259     public Map JavaDoc<File JavaDoc, List JavaDoc<InfoEntry>> getAbsURL2Info() {
260         return absURL2Info;
261     }
262     
263 }
264
Popular Tags