1 19 20 package org.netbeans.modules.ruby.rubyproject; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.util.ArrayList ; 25 import java.util.Arrays ; 26 import java.util.HashSet ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 import java.util.Set ; 30 import org.netbeans.api.project.Project; 31 import org.netbeans.api.project.ProjectManager; 32 import org.netbeans.api.project.ProjectUtils; 33 import org.netbeans.api.queries.CollocationQuery; 34 import org.netbeans.spi.project.AuxiliaryConfiguration; 35 import org.netbeans.modules.ruby.spi.project.support.rake.RakeProjectHelper; 36 import org.netbeans.modules.ruby.spi.project.support.rake.PropertyEvaluator; 37 import org.netbeans.modules.ruby.spi.project.support.rake.PropertyUtils; 38 import org.openide.ErrorManager; 39 import org.openide.filesystems.FileObject; 40 import org.openide.filesystems.FileUtil; 41 import org.w3c.dom.Element ; 42 import org.w3c.dom.Node ; 43 import org.w3c.dom.NodeList ; 44 import org.w3c.dom.Text ; 45 46 51 public class Util { 52 53 private Util() {} 54 55 57 67 public static Element findElement(Element parent, String name, String namespace) { 68 Element result = null; 69 NodeList l = parent.getChildNodes(); 70 for (int i = 0; i < l.getLength(); i++) { 71 if (l.item(i).getNodeType() == Node.ELEMENT_NODE) { 72 Element el = (Element )l.item(i); 73 if (name.equals(el.getLocalName()) && namespace.equals(el.getNamespaceURI())) { 74 if (result == null) { 75 result = el; } else { 77 return null; 78 } 79 } 80 } 81 } 82 return result; 83 } 84 85 91 public static String findText(Element parent) { 92 NodeList l = parent.getChildNodes(); 93 for (int i = 0; i < l.getLength(); i++) { 94 if (l.item(i).getNodeType() == Node.TEXT_NODE) { 95 Text text = (Text )l.item(i); 96 return text.getNodeValue(); 97 } 98 } 99 return null; 100 } 101 102 112 public static List <Element > findSubElements(Element parent) throws IllegalArgumentException { 113 NodeList l = parent.getChildNodes(); 114 List <Element > elements = new ArrayList <Element >(l.getLength()); 115 for (int i = 0; i < l.getLength(); i++) { 116 Node n = l.item(i); 117 if (n.getNodeType() == Node.ELEMENT_NODE) { 118 elements.add((Element )n); 119 } else if (n.getNodeType() == Node.TEXT_NODE) { 120 String text = ((Text )n).getNodeValue(); 121 if (text.trim().length() > 0) { 122 throw new IllegalArgumentException ("non-ws text encountered in " + parent + ": " + text); } 124 } else if (n.getNodeType() == Node.COMMENT_NODE) { 125 } else { 127 throw new IllegalArgumentException ("unexpected non-element child of " + parent + ": " + n); } 129 } 130 return elements; 131 } 132 133 141 public static AuxiliaryConfiguration getAuxiliaryConfiguration(RakeProjectHelper helper) { 142 try { 143 Project p = ProjectManager.getDefault().findProject(helper.getProjectDirectory()); 144 AuxiliaryConfiguration aux = p.getLookup().lookup(AuxiliaryConfiguration.class); 145 assert aux != null; 146 return aux; 147 } catch (IOException e) { 148 ErrorManager.getDefault().notify(e); 149 return null; 150 } 151 } 152 153 176 184 public static File resolveFile(PropertyEvaluator evaluator, File freeformProjectBase, String val) { 185 String location = evaluator.evaluate(val); 186 if (location == null) { 187 return null; 188 } 189 return PropertyUtils.resolveFile(freeformProjectBase, location); 190 } 191 192 209 216 public static void appendChildElement(Element parent, Element el, String [] order) { 217 Element insertBefore = null; 218 List l = Arrays.asList(order); 219 int index = l.indexOf(el.getLocalName()); 220 assert index != -1 : el.getLocalName()+" was not found in "+l; Iterator it = Util.findSubElements(parent).iterator(); 222 while (it.hasNext()) { 223 Element e = (Element )it.next(); 224 int index2 = l.indexOf(e.getLocalName()); 225 assert index2 != -1 : e.getLocalName()+" was not found in "+l; if (index2 > index) { 227 insertBefore = e; 228 break; 229 } 230 } 231 parent.insertBefore(el, insertBefore); 232 } 233 234 254 } 255 | Popular Tags |