commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Dan Tarkenton <tarken...@gmail.com>
Subject Re: [Digester] basic usage problem. Help me understand the stack.
Date Tue, 27 Jul 2004 18:06:46 GMT
Perhaps I spoke too soon in my last response.  I would like to have a
look at the chains source you spoke of for configuration however when
I visit http://jakarta.apache.org/commons/chain/ there is a note
stating the source is unreleased.  Do I need to go to CVS to grab the
src?

I did however to manage to get recursion to work in my code with the
*/part idea.  I'll post that code below, but first my issue:

I have run into another behavioral issue that I do not understand; the
problem revolves around the digester.addCallMethod method.  My
understanding is that this method calls a method on the top (parent)
object.  Since the only object I ever deal with in my stack is
Part.java, I will go ahead and show that code:

public class Part {

    public ArrayList getParts() {
        return parts;
    }

    private ArrayList parts = new ArrayList();

    private String partNumber;
    private String partDescription;
    private String partMaterialCode;
	private String partUM;
	private String quantity;
    private Vector refDes;
	private Vector specCodes;

    public String getPartNumber() {
        return partNumber;
    }

    public void setPartNumber(String partNumber) {
        this.partNumber = partNumber;
    }

    public Vector getSpecCodes() {
        return specCodes;
    }

    public void setSpecCodes(Vector specCodes) {
        this.specCodes = specCodes;
    }

    public Vector getRefDes() {
        return refDes;
    }

    public void setRefDes(Vector refDes) {
        this.refDes = refDes;
    }

    public String getQuantity() {
        return quantity;
    }

    public void setQuantity(String quantity) {
        this.quantity = quantity;
    }

    public String getPartUM() {
        return partUM;
    }

    public void setPartUM(String partUM) {
        this.partUM = partUM;
    }

    public String getPartMaterialCode() {
        return partMaterialCode;
    }

    public void setPartMaterialCode(String partMaterialCode) {
        this.partMaterialCode = partMaterialCode;
    }

    public String getPartDescription() {
        return partDescription;
    }

    public void setPartDescription(String partDescription) {
        this.partDescription = partDescription;
    }

    public boolean isValidSpecCode(String specCode) {
        //TODO find out valid spec codes
        return true;
    }

    public void addRefDes(String refDes) {
        if (refDes == null) return;
        this.refDes.add(refDes);
    }

    public void addSpecCode(String specCode) {
        this.specCodes.add(specCode);
    }

    public void addPart(Part part) {
        parts.add(part);
    }
}

Note that this class has addPart(Part part) and addRefDes(String
refDes).  Now when I run my Parser code (below) with the exception of
the addCallMethod line, I get the Part objects I need.  However, when
i have digester.addCallMethod("*/component/refDesignators/refDes",
"addRefDes", 0); in my code, I get a null pointer.  What I don't
understand is that Part object has addRefDes(String x) method.  The
way I've written it, every object in the stack is always a part
object, correct?  So why does the addCallMethod call not grab the body
text of the refDes element and add it to my Part object?

public void parse(String document) throws ParserException {

        Digester digester = new Digester();

        digester.setErrorHandler(new BOMParserErrorHandler());
        digester.setValidating(true);
        digester.addObjectCreate("*/component", Part.class );

        digester.addBeanPropertySetter( "*/component/quantity", "quantity");
        digester.addCallMethod("*/component/refDesignators/refDes",
"addRefDes", 0);  //this line is my culprit

        digester.addBeanPropertySetter( "*/component/part/partNumber",
"partNumber");
        digester.addBeanPropertySetter(
"*/component/part/partDescription", "partDescription");
        digester.addBeanPropertySetter(
"*/component/part/partMaterialCode", "partMaterialCode");
        digester.addBeanPropertySetter( "*/component/part/partUM", "partUM");
        digester.addSetNext("*/component", "addPart",
"com.titan.bat.parsers.ise.Part");

        Part dummy = new Part();
        digester.push(dummy);

        try {
            digester.parse(new
File("/usr/local/projects/BOMAnalysis/web/xml/psbk.xml"));
        } 
            //catch exceptions
    	}

        Iterator iter = dummy.getParts().iterator();
        Part part;
        while (iter.hasNext()) {
            part = (Part) iter.next();
            parts.put(part.getPartNumber(), part);
        }

        this.addPart(dummy);
    }

    public void addPart(Part root) {

       Part part;
       if (root.getParts() !=null || root.getParts().size() > 0) {
//has children
           System.out.println("part #" + root.getPartNumber() + " has
children");
           Iterator iter = root.getParts().iterator();
           while (iter.hasNext()) {
                part = (Part) iter.next();
                System.out.println("part.getRefDes() = " + part.getRefDes());
                if (part !=null && part.getPartNumber() !=null) {
                    parts.put(part.getPartNumber(), part);
                    this.addPart(part);
                }

           }
       }
    }

Anyone have any insight as to why that addCallMethod is blowing up my code?

Thanks,
Dan

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Mime
View raw message