1 19 20 package org.netbeans.modules.web.core; 21 22 import java.io.BufferedReader ; 23 import java.io.InputStreamReader ; 24 import java.io.IOException ; 25 import java.net.Socket ; 26 import java.net.URL ; 27 import java.net.InetAddress ; 28 import java.net.UnknownHostException ; 29 import java.util.ArrayList ; 30 import java.util.Collections ; 31 import java.util.HashMap ; 32 import java.util.HashSet ; 33 import java.util.List ; 34 import java.util.Map ; 35 import java.util.Set ; 36 import org.netbeans.api.java.project.JavaProjectConstants; 37 import org.netbeans.api.java.queries.UnitTestForSourceQuery; 38 import org.netbeans.api.project.Project; 39 import org.netbeans.api.project.ProjectUtils; 40 import org.netbeans.api.project.SourceGroup; 41 import org.openide.ErrorManager; 42 import org.openide.filesystems.FileObject; 43 import org.openide.filesystems.URLMapper; 44 import org.xml.sax.*; 46 48 52 public class Util { 53 54 56 57 public static boolean waitForURLConnection(URL url, int timeout, int retryTime) { 58 Connect connect = new Connect(url, retryTime); 59 Thread t = new Thread (connect); 60 t.start(); 61 try { 62 t.join(timeout); 63 } catch(InterruptedException ie) { 64 } 65 if (t.isAlive()) { 66 connect.finishLoop(); 67 t.interrupt(); } 69 return connect.getStatus(); 70 } 71 72 public static String issueGetRequest(URL url) { 73 BufferedReader in = null; 74 StringBuffer input = new StringBuffer (); 75 try { 76 in = new BufferedReader (new InputStreamReader ( 77 url.openStream())); 78 String inputLine; 79 while ((inputLine = in.readLine()) != null) { 80 input.append(inputLine); 81 input.append("\n"); } 83 return input.toString(); 84 } 85 catch (Exception e) { 86 return null; 88 } 89 finally { 90 if (in != null) 91 try { 92 in.close(); 93 } 94 catch(IOException e) { 95 } 97 } 98 } 99 100 private static class Connect implements Runnable { 101 102 URL url = null; 103 int retryTime; 104 boolean status = false; 105 boolean loop = true; 106 107 public Connect(URL url, int retryTime) { 108 this.url = url; 109 this.retryTime = retryTime; 110 } 111 112 public void finishLoop() { 113 loop = false; 114 } 115 116 public void run() { 117 try { 118 InetAddress.getByName(url.getHost()); 119 } catch (UnknownHostException e) { 120 return; 121 } 122 while (loop) { 123 try { 124 Socket socket = new Socket (url.getHost(), url.getPort()); 125 socket.close(); 126 status = true; 127 break; 128 } catch (UnknownHostException e) { } catch (IOException e) { } 131 try { 132 Thread.currentThread().sleep(retryTime); 133 } catch(InterruptedException ie) { 134 } 135 } 136 } 137 138 boolean getStatus() { 139 return status; 140 } 141 } 142 143 146 153 public static SourceGroup[] getJavaSourceGroups(Project project) { 154 SourceGroup[] sourceGroups = ProjectUtils.getSources(project).getSourceGroups( 155 JavaProjectConstants.SOURCES_TYPE_JAVA); 156 Set testGroups = getTestSourceGroups(project, sourceGroups); 157 List result = new ArrayList (); 158 for (int i = 0; i < sourceGroups.length; i++) { 159 if (!testGroups.contains(sourceGroups[i])) { 160 result.add(sourceGroups[i]); 161 } 162 } 163 return (SourceGroup[]) result.toArray(new SourceGroup[result.size()]); 164 } 165 166 private static Set getTestSourceGroups(Project project, SourceGroup[] sourceGroups) { 167 Map foldersToSourceGroupsMap = createFoldersToSourceGroupsMap(sourceGroups); 168 Set testGroups = new HashSet (); 169 for (int i = 0; i < sourceGroups.length; i++) { 170 testGroups.addAll(getTestTargets(sourceGroups[i], foldersToSourceGroupsMap)); 171 } 172 return testGroups; 173 } 174 175 private static Map createFoldersToSourceGroupsMap(final SourceGroup[] sourceGroups) { 176 Map result; 177 if (sourceGroups.length == 0) { 178 result = Collections.EMPTY_MAP; 179 } else { 180 result = new HashMap (2 * sourceGroups.length, .5f); 181 for (int i = 0; i < sourceGroups.length; i++) { 182 SourceGroup sourceGroup = sourceGroups[i]; 183 result.put(sourceGroup.getRootFolder(), sourceGroup); 184 } 185 } 186 return result; 187 } 188 189 private static List getFileObjects(URL [] urls) { 190 List result = new ArrayList (); 191 for (int i = 0; i < urls.length; i++) { 192 FileObject sourceRoot = URLMapper.findFileObject(urls[i]); 193 if (sourceRoot != null) { 194 result.add(sourceRoot); 195 } else { 196 int severity = ErrorManager.INFORMATIONAL; 197 if (ErrorManager.getDefault().isNotifiable(severity)) { 198 ErrorManager.getDefault().notify(severity, new IllegalStateException ( 199 "No FileObject found for the following URL: " + urls[i])); } 201 } 202 } 203 return result; 204 } 205 206 private static List getTestTargets(SourceGroup sourceGroup, Map foldersToSourceGroupsMap) { 207 final URL [] rootURLs = UnitTestForSourceQuery.findUnitTests(sourceGroup.getRootFolder()); 208 if (rootURLs.length == 0) { 209 return new ArrayList (); 210 } 211 List result = new ArrayList (); 212 List sourceRoots = getFileObjects(rootURLs); 213 for (int i = 0; i < sourceRoots.size(); i++) { 214 FileObject sourceRoot = (FileObject) sourceRoots.get(i); 215 SourceGroup srcGroup = (SourceGroup) foldersToSourceGroupsMap.get(sourceRoot); 216 if (srcGroup != null) { 217 result.add(srcGroup); 218 } 219 } 220 return result; 221 } 222 223 226 public static Set getTagValues (java.io.InputStream is, String elName, String tagName) throws java.io.IOException , SAXException { 227 return getTagValues(is,new String []{elName},tagName); 228 } 229 232 public static Set getTagValues (java.io.InputStream is, String [] elNames, String tagName) throws java.io.IOException , SAXException { 233 javax.xml.parsers.SAXParserFactory fact = javax.xml.parsers.SAXParserFactory.newInstance(); 234 fact.setValidating(false); 235 try { 236 javax.xml.parsers.SAXParser parser = fact.newSAXParser(); 237 XMLReader reader = parser.getXMLReader(); 238 TLDVersionHandler handler = new TLDVersionHandler(elNames,tagName); 239 reader.setContentHandler(handler); 240 try { 241 reader.parse(new InputSource(is)); 242 } catch (SAXException ex) { 243 String message = ex.getMessage(); 244 } 245 return handler.getValues(); 246 } catch(javax.xml.parsers.ParserConfigurationException ex) { 247 return new java.util.HashSet (); 248 } 249 } 250 251 private static class TLDVersionHandler extends org.xml.sax.helpers.DefaultHandler { 252 private String tagName; 253 private Set elNames; 254 private Set values; 255 private boolean insideEl, insideTag; 256 257 TLDVersionHandler(String [] elNames, String tagName) { 258 this.elNames=new java.util.HashSet (); 259 for (int i=0;i<elNames.length;i++) { 260 this.elNames.add(elNames[i]); 261 } 262 this.tagName=tagName; 263 values = new HashSet (); 264 } 265 public void startElement(String uri, String localName, String rawName, Attributes atts) throws SAXException { 266 if (elNames.contains(rawName)) insideEl=true; 267 else if (tagName.equals(rawName) && insideEl) { insideTag=true; 269 } 270 } 271 public void endElement(String uri, String localName, String rawName) throws SAXException { 272 if (elNames.contains(rawName)) insideEl=false; 273 else if (tagName.equals(rawName) && insideEl) { insideTag=false; 275 } 276 } 277 278 public void characters(char[] ch,int start,int length) throws SAXException { 279 if (insideTag) { 280 values.add(String.valueOf(ch,start,length).trim()); 281 } 282 } 283 public Set getValues() { 284 return values; 285 } 286 } 287 288 } 289 | Popular Tags |