1 |
|
package org.apache.commons.betwixt.digester; |
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
import java.beans.PropertyDescriptor; |
20 |
|
import java.lang.reflect.Method; |
21 |
|
|
22 |
|
import org.apache.commons.betwixt.ElementDescriptor; |
23 |
|
import org.apache.commons.betwixt.TextDescriptor; |
24 |
|
import org.apache.commons.betwixt.XMLBeanInfo; |
25 |
|
import org.apache.commons.betwixt.expression.ConstantExpression; |
26 |
|
import org.apache.commons.betwixt.expression.MethodExpression; |
27 |
|
import org.apache.commons.betwixt.expression.MethodUpdater; |
28 |
|
import org.apache.commons.logging.Log; |
29 |
|
import org.apache.commons.logging.LogFactory; |
30 |
|
import org.xml.sax.Attributes; |
31 |
|
import org.xml.sax.SAXException; |
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
|
45 |
604 |
public class TextRule extends MappedPropertyRule { |
46 |
|
|
47 |
|
|
48 |
604 |
private static final Log log = LogFactory.getLog( TextRule.class ); |
49 |
|
|
50 |
1624 |
public TextRule() {} |
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
public void begin(String name, String namespace, Attributes attributes) throws SAXException { |
64 |
|
|
65 |
78 |
TextDescriptor descriptor = new TextDescriptor(); |
66 |
|
|
67 |
78 |
String value = attributes.getValue( "value" ); |
68 |
78 |
String propertyName = attributes.getValue( "property" ); |
69 |
78 |
String propertyType = attributes.getValue( "type" ); |
70 |
|
|
71 |
78 |
if ( value != null) { |
72 |
13 |
if ( propertyName != null || propertyType != null ) { |
73 |
|
|
74 |
0 |
throw new SAXException( |
75 |
0 |
"You cannot specify attribute 'value' together with either " |
76 |
|
+ " the 'property' or 'type' attributes"); |
77 |
|
} |
78 |
|
|
79 |
13 |
descriptor.setTextExpression( new ConstantExpression( value ) ); |
80 |
|
|
81 |
|
} else { |
82 |
|
|
83 |
65 |
descriptor.setPropertyName( propertyName ); |
84 |
|
|
85 |
65 |
Class beanClass = getBeanClass(); |
86 |
|
|
87 |
|
|
88 |
130 |
descriptor.setPropertyType( |
89 |
65 |
getPropertyType( propertyType, beanClass, propertyName ) |
90 |
|
); |
91 |
|
|
92 |
65 |
if ( beanClass != null ) { |
93 |
65 |
String descriptorPropertyName = descriptor.getPropertyName(); |
94 |
65 |
PropertyDescriptor propertyDescriptor = |
95 |
65 |
getPropertyDescriptor( beanClass, descriptorPropertyName ); |
96 |
65 |
if ( propertyDescriptor != null ) { |
97 |
65 |
Method readMethod = propertyDescriptor.getReadMethod(); |
98 |
65 |
descriptor.setTextExpression( new MethodExpression( readMethod ) ); |
99 |
65 |
Method writeMethod = propertyDescriptor.getWriteMethod(); |
100 |
65 |
if (writeMethod != null) { |
101 |
65 |
descriptor.setUpdater( new MethodUpdater(writeMethod)); |
102 |
|
} |
103 |
65 |
getProcessedPropertyNameSet().add( descriptorPropertyName ); |
104 |
|
} |
105 |
|
} |
106 |
|
} |
107 |
|
|
108 |
78 |
Object top = digester.peek(); |
109 |
78 |
if ( top instanceof XMLBeanInfo ) { |
110 |
0 |
XMLBeanInfo beanInfo = (XMLBeanInfo) top; |
111 |
0 |
ElementDescriptor elementDescriptor = beanInfo.getElementDescriptor(); |
112 |
0 |
if (elementDescriptor != null) { |
113 |
0 |
elementDescriptor.addContentDescriptor( descriptor ); |
114 |
|
} |
115 |
|
|
116 |
78 |
} else if ( top instanceof ElementDescriptor ) { |
117 |
78 |
ElementDescriptor parent = (ElementDescriptor) top; |
118 |
78 |
parent.addContentDescriptor( descriptor ); |
119 |
|
|
120 |
|
} else { |
121 |
0 |
throw new SAXException( "Invalid use of <text>. It should " |
122 |
|
+ "be nested <text> nodes" ); |
123 |
|
} |
124 |
78 |
} |
125 |
|
} |