KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > ui > editor > utils > ProjectHelper


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - derived implementation
10  *******************************************************************************/

11
12 package org.eclipse.ant.internal.ui.editor.utils;
13   
14 import java.io.File JavaDoc;
15 import java.io.FileNotFoundException JavaDoc;
16 import java.io.FileReader JavaDoc;
17 import java.io.IOException JavaDoc;
18 import java.io.Reader JavaDoc;
19 import java.io.StringReader JavaDoc;
20 import java.io.UnsupportedEncodingException JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import javax.xml.parsers.ParserConfigurationException JavaDoc;
25 import javax.xml.parsers.SAXParser JavaDoc;
26 import javax.xml.parsers.SAXParserFactory JavaDoc;
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 JavaDoc;
42 import org.xml.sax.InputSource JavaDoc;
43 import org.xml.sax.Locator JavaDoc;
44 import org.xml.sax.SAXException JavaDoc;
45 import org.xml.sax.SAXParseException JavaDoc;
46 import org.xml.sax.XMLReader JavaDoc;
47 import org.xml.sax.ext.LexicalHandler JavaDoc;
48
49 /**
50  * Derived from the original Ant ProjectHelper2 with help from the JAXPUtils class.
51  * This class provides parsing for using a String as a source and provides
52  * handlers that will continue parsing to completion upon hitting errors.
53  */

54 public class ProjectHelper extends ProjectHelper2 {
55
56     /**
57      * helper for path -> URI and URI -> path conversions.
58      */

59     private static FileUtils fu= null;
60     
61     /**
62      * The buildfile that is to be parsed. Must be set if parsing is to
63      * be successful.
64      */

65     private File JavaDoc buildFile= null;
66     
67     private static String JavaDoc currentEntityName= null;
68     private static String JavaDoc currentEntityPath= null;
69     
70     private static int currentImportStackSize= 1;
71     
72     /**
73      * The Ant Model
74      */

75     private static IAntModel fgAntModel;
76
77     /**
78      * The current Ant parsing context
79      */

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 JavaDoc lexicalHandler= new LexHandler();
87     
88     private static XMLReader JavaDoc 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 JavaDoc fNormalizedFileNames= new HashMap JavaDoc();
96         
97         /* (non-Javadoc)
98          * @see org.apache.tools.ant.helper.ProjectHelper2.AntHandler#onStartChild(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes, org.apache.tools.ant.helper.AntXMLContext)
99          */

100         public AntHandler onStartChild(String JavaDoc uri, String JavaDoc tag, String JavaDoc qname, Attributes JavaDoc attrs, AntXMLContext context) {
101             return ProjectHelper.elementHandler;
102         }
103         /* (non-Javadoc)
104          * @see org.apache.tools.ant.helper.ProjectHelper2.AntHandler#onStartElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes, org.apache.tools.ant.helper.AntXMLContext)
105          */

106         public void onStartElement(String JavaDoc uri, String JavaDoc tag, String JavaDoc qname, Attributes JavaDoc 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 JavaDoc locator= context.getLocator();
117                 getAntModel().addTask(task, currentTask, attrs, locator.getLineNumber(), locator.getColumnNumber());
118                 
119             } catch (BuildException be) {
120                 Locator JavaDoc locator= context.getLocator();
121                 getAntModel().addTask(task, currentTask, attrs, locator.getLineNumber(), locator.getColumnNumber());
122                 getAntModel().error(be);
123             }
124         }
125
126         /* (non-Javadoc)
127          * @see org.apache.tools.ant.helper.ProjectHelper2.AntHandler#onEndElement(java.lang.String, java.lang.String, org.apache.tools.ant.helper.AntXMLContext)
128          */

129         public void onEndElement(String JavaDoc uri, String JavaDoc tag, AntXMLContext context) {
130             super.onEndElement(uri, tag, context);
131             
132             Locator JavaDoc locator= context.getLocator();
133             if (getAntModel().canGetTaskInfo()) {
134                 getAntModel().setCurrentElementLength(locator.getLineNumber(), locator.getColumnNumber());
135             }
136         }
137         
138         private void onStartElement0(String JavaDoc uri, String JavaDoc tag, String JavaDoc qname, Attributes JavaDoc attrs, AntXMLContext context) {
139             
140             RuntimeConfigurable parentWrapper = context.currentWrapper();
141             Object JavaDoc parent = null;
142
143             if (parentWrapper != null) {
144                 parent = parentWrapper.getProxy();
145             }
146
147             /* UnknownElement is used for tasks and data types - with
148                delayed eval */

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 JavaDoc contextLocator= context.getLocator();
157             String JavaDoc fileName= contextLocator.getSystemId();
158             String JavaDoc normalizedFileName= (String JavaDoc) fNormalizedFileNames.get(fileName);
159             if (normalizedFileName == null) {
160                 if (fileName.startsWith("file:")) { //$NON-NLS-1$
161
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                 // Nested element
179
((UnknownElement) parent).addChild(task);
180             } else {
181                 // Task included in a target ( including the default one ).
182
currentTarget.addTask(task);
183             }
184
185             // container.addTask(task);
186
// This is a nop in UE: task.init();
187

188             RuntimeConfigurable wrapper
189                 = new RuntimeConfigurable(task, task.getTaskName());
190
191             for (int i = 0; i < attrs.getLength(); i++) {
192                 String JavaDoc attrUri = attrs.getURI(i);
193                 if (attrUri != null
194                     && attrUri.length() != 0
195                     && !attrUri.equals(uri)) {
196                     continue; // Ignore attributes from unknown uris
197
}
198                 String JavaDoc name = attrs.getLocalName(i);
199                 String JavaDoc value = attrs.getValue(i);
200                 // PR: Hack for ant-type value
201
// an ant-type is a component name which can
202
// be namespaced, need to extract the name
203
// and convert from qualified name to uri/name
204
if (name.equals("ant-type")) { //$NON-NLS-1$
205
int index = value.indexOf(':');
206                     if (index != -1) {
207                         String JavaDoc prefix = value.substring(0, index);
208                         String JavaDoc mappedUri = context.getPrefixMapping(prefix);
209                         if (mappedUri == null) {
210                             throw new BuildException(
211                                 "Unable to find XML NS prefix " + prefix); //$NON-NLS-1$
212
}
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         /* (non-Javadoc)
227          * @see org.apache.tools.ant.helper.ProjectHelper2.AntHandler#characters(char[], int, int, org.apache.tools.ant.helper.AntXMLContext)
228          */

229         public void characters(char[] buf, int start, int count, AntXMLContext context) {
230             try {
231                 super.characters(buf, start, count, context);
232             } catch (SAXParseException JavaDoc 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         /* (non-Javadoc)
249          * @see org.apache.tools.ant.helper.ProjectHelper2.AntHandler#onStartChild(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes, org.apache.tools.ant.helper.AntXMLContext)
250          */

251         public AntHandler onStartChild(String JavaDoc uri, String JavaDoc name, String JavaDoc qname, Attributes JavaDoc attrs, AntXMLContext context) throws SAXParseException JavaDoc {
252             if (name.equals("project") //$NON-NLS-1$
253
&& (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 JavaDoc e) {
259                 getAntModel().error(e);
260                 throw e;
261             }
262         }
263     }
264     
265     public static class ProjectHandler extends ProjectHelper2.ProjectHandler {
266         
267         /* (non-Javadoc)
268          * @see org.apache.tools.ant.helper.ProjectHelper2.AntHandler#onStartChild(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes, org.apache.tools.ant.helper.AntXMLContext)
269          */

270         public AntHandler onStartChild(String JavaDoc uri, String JavaDoc name, String JavaDoc qname, Attributes JavaDoc attrs, AntXMLContext context) {
271             if (name.equals("target") //$NON-NLS-1$
272
&& (uri.length() == 0 || uri.equals(ANT_CORE_URI))) {
273                 return ProjectHelper.targetHandler;
274             }
275             return ProjectHelper.elementHandler;
276         }
277         /* (non-Javadoc)
278          * @see org.apache.tools.ant.helper.ProjectHelper2.AntHandler#onEndElement(java.lang.String, java.lang.String, org.apache.tools.ant.helper.AntXMLContext)
279          */

280         public void onEndElement(String JavaDoc uri, String JavaDoc tag, AntXMLContext context) {
281             super.onEndElement(uri, tag, context);
282             if (currentImportStackSize == 1) {
283                 Locator JavaDoc locator= context.getLocator();
284                 getAntModel().setCurrentElementLength(locator.getLineNumber(), locator.getColumnNumber());
285             }
286         }
287         /* (non-Javadoc)
288          * @see org.apache.tools.ant.helper.ProjectHelper2.AntHandler#onStartElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes, org.apache.tools.ant.helper.AntXMLContext)
289          */

290         public void onStartElement(String JavaDoc uri, String JavaDoc tag, String JavaDoc qname, Attributes JavaDoc attrs, AntXMLContext context) {
291             try {
292                 super.onStartElement(uri, tag, qname, attrs, context);
293             } catch (SAXParseException JavaDoc e) {
294                 getAntModel().error(e);
295             } catch (BuildException be) {
296                 getAntModel().error(be);
297             }
298             if (context.getCurrentTarget() == null) {
299                 //exception occurred creating the project
300
context.getProject().addTarget("", context.getImplicitTarget()); //$NON-NLS-1$
301
context.setCurrentTarget(context.getImplicitTarget());
302             }
303             if (currentImportStackSize == 1) {
304                 Locator JavaDoc locator= context.getLocator();
305                 getAntModel().addProject(context.getProject(), locator.getLineNumber(), locator.getColumnNumber());
306             }
307         }
308         
309         /* (non-Javadoc)
310          * @see org.apache.tools.ant.helper.ProjectHelper2.AntHandler#characters(char[], int, int, org.apache.tools.ant.helper.AntXMLContext)
311          */

312         public void characters(char[] buf, int start, int count, AntXMLContext context) {
313             try {
314                 super.characters(buf, start, count, context);
315             } catch (SAXParseException JavaDoc 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         /* (non-Javadoc)
325          * @see org.apache.tools.ant.helper.ProjectHelper2.AntHandler#onStartChild(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes, org.apache.tools.ant.helper.AntXMLContext)
326          */

327         public AntHandler onStartChild(String JavaDoc uri, String JavaDoc name, String JavaDoc qname, Attributes JavaDoc attrs, AntXMLContext context) {
328             return ProjectHelper.elementHandler;
329         }
330         /* (non-Javadoc)
331          * @see org.apache.tools.ant.helper.ProjectHelper2.AntHandler#onStartElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes, org.apache.tools.ant.helper.AntXMLContext)
332          */

333         public void onStartElement(String JavaDoc uri, String JavaDoc tag, String JavaDoc qname, Attributes JavaDoc attrs, AntXMLContext context) {
334             try {
335                 super.onStartElement(uri, tag, qname, attrs, context);
336                 Target newTarget= context.getCurrentTarget();
337                 Locator JavaDoc locator= context.getLocator();
338                 getAntModel().addTarget(newTarget, locator.getLineNumber(), locator.getColumnNumber());
339             } catch (SAXParseException JavaDoc e) {
340                 handleErrorInTarget(context, e);
341             } catch (BuildException be) {
342                 handleErrorInTarget(context, be);
343             }
344         }
345         
346         private void handleErrorInTarget(AntXMLContext context, Exception JavaDoc e) {
347             Target newTarget= context.getCurrentTarget();
348             Locator JavaDoc locator= context.getLocator();
349             getAntModel().addTarget(newTarget, locator.getLineNumber(), locator.getColumnNumber());
350             getAntModel().errorFromElement(e, null, locator.getLineNumber(), locator.getColumnNumber());
351         }
352         
353         /* (non-Javadoc)
354          * @see org.apache.tools.ant.helper.ProjectHelper2.AntHandler#onEndElement(java.lang.String, java.lang.String, org.apache.tools.ant.helper.AntXMLContext)
355          */

356         public void onEndElement(String JavaDoc uri, String JavaDoc tag, AntXMLContext context) {
357             super.onEndElement(uri, tag, context);
358             Locator JavaDoc locator= context.getLocator();
359             getAntModel().setCurrentElementLength(locator.getLineNumber(), locator.getColumnNumber());
360         }
361         
362         /* (non-Javadoc)
363          * @see org.apache.tools.ant.helper.ProjectHelper2.AntHandler#characters(char[], int, int, org.apache.tools.ant.helper.AntXMLContext)
364          */

365         public void characters(char[] buf, int start, int count, AntXMLContext context) {
366             try {
367                 super.characters(buf, start, count, context);
368             } catch (SAXParseException JavaDoc 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         /* (non-Javadoc)
383          * @see org.xml.sax.ErrorHandler#error(org.xml.sax.SAXParseException)
384          */

385         public void error(SAXParseException JavaDoc e) {
386             getAntModel().error(e);
387         }
388         /* (non-Javadoc)
389          * @see org.xml.sax.ErrorHandler#fatalError(org.xml.sax.SAXParseException)
390          */

391         public void fatalError(SAXParseException JavaDoc e) {
392             getAntModel().fatalError(e);
393         }
394         /* (non-Javadoc)
395          * @see org.xml.sax.ErrorHandler#warning(org.xml.sax.SAXParseException)
396          */

397         public void warning(SAXParseException JavaDoc e) {
398             getAntModel().warning(e);
399         }
400         /* (non-Javadoc)
401          * @see org.xml.sax.EntityResolver#resolveEntity(java.lang.String, java.lang.String)
402          */

403         public InputSource JavaDoc resolveEntity(String JavaDoc publicId, String JavaDoc systemId) {
404             InputSource JavaDoc source= super.resolveEntity(publicId, systemId);
405             if (source != null) {
406                 String JavaDoc 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         /* (non-Javadoc)
418          * @see org.xml.sax.ContentHandler#startPrefixMapping(java.lang.String, java.lang.String)
419          */

420         public void startPrefixMapping(String JavaDoc prefix, String JavaDoc 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 JavaDoc e) {
429             Locator JavaDoc 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 JavaDoc {
445         /* (non-Javadoc)
446          * @see org.xml.sax.ext.LexicalHandler#endCDATA()
447          */

448         public void endCDATA() throws SAXException JavaDoc {
449         }
450
451         /* (non-Javadoc)
452          * @see org.xml.sax.ext.LexicalHandler#endDTD()
453          */

454         public void endDTD() throws SAXException JavaDoc {
455             if (getAntModel().canGetLexicalInfo()) {
456                 AntXMLContext context= getContext();
457                 Locator JavaDoc locator= context.getLocator();
458                 getAntModel().setCurrentElementLength(locator.getLineNumber(), locator.getColumnNumber());
459             }
460         }
461
462         /* (non-Javadoc)
463          * @see org.xml.sax.ext.LexicalHandler#startCDATA()
464          */

465         public void startCDATA() throws SAXException JavaDoc {
466         }
467
468         /* (non-Javadoc)
469          * @see org.xml.sax.ext.LexicalHandler#comment(char[], int, int)
470          */

471         public void comment(char[] ch, int start, int length) throws SAXException JavaDoc {
472             if (getAntModel().canGetLexicalInfo()) {
473                 AntXMLContext context= getContext();
474                 Locator JavaDoc locator= context.getLocator();
475                 if (locator != null) {
476                     getAntModel().addComment(locator.getLineNumber(), locator.getColumnNumber(), length);
477                 }
478             }
479         }
480
481         /* (non-Javadoc)
482          * @see org.xml.sax.ext.LexicalHandler#endEntity(java.lang.String)
483          */

484         public void endEntity(String JavaDoc name) throws SAXException JavaDoc {
485         }
486
487         /* (non-Javadoc)
488          * @see org.xml.sax.ext.LexicalHandler#startEntity(java.lang.String)
489          */

490         public void startEntity(String JavaDoc name) throws SAXException JavaDoc {
491             if (currentEntityPath == null) {
492                 currentEntityName= name;
493             } else {
494                 getAntModel().addEntity(name, currentEntityPath);
495                 currentEntityPath= null;
496             }
497         }
498
499         /* (non-Javadoc)
500          * @see org.xml.sax.ext.LexicalHandler#startDTD(java.lang.String, java.lang.String, java.lang.String)
501          */

502         public void startDTD(String JavaDoc name, String JavaDoc publicId, String JavaDoc systemId) throws SAXException JavaDoc {
503             if (getAntModel().canGetLexicalInfo()) {
504                 AntXMLContext context= getContext();
505                 Locator JavaDoc 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     /**
516      * Parses the project file, configuring the project as it goes.
517      *
518      * @param project the current project
519      * @param source the xml source or a java.io.File
520      * @param handler the root handler to use (contains the current context)
521      * @exception BuildException if the configuration is invalid or cannot
522      * be read
523      */

524     public void parse(Project project, Object JavaDoc source, org.apache.tools.ant.helper.ProjectHelper2.RootHandler handler) throws BuildException {
525         
526         if (!(source instanceof String JavaDoc) && !(source instanceof File JavaDoc)) {
527             //this should only occur with a source URL and that should not be possible currently
528
//as Antlib hard codes using ProjectHelper2 (bug 152793)
529
super.parse(project, source, handler);
530             return;
531         }
532         
533         AntXMLContext context= (AntXMLContext)project.getReference("ant.parsing.context"); //$NON-NLS-1$
534
//switch to using "our" handler so parsing will continue on hitting errors.
535
handler= new RootHandler(context, mainHandler);
536         Reader JavaDoc stream= null;
537         try {
538             InputSource JavaDoc inputSource= null;
539             if ((source instanceof File JavaDoc)) {
540                 buildFile = (File JavaDoc) source;
541                 buildFile = getFileUtils().normalize(buildFile.getAbsolutePath());
542                 stream = new FileReader JavaDoc(buildFile);
543                 inputSource = new InputSource JavaDoc(stream);
544             } else if (source instanceof String JavaDoc) {
545                 stream= new StringReader JavaDoc((String JavaDoc)source);
546                 inputSource = new InputSource JavaDoc(stream);
547             }
548             
549             /**
550              * SAX 2 style parser used to parse the given file.
551              */

552             //We cannot use the JAXPUtils support here as the underlying parser factory is cached and
553
//will not reflect classpath changes that effect which XML parser will be returned.
554
//see bug 59764
555
//XMLReader parser = JAXPUtils.getNamespaceXMLReader();
556
XMLReader JavaDoc parser= getNamespaceXMLReader();
557             if (parser == null) {
558                 throw new BuildException(ProjectHelperMessages.ProjectHelper_0);
559             }
560             String JavaDoc 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); //$NON-NLS-1$
576

577             parser.parse(inputSource);
578         } catch (SAXParseException JavaDoc exc) {
579             getAntModel().fatalError(exc);
580         } catch (SAXException JavaDoc exc) {
581             //ignore as we will be parsing incomplete source
582
} catch (FileNotFoundException JavaDoc exc) {
583             throw new BuildException(exc);
584         } catch (UnsupportedEncodingException JavaDoc exc) {
585               throw new BuildException(exc);
586         } catch (IOException JavaDoc exc) {
587             throw new BuildException(exc);
588         } finally {
589             try {
590                 if (stream != null) {
591                     stream.close();
592                 }
593             } catch (IOException JavaDoc ioe) {
594                 // ignore this
595
}
596         }
597     }
598
599     /**
600      * Sets the buildfile that is about to be parsed or <code>null</code> if
601      * parsing has completed.
602      *
603      * @param file The buildfile about to be parsed
604      */

605     public void setBuildFile(File JavaDoc file) {
606         buildFile= file;
607         currentImportStackSize= 1;
608     }
609     
610     /* (non-Javadoc)
611      * We override this method from ProjectHelper2 as we do not want to execute the implicit target or
612      * any other target for that matter as it could hang Eclipse.
613      * See https://bugs.eclipse.org/bugs/show_bug.cgi?id=50795 for more details.
614      *
615      * @see org.apache.tools.ant.ProjectHelper#parse(org.apache.tools.ant.Project, java.lang.Object)
616      */

617     public void parse(Project project, Object JavaDoc source) throws BuildException {
618         AntXMLContext context = (AntXMLContext) project.getReference("ant.parsing.context"); //$NON-NLS-1$
619
if (context == null) {
620             getImportStack().removeAllElements();
621             context = new AntXMLContext(project);
622             project.addReference("ant.parsing.context", context); //$NON-NLS-1$
623
project.addReference("ant.targets", context.getTargets()); //$NON-NLS-1$
624
fgAntContext= context;
625         }
626         getImportStack().addElement(source);
627         currentImportStackSize= getImportStack().size();
628         if (getImportStack().size() > 1) {
629             // we are in an imported file.
630
context.setIgnoreProjectTag(true);
631             Target currentTarget = context.getCurrentTarget();
632             Target currentImplicit = context.getImplicitTarget();
633             Map JavaDoc currentTargets = context.getCurrentTargets();
634             
635             try {
636                 Target newCurrent = new Target();
637                 newCurrent.setProject(project);
638                 newCurrent.setName(""); //$NON-NLS-1$
639
context.setCurrentTarget(newCurrent);
640                 context.setCurrentTargets(new HashMap JavaDoc());
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             // top level file
650
context.setCurrentTargets(new HashMap JavaDoc());
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      /**
683      * Returns a newly created SAX 2 XMLReader, which is namespace aware
684      *
685      * @return a SAX 2 XMLReader.
686      * @since Ant 1.6 from org.apache.tools.ant.util.JAXPUtils
687      */

688     private XMLReader JavaDoc getNamespaceXMLReader() throws BuildException {
689         if (fgXMLReader == null) {
690             try {
691                 fgXMLReader= newSAXParser(getNSParserFactory()).getXMLReader();
692             } catch (SAXException JavaDoc e) {
693             }
694         }
695         return fgXMLReader;
696     }
697     
698     /**
699      * Returns the parser factory to use to create namespace aware parsers.
700      *
701      * @return a SAXParserFactory to use which supports manufacture of
702      * namespace aware parsers
703      *
704      * @since Ant 1.6 from org.apache.tools.ant.util.JAXPUtils
705      */

706     private SAXParserFactory JavaDoc getNSParserFactory() throws BuildException {
707
708         SAXParserFactory JavaDoc nsParserFactory = JAXPUtils.newParserFactory();
709         nsParserFactory.setNamespaceAware(true);
710         
711         return nsParserFactory;
712     }
713     
714     /**
715      * @return a new SAXParser instance as helper for getParser and
716      * getXMLReader.
717      *
718      * @since Ant 1.5 from org.apache.tools.ant.util.JAXPUtils
719      */

720     private SAXParser JavaDoc newSAXParser(SAXParserFactory JavaDoc factory) {
721         try {
722             return factory.newSAXParser();
723         } catch (ParserConfigurationException JavaDoc e) {
724         } catch (SAXException JavaDoc e) {
725         }
726         return null;
727     }
728 }
729
Popular Tags