JavaActions.java 文件源码

java
阅读 29 收藏 0 点赞 0 评论 0

项目:incubator-netbeans 作者:
/**
 * Find the line number of a target in an Ant script, or some other line in an XML file.
 * Able to find a certain element with a certain attribute matching a given value.
 * See also AntTargetNode.TargetOpenCookie.
 * @param file an Ant script or other XML file
 * @param match the attribute value to match (e.g. target name)
 * @param elementLocalName the (local) name of the element to look for
 * @param elementAttributeName the name of the attribute to match on
 * @return the line number (0-based), or -1 if not found
 */
static final int findLine(FileObject file, final String match, final String elementLocalName, final String elementAttributeName) throws IOException, SAXException, ParserConfigurationException {
    InputSource in = new InputSource(file.getURL().toString());
    SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setNamespaceAware(true);
    SAXParser parser = factory.newSAXParser();
    final int[] line = new int[] {-1};
    class Handler extends DefaultHandler {
        private Locator locator;
        public void setDocumentLocator(Locator l) {
            locator = l;
        }
        public void startElement(String uri, String localname, String qname, Attributes attr) throws SAXException {
            if (line[0] == -1) {
                if (localname.equals(elementLocalName) && match.equals(attr.getValue(elementAttributeName))) { // NOI18N
                    line[0] = locator.getLineNumber() - 1;
                }
            }
        }
    }
    parser.parse(in, new Handler());
    return line[0];
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号