📄 projecthelper2.java
字号:
+ " \"", context.getLocator()); } /** * Handle the end of a element. * * @param uri the namespace uri of the element * @param tag the tag of the element * @param qname the qualified name of the element * @param context the current context * @exception SAXParseException if an error occurs */ public void onEndChild(String uri, String tag, String qname, AntXMLContext context) throws SAXParseException { } /** * This method is called when this element and all elements nested into it have been * handled. I.e., this happens at the </end_tag_of_the_element>. * @param uri the namespace uri for this element * @param tag the element name * @param context the current context */ public void onEndElement(String uri, String tag, AntXMLContext context) { } /** * Handles text within an element. This base implementation just * throws an exception, you must override it if you expect content. * * @param buf A character array of the text within the element. * Will not be <code>null</code>. * @param start The start element in the array. * @param count The number of characters to read from the array. * @param context The current context. * * @exception SAXParseException if this method is not overridden, or in * case of error in an overridden version */ public void characters(char[] buf, int start, int count, AntXMLContext context) throws SAXParseException { String s = new String(buf, start, count).trim(); if (s.length() > 0) { throw new SAXParseException("Unexpected text \"" + s + "\"", context.getLocator()); } } /** * Will be called every time a namespace is reached. * It'll verify if the ns was processed, and if not load the task definitions. * @param uri The namespace uri. */ protected void checkNamespace(String uri) { } } /** * Handler for ant processing. Uses a stack of AntHandlers to * implement each element ( the original parser used a recursive behavior, * with the implicit execution stack ) */ public static class RootHandler extends DefaultHandler { private Stack antHandlers = new Stack(); private AntHandler currentHandler = null; private AntXMLContext context; /** * Creates a new RootHandler instance. * * @param context The context for the handler. * @param rootHandler The handler for the root element. */ public RootHandler(AntXMLContext context, AntHandler rootHandler) { currentHandler = rootHandler; antHandlers.push(currentHandler); this.context = context; } /** * Returns the current ant handler object. * @return the current ant handler. */ public AntHandler getCurrentAntHandler() { return currentHandler; } /** * Resolves file: URIs relative to the build file. * * @param publicId The public identifier, or <code>null</code> * if none is available. Ignored in this * implementation. * @param systemId The system identifier provided in the XML * document. Will not be <code>null</code>. * @return an inputsource for this identifier */ public InputSource resolveEntity(String publicId, String systemId) { context.getProject().log("resolving systemId: " + systemId, Project.MSG_VERBOSE); if (systemId.startsWith("file:")) { String path = FILE_UTILS.fromURI(systemId); File file = new File(path); if (!file.isAbsolute()) { file = FILE_UTILS.resolveFile(context.getBuildFileParent(), path); context.getProject().log( "Warning: '" + systemId + "' in " + context.getBuildFile() + " should be expressed simply as '" + path.replace('\\', '/') + "' for compliance with other XML tools", Project.MSG_WARN); } context.getProject().log("file=" + file, Project.MSG_DEBUG); try { InputSource inputSource = new InputSource(new FileInputStream(file)); inputSource.setSystemId(FILE_UTILS.toURI(file.getAbsolutePath())); return inputSource; } catch (FileNotFoundException fne) { context.getProject().log(file.getAbsolutePath() + " could not be found", Project.MSG_WARN); } } // use default if not file or file not found context.getProject().log("could not resolve systemId", Project.MSG_DEBUG); return null; } /** * Handles the start of a project element. A project handler is created * and initialised with the element name and attributes. * * @param uri The namespace uri for this element. * @param tag The name of the element being started. * Will not be <code>null</code>. * @param qname The qualified name for this element. * @param attrs Attributes of the element being started. * Will not be <code>null</code>. * * @exception org.xml.sax.SAXParseException if the tag given is not * <code>"project"</code> */ public void startElement(String uri, String tag, String qname, Attributes attrs) throws SAXParseException { AntHandler next = currentHandler.onStartChild(uri, tag, qname, attrs, context); antHandlers.push(currentHandler); currentHandler = next; currentHandler.onStartElement(uri, tag, qname, attrs, context); } /** * Sets the locator in the project helper for future reference. * * @param locator The locator used by the parser. * Will not be <code>null</code>. */ public void setDocumentLocator(Locator locator) { context.setLocator(locator); } /** * Handles the end of an element. Any required clean-up is performed * by the onEndElement() method and then the original handler is restored to the parser. * * @param uri The namespace URI for this element. * @param name The name of the element which is ending. * Will not be <code>null</code>. * @param qName The qualified name for this element. * * @exception SAXException in case of error (not thrown in this implementation) */ public void endElement(String uri, String name, String qName) throws SAXException { currentHandler.onEndElement(uri, name, context); AntHandler prev = (AntHandler) antHandlers.pop(); currentHandler = prev; if (currentHandler != null) { currentHandler.onEndChild(uri, name, qName, context); } } /** * Handle text within an element, calls currentHandler.characters. * * @param buf A character array of the test. * @param start The start offset in the array. * @param count The number of characters to read. * @exception SAXParseException if an error occurs */ public void characters(char[] buf, int start, int count) throws SAXParseException { currentHandler.characters(buf, start, count, context); } /** * Start a namespace prefix to uri mapping * * @param prefix the namespace prefix * @param uri the namespace uri */ public void startPrefixMapping(String prefix, String uri) { context.startPrefixMapping(prefix, uri); } /** * End a namepace prefix to uri mapping * * @param prefix the prefix that is not mapped anymore */ public void endPrefixMapping(String prefix) { context.endPrefixMapping(prefix); } } /** * The main handler - it handles the <project> tag. * * @see org.apache.tools.ant.helper.ProjectHelper2.AntHandler */ public static class MainHandler extends AntHandler { /** * Handle the project tag * * @param uri The namespace uri. * @param name The element tag. * @param qname The element qualified name. * @param attrs The attributes of the element. * @param context The current context. * @return The project handler that handles subelements of project * @exception SAXParseException if the qualified name is not "project". */ public AntHandler onStartChild(String uri, String name, String qname, Attributes attrs, AntXMLContext context) throws SAXParseException { if (name.equals("project") && (uri.equals("") || uri.equals(ANT_CORE_URI))) { return ProjectHelper2.projectHandler; }// if (context.importlevel > 0) {// // we are in an imported file. Allow top-level <target>.// if (qname.equals( "target" ) )// return ProjectHelper2.targetHandler;// } if (name.equals(qname)) { throw new SAXParseException("Unexpected element \"{" + uri + "}" + name + "\" {" + ANT_CORE_URI + "}" + name, context.getLocator()); } throw new SAXParseException("Unexpected element \"" + qname + "\" " + name, context.getLocator()); } } /** * Handler for the top level "project" element. */ public static class ProjectHandler extends AntHandler { /** * Initialisation routine called after handler creation * with the element name and attributes. The attributes which * this handler can deal with are: <code>"default"</code>, * <code>"name"</code>, <code>"id"</code> and <code>"basedir"</code>. * * @param uri The namespace URI for this element. * @param tag Name of the element which caused this handler * to be created. Should not be <code>null</code>. * Ignored in this implementation. * @param qname The qualified name for this element. * @param attrs Attributes of the element which caused this * handler to be created. Must not be <code>null</code>. * @param context The current context. * * @exception SAXParseException if an unexpected attribute is * encountered or if the <code>"default"</code> attribute * is missing. */ public void onStartElement(String uri, String tag, String qname, Attributes attrs, AntXMLContext context) throws SAXParseException { String baseDir = null; boolean nameAttributeSet = false; Project project = context.getProject(); // Set the location of the implicit target associated with the project tag context.getImplicitTarget().setLocation(new Location(context.getLocator())); /** XXX I really don't like this - the XML processor is still * too 'involved' in the processing. A better solution (IMO) * would be to create UE for Project and Target too, and * then process the tree and have Project/Target deal with * its attributes ( similar with Description ). * * If we eventually switch to ( or add support for ) DOM, * things will work smoothly - UE can be avoided almost completely * ( it could still be created on demand, for backward compatibility ) */ for (int i = 0; i < attrs.getLength(); i++) { String attrUri = attrs.getURI(i); if (attrUri != null && !attrUri.equals("") && !attrUri.equals(uri)) { continue; // Ignore attributes from unknown uris } String key = attrs.getLocalName(i); String value = attrs.getValue(i); if (key.equals("default")) { if (value != null && !value.equals("")) { if (!context.isIgnoringProjectTag()) { project.setDefault(value); } } } else if (key.equals("name")) { if (value != null) { context.setCurrentProjectName(value); nameAttributeSet = true; if (!context.isIgnoringProjectTag()) { project.setName(value); project.addReference(value, project); } } } else if (key.equals("id")) { if (value != null) { // What's the difference between id and name ? if (!context.isIgnoringProjectTag()) { project.addReference(value, project); } } } else if (key.equals("basedir")) { if (!context.isIgnoringProjectTag()) { baseDir = value; } } else { // XXX ignore attributes in a different NS ( maybe store them ? ) throw new SAXParseException("Unexpected attribute \"" + attrs.getQName(i) + "\"", context.getLocator()); } } // XXX Move to Project ( so it is shared by all helpers ) String antFileProp = "ant.file." + context.getCurrentProjectName(); String dup = project.getProperty(antFileProp); if (dup != null && nameAttributeSet) { File dupFile = new File(dup); if (context.isIgnoringProjectTag() && !dupFile.equals(context.getBuildFile())) { project.log("Duplicated project name in import. Project " + context.getCurrentProjectName() + " defined first in " + dup + " and again in " + context.getBuildFile(), Project.MSG_WARN); } } if (context.getBuildFile() != null && nameAttributeSet) { project.setUserProperty(MagicNames.ANT_FILE + "." + context.getCurrentProjectName(), context.getBuildFile().toString()); } if (context.isIgnoringProjectTag()) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -