1 package org.apache.commons.digester3.xmlrules;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import static java.util.Collections.unmodifiableSet;
23 import static org.apache.commons.digester3.binder.DigesterLoader.newLoader;
24
25 import java.io.File;
26 import java.io.InputStream;
27 import java.io.Reader;
28 import java.io.StringReader;
29 import java.net.MalformedURLException;
30 import java.net.URL;
31 import java.net.URLConnection;
32 import java.util.HashSet;
33 import java.util.Set;
34
35 import org.apache.commons.digester3.Digester;
36 import org.apache.commons.digester3.binder.AbstractRulesModule;
37 import org.xml.sax.InputSource;
38
39
40
41
42
43
44
45 public abstract class FromXmlRulesModule
46 extends AbstractRulesModule
47 {
48
49 private static final String DIGESTER_PUBLIC_ID = "-//Apache Commons //DTD digester-rules XML V1.0//EN";
50
51 private static final String DIGESTER_DTD_PATH = "digester-rules.dtd";
52
53 private final URL xmlRulesDtdUrl = FromXmlRulesModule.class.getResource( DIGESTER_DTD_PATH );
54
55 private final Set<String> systemIds = new HashSet<String>();
56
57 private String rootPath;
58
59
60
61
62 @Override
63 protected void configure()
64 {
65 if ( !systemIds.isEmpty() )
66 {
67 throw new IllegalStateException( "Re-entry is not allowed." );
68 }
69
70 try
71 {
72 loadRules();
73 }
74 finally
75 {
76 systemIds.clear();
77 }
78 }
79
80
81
82
83 protected abstract void loadRules();
84
85
86
87
88
89
90 protected final void loadXMLRules( InputSource inputSource )
91 {
92 if ( inputSource == null )
93 {
94 throw new IllegalArgumentException( "Argument 'inputSource' must be not null" );
95 }
96
97 String systemId = inputSource.getSystemId();
98 if ( systemId != null && !systemIds.add( systemId ) )
99 {
100 addError( "XML rules file '%s' already bound", systemId );
101 }
102
103 XmlRulesModule xmlRulesModule = new XmlRulesModule( new NameSpaceURIRulesBinder( rulesBinder() ),
104 getSystemIds(), rootPath );
105 Digester digester = newLoader( xmlRulesModule )
106 .register( DIGESTER_PUBLIC_ID, xmlRulesDtdUrl.toString() )
107 .setXIncludeAware( true )
108 .setValidating( true )
109 .newDigester();
110
111 try
112 {
113 digester.parse( inputSource );
114 }
115 catch ( Exception e )
116 {
117 addError( "Impossible to load XML defined in the InputSource '%s': %s", inputSource.getSystemId(),
118 e.getMessage() );
119 }
120 }
121
122
123
124
125
126
127 protected final void loadXMLRules( InputStream input )
128 {
129 if ( input == null )
130 {
131 throw new IllegalArgumentException( "Argument 'input' must be not null" );
132 }
133
134 loadXMLRules( new InputSource( input ) );
135 }
136
137
138
139
140
141
142 protected final void loadXMLRules( Reader reader )
143 {
144 if ( reader == null )
145 {
146 throw new IllegalArgumentException( "Argument 'input' must be not null" );
147 }
148
149 loadXMLRules( new InputSource( reader ) );
150 }
151
152
153
154
155
156
157 protected final void loadXMLRules( File file )
158 {
159 if ( file == null )
160 {
161 throw new IllegalArgumentException( "Argument 'input' must be not null" );
162 }
163
164 try
165 {
166 loadXMLRules( file.toURI().toURL() );
167 }
168 catch ( MalformedURLException e )
169 {
170 rulesBinder().addError( e );
171 }
172 }
173
174
175
176
177
178
179 protected final void loadXMLRules( String uri )
180 {
181 if ( uri == null )
182 {
183 throw new IllegalArgumentException( "Argument 'uri' must be not null" );
184 }
185
186 try
187 {
188 loadXMLRules( new URL( uri ) );
189 }
190 catch ( MalformedURLException e )
191 {
192 rulesBinder().addError( e );
193 }
194 }
195
196
197
198
199
200
201 protected final void loadXMLRules( URL url )
202 {
203 if ( url == null )
204 {
205 throw new IllegalArgumentException( "Argument 'url' must be not null" );
206 }
207
208 try
209 {
210 URLConnection connection = url.openConnection();
211 connection.setUseCaches( false );
212 InputStream stream = connection.getInputStream();
213 InputSource source = new InputSource( stream );
214 source.setSystemId( url.toExternalForm() );
215
216 loadXMLRules( source );
217 }
218 catch ( Exception e )
219 {
220 rulesBinder().addError( e );
221 }
222 }
223
224
225
226
227
228
229 protected final void loadXMLRulesFromText( String xmlText )
230 {
231 if ( xmlText == null )
232 {
233 throw new IllegalArgumentException( "Argument 'xmlText' must be not null" );
234 }
235
236 loadXMLRules( new StringReader( xmlText ) );
237 }
238
239
240
241
242
243
244 protected final void useRootPath( String rootPath )
245 {
246 this.rootPath = rootPath;
247 }
248
249
250
251
252
253
254 public final Set<String> getSystemIds()
255 {
256 return unmodifiableSet( systemIds );
257 }
258
259 }