1 11 12 package org.eclipse.ant.internal.ui.editor.utils; 13 14 import java.io.File ; 15 import java.io.FileNotFoundException ; 16 import java.io.FileReader ; 17 import java.io.IOException ; 18 import java.io.Reader ; 19 import java.io.StringReader ; 20 import java.io.UnsupportedEncodingException ; 21 import java.util.HashMap ; 22 import java.util.Map ; 23 24 import javax.xml.parsers.ParserConfigurationException ; 25 import javax.xml.parsers.SAXParser ; 26 import javax.xml.parsers.SAXParserFactory ; 27 28 import org.apache.tools.ant.BuildException; 29 import org.apache.tools.ant.Location; 30 import org.apache.tools.ant.Project; 31 import org.apache.tools.ant.RuntimeConfigurable; 32 import org.apache.tools.ant.Target; 33 import org.apache.tools.ant.Task; 34 import org.apache.tools.ant.UnknownElement; 35 import org.apache.tools.ant.helper.AntXMLContext; 36 import org.apache.tools.ant.helper.ProjectHelper2; 37 import org.apache.tools.ant.util.FileUtils; 38 import org.apache.tools.ant.util.JAXPUtils; 39 import org.eclipse.ant.internal.ui.model.IAntModel; 40 import org.eclipse.jface.text.BadLocationException; 41 import org.xml.sax.Attributes ; 42 import org.xml.sax.InputSource ; 43 import org.xml.sax.Locator ; 44 import org.xml.sax.SAXException ; 45 import org.xml.sax.SAXParseException ; 46 import org.xml.sax.XMLReader ; 47 import org.xml.sax.ext.LexicalHandler ; 48 49 54 public class ProjectHelper extends ProjectHelper2 { 55 56 59 private static FileUtils fu= null; 60 61 65 private File buildFile= null; 66 67 private static String currentEntityName= null; 68 private static String currentEntityPath= null; 69 70 private static int currentImportStackSize= 1; 71 72 75 private static IAntModel fgAntModel; 76 77 80 private static AntXMLContext fgAntContext; 81 82 private static AntHandler elementHandler= new ElementHandler(); 83 private static AntHandler projectHandler= new ProjectHandler(); 84 private static AntHandler targetHandler= new TargetHandler(); 85 private static AntHandler mainHandler= new MainHandler(); 86 private static LexicalHandler lexicalHandler= new LexHandler(); 87 88 private static XMLReader fgXMLReader= null; 89 90 public static class ElementHandler extends ProjectHelper2.ElementHandler { 91 92 private UnknownElement task= null; 93 private Task currentTask= null; 94 95 private Map fNormalizedFileNames= new HashMap (); 96 97 100 public AntHandler onStartChild(String uri, String tag, String qname, Attributes attrs, AntXMLContext context) { 101 return ProjectHelper.elementHandler; 102 } 103 106 public void onStartElement(String uri, String tag, String qname, Attributes attrs, AntXMLContext context) { 107 try { 108 RuntimeConfigurable wrapper= context.currentWrapper(); 109 currentTask= null; 110 task= null; 111 if (wrapper != null) { 112 currentTask= (Task)wrapper.getProxy(); 113 } 114 onStartElement0(uri, tag, qname, attrs, context); 115 116 Locator locator= context.getLocator(); 117 getAntModel().addTask(task, currentTask, attrs, locator.getLineNumber(), locator.getColumnNumber()); 118 119 } catch (BuildException be) { 120 Locator locator= context.getLocator(); 121 getAntModel().addTask(task, currentTask, attrs, locator.getLineNumber(), locator.getColumnNumber()); 122 getAntModel().error(be); 123 } 124 } 125 126 129 public void onEndElement(String uri, String tag, AntXMLContext context) { 130 super.onEndElement(uri, tag, context); 131 132 Locator locator= context.getLocator(); 133 if (getAntModel().canGetTaskInfo()) { 134 getAntModel().setCurrentElementLength(locator.getLineNumber(), locator.getColumnNumber()); 135 } 136 } 137 138 private void onStartElement0(String uri, String tag, String qname, Attributes attrs, AntXMLContext context) { 139 140 RuntimeConfigurable parentWrapper = context.currentWrapper(); 141 Object parent = null; 142 143 if (parentWrapper != null) { 144 parent = parentWrapper.getProxy(); 145 } 146 147 149 task = new UnknownElement(tag); 150 task.setProject(context.getProject()); 151 task.setNamespace(uri); 152 task.setQName(qname); 153 task.setTaskType(org.apache.tools.ant.ProjectHelper.genComponentName(task.getNamespace(), tag)); 154 task.setTaskName(qname); 155 156 Locator contextLocator= context.getLocator(); 157 String fileName= contextLocator.getSystemId(); 158 String normalizedFileName= (String ) fNormalizedFileNames.get(fileName); 159 if (normalizedFileName == null) { 160 if (fileName.startsWith("file:")) { normalizedFileName= getFileUtils().fromURI(fileName); 162 fNormalizedFileNames.put(fileName, normalizedFileName); 163 } else { 164 normalizedFileName= fileName; 165 } 166 } 167 168 Target currentTarget= context.getCurrentTarget(); 169 Location location = new Location(normalizedFileName, 170 contextLocator.getLineNumber(), 171 contextLocator.getColumnNumber()); 172 task.setLocation(location); 173 task.setOwningTarget(currentTarget); 174 175 context.configureId(task, attrs); 176 177 if (parent != null) { 178 ((UnknownElement) parent).addChild(task); 180 } else { 181 currentTarget.addTask(task); 183 } 184 185 188 RuntimeConfigurable wrapper 189 = new RuntimeConfigurable(task, task.getTaskName()); 190 191 for (int i = 0; i < attrs.getLength(); i++) { 192 String attrUri = attrs.getURI(i); 193 if (attrUri != null 194 && attrUri.length() != 0 195 && !attrUri.equals(uri)) { 196 continue; } 198 String name = attrs.getLocalName(i); 199 String value = attrs.getValue(i); 200 if (name.equals("ant-type")) { int index = value.indexOf(':'); 206 if (index != -1) { 207 String prefix = value.substring(0, index); 208 String mappedUri = context.getPrefixMapping(prefix); 209 if (mappedUri == null) { 210 throw new BuildException( 211 "Unable to find XML NS prefix " + prefix); } 213 value = org.apache.tools.ant.ProjectHelper.genComponentName(mappedUri, value.substring(index + 1)); 214 } 215 } 216 wrapper.setAttribute(name, value); 217 } 218 219 if (parentWrapper != null) { 220 parentWrapper.addChild(wrapper); 221 } 222 223 context.pushWrapper(wrapper); 224 } 225 226 229 public void characters(char[] buf, int start, int count, AntXMLContext context) { 230 try { 231 super.characters(buf, start, count, context); 232 } catch (SAXParseException e) { 233 ErrorHelper.handleErrorFromElementText(start, count, context, e); 234 } catch (BuildException be) { 235 ErrorHelper.handleErrorFromElementText(start, count, context, be); 236 } 237 } 238 239 public void reset() { 240 task= null; 241 currentTask= null; 242 fNormalizedFileNames.clear(); 243 } 244 } 245 246 public static class MainHandler extends ProjectHelper2.MainHandler { 247 248 251 public AntHandler onStartChild(String uri, String name, String qname, Attributes attrs, AntXMLContext context) throws SAXParseException { 252 if (name.equals("project") && (uri.length() == 0 || uri.equals(ANT_CORE_URI))) { 254 return ProjectHelper.projectHandler; 255 } 256 try { 257 return super.onStartChild(uri, name, qname, attrs, context); 258 } catch (SAXParseException e) { 259 getAntModel().error(e); 260 throw e; 261 } 262 } 263 } 264 265 public static class ProjectHandler extends ProjectHelper2.ProjectHandler { 266 267 270 public AntHandler onStartChild(String uri, String name, String qname, Attributes attrs, AntXMLContext context) { 271 if (name.equals("target") && (uri.length() == 0 || uri.equals(ANT_CORE_URI))) { 273 return ProjectHelper.targetHandler; 274 } 275 return ProjectHelper.elementHandler; 276 } 277 280 public void onEndElement(String uri, String tag, AntXMLContext context) { 281 super.onEndElement(uri, tag, context); 282 if (currentImportStackSize == 1) { 283 Locator locator= context.getLocator(); 284 getAntModel().setCurrentElementLength(locator.getLineNumber(), locator.getColumnNumber()); 285 } 286 } 287 290 public void onStartElement(String uri, String tag, String qname, Attributes attrs, AntXMLContext context) { 291 try { 292 super.onStartElement(uri, tag, qname, attrs, context); 293 } catch (SAXParseException e) { 294 getAntModel().error(e); 295 } catch (BuildException be) { 296 getAntModel().error(be); 297 } 298 if (context.getCurrentTarget() == null) { 299 context.getProject().addTarget("", context.getImplicitTarget()); context.setCurrentTarget(context.getImplicitTarget()); 302 } 303 if (currentImportStackSize == 1) { 304 Locator locator= context.getLocator(); 305 getAntModel().addProject(context.getProject(), locator.getLineNumber(), locator.getColumnNumber()); 306 } 307 } 308 309 312 public void characters(char[] buf, int start, int count, AntXMLContext context) { 313 try { 314 super.characters(buf, start, count, context); 315 } catch (SAXParseException e) { 316 ErrorHelper.handleErrorFromElementText(start, count, context, e); 317 } catch (BuildException be) { 318 ErrorHelper.handleErrorFromElementText(start, count, context, be); 319 } 320 } 321 } 322 323 public static class TargetHandler extends ProjectHelper2.TargetHandler { 324 327 public AntHandler onStartChild(String uri, String name, String qname, Attributes attrs, AntXMLContext context) { 328 return ProjectHelper.elementHandler; 329 } 330 333 public void onStartElement(String uri, String tag, String qname, Attributes attrs, AntXMLContext context) { 334 try { 335 super.onStartElement(uri, tag, qname, attrs, context); 336 Target newTarget= context.getCurrentTarget(); 337 Locator locator= context.getLocator(); 338 getAntModel().addTarget(newTarget, locator.getLineNumber(), locator.getColumnNumber()); 339 } catch (SAXParseException e) { 340 handleErrorInTarget(context, e); 341 } catch (BuildException be) { 342 handleErrorInTarget(context, be); 343 } 344 } 345 346 private void handleErrorInTarget(AntXMLContext context, Exception e) { 347 Target newTarget= context.getCurrentTarget(); 348 Locator locator= context.getLocator(); 349 getAntModel().addTarget(newTarget, locator.getLineNumber(), locator.getColumnNumber()); 350 getAntModel().errorFromElement(e, null, locator.getLineNumber(), locator.getColumnNumber()); 351 } 352 353 356 public void onEndElement(String uri, String tag, AntXMLContext context) { 357 super.onEndElement(uri, tag, context); 358 Locator locator= context.getLocator(); 359 getAntModel().setCurrentElementLength(locator.getLineNumber(), locator.getColumnNumber()); 360 } 361 362 365 public void characters(char[] buf, int start, int count, AntXMLContext context) { 366 try { 367 super.characters(buf, start, count, context); 368 } catch (SAXParseException e) { 369 ErrorHelper.handleErrorFromElementText(start, count, context, e); 370 } catch (BuildException be) { 371 ErrorHelper.handleErrorFromElementText(start, count, context, be); 372 } 373 } 374 } 375 376 public static class RootHandler extends ProjectHelper2.RootHandler { 377 378 public RootHandler(AntXMLContext context, AntHandler rootHandler) { 379 super(context, rootHandler); 380 } 381 382 385 public void error(SAXParseException e) { 386 getAntModel().error(e); 387 } 388 391 public void fatalError(SAXParseException e) { 392 getAntModel().fatalError(e); 393 } 394 397 public void warning(SAXParseException e) { 398 getAntModel().warning(e); 399 } 400 403 public InputSource resolveEntity(String publicId, String systemId) { 404 InputSource source= super.resolveEntity(publicId, systemId); 405 if (source != null) { 406 String path = getFileUtils().fromURI(source.getSystemId()); 407 if (currentEntityName == null) { 408 currentEntityPath= path; 409 } else { 410 getAntModel().addEntity(currentEntityName, path); 411 currentEntityName= null; 412 } 413 } 414 return source; 415 } 416 417 420 public void startPrefixMapping(String prefix, String uri) { 421 422 super.startPrefixMapping(prefix, uri); 423 getAntModel().addPrefixMapping(prefix, uri); 424 } 425 } 426 427 private static class ErrorHelper { 428 public static void handleErrorFromElementText(int start, int count, AntXMLContext context, Exception e) { 429 Locator locator= context.getLocator(); 430 int columnNumber= locator.getColumnNumber(); 431 if (columnNumber > -1) { 432 int offset= start; 433 try { 434 offset= getAntModel().getOffset(locator.getLineNumber(), 1); 435 } catch (BadLocationException e1) { 436 } 437 getAntModel().errorFromElementText(e, offset, locator.getColumnNumber()); 438 } else { 439 getAntModel().errorFromElementText(e, start, count); 440 } 441 } 442 } 443 444 private static class LexHandler implements LexicalHandler { 445 448 public void endCDATA() throws SAXException { 449 } 450 451 454 public void endDTD() throws SAXException { 455 if (getAntModel().canGetLexicalInfo()) { 456 AntXMLContext context= getContext(); 457 Locator locator= context.getLocator(); 458 getAntModel().setCurrentElementLength(locator.getLineNumber(), locator.getColumnNumber()); 459 } 460 } 461 462 465 public void startCDATA() throws SAXException { 466 } 467 468 471 public void comment(char[] ch, int start, int length) throws SAXException { 472 if (getAntModel().canGetLexicalInfo()) { 473 AntXMLContext context= getContext(); 474 Locator locator= context.getLocator(); 475 if (locator != null) { 476 getAntModel().addComment(locator.getLineNumber(), locator.getColumnNumber(), length); 477 } 478 } 479 } 480 481 484 public void endEntity(String name) throws SAXException { 485 } 486 487 490 public void startEntity(String name) throws SAXException { 491 if (currentEntityPath == null) { 492 currentEntityName= name; 493 } else { 494 getAntModel().addEntity(name, currentEntityPath); 495 currentEntityPath= null; 496 } 497 } 498 499 502 public void startDTD(String name, String publicId, String systemId) throws SAXException { 503 if (getAntModel().canGetLexicalInfo()) { 504 AntXMLContext context= getContext(); 505 Locator locator= context.getLocator(); 506 getAntModel().addDTD(name, locator.getLineNumber(), locator.getColumnNumber()); 507 } 508 } 509 } 510 511 public ProjectHelper(IAntModel model) { 512 setAntModel(model); 513 } 514 515 524 public void parse(Project project, Object source, org.apache.tools.ant.helper.ProjectHelper2.RootHandler handler) throws BuildException { 525 526 if (!(source instanceof String ) && !(source instanceof File )) { 527 super.parse(project, source, handler); 530 return; 531 } 532 533 AntXMLContext context= (AntXMLContext)project.getReference("ant.parsing.context"); handler= new RootHandler(context, mainHandler); 536 Reader stream= null; 537 try { 538 InputSource inputSource= null; 539 if ((source instanceof File )) { 540 buildFile = (File ) source; 541 buildFile = getFileUtils().normalize(buildFile.getAbsolutePath()); 542 stream = new FileReader (buildFile); 543 inputSource = new InputSource (stream); 544 } else if (source instanceof String ) { 545 stream= new StringReader ((String )source); 546 inputSource = new InputSource (stream); 547 } 548 549 552 XMLReader parser= getNamespaceXMLReader(); 557 if (parser == null) { 558 throw new BuildException(ProjectHelperMessages.ProjectHelper_0); 559 } 560 String uri = null; 561 if (buildFile != null) { 562 uri = getFileUtils().toURI(buildFile.getAbsolutePath()); 563 } 564 565 if (uri != null) { 566 inputSource.setSystemId(uri); 567 } 568 569 context.setBuildFile(buildFile); 570 571 parser.setContentHandler(handler); 572 parser.setEntityResolver(handler); 573 parser.setErrorHandler(handler); 574 parser.setDTDHandler(handler); 575 parser.setProperty("http://xml.org/sax/properties/lexical-handler", lexicalHandler); 577 parser.parse(inputSource); 578 } catch (SAXParseException exc) { 579 getAntModel().fatalError(exc); 580 } catch (SAXException exc) { 581 } catch (FileNotFoundException exc) { 583 throw new BuildException(exc); 584 } catch (UnsupportedEncodingException exc) { 585 throw new BuildException(exc); 586 } catch (IOException exc) { 587 throw new BuildException(exc); 588 } finally { 589 try { 590 if (stream != null) { 591 stream.close(); 592 } 593 } catch (IOException ioe) { 594 } 596 } 597 } 598 599 605 public void setBuildFile(File file) { 606 buildFile= file; 607 currentImportStackSize= 1; 608 } 609 610 617 public void parse(Project project, Object source) throws BuildException { 618 AntXMLContext context = (AntXMLContext) project.getReference("ant.parsing.context"); if (context == null) { 620 getImportStack().removeAllElements(); 621 context = new AntXMLContext(project); 622 project.addReference("ant.parsing.context", context); project.addReference("ant.targets", context.getTargets()); fgAntContext= context; 625 } 626 getImportStack().addElement(source); 627 currentImportStackSize= getImportStack().size(); 628 if (getImportStack().size() > 1) { 629 context.setIgnoreProjectTag(true); 631 Target currentTarget = context.getCurrentTarget(); 632 Target currentImplicit = context.getImplicitTarget(); 633 Map currentTargets = context.getCurrentTargets(); 634 635 try { 636 Target newCurrent = new Target(); 637 newCurrent.setProject(project); 638 newCurrent.setName(""); context.setCurrentTarget(newCurrent); 640 context.setCurrentTargets(new HashMap ()); 641 context.setImplicitTarget(newCurrent); 642 parse(project, source, new RootHandler(context, mainHandler)); 643 } finally { 644 context.setCurrentTarget(currentTarget); 645 context.setImplicitTarget(currentImplicit); 646 context.setCurrentTargets(currentTargets); 647 } 648 } else { 649 context.setCurrentTargets(new HashMap ()); 651 parse(project, source, new RootHandler(context, mainHandler)); 652 } 653 } 654 655 public static void reset() { 656 fgXMLReader= null; 657 fu= null; 658 } 659 660 public static void setAntModel(IAntModel antModel) { 661 fgAntModel= antModel; 662 ((ProjectHelper.ElementHandler)elementHandler).reset(); 663 fu= null; 664 fgAntContext= null; 665 } 666 667 public static IAntModel getAntModel() { 668 return fgAntModel; 669 } 670 671 public static AntXMLContext getContext() { 672 return fgAntContext; 673 } 674 675 private static FileUtils getFileUtils() { 676 if (fu == null) { 677 fu= FileUtils.getFileUtils(); 678 } 679 return fu; 680 } 681 682 688 private XMLReader getNamespaceXMLReader() throws BuildException { 689 if (fgXMLReader == null) { 690 try { 691 fgXMLReader= newSAXParser(getNSParserFactory()).getXMLReader(); 692 } catch (SAXException e) { 693 } 694 } 695 return fgXMLReader; 696 } 697 698 706 private SAXParserFactory getNSParserFactory() throws BuildException { 707 708 SAXParserFactory nsParserFactory = JAXPUtils.newParserFactory(); 709 nsParserFactory.setNamespaceAware(true); 710 711 return nsParserFactory; 712 } 713 714 720 private SAXParser newSAXParser(SAXParserFactory factory) { 721 try { 722 return factory.newSAXParser(); 723 } catch (ParserConfigurationException e) { 724 } catch (SAXException e) { 725 } 726 return null; 727 } 728 } 729 | Popular Tags |