1 package org.apache.commons.digester3.binder;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import static org.w3c.dom.Node.ATTRIBUTE_NODE;
23 import static org.w3c.dom.Node.CDATA_SECTION_NODE;
24 import static org.w3c.dom.Node.COMMENT_NODE;
25 import static org.w3c.dom.Node.DOCUMENT_FRAGMENT_NODE;
26 import static org.w3c.dom.Node.DOCUMENT_NODE;
27 import static org.w3c.dom.Node.DOCUMENT_TYPE_NODE;
28 import static org.w3c.dom.Node.ELEMENT_NODE;
29 import static org.w3c.dom.Node.ENTITY_NODE;
30 import static org.w3c.dom.Node.ENTITY_REFERENCE_NODE;
31 import static org.w3c.dom.Node.NOTATION_NODE;
32 import static org.w3c.dom.Node.PROCESSING_INSTRUCTION_NODE;
33 import static org.w3c.dom.Node.TEXT_NODE;
34
35 import javax.xml.parsers.DocumentBuilder;
36 import javax.xml.parsers.ParserConfigurationException;
37
38 import org.apache.commons.digester3.NodeCreateRule;
39
40
41
42
43
44
45 public final class NodeCreateRuleProvider
46 extends AbstractBackToLinkedRuleBuilder<NodeCreateRule>
47 {
48
49 private NodeType nodeType = NodeType.ELEMENT;
50
51 private DocumentBuilder documentBuilder;
52
53 NodeCreateRuleProvider( String keyPattern, String namespaceURI, RulesBinder mainBinder,
54 LinkedRuleBuilder mainBuilder )
55 {
56 super( keyPattern, namespaceURI, mainBinder, mainBuilder );
57 }
58
59
60
61
62
63
64
65
66
67
68
69 public NodeCreateRuleProvider ofType( NodeType nodeType )
70 {
71 if ( nodeType == null )
72 {
73 reportError( "createNode().ofType( NodeType )", "Null NodeType not allowed" );
74 }
75
76 this.nodeType = nodeType;
77 return this;
78 }
79
80
81
82
83
84
85
86
87 public NodeCreateRuleProvider usingDocumentBuilder( DocumentBuilder documentBuilder )
88 {
89 this.documentBuilder = documentBuilder;
90 return this;
91 }
92
93
94
95
96 @Override
97 protected NodeCreateRule createRule()
98 {
99 if ( documentBuilder == null )
100 {
101 try
102 {
103 return new NodeCreateRule( nodeType.getDocumentType() );
104 }
105 catch ( ParserConfigurationException e )
106 {
107 throw new RuntimeException( e );
108 }
109 }
110
111 return new NodeCreateRule( nodeType.getDocumentType(), documentBuilder );
112 }
113
114
115
116
117 public enum NodeType
118 {
119
120
121
122
123 ATTRIBUTE( ATTRIBUTE_NODE ),
124
125
126
127 CDATA( CDATA_SECTION_NODE ),
128
129
130
131 COMMENT( COMMENT_NODE ),
132
133
134
135 DOCUMENT_FRAGMENT( DOCUMENT_FRAGMENT_NODE ),
136
137
138
139 DOCUMENT( DOCUMENT_NODE ),
140
141
142
143 DOCUMENT_TYPE( DOCUMENT_TYPE_NODE ),
144
145
146
147 ELEMENT( ELEMENT_NODE ),
148
149
150
151 ENTITY( ENTITY_NODE ),
152
153
154
155 ENTITY_REFERENCE( ENTITY_REFERENCE_NODE ),
156
157
158
159 NOTATION( NOTATION_NODE ),
160
161
162
163 PROCESSING_INSTRUCTION( PROCESSING_INSTRUCTION_NODE ),
164
165
166
167 TEXT( TEXT_NODE );
168
169 private final int documentType;
170
171 private NodeType( final int documentType )
172 {
173 this.documentType = documentType;
174 }
175
176 private int getDocumentType()
177 {
178 return documentType;
179 }
180
181 }
182
183 }