1 package org.apache.commons.digester3.binder;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
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 * Builder chained when invoking {@link LinkedRuleBuilder#createNode()}.
42 *
43 * @since 3.0
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 * {@link NodeCreateRule} instance will be created either a DOM {@link org.w3c.dom.Element Element}
61 * or a DOM {@link org.w3c.dom.DocumentFragment DocumentFragment}, depending on the value of the
62 * <code>nodeType</code> parameter.
63 *
64 * @param nodeType the type of node to create, which can be either
65 * {@link org.w3c.dom.Node#ELEMENT_NODE Node.ELEMENT_NODE} or
66 * {@link org.w3c.dom.Node#DOCUMENT_FRAGMENT_NODE Node.DOCUMENT_FRAGMENT_NODE}
67 * @return this builder instance
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 * {@link NodeCreateRule} instance will be created a DOM {@link org.w3c.dom.Element Element}, but
82 * lets users specify the JAXP <code>DocumentBuilder</code> that should be used when constructing the node tree.
83 *
84 * @param documentBuilder the JAXP <code>DocumentBuilder</code> to use
85 * @return this builder instance
86 */
87 public NodeCreateRuleProvider usingDocumentBuilder( DocumentBuilder documentBuilder )
88 {
89 this.documentBuilder = documentBuilder;
90 return this;
91 }
92
93 /**
94 * {@inheritDoc}
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 * Enumeration that wraps admitted {@link org.w3c.dom.Node} node constants.
116 */
117 public enum NodeType
118 {
119
120 /**
121 * @see org.w3c.dom.Node#ATTRIBUTE_NODE
122 */
123 ATTRIBUTE( ATTRIBUTE_NODE ),
124 /**
125 * @see org.w3c.dom.Node#CDATA_SECTION_NODE
126 */
127 CDATA( CDATA_SECTION_NODE ),
128 /**
129 * @see org.w3c.dom.Node#COMMENT_NODE
130 */
131 COMMENT( COMMENT_NODE ),
132 /**
133 * @see org.w3c.dom.Node#DOCUMENT_FRAGMENT_NODE
134 */
135 DOCUMENT_FRAGMENT( DOCUMENT_FRAGMENT_NODE ),
136 /**
137 * @see org.w3c.dom.Node#DOCUMENT_NODE
138 */
139 DOCUMENT( DOCUMENT_NODE ),
140 /**
141 * @see org.w3c.dom.Node#DOCUMENT_TYPE_NODE
142 */
143 DOCUMENT_TYPE( DOCUMENT_TYPE_NODE ),
144 /**
145 * @see org.w3c.dom.Node#ELEMENT_NODE
146 */
147 ELEMENT( ELEMENT_NODE ),
148 /**
149 * @see org.w3c.dom.Node#ENTITY_NODE
150 */
151 ENTITY( ENTITY_NODE ),
152 /**
153 * @see org.w3c.dom.Node#ENTITY_REFERENCE_NODE
154 */
155 ENTITY_REFERENCE( ENTITY_REFERENCE_NODE ),
156 /**
157 * @see org.w3c.dom.Node#NOTATION_NODE
158 */
159 NOTATION( NOTATION_NODE ),
160 /**
161 * @see org.w3c.dom.Node#PROCESSING_INSTRUCTION_NODE
162 */
163 PROCESSING_INSTRUCTION( PROCESSING_INSTRUCTION_NODE ),
164 /**
165 * @see org.w3c.dom.Node#TEXT_NODE
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 }