1 19 20 package org.netbeans.modules.ant.freeform.spi.support; 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.Iterator ; 27 import java.util.List ; 28 import javax.xml.XMLConstants ; 29 import javax.xml.transform.dom.DOMSource ; 30 import javax.xml.validation.Schema ; 31 import javax.xml.validation.SchemaFactory ; 32 import javax.xml.validation.Validator ; 33 import org.netbeans.api.project.Project; 34 import org.netbeans.api.project.ProjectManager; 35 import org.netbeans.api.queries.CollocationQuery; 36 import org.netbeans.modules.ant.freeform.FreeformProject; 37 import org.netbeans.modules.ant.freeform.FreeformProjectGenerator; 38 import org.netbeans.modules.ant.freeform.FreeformProjectType; 39 import org.netbeans.modules.ant.freeform.spi.ProjectAccessor; 40 import org.netbeans.modules.ant.freeform.spi.ProjectConstants; 41 import org.netbeans.spi.project.AuxiliaryConfiguration; 42 import org.netbeans.spi.project.support.ant.AntProjectHelper; 43 import org.netbeans.spi.project.support.ant.PropertyEvaluator; 44 import org.netbeans.spi.project.support.ant.PropertyUtils; 45 import org.openide.ErrorManager; 46 import org.openide.filesystems.FileObject; 47 import org.openide.filesystems.FileUtil; 48 import org.openide.util.Mutex; 49 import org.w3c.dom.DOMException ; 50 import org.w3c.dom.Document ; 51 import org.w3c.dom.Element ; 52 import org.w3c.dom.NamedNodeMap ; 53 import org.w3c.dom.Node ; 54 import org.w3c.dom.NodeList ; 55 import org.w3c.dom.Text ; 56 import org.w3c.dom.ls.DOMImplementationLS ; 57 import org.w3c.dom.ls.LSSerializer ; 58 import org.xml.sax.ErrorHandler ; 59 import org.xml.sax.SAXException ; 60 import org.xml.sax.SAXParseException ; 61 62 66 public class Util { 67 68 private Util() {} 69 70 72 82 public static Element findElement(Element parent, String name, String namespace) { 83 Element result = null; 84 NodeList l = parent.getChildNodes(); 85 for (int i = 0; i < l.getLength(); i++) { 86 if (l.item(i).getNodeType() == Node.ELEMENT_NODE) { 87 Element el = (Element )l.item(i); 88 if (name.equals(el.getLocalName()) && namespace.equals(el.getNamespaceURI())) { 89 if (result == null) { 90 result = el; 91 } else { 92 return null; 93 } 94 } 95 } 96 } 97 return result; 98 } 99 100 106 public static String findText(Element parent) { 107 NodeList l = parent.getChildNodes(); 108 for (int i = 0; i < l.getLength(); i++) { 109 if (l.item(i).getNodeType() == Node.TEXT_NODE) { 110 Text text = (Text )l.item(i); 111 return text.getNodeValue(); 112 } 113 } 114 return null; 115 } 116 117 127 public static List <Element > findSubElements(Element parent) throws IllegalArgumentException { 128 NodeList l = parent.getChildNodes(); 129 List <Element > elements = new ArrayList <Element >(l.getLength()); 130 for (int i = 0; i < l.getLength(); i++) { 131 Node n = l.item(i); 132 if (n.getNodeType() == Node.ELEMENT_NODE) { 133 elements.add((Element )n); 134 } else if (n.getNodeType() == Node.TEXT_NODE) { 135 String text = ((Text )n).getNodeValue(); 136 if (text.trim().length() > 0) { 137 throw new IllegalArgumentException ("non-ws text encountered in " + parent + ": " + text); } 139 } else if (n.getNodeType() == Node.COMMENT_NODE) { 140 } else { 142 throw new IllegalArgumentException ("unexpected non-element child of " + parent + ": " + n); } 144 } 145 return elements; 146 } 147 148 156 public static AuxiliaryConfiguration getAuxiliaryConfiguration(AntProjectHelper helper) { 157 try { 158 Project p = ProjectManager.getDefault().findProject(helper.getProjectDirectory()); 159 AuxiliaryConfiguration aux = p.getLookup().lookup(AuxiliaryConfiguration.class); 160 assert aux != null; 161 return aux; 162 } catch (IOException e) { 163 ErrorManager.getDefault().notify(e); 164 return null; 165 } 166 } 167 168 177 public static String relativizeLocation(File projectBase, File freeformBase, File location) { 178 if (CollocationQuery.areCollocated(projectBase, location)) { 179 if (projectBase.equals(freeformBase)) { 180 return PropertyUtils.relativizeFile(projectBase, location); 181 } else if (projectBase.equals(location) && ProjectConstants.PROJECT_LOCATION_PREFIX.endsWith("/")) { return ProjectConstants.PROJECT_LOCATION_PREFIX.substring(0, ProjectConstants.PROJECT_LOCATION_PREFIX.length() - 1); 183 } else { 184 return ProjectConstants.PROJECT_LOCATION_PREFIX + PropertyUtils.relativizeFile(projectBase, location); 185 } 186 } else { 187 return location.getAbsolutePath(); 188 } 189 } 190 191 199 public static File resolveFile(PropertyEvaluator evaluator, File freeformProjectBase, String val) { 200 String location = evaluator.evaluate(val); 201 if (location == null) { 202 return null; 203 } 204 return PropertyUtils.resolveFile(freeformProjectBase, location); 205 } 206 207 214 public static File getProjectLocation(AntProjectHelper helper, PropertyEvaluator evaluator) { 215 String loc = evaluator.getProperty(ProjectConstants.PROP_PROJECT_LOCATION); 217 if (loc != null) { 218 return helper.resolveFile(loc); 219 } else { 220 return FileUtil.toFile(helper.getProjectDirectory()); 221 } 222 } 223 224 231 public static void appendChildElement(Element parent, Element el, String [] order) { 232 Element insertBefore = null; 233 List l = Arrays.asList(order); 234 int index = l.indexOf(el.getLocalName()); 235 assert index != -1 : el.getLocalName()+" was not found in "+l; Iterator it = Util.findSubElements(parent).iterator(); 237 while (it.hasNext()) { 238 Element e = (Element )it.next(); 239 int index2 = l.indexOf(e.getLocalName()); 240 assert index2 != -1 : e.getLocalName()+" was not found in "+l; if (index2 > index) { 242 insertBefore = e; 243 break; 244 } 245 } 246 parent.insertBefore(el, insertBefore); 247 } 248 249 259 public static FileObject getDefaultAntScript(Project prj) throws IllegalArgumentException { 260 ProjectAccessor accessor = prj.getLookup().lookup(ProjectAccessor.class); 261 262 if (accessor == null) { 263 throw new IllegalArgumentException ("Only FreeformProjects are supported."); 264 } 265 266 return FreeformProjectGenerator.getAntScript(accessor.getHelper(), accessor.getEvaluator()); 267 } 268 269 272 private static Element translateXML(Element from, String namespace) { 273 Element to = from.getOwnerDocument().createElementNS(namespace, from.getLocalName()); 274 NodeList nl = from.getChildNodes(); 275 int length = nl.getLength(); 276 for (int i = 0; i < length; i++) { 277 Node node = nl.item(i); 278 Node newNode; 279 if (node.getNodeType() == Node.ELEMENT_NODE) { 280 newNode = translateXML((Element ) node, namespace); 281 } else { 282 newNode = node.cloneNode(true); 283 } 284 to.appendChild(newNode); 285 } 286 NamedNodeMap m = from.getAttributes(); 287 for (int i = 0; i < m.getLength(); i++) { 288 Node attr = m.item(i); 289 to.setAttribute(attr.getNodeName(), attr.getNodeValue()); 290 } 291 return to; 292 } 293 294 298 public static final String NAMESPACE = "http://www.netbeans.org/ns/freeform-project/2"; 300 307 public static Element getPrimaryConfigurationData(final AntProjectHelper helper) { 308 return ProjectManager.mutex().readAccess(new Mutex.Action<Element >() { 309 public Element run() { 310 AuxiliaryConfiguration ac = helper.createAuxiliaryConfiguration(); 311 Element data = ac.getConfigurationFragment(FreeformProjectType.NAME_SHARED, NAMESPACE, true); 312 if (data != null) { 313 return data; 314 } else { 315 return translateXML(helper.getPrimaryConfigurationData(true), NAMESPACE); 316 } 317 } 318 }); 319 } 320 321 330 public static void putPrimaryConfigurationData(final AntProjectHelper helper, final Element data) { 331 if (!data.getNamespaceURI().equals(FreeformProjectType.NS_GENERAL)) { 332 throw new IllegalArgumentException ("Bad namespace"); } 334 ProjectManager.mutex().writeAccess(new Mutex.Action<Void >() { 335 public Void run() { 336 351 if (data.getElementsByTagName("includes").getLength() > 0 || data.getElementsByTagName("excludes").getLength() > 0) { 353 putPrimaryConfigurationDataAs2(helper, data); 354 } else { 355 Element dataAs1 = translateXML(data, FreeformProjectType.NS_GENERAL_1); 356 putPrimaryConfigurationDataAs1(helper, dataAs1); 357 } 358 return null; 359 } 360 }); 361 } 362 private static void putPrimaryConfigurationDataAs1(AntProjectHelper helper, Element data) { 363 helper.createAuxiliaryConfiguration().removeConfigurationFragment(FreeformProjectType.NAME_SHARED, NAMESPACE, true); 364 helper.putPrimaryConfigurationData(data, true); 365 } 366 private static void putPrimaryConfigurationDataAs2(AntProjectHelper helper, Element data) { 367 Document doc = data.getOwnerDocument(); 368 Element dummy1 = doc.createElementNS(FreeformProjectType.NS_GENERAL_1, FreeformProjectType.NAME_SHARED); 369 dummy1.appendChild(doc.createElementNS(FreeformProjectType.NS_GENERAL_1, "name")). appendChild(doc.createTextNode(findText(findElement(data, "name", NAMESPACE)))); helper.putPrimaryConfigurationData(dummy1, true); 373 helper.createAuxiliaryConfiguration().putConfigurationFragment(data, true); 374 } 375 private static final Schema SCHEMA_1, SCHEMA_2; 376 static { 377 try { 378 SchemaFactory f = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); 379 SCHEMA_1 = f.newSchema(FreeformProject.class.getResource("resources/freeform-project-general.xsd")); SCHEMA_2 = f.newSchema(FreeformProject.class.getResource("resources/freeform-project-general-2.xsd")); } catch (SAXException e) { 382 throw new ExceptionInInitializerError (e); 383 } 384 } 385 private static void validate(Element data, Schema schema) throws SAXException { 386 Validator v = schema.newValidator(); 387 final SAXException [] error = {null}; 388 v.setErrorHandler(new ErrorHandler () { 389 public void warning(SAXParseException x) throws SAXException {} 390 public void error(SAXParseException x) throws SAXException { 391 error[0] = x; 393 } 394 public void fatalError(SAXParseException x) throws SAXException { 395 error[0] = x; 396 } 397 }); 398 try { 399 v.validate(new DOMSource (data)); 400 } catch (IOException x) { 401 assert false : x; 402 } 403 if (error[0] != null) { 404 throw error[0]; 405 } 406 } 407 private static String format(Element data) { 408 LSSerializer ser = ((DOMImplementationLS ) data.getOwnerDocument().getImplementation().getFeature("LS", "3.0")).createLSSerializer(); 409 try { 410 ser.getDomConfig().setParameter("format-pretty-print", true); 411 ser.getDomConfig().setParameter("xml-declaration", false); 412 } catch (DOMException ignore) {} 413 return ser.writeToString(data); 414 } 415 416 } 417 | Popular Tags |