1 19 20 28 29 package org.netbeans.modules.xml.retriever; 30 31 import java.io.File ; 32 import java.net.URI ; 33 import java.net.URISyntaxException ; 34 import java.util.ArrayList ; 35 import java.util.Collections ; 36 import java.util.HashMap ; 37 import java.util.List ; 38 import java.util.Map ; 39 import org.netbeans.modules.xml.retriever.catalog.Utilities.DocumentTypesEnum; 40 import org.netbeans.modules.xml.retriever.catalog.Utilities; 41 42 46 public class InfoCollector { 47 enum InfoType{ 48 url, relative_ok, relative_bad_found, relative_bad_not_found, relative_notfound, absolute_ok, absolute_bad, bad_uri }; 57 58 Map <File , List <InfoEntry>> allFiles2Info = new HashMap <File ,List <InfoEntry>>(); 59 60 Map <File , List <InfoEntry>> errorFiles2Info = new HashMap <File ,List <InfoEntry>>(); 61 62 Map <File , List <InfoEntry>> warningFiles2Info = new HashMap <File ,List <InfoEntry>>(); 63 64 private Map <File , List <InfoEntry>> absURL2Info = new HashMap <File ,List <InfoEntry>>(); 65 66 List <File > goodFileList = new ArrayList <File >(); 67 68 File root = null; 69 70 public InfoCollector(File root) { 71 this.root = root; 72 goCollect(); 73 } 74 75 public void goCollect(){ 76 List <File > xsdFiles = Utilities.getFilesWithExtension(root, DocumentTypesEnum.schema.toString(), null); 78 Map <File ,List <String >> xsdFile2Refs = getAllExternalRefs(xsdFiles, DocumentTypesEnum.schema); 80 81 List <File > wsdlFiles = Utilities.getFilesWithExtension(root, DocumentTypesEnum.wsdl.toString(), null); 83 Map <File ,List <String >> wsdlFile2Refs = getAllExternalRefs(wsdlFiles, DocumentTypesEnum.wsdl); 85 86 Map <File ,List <String >> file2Refs = new HashMap <File ,List <String >>(); 88 file2Refs.putAll(xsdFile2Refs); 89 file2Refs.putAll(wsdlFile2Refs); 90 91 analyzeResult(file2Refs); 93 makeGoodFileList(); 94 } 95 96 private Map <File ,List <String >> getAllExternalRefs(List <File > files, DocumentTypesEnum docType){ 97 Map <File ,List <String >> file2Refs = new HashMap <File ,List <String >>(); 98 for(File file: files){ 99 DocumentTypeParser schParser = DocumentParserFactory.getParser(docType); 100 List <String > externalRefList = null; 101 try { 102 externalRefList = schParser.getAllLocationOfReferencedEntities(file); 103 } catch (Exception ex) { 104 externalRefList = Collections.emptyList(); 105 } 106 file2Refs.put(file, externalRefList); 107 } 108 return file2Refs; 109 } 110 111 private void analyzeResult(Map <File ,List <String >> file2Refs) { 112 for(File file : file2Refs.keySet()){ 113 List <String > extRefList = file2Refs.get(file); 114 if(extRefList.size() <= 0){ 115 placeInProperBucket(file, new InfoEntry(null, InfoType.relative_ok)); 117 continue; 118 } 119 for(String refStr : extRefList){ 120 InfoEntry infEnt = analyze(file, refStr); 121 placeInProperBucket(file, infEnt); 122 } 123 } 124 } 125 126 private void placeInProperBucket(File 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 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 break; 149 } 150 } 151 152 private void placeIn(Map <File , List <InfoCollector.InfoEntry>> files2Info, File file, InfoEntry infEnt) { 153 List <InfoEntry> infEntList = files2Info.get(file); 154 if(infEntList == null){ 155 List <InfoEntry> newEntList = new ArrayList <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 file : allFiles2Info.keySet()){ 165 if(!errorFiles2Info.containsKey(file)) 167 goodFileList.add(file); 168 } 169 } 170 171 private InfoEntry analyze(File file, String refStr) { 172 String rootURIStr = root.toURI().toString(); 173 URI fileURI = file.toURI(); 174 URI refURI = null; 175 try { 176 refURI = new URI (refStr); 177 } catch (URISyntaxException ex) { 178 return new InfoEntry(refStr, InfoType.bad_uri); 179 } catch(NullPointerException npe){ 180 return new InfoEntry(refStr, InfoType.bad_uri); 181 } 182 if(refURI.isAbsolute()){ 183 if(refURI.getScheme().equalsIgnoreCase("http")) return new InfoEntry(refStr, InfoType.url); 185 return new InfoEntry(refStr, InfoType.absolute_ok); 186 } 187 188 URI finalRes = fileURI.resolve(refURI); 189 if(finalRes.toString().startsWith(rootURIStr)){ 190 File childFile = new File (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 childfile = new File (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 <File > getCopyableFileList(){ 205 return goodFileList; 206 } 207 208 209 public Map <File , List <InfoEntry>> getWarnings(){ 210 return warningFiles2Info; 211 } 212 213 public Map <File , List <InfoEntry>> getErrors(){ 214 return errorFiles2Info; 215 } 216 217 Map <File , List <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 childStr; 241 InfoType infoType; 242 public InfoEntry(String childStr, InfoType infoType){ 243 this.childStr = childStr; 244 this.infoType = infoType; 245 } 246 247 public String getChildStr(){ 248 return childStr; 249 } 250 public InfoType getInfoType(){ 251 return infoType; 252 } 253 254 public String toString(){ 255 return "[Ref:"+childStr+", InfoType:"+infoType.toString()+"]"; } 257 } 258 259 public Map <File , List <InfoEntry>> getAbsURL2Info() { 260 return absURL2Info; 261 } 262 263 } 264 | Popular Tags |