1 19 20 package org.netbeans.modules.ant.freeform; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.text.Collator ; 25 import java.util.ArrayList ; 26 import java.util.Collections ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 import java.util.Set ; 30 import java.util.SortedSet ; 31 import java.util.TreeSet ; 32 import java.util.regex.Pattern ; 33 import javax.swing.event.ChangeListener ; 34 import org.apache.tools.ant.module.api.AntProjectCookie; 35 import org.apache.tools.ant.module.api.support.TargetLister; 36 import org.netbeans.api.project.Project; 37 import org.netbeans.modules.ant.freeform.spi.HelpIDFragmentProvider; 38 import org.openide.ErrorManager; 39 import org.openide.filesystems.FileObject; 40 import org.openide.filesystems.FileUtil; 41 import org.openide.loaders.DataObject; 42 import org.openide.loaders.DataObjectNotFoundException; 43 import org.openide.xml.XMLUtil; 44 import org.w3c.dom.Document ; 45 import org.w3c.dom.Element ; 46 import org.xml.sax.ErrorHandler ; 47 import org.xml.sax.InputSource ; 48 import org.xml.sax.SAXException ; 49 import org.xml.sax.SAXParseException ; 50 51 55 public class Util { 56 57 private Util() {} 58 59 public static final ErrorManager err = ErrorManager.getDefault().getInstance("org.netbeans.modules.ant.freeform"); 61 68 public static String getAntScriptName(FileObject fo) { 69 AntProjectCookie apc = getAntProjectCookie(fo); 70 if (apc == null) { 71 return null; 72 } 73 Element projEl = apc.getProjectElement(); 74 if (projEl == null) { 75 return null; 76 } 77 String name = projEl.getAttribute("name"); return name.length() > 0 ? name : null; 80 } 81 82 private static AntProjectCookie getAntProjectCookie(FileObject fo) { 83 DataObject dob; 84 try { 85 dob = DataObject.find(fo); 86 } catch (DataObjectNotFoundException ex) { 87 Util.err.notify(ErrorManager.INFORMATIONAL, ex); 88 return null; 89 } 90 assert dob != null; 91 AntProjectCookie apc = dob.getCookie(AntProjectCookie.class); 92 if (apc == null && fo.isData()) { 93 try { 98 apc = forceParse(fo); 99 } catch (IOException e) { 100 err.notify(ErrorManager.INFORMATIONAL, e); 101 } catch (SAXException e) { 102 err.log("Parse error in " + fo + ": " + e); 103 } 104 } 105 return apc; 106 } 107 108 private static final Pattern VALIDATION = Pattern.compile("([A-Za-z0-9])+"); 110 public static String getMergedHelpIDFragments(Project p) { 111 List <String > fragments = new ArrayList <String >(); 112 113 for (HelpIDFragmentProvider provider : p.getLookup().lookupAll(HelpIDFragmentProvider.class)) { 114 String fragment = provider.getHelpIDFragment(); 115 116 if (fragment == null || !VALIDATION.matcher(fragment).matches()) { 117 throw new IllegalStateException ("HelpIDFragmentProvider \"" + provider + "\" provided invalid help ID fragment \"" + fragment + "\"."); } 119 120 fragments.add(fragment); 121 } 122 123 Collections.sort(fragments); 124 125 StringBuffer result = new StringBuffer (); 126 127 for (Iterator <String > i = fragments.iterator(); i.hasNext(); ) { 128 result.append(i.next()); 129 130 if (i.hasNext()) { 131 result.append('.'); 132 } 133 } 134 135 return result.toString(); 136 } 137 138 145 public static List <String > getAntScriptTargetNames(FileObject fo) { 146 if (fo == null) { 147 throw new IllegalArgumentException ("Cannot call Util.getAntScriptTargetNames with null"); } 149 AntProjectCookie apc = getAntProjectCookie(fo); 150 if (apc == null) { 151 return null; 152 } 153 Set <TargetLister.Target> allTargets; 154 try { 155 allTargets = TargetLister.getTargets(apc); 156 } catch (IOException e) { 157 err.notify(ErrorManager.INFORMATIONAL, e); 158 return null; 159 } 160 SortedSet <String > targetNames = new TreeSet <String >(Collator.getInstance()); 161 for (TargetLister.Target target : allTargets) { 162 if (target.isOverridden()) { 163 continue; 165 } 166 if (target.isInternal()) { 167 continue; 169 } 170 targetNames.add(target.getName()); 171 } 172 return new ArrayList <String >(targetNames); 173 } 174 175 178 private static AntProjectCookie forceParse(FileObject fo) throws IOException , SAXException { 179 Document doc = XMLUtil.parse(new InputSource (fo.getURL().toExternalForm()), false, true, new ErrH(), null); 180 return new TrivialAntProjectCookie(fo, doc); 181 } 182 183 private static final class ErrH implements ErrorHandler { 184 public ErrH() {} 185 public void fatalError(SAXParseException exception) throws SAXException { 186 throw exception; 187 } 188 public void error(SAXParseException exception) throws SAXException { 189 throw exception; 190 } 191 public void warning(SAXParseException exception) throws SAXException { 192 } 194 } 195 196 private static final class TrivialAntProjectCookie implements AntProjectCookie.ParseStatus { 197 198 private final FileObject fo; 199 private final Document doc; 200 201 public TrivialAntProjectCookie(FileObject fo, Document doc) { 202 this.fo = fo; 203 this.doc = doc; 204 } 205 206 public FileObject getFileObject() { 207 return fo; 208 } 209 210 public File getFile() { 211 return FileUtil.toFile(fo); 212 } 213 214 public Document getDocument() { 215 return doc; 216 } 217 218 public Element getProjectElement() { 219 return doc.getDocumentElement(); 220 } 221 222 public boolean isParsed() { 223 return true; 224 } 225 226 public Throwable getParseException() { 227 return null; 228 } 229 230 public void addChangeListener(ChangeListener l) {} 231 232 public void removeChangeListener(ChangeListener l) {} 233 234 } 235 236 } 237 | Popular Tags |