KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > digester > xmlrules > DigesterRuleParser


1 /* $Id: DigesterRuleParser.java 155412 2005-02-26 12:58:36Z dirkv $
2  *
3  * Copyright 2001-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18
19 package org.apache.commons.digester.xmlrules;
20
21
22 import java.io.FileNotFoundException JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.net.URL JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Set JavaDoc;
29 import java.util.StringTokenizer JavaDoc;
30
31 import org.apache.commons.beanutils.ConvertUtils;
32
33 import org.apache.commons.collections.ArrayStack;
34
35 import org.apache.commons.digester.AbstractObjectCreationFactory;
36 import org.apache.commons.digester.BeanPropertySetterRule;
37 import org.apache.commons.digester.CallMethodRule;
38 import org.apache.commons.digester.CallParamRule;
39 import org.apache.commons.digester.Digester;
40 import org.apache.commons.digester.FactoryCreateRule;
41 import org.apache.commons.digester.ObjectCreateRule;
42 import org.apache.commons.digester.Rule;
43 import org.apache.commons.digester.RuleSetBase;
44 import org.apache.commons.digester.Rules;
45 import org.apache.commons.digester.SetNestedPropertiesRule;
46 import org.apache.commons.digester.SetNextRule;
47 import org.apache.commons.digester.SetPropertiesRule;
48 import org.apache.commons.digester.SetPropertyRule;
49 import org.apache.commons.digester.SetRootRule;
50 import org.apache.commons.digester.SetTopRule;
51 import org.apache.commons.digester.ObjectParamRule;
52
53 import org.xml.sax.Attributes JavaDoc;
54 import org.xml.sax.SAXException JavaDoc;
55
56
57 /**
58  * This is a RuleSet that parses XML into Digester rules, and then
59  * adds those rules to a 'target' Digester.
60  *
61  * @since 1.2
62  */

63
64 public class DigesterRuleParser extends RuleSetBase {
65     
66     public static final String JavaDoc DIGESTER_PUBLIC_ID = "-//Jakarta Apache //DTD digester-rules XML V1.0//EN";
67     
68     /**
69      * path to the DTD
70      */

71     private String JavaDoc digesterDtdUrl;
72     
73     /**
74      * This is the digester to which we are adding the rules that we parse
75      * from the Rules XML document.
76      */

77     protected Digester targetDigester;
78
79     /** See {@link #setBasePath}. */
80     protected String JavaDoc basePath = "";
81     
82     /**
83      * A stack whose toString method returns a '/'-separated concatenation
84      * of all the elements in the stack.
85      */

86     protected class PatternStack extends ArrayStack {
87         public String JavaDoc toString() {
88             StringBuffer JavaDoc str = new StringBuffer JavaDoc();
89             for (int i = 0; i < size(); i++) {
90                 String JavaDoc elem = get(i).toString();
91                 if (elem.length() > 0) {
92                     if (str.length() > 0) {
93                         str.append('/');
94                     }
95                     str.append(elem);
96                 }
97             }
98             return str.toString();
99         }
100     }
101     
102     /**
103      * A stack used to maintain the current pattern. The Rules XML document
104      * type allows nesting of patterns. If an element defines a matching
105      * pattern, the resulting pattern is a concatenation of that pattern with
106      * all the ancestor elements' patterns. Hence the need for a stack.
107      */

108     protected PatternStack patternStack;
109     
110     /**
111      * Used to detect circular includes
112      */

113     private Set JavaDoc includedFiles = new HashSet JavaDoc();
114     
115     /**
116      * Constructs a DigesterRuleParser. This object will be inoperable
117      * until the target digester is set, via <code>setTarget(Digester)</code>
118      */

119     public DigesterRuleParser() {
120         patternStack = new PatternStack();
121     }
122     
123     /**
124      * Constructs a rule set for converting XML digester rule descriptions
125      * into Rule objects, and adding them to the given Digester
126      * @param targetDigester the Digester to add the rules to
127      */

128     public DigesterRuleParser(Digester targetDigester) {
129         this.targetDigester = targetDigester;
130         patternStack = new PatternStack();
131     }
132     
133     /**
134      * Constructs a rule set for parsing an XML digester rule file that
135      * has been included within an outer XML digester rule file. In this
136      * case, we must pass the pattern stack and the target digester
137      * to the rule set, as well as the list of files that have already
138      * been included, for cycle detection.
139      * @param targetDigester the Digester to add the rules to
140      * @param stack Stack containing the prefix pattern string to be prepended
141      * to any pattern parsed by this rule set.
142      */

143     private DigesterRuleParser(Digester targetDigester,
144                                 PatternStack stack, Set JavaDoc includedFiles) {
145         this.targetDigester = targetDigester;
146         patternStack = stack;
147         this.includedFiles = includedFiles;
148     }
149     
150     /**
151      * Sets the digester into which to add the parsed rules
152      * @param d the Digester to add the rules to
153      */

154     public void setTarget(Digester d) {
155         targetDigester = d;
156     }
157     
158     /**
159      * Set a base pattern beneath which all the rules loaded by this
160      * object will be registered. If this string is not empty, and does
161      * not end in a "/", then one will be added.
162      *
163      * @since 1.6
164      */

165     public void setBasePath(String JavaDoc path) {
166         if (path == null) {
167             basePath = "";
168         }
169         else if ((path.length() > 0) && !path.endsWith("/")) {
170             basePath = path + "/";
171         } else {
172             basePath = path;
173         }
174     }
175
176     /**
177      * Sets the location of the digester rules DTD. This is the DTD used
178      * to validate the rules XML file.
179      */

180     public void setDigesterRulesDTD(String JavaDoc dtdURL) {
181         digesterDtdUrl = dtdURL;
182     }
183     
184     /**
185      * Returns the location of the DTD used to validate the digester rules
186      * XML document.
187      */

188     protected String JavaDoc getDigesterRulesDTD() {
189         //ClassLoader classLoader = getClass().getClassLoader();
190
//URL url = classLoader.getResource(DIGESTER_DTD_PATH);
191
//return url.toString();
192
return digesterDtdUrl;
193     }
194     
195     /**
196      * Adds a rule the the target digester. After a rule has been created by
197      * parsing the XML, it is added to the digester by calling this method.
198      * Typically, this method is called via reflection, when executing
199      * a SetNextRule, from the Digester that is parsing the rules XML.
200      * @param rule a Rule to add to the target digester.
201      */

202     public void add(Rule rule) {
203         targetDigester.addRule(
204             basePath + patternStack.toString(), rule);
205     }
206     
207     
208     /**
209      * Add to the given digester the set of Rule instances used to parse an XML
210      * document defining Digester rules. When the digester parses an XML file,
211      * it will add the resulting rules & patterns to the 'target digester'
212      * that was passed in this RuleSet's constructor.<P>
213      * If you extend this class to support additional rules, your implementation
214      * should of this method should call this implementation first: i.e.
215      * <code>super.addRuleInstances(digester);</code>
216      */

217     public void addRuleInstances(Digester digester) {
218         final String JavaDoc ruleClassName = Rule.class.getName();
219         digester.register(DIGESTER_PUBLIC_ID, getDigesterRulesDTD());
220         
221         digester.addRule("*/pattern", new PatternRule("value"));
222         
223         digester.addRule("*/include", new IncludeRule());
224         
225         digester.addFactoryCreate("*/bean-property-setter-rule", new BeanPropertySetterRuleFactory());
226         digester.addRule("*/bean-property-setter-rule", new PatternRule("pattern"));
227         digester.addSetNext("*/bean-property-setter-rule", "add", ruleClassName);
228         
229         digester.addFactoryCreate("*/call-method-rule", new CallMethodRuleFactory());
230         digester.addRule("*/call-method-rule", new PatternRule("pattern"));
231         digester.addSetNext("*/call-method-rule", "add", ruleClassName);
232
233         digester.addFactoryCreate("*/object-param-rule", new ObjectParamRuleFactory());
234         digester.addRule("*/object-param-rule", new PatternRule("pattern"));
235         digester.addSetNext("*/object-param-rule", "add", ruleClassName);
236         
237         digester.addFactoryCreate("*/call-param-rule", new CallParamRuleFactory());
238         digester.addRule("*/call-param-rule", new PatternRule("pattern"));
239         digester.addSetNext("*/call-param-rule", "add", ruleClassName);
240         
241         digester.addFactoryCreate("*/factory-create-rule", new FactoryCreateRuleFactory());
242         digester.addRule("*/factory-create-rule", new PatternRule("pattern"));
243         digester.addSetNext("*/factory-create-rule", "add", ruleClassName);
244         
245         digester.addFactoryCreate("*/object-create-rule", new ObjectCreateRuleFactory());
246         digester.addRule("*/object-create-rule", new PatternRule("pattern"));
247         digester.addSetNext("*/object-create-rule", "add", ruleClassName);
248         
249         digester.addFactoryCreate("*/set-properties-rule", new SetPropertiesRuleFactory());
250         digester.addRule("*/set-properties-rule", new PatternRule("pattern"));
251         digester.addSetNext("*/set-properties-rule", "add", ruleClassName);
252         
253         digester.addRule("*/set-properties-rule/alias", new SetPropertiesAliasRule());
254         
255         digester.addFactoryCreate("*/set-property-rule", new SetPropertyRuleFactory());
256         digester.addRule("*/set-property-rule", new PatternRule("pattern"));
257         digester.addSetNext("*/set-property-rule", "add", ruleClassName);
258         
259         digester.addFactoryCreate("*/set-nested-properties-rule", new SetNestedPropertiesRuleFactory());
260         digester.addRule("*/set-nested-properties-rule", new PatternRule("pattern"));
261         digester.addSetNext("*/set-nested-properties-rule", "add", ruleClassName);
262         
263         digester.addRule("*/set-nested-properties-rule/alias", new SetNestedPropertiesAliasRule());
264         
265         digester.addFactoryCreate("*/set-top-rule", new SetTopRuleFactory());
266         digester.addRule("*/set-top-rule", new PatternRule("pattern"));
267         digester.addSetNext("*/set-top-rule", "add", ruleClassName);
268         
269         digester.addFactoryCreate("*/set-next-rule", new SetNextRuleFactory());
270         digester.addRule("*/set-next-rule", new PatternRule("pattern"));
271         digester.addSetNext("*/set-next-rule", "add", ruleClassName);
272         digester.addFactoryCreate("*/set-root-rule", new SetRootRuleFactory());
273         digester.addRule("*/set-root-rule", new PatternRule("pattern"));
274         digester.addSetNext("*/set-root-rule", "add", ruleClassName);
275     }
276     
277     
278     /**
279      * A rule for extracting the pattern matching strings from the rules XML.
280      * In the digester-rules document type, a pattern can either be declared
281      * in the 'value' attribute of a <pattern> element (in which case the pattern
282      * applies to all rules elements contained within the <pattern> element),
283      * or it can be declared in the optional 'pattern' attribute of a rule
284      * element.
285      */

286     private class PatternRule extends Rule {
287         
288         private String JavaDoc attrName;
289         private String JavaDoc pattern = null;
290         
291         /**
292          * @param attrName The name of the attribute containing the pattern
293          */

294         public PatternRule(String JavaDoc attrName) {
295             super();
296             this.attrName = attrName;
297         }
298         
299         /**
300          * If a pattern is defined for the attribute, push it onto the
301          * pattern stack.
302          */

303         public void begin(Attributes JavaDoc attributes) {
304             pattern = attributes.getValue(attrName);
305             if (pattern != null) {
306                 patternStack.push(pattern);
307             }
308         }
309         
310         /**
311          * If there was a pattern for this element, pop it off the pattern
312          * stack.
313          */

314         public void end() {
315             if (pattern != null) {
316                 patternStack.pop();
317             }
318         }
319     }
320     
321     /**
322      * A rule for including one rules XML file within another. Included files
323      * behave as if they are 'macro-expanded' within the includer. This means
324      * that the values of the pattern stack are prefixed to every pattern
325      * in the included rules. <p>This rule will detect 'circular' includes,
326      * which would result in infinite recursion. It throws a
327      * CircularIncludeException when a cycle is detected, which will terminate
328      * the parse.
329      */

330     private class IncludeRule extends Rule {
331         public IncludeRule() {
332             super();
333         }
334         
335         /**
336          * To include a rules xml file, we instantiate another Digester, and
337          * another DigesterRulesRuleSet. We pass the
338          * pattern stack and the target Digester to the new rule set, and
339          * tell the Digester to parse the file.
340          */

341         public void begin(Attributes JavaDoc attributes) throws Exception JavaDoc {
342             // The path attribute gives the URI to another digester rules xml file
343
String JavaDoc fileName = attributes.getValue("path");
344             if (fileName != null && fileName.length() > 0) {
345                 includeXMLRules(fileName);
346             }
347             
348             // The class attribute gives the name of a class that implements
349
// the DigesterRulesSource interface
350
String JavaDoc className = attributes.getValue("class");
351             if (className != null && className.length() > 0) {
352                 includeProgrammaticRules(className);
353             }
354         }
355         
356         /**
357          * Creates another DigesterRuleParser, and uses it to extract the rules
358          * out of the give XML file. The contents of the current pattern stack
359          * will be prepended to all of the pattern strings parsed from the file.
360          */

361         private void includeXMLRules(String JavaDoc fileName)
362                         throws IOException JavaDoc, SAXException JavaDoc, CircularIncludeException {
363             ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
364             if (cl == null) {
365                 cl = DigesterRuleParser.this.getClass().getClassLoader();
366             }
367             URL JavaDoc fileURL = cl.getResource(fileName);
368             if (fileURL == null) {
369                 throw new FileNotFoundException JavaDoc("File \"" + fileName + "\" not found.");
370             }
371             fileName = fileURL.toExternalForm();
372             if (includedFiles.add(fileName) == false) {
373                 // circular include detected
374
throw new CircularIncludeException(fileName);
375             }
376             // parse the included xml file
377
DigesterRuleParser includedSet =
378                         new DigesterRuleParser(targetDigester, patternStack, includedFiles);
379             includedSet.setDigesterRulesDTD(getDigesterRulesDTD());
380             Digester digester = new Digester();
381             digester.addRuleSet(includedSet);
382             digester.push(DigesterRuleParser.this);
383             digester.parse(fileName);
384             includedFiles.remove(fileName);
385         }
386         
387         /**
388          * Creates an instance of the indicated class. The class must implement
389          * the DigesterRulesSource interface. Passes the target digester to
390          * that instance. The DigesterRulesSource instance is supposed to add
391          * rules into the digester. The contents of the current pattern stack
392          * will be automatically prepended to all of the pattern strings added
393          * by the DigesterRulesSource instance.
394          */

395         private void includeProgrammaticRules(String JavaDoc className)
396                         throws ClassNotFoundException JavaDoc, ClassCastException JavaDoc,
397                         InstantiationException JavaDoc, IllegalAccessException JavaDoc {
398             
399             Class JavaDoc cls = Class.forName(className);
400             DigesterRulesSource rulesSource = (DigesterRulesSource) cls.newInstance();
401             
402             // wrap the digester's Rules object, to prepend pattern
403
Rules digesterRules = targetDigester.getRules();
404             Rules prefixWrapper =
405                     new RulesPrefixAdapter(patternStack.toString(), digesterRules);
406             
407             targetDigester.setRules(prefixWrapper);
408             try {
409                 rulesSource.getRules(targetDigester);
410             } finally {
411                 // Put the unwrapped rules back
412
targetDigester.setRules(digesterRules);
413             }
414         }
415     }
416     
417     
418     /**
419      * Wraps a Rules object. Delegates all the Rules interface methods
420      * to the underlying Rules object. Overrides the add method to prepend
421      * a prefix to the pattern string.
422      */

423     private class RulesPrefixAdapter implements Rules {
424         
425         private Rules delegate;
426         private String JavaDoc prefix;
427         
428         /**
429          * @param patternPrefix the pattern string to prepend to the pattern
430          * passed to the add method.
431          * @param rules The wrapped Rules object. All of this class's methods
432          * pass through to this object.
433          */

434         public RulesPrefixAdapter(String JavaDoc patternPrefix, Rules rules) {
435             prefix = patternPrefix;
436             delegate = rules;
437         }
438         
439         /**
440          * Register a new Rule instance matching a pattern which is constructed
441          * by concatenating the pattern prefix with the given pattern.
442          */

443         public void add(String JavaDoc pattern, Rule rule) {
444             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
445             buffer.append(prefix);
446             if (!pattern.startsWith("/")) {
447                 buffer.append('/');
448             }
449             buffer.append(pattern);
450             delegate.add(buffer.toString(), rule);
451         }
452         
453         /**
454          * This method passes through to the underlying Rules object.
455          */

456         public void clear() {
457             delegate.clear();
458         }
459         
460         /**
461          * This method passes through to the underlying Rules object.
462          */

463         public Digester getDigester() {
464             return delegate.getDigester();
465         }
466         
467         /**
468          * This method passes through to the underlying Rules object.
469          */

470         public String JavaDoc getNamespaceURI() {
471             return delegate.getNamespaceURI();
472         }
473         
474         /**
475          * @deprecated Call match(namespaceURI,pattern) instead.
476          */

477         public List JavaDoc match(String JavaDoc pattern) {
478             return delegate.match(pattern);
479         }
480         
481         /**
482          * This method passes through to the underlying Rules object.
483          */

484         public List JavaDoc match(String JavaDoc namespaceURI, String JavaDoc pattern) {
485             return delegate.match(namespaceURI, pattern);
486         }
487         
488         /**
489          * This method passes through to the underlying Rules object.
490          */

491         public List JavaDoc rules() {
492             return delegate.rules();
493         }
494         
495         /**
496          * This method passes through to the underlying Rules object.
497          */

498         public void setDigester(Digester digester) {
499             delegate.setDigester(digester);
500         }
501         
502         /**
503          * This method passes through to the underlying Rules object.
504          */

505         public void setNamespaceURI(String JavaDoc namespaceURI) {
506             delegate.setNamespaceURI(namespaceURI);
507         }
508     }
509     
510     
511     ///////////////////////////////////////////////////////////////////////
512
// Classes beyond this point are ObjectCreationFactory implementations,
513
// used to create Rule objects and initialize them from SAX attributes.
514
///////////////////////////////////////////////////////////////////////
515

516     /**
517      * Factory for creating a BeanPropertySetterRule.
518      */

519     private class BeanPropertySetterRuleFactory extends AbstractObjectCreationFactory {
520         public Object JavaDoc createObject(Attributes JavaDoc attributes) throws Exception JavaDoc {
521             Rule beanPropertySetterRule = null;
522             String JavaDoc propertyname = attributes.getValue("propertyname");
523                 
524             if (propertyname == null) {
525                 // call the setter method corresponding to the element name.
526
beanPropertySetterRule = new BeanPropertySetterRule();
527             } else {
528                 beanPropertySetterRule = new BeanPropertySetterRule(propertyname);
529             }
530             
531             return beanPropertySetterRule;
532         }
533         
534     }
535
536     /**
537      * Factory for creating a CallMethodRule.
538      */

539     protected class CallMethodRuleFactory extends AbstractObjectCreationFactory {
540         public Object JavaDoc createObject(Attributes JavaDoc attributes) {
541             Rule callMethodRule = null;
542             String JavaDoc methodName = attributes.getValue("methodname");
543
544             // Select which element is to be the target. Default to zero,
545
// ie the top object on the stack.
546
int targetOffset = 0;
547             String JavaDoc targetOffsetStr = attributes.getValue("targetoffset");
548             if (targetOffsetStr != null) {
549                 targetOffset = Integer.parseInt(targetOffsetStr);
550             }
551
552             if (attributes.getValue("paramcount") == null) {
553                 // call against empty method
554
callMethodRule = new CallMethodRule(targetOffset, methodName);
555             
556             } else {
557                 int paramCount = Integer.parseInt(attributes.getValue("paramcount"));
558                 
559                 String JavaDoc paramTypesAttr = attributes.getValue("paramtypes");
560                 if (paramTypesAttr == null || paramTypesAttr.length() == 0) {
561                     callMethodRule = new CallMethodRule(targetOffset, methodName, paramCount);
562                 } else {
563                     String JavaDoc[] paramTypes = getParamTypes(paramTypesAttr);
564                     callMethodRule = new CallMethodRule(
565                         targetOffset, methodName, paramCount, paramTypes);
566                 }
567             }
568             return callMethodRule;
569         }
570
571         /**
572          * Process the comma separated list of paramTypes
573          * into an array of String class names
574          */

575         private String JavaDoc[] getParamTypes(String JavaDoc paramTypes) {
576             String JavaDoc[] paramTypesArray;
577             if( paramTypes != null ) {
578                 ArrayList JavaDoc paramTypesList = new ArrayList JavaDoc();
579                 StringTokenizer JavaDoc tokens = new StringTokenizer JavaDoc(
580                         paramTypes, " \t\n\r,");
581                 while (tokens.hasMoreTokens()) {
582                     paramTypesList.add(tokens.nextToken());
583                 }
584                 paramTypesArray = (String JavaDoc[])paramTypesList.toArray(new String JavaDoc[0]);
585             } else {
586                 paramTypesArray = new String JavaDoc[0];
587             }
588             return paramTypesArray;
589         }
590     }
591     
592     /**
593      * Factory for creating a CallParamRule.
594      */

595     protected class CallParamRuleFactory extends AbstractObjectCreationFactory {
596     
597         public Object JavaDoc createObject(Attributes JavaDoc attributes) {
598             // create callparamrule
599
int paramIndex = Integer.parseInt(attributes.getValue("paramnumber"));
600             String JavaDoc attributeName = attributes.getValue("attrname");
601             String JavaDoc fromStack = attributes.getValue("from-stack");
602             String JavaDoc stackIndex = attributes.getValue("stack-index");
603             Rule callParamRule = null;
604
605             if (attributeName == null) {
606                 if (stackIndex != null) {
607                     callParamRule = new CallParamRule(
608                         paramIndex, Integer.parseInt(stackIndex));
609                 } else if (fromStack != null) {
610                     callParamRule = new CallParamRule(
611                         paramIndex, Boolean.valueOf(fromStack).booleanValue());
612                 } else {
613                     callParamRule = new CallParamRule(paramIndex);
614                 }
615             } else {
616                 if (fromStack == null) {
617                     callParamRule = new CallParamRule(paramIndex, attributeName);
618                 } else {
619                     // specifying both from-stack and attribute name is not allowed
620
throw new RuntimeException JavaDoc(
621                         "Attributes from-stack and attrname cannot both be present.");
622                 }
623             }
624             return callParamRule;
625         }
626     }
627     
628     /**
629      * Factory for creating a ObjectParamRule
630      */

631     protected class ObjectParamRuleFactory extends AbstractObjectCreationFactory {
632         public Object JavaDoc createObject(Attributes JavaDoc attributes) throws Exception JavaDoc {
633             // create callparamrule
634
int paramIndex = Integer.parseInt(attributes.getValue("paramnumber"));
635             String JavaDoc attributeName = attributes.getValue("attrname");
636             String JavaDoc type = attributes.getValue("type");
637             String JavaDoc value = attributes.getValue("value");
638
639             Rule objectParamRule = null;
640
641             // type name is requried
642
if (type == null) {
643                 throw new RuntimeException JavaDoc("Attribute 'type' is required.");
644             }
645
646             // create object instance
647
Object JavaDoc param = null;
648             Class JavaDoc clazz = Class.forName(type);
649             if (value == null) {
650                 param = clazz.newInstance();
651             } else {
652                 param = ConvertUtils.convert(value, clazz);
653             }
654
655             if (attributeName == null) {
656                 objectParamRule = new ObjectParamRule(paramIndex, param);
657             } else {
658                 objectParamRule = new ObjectParamRule(paramIndex, attributeName, param);
659             }
660             return objectParamRule;
661         }
662      }
663
664     
665     /**
666      * Factory for creating a FactoryCreateRule
667      */

668     protected class FactoryCreateRuleFactory extends AbstractObjectCreationFactory {
669         public Object JavaDoc createObject(Attributes JavaDoc attributes) {
670             String JavaDoc className = attributes.getValue("classname");
671             String JavaDoc attrName = attributes.getValue("attrname");
672             boolean ignoreExceptions =
673                 "true".equalsIgnoreCase(attributes.getValue("ignore-exceptions"));
674             return (attrName == null || attrName.length() == 0) ?
675                 new FactoryCreateRule( className, ignoreExceptions) :
676                 new FactoryCreateRule( className, attrName, ignoreExceptions);
677         }
678     }
679     
680     /**
681      * Factory for creating a ObjectCreateRule
682      */

683     protected class ObjectCreateRuleFactory extends AbstractObjectCreationFactory {
684         public Object JavaDoc createObject(Attributes JavaDoc attributes) {
685             String JavaDoc className = attributes.getValue("classname");
686             String JavaDoc attrName = attributes.getValue("attrname");
687             return (attrName == null || attrName.length() == 0) ?
688                 new ObjectCreateRule( className) :
689                 new ObjectCreateRule( className, attrName);
690         }
691     }
692     
693     /**
694      * Factory for creating a SetPropertiesRule
695      */

696     protected class SetPropertiesRuleFactory extends AbstractObjectCreationFactory {
697         public Object JavaDoc createObject(Attributes JavaDoc attributes) {
698                 return new SetPropertiesRule();
699         }
700     }
701     
702     /**
703      * Factory for creating a SetPropertyRule
704      */

705     protected class SetPropertyRuleFactory extends AbstractObjectCreationFactory {
706         public Object JavaDoc createObject(Attributes JavaDoc attributes) {
707             String JavaDoc name = attributes.getValue("name");
708             String JavaDoc value = attributes.getValue("value");
709             return new SetPropertyRule( name, value);
710         }
711     }
712     
713     /**
714      * Factory for creating a SetNestedPropertiesRule
715      */

716     protected class SetNestedPropertiesRuleFactory extends AbstractObjectCreationFactory {
717         public Object JavaDoc createObject(Attributes JavaDoc attributes) {
718            boolean allowUnknownChildElements =
719                 "true".equalsIgnoreCase(attributes.getValue("allow-unknown-child-elements"));
720                 SetNestedPropertiesRule snpr = new SetNestedPropertiesRule();
721                 snpr.setAllowUnknownChildElements( allowUnknownChildElements );
722                 return snpr;
723         }
724     }
725     
726     /**
727      * Factory for creating a SetTopRuleFactory
728      */

729     protected class SetTopRuleFactory extends AbstractObjectCreationFactory {
730         public Object JavaDoc createObject(Attributes JavaDoc attributes) {
731             String JavaDoc methodName = attributes.getValue("methodname");
732             String JavaDoc paramType = attributes.getValue("paramtype");
733             return (paramType == null || paramType.length() == 0) ?
734                 new SetTopRule( methodName) :
735                 new SetTopRule( methodName, paramType);
736         }
737     }
738     
739     /**
740      * Factory for creating a SetNextRuleFactory
741      */

742     protected class SetNextRuleFactory extends AbstractObjectCreationFactory {
743         public Object JavaDoc createObject(Attributes JavaDoc attributes) {
744             String JavaDoc methodName = attributes.getValue("methodname");
745             String JavaDoc paramType = attributes.getValue("paramtype");
746             return (paramType == null || paramType.length() == 0) ?
747                 new SetNextRule( methodName) :
748                 new SetNextRule( methodName, paramType);
749         }
750     }
751     
752     /**
753      * Factory for creating a SetRootRuleFactory
754      */

755     protected class SetRootRuleFactory extends AbstractObjectCreationFactory {
756         public Object JavaDoc createObject(Attributes JavaDoc attributes) {
757             String JavaDoc methodName = attributes.getValue("methodname");
758             String JavaDoc paramType = attributes.getValue("paramtype");
759             return (paramType == null || paramType.length() == 0) ?
760                 new SetRootRule( methodName) :
761                 new SetRootRule( methodName, paramType);
762         }
763     }
764     
765     /**
766      * A rule for adding a attribute-property alias to the custom alias mappings of
767      * the containing SetPropertiesRule rule.
768      */

769     protected class SetPropertiesAliasRule extends Rule {
770         
771         /**
772          * <p>Base constructor.</p>
773          */

774         public SetPropertiesAliasRule() {
775             super();
776         }
777         
778         /**
779          * Add the alias to the SetPropertiesRule object created by the
780          * enclosing <set-properties-rule> tag.
781          */

782         public void begin(Attributes JavaDoc attributes) {
783             String JavaDoc attrName = attributes.getValue("attr-name");
784             String JavaDoc propName = attributes.getValue("prop-name");
785     
786             SetPropertiesRule rule = (SetPropertiesRule) digester.peek();
787             rule.addAlias(attrName, propName);
788         }
789     }
790
791     /**
792      * A rule for adding a attribute-property alias to the custom alias mappings of
793      * the containing SetNestedPropertiesRule rule.
794      */

795     protected class SetNestedPropertiesAliasRule extends Rule {
796         
797         /**
798          * <p>Base constructor.</p>
799          */

800         public SetNestedPropertiesAliasRule() {
801             super();
802         }
803         
804         /**
805          * Add the alias to the SetNestedPropertiesRule object created by the
806          * enclosing <set-nested-properties-rule> tag.
807          */

808         public void begin(Attributes JavaDoc attributes) {
809             String JavaDoc attrName = attributes.getValue("attr-name");
810             String JavaDoc propName = attributes.getValue("prop-name");
811     
812             SetNestedPropertiesRule rule = (SetNestedPropertiesRule) digester.peek();
813             rule.addAlias(attrName, propName);
814         }
815     }
816         
817 }
818
Popular Tags