alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 * 
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 * 
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2004 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.netbeans.nbbuild;

import java.io.*;
import java.util.*;
import org.apache.tools.ant.*;
import org.w3c.dom.*;
import org.xml.sax.*;

/**
 * Checks whether build-time dependencies of projectized modules
 * are correct.
 * @author Jesse Glick
 */
public final class CheckBuildDependencies extends Task {
    
    private static final String PROJECT_NS = "http://www.netbeans.org/ns/project/1";
    private static final String NBM_NS = "http://www.netbeans.org/ns/nb-module-project/1";
    private static final String NBM_TYPE = "org.netbeans.modules.apisupport.project";
    
    private File nbroot;
    /**
     * Set the root of the NB source tree.
     */
    public void setNbroot(File f) {
        nbroot = f;
    }
    
    public void execute() throws BuildException {
        if (nbroot == null) {
            throw new BuildException("Must set 'nbroot' param", getLocation());
        }
        if (nbroot != null/*true*/) throw new BuildException("THIS TASK IS OUT OF DATE NOW, SORRY!");//XXX
        try {
            File masterBuildF = new File(new File(nbroot, "nbbuild"), "build.xml");
            Document masterBuildDoc = XMLUtil.parse(new InputSource(masterBuildF.toURI().toString()), false, false, /*XXX*/null, null);
            File modulesF = new File(new File(new File(nbroot, "nbbuild"), "templates"), "modules.xml");
            ModuleListParser modules = new ModuleListParser(modulesF);
            List/*", Project.MSG_DEBUG);
                    continue;
                }
                String targetName = target.getAttribute("name");
                if (!targetName.startsWith("all-")) {
                    log("Skipping target " + targetName, Project.MSG_DEBUG);
                    continue;
                }
                String name = targetName.substring(4);
                File projectXmlF = new File(nbroot, (name + "/nbproject/project.xml").replace('/', File.separatorChar));
                if (!projectXmlF.isFile()) {
                    log("No such file " + projectXmlF, Project.MSG_DEBUG);
                    continue;
                }
                String depends = target.getAttribute("depends");
                Element data = getConfig(projectXmlF);
                if (data == null) {
                    log("Not one of ours: " + projectXmlF, Project.MSG_DEBUG);
                    continue;
                }
                Element moduleDependencies = XMLUtil.findElement(data, "module-dependencies", NBM_NS);
                List/**/ deps = XMLUtil.findSubElements(moduleDependencies);
                Iterator it2 = deps.iterator();
                StringBuffer desiredDependsB = new StringBuffer();
                while (it2.hasNext()) {
                    Element dep = (Element)it2.next();
                    if (XMLUtil.findElement(dep, "build-prerequisite", NBM_NS) == null) {
                        continue;
                    }
                    Element cnbEl = XMLUtil.findElement(dep, "code-name-base", NBM_NS);
                    String cnb = XMLUtil.findText(cnbEl);
                    ModuleListParser.Entry module = modules.findByCodeNameBase(cnb);
                    if (module == null) {
                        throw new BuildException("No dependent module " + cnb + " for " + name, getLocation());
                    }
                    if (desiredDependsB.length() > 0) {
                        desiredDependsB.append(',');
                    }
                    desiredDependsB.append("all-");
                    desiredDependsB.append(module.getPath());
                }
                String desiredDepends;
                if (desiredDependsB.length() > 0) {
                    desiredDepends = desiredDependsB.toString();
                } else {
                    // Currently we add an explicit dep on init only for modules with no other deps.
                    desiredDepends = "init";
                }
                if (!depends.equals(desiredDepends)) {
                    // XXX would be nice to detect case where only the order is wrong...
                    // or should be always sort them?
                    log("Incorrect dependencies list for " + targetName + " in " + masterBuildF + ":", Project.MSG_WARN);
                    log("  should be: " + desiredDepends, Project.MSG_WARN);
                    log("  but is:    " + depends, Project.MSG_WARN);
                    ok = false;
                } else {
                    log("Correct dependencies list for " + targetName, Project.MSG_VERBOSE);
                }
            }
            if (!ok) {
                throw new BuildException("Some targets did not have the correct dependencies", getLocation());
            }
        } catch (IOException e) {
            throw new BuildException(e, getLocation());
        } catch (SAXException e) {
            throw new BuildException(e, getLocation());
        }
    }
    
    private Element getConfig(File projectXmlF) throws BuildException, IOException, SAXException {
        Document pDoc = XMLUtil.parse(new InputSource(projectXmlF.toURI().toString()), false, true, /*XXX*/null, null);
        Element e = pDoc.getDocumentElement();
        Element t = XMLUtil.findElement(e, "type", PROJECT_NS);
        String type = XMLUtil.findText(t);
        if (!type.equals(NBM_TYPE)) {
            return null;
        }
        Element c = XMLUtil.findElement(e, "configuration", PROJECT_NS);
        if (c == null) {
            throw new BuildException("no ", new Location(projectXmlF.getAbsolutePath()));
        }
        Element d = XMLUtil.findElement(c, "data", NBM_NS);
        if (d == null) {
            throw new BuildException("no ", new Location(projectXmlF.getAbsolutePath()));
        }
        return d;
    }

}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.