From 129521a91386b67319b30e64bea91ff29b9f385a Mon Sep 17 00:00:00 2001 From: mdb Date: Thu, 29 Nov 2001 18:11:42 +0000 Subject: [PATCH] Created a rule like SetNext but that sets the child object into a field of the parent rather than calling a method with the child as an argument. git-svn-id: https://samskivert.googlecode.com/svn/trunk@480 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- .../com/samskivert/xml/SetNextFieldRule.java | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 projects/samskivert/src/java/com/samskivert/xml/SetNextFieldRule.java diff --git a/projects/samskivert/src/java/com/samskivert/xml/SetNextFieldRule.java b/projects/samskivert/src/java/com/samskivert/xml/SetNextFieldRule.java new file mode 100644 index 00000000..eae86515 --- /dev/null +++ b/projects/samskivert/src/java/com/samskivert/xml/SetNextFieldRule.java @@ -0,0 +1,65 @@ +// +// $Id: SetNextFieldRule.java,v 1.1 2001/11/29 18:11:42 mdb Exp $ +// +// samskivert library - useful routines for java programs +// Copyright (C) 2001 Walter Korman +// +// This library is free software; you can redistribute it and/or modify it +// under the terms of the GNU Lesser General Public License as published +// by the Free Software Foundation; either version 2.1 of the License, or +// (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package com.samskivert.xml; + +import java.lang.reflect.Field; + +import org.xml.sax.Attributes; +import org.apache.commons.digester.Digester; +import org.apache.commons.digester.Rule; + +/** + * Like the SetNextRule except that the object on the top of + * the stack is placed into a field of the penultimate object. + */ +public class SetNextFieldRule extends Rule +{ + /** + * Constructs a set field rule for the specified field. + */ + public SetNextFieldRule (Digester digester, String fieldName) + { + super(digester); + + // keep this for later + _fieldName = fieldName; + } + + public void end () + throws Exception + { + // identify the objects to be used + Object child = digester.peek(0); + Object parent = digester.peek(1); + Class pclass = parent.getClass(); + + if (digester.getDebug() >= 1) { + digester.log("Set " + pclass.getName() + "." + _fieldName + + " = " + child); + } + + // stuff the child object into the field of the parent + Field field = pclass.getField(_fieldName); + field.set(parent, child); + } + + protected String _fieldName; +}