|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
ConfigurationUtil.java | 100% | 100% | 100% | 100% |
|
1 |
/*
|
|
2 |
* Copyright (C) The JContainer Group. All rights reserved.
|
|
3 |
*
|
|
4 |
* This software is published under the terms of the JContainer
|
|
5 |
* Software License version 1.1, a copy of which has been included
|
|
6 |
* with this distribution in the LICENSE.txt file.
|
|
7 |
*/
|
|
8 |
package org.jcontainer.dna.impl;
|
|
9 |
|
|
10 |
import java.util.Properties;
|
|
11 |
import javax.xml.parsers.DocumentBuilder;
|
|
12 |
import javax.xml.parsers.DocumentBuilderFactory;
|
|
13 |
import javax.xml.parsers.SAXParser;
|
|
14 |
import javax.xml.parsers.SAXParserFactory;
|
|
15 |
import javax.xml.transform.OutputKeys;
|
|
16 |
import javax.xml.transform.Result;
|
|
17 |
import javax.xml.transform.TransformerFactory;
|
|
18 |
import javax.xml.transform.sax.SAXTransformerFactory;
|
|
19 |
import javax.xml.transform.sax.TransformerHandler;
|
|
20 |
import org.jcontainer.dna.Configuration;
|
|
21 |
import org.w3c.dom.Document;
|
|
22 |
import org.w3c.dom.Element;
|
|
23 |
import org.w3c.dom.NamedNodeMap;
|
|
24 |
import org.w3c.dom.Node;
|
|
25 |
import org.w3c.dom.NodeList;
|
|
26 |
import org.w3c.dom.Text;
|
|
27 |
import org.xml.sax.InputSource;
|
|
28 |
|
|
29 |
/**
|
|
30 |
* Class containing utility methods to work with Configuration
|
|
31 |
* objects.
|
|
32 |
*
|
|
33 |
* @version $Revision: 1.15 $ $Date: 2003/10/05 10:25:26 $
|
|
34 |
*/
|
|
35 |
public class ConfigurationUtil |
|
36 |
{ |
|
37 |
/**
|
|
38 |
* Constant defining separator for paths in document.
|
|
39 |
*/
|
|
40 |
public static final String PATH_SEPARATOR = "/"; |
|
41 |
|
|
42 |
/**
|
|
43 |
* Constant defining root path of document.
|
|
44 |
*/
|
|
45 |
public static final String ROOT_PATH = ""; |
|
46 |
|
|
47 |
/**
|
|
48 |
* Constant indicating location was generated from DOM
|
|
49 |
* Element.
|
|
50 |
*/
|
|
51 |
private static final String ELEMENT_LOCATION = "dom-gen"; |
|
52 |
|
|
53 |
/**
|
|
54 |
* Serialize Configuration object to sepcified Result object.
|
|
55 |
* The developer can serialize to a system out by using
|
|
56 |
* {@link javax.xml.transform.stream.StreamResult} in code
|
|
57 |
* such as;
|
|
58 |
*
|
|
59 |
* <pre>
|
|
60 |
* ConfigurationUtil.
|
|
61 |
* serializeToResult( new StreamResult( System.out ),
|
|
62 |
* configuration );
|
|
63 |
* </pre>
|
|
64 |
*
|
|
65 |
* <p>The developer can also output to SAX stream or DOM trees
|
|
66 |
* via {@link javax.xml.transform.sax.SAXResult} and
|
|
67 |
* {@link javax.xml.transform.dom.DOMResult}.</p>
|
|
68 |
*
|
|
69 |
* @param result the result object to serialize configuration to
|
|
70 |
* @param configuration the configuration
|
|
71 |
* @throws Exception if unable to serialize configuration
|
|
72 |
*/
|
|
73 | 2 |
public static void serializeToResult( final Result result, |
74 |
final Configuration configuration ) |
|
75 |
throws Exception
|
|
76 |
{ |
|
77 | 2 |
final SAXTransformerFactory factory = |
78 |
(SAXTransformerFactory)TransformerFactory.newInstance(); |
|
79 | 2 |
final TransformerHandler handler = factory.newTransformerHandler(); |
80 |
|
|
81 | 2 |
final Properties format = new Properties();
|
82 | 2 |
format.put( OutputKeys.METHOD, "xml" );
|
83 | 2 |
format.put( OutputKeys.INDENT, "yes" );
|
84 | 2 |
handler.setResult( result ); |
85 | 2 |
handler.getTransformer().setOutputProperties( format ); |
86 |
|
|
87 | 2 |
final SAXConfigurationSerializer serializer = new SAXConfigurationSerializer();
|
88 | 2 |
serializer.serialize( configuration, handler ); |
89 |
} |
|
90 |
|
|
91 |
/**
|
|
92 |
* Create a configuration object from specified XML InputSource.
|
|
93 |
*
|
|
94 |
* @param input the InputSource
|
|
95 |
* @return the configuration object
|
|
96 |
* @throws Exception if unable to create configuration object
|
|
97 |
* from input
|
|
98 |
*/
|
|
99 | 2 |
public static Configuration buildFromXML( final InputSource input ) |
100 |
throws Exception
|
|
101 |
{ |
|
102 | 2 |
final SAXParserFactory saxParserFactory = SAXParserFactory.newInstance(); |
103 | 2 |
saxParserFactory.setNamespaceAware( false );
|
104 | 2 |
final SAXParser saxParser = saxParserFactory.newSAXParser(); |
105 | 2 |
final SAXConfigurationHandler handler = new SAXConfigurationHandler();
|
106 | 2 |
saxParser.parse( input, handler ); |
107 | 2 |
return handler.getConfiguration();
|
108 |
} |
|
109 |
|
|
110 |
/**
|
|
111 |
* Convert specified Element into a configuration object.
|
|
112 |
*
|
|
113 |
* @param element the Element
|
|
114 |
* @return the Configuration object
|
|
115 |
*/
|
|
116 | 12 |
public static Configuration toConfiguration( final Element element ) |
117 |
{ |
|
118 | 12 |
return toConfiguration( element, ROOT_PATH );
|
119 |
} |
|
120 |
|
|
121 |
/**
|
|
122 |
* Internal utility method to convert specified Element into
|
|
123 |
* a configuration object.
|
|
124 |
*
|
|
125 |
* @param element the Element
|
|
126 |
* @param parentPath the path to root of document
|
|
127 |
* @return the Configuration object
|
|
128 |
*/
|
|
129 | 14 |
private static Configuration toConfiguration( final Element element, |
130 |
final String parentPath ) |
|
131 |
{ |
|
132 | 14 |
final DefaultConfiguration configuration = |
133 |
new DefaultConfiguration( element.getNodeName(), ELEMENT_LOCATION, parentPath );
|
|
134 | 14 |
final NamedNodeMap attributes = element.getAttributes(); |
135 | 14 |
final int length = attributes.getLength();
|
136 | 14 |
for( int i = 0; i < length; i++ ) |
137 |
{ |
|
138 | 2 |
final Node node = attributes.item( i ); |
139 | 2 |
final String name = node.getNodeName(); |
140 | 2 |
final String value = node.getNodeValue(); |
141 | 2 |
configuration.setAttribute( name, value ); |
142 |
} |
|
143 |
|
|
144 | 14 |
final String childPath = |
145 |
generatePathName( parentPath, configuration.getName() ); |
|
146 |
|
|
147 | 14 |
String content = null;
|
148 | 14 |
final NodeList nodes = element.getChildNodes(); |
149 | 14 |
final int count = nodes.getLength();
|
150 | 14 |
for( int i = 0; i < count; i++ ) |
151 |
{ |
|
152 | 10 |
final Node node = nodes.item( i ); |
153 | 10 |
if( node instanceof Element ) |
154 |
{ |
|
155 | 2 |
final Configuration child = toConfiguration( (Element)node, childPath ); |
156 | 2 |
configuration.addChild( child ); |
157 |
} |
|
158 | 8 |
else if( node instanceof Text ) |
159 |
{ |
|
160 | 6 |
final Text data = (Text)node; |
161 | 6 |
if( null != content ) |
162 |
{ |
|
163 | 2 |
content += data.getData(); |
164 |
} |
|
165 |
else
|
|
166 |
{ |
|
167 | 4 |
content = data.getData(); |
168 |
} |
|
169 |
} |
|
170 |
} |
|
171 |
|
|
172 | 14 |
if( null != content ) |
173 |
{ |
|
174 | 4 |
configuration.setValue( content ); |
175 |
} |
|
176 |
|
|
177 | 14 |
return configuration;
|
178 |
} |
|
179 |
|
|
180 |
/**
|
|
181 |
* Add in utity method to generate path string from parent.
|
|
182 |
*
|
|
183 |
* @param path parents path
|
|
184 |
* @param name parents name
|
|
185 |
* @return the path string
|
|
186 |
*/
|
|
187 | 30 |
static String generatePathName( final String path,
|
188 |
final String name ) |
|
189 |
{ |
|
190 | 30 |
if( ROOT_PATH.equals( path ) )
|
191 |
{ |
|
192 | 24 |
return name;
|
193 |
} |
|
194 |
else
|
|
195 |
{ |
|
196 | 6 |
return path + PATH_SEPARATOR + name;
|
197 |
} |
|
198 |
} |
|
199 |
|
|
200 |
/**
|
|
201 |
* Convert specified Configuration object into a Element.
|
|
202 |
*
|
|
203 |
* @param configuration the Configuration
|
|
204 |
* @return the Element object
|
|
205 |
*/
|
|
206 | 11 |
public static Element toElement( final Configuration configuration ) |
207 |
{ |
|
208 | 11 |
try
|
209 |
{ |
|
210 | 11 |
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
211 | 9 |
final DocumentBuilder builder = factory.newDocumentBuilder(); |
212 | 9 |
final Document document = builder.newDocument(); |
213 |
|
|
214 | 9 |
return createElement( document, configuration );
|
215 |
} |
|
216 |
catch( final Throwable t )
|
|
217 |
{ |
|
218 | 2 |
throw new IllegalStateException( t.toString() ); |
219 |
} |
|
220 |
} |
|
221 |
|
|
222 |
/**
|
|
223 |
* Internal helper method to convert specified Configuration object
|
|
224 |
* into a Element.
|
|
225 |
*
|
|
226 |
* @param document the owner document
|
|
227 |
* @param configuration the Configuration
|
|
228 |
* @return the Element object
|
|
229 |
*/
|
|
230 | 11 |
private static Element createElement( final Document document, |
231 |
final Configuration configuration ) |
|
232 |
{ |
|
233 | 11 |
final Element element = document.createElement( configuration.getName() ); |
234 |
|
|
235 | 11 |
final String content = configuration.getValue( null );
|
236 | 11 |
if( null != content ) |
237 |
{ |
|
238 | 2 |
final Text child = document.createTextNode( content ); |
239 | 2 |
element.appendChild( child ); |
240 |
} |
|
241 |
|
|
242 | 11 |
final String[] names = configuration.getAttributeNames(); |
243 | 11 |
for( int i = 0; i < names.length; i++ ) |
244 |
{ |
|
245 | 2 |
final String name = names[ i ]; |
246 | 2 |
final String value = configuration.getAttribute( name, null );
|
247 | 2 |
element.setAttribute( name, value ); |
248 |
} |
|
249 | 11 |
final Configuration[] children = configuration.getChildren(); |
250 | 11 |
for( int i = 0; i < children.length; i++ ) |
251 |
{ |
|
252 | 2 |
final Element child = createElement( document, children[ i ] ); |
253 | 2 |
element.appendChild( child ); |
254 |
} |
|
255 | 11 |
return element;
|
256 |
} |
|
257 |
|
|
258 |
/**
|
|
259 |
* Test if two configuration objects are equal. To be equal
|
|
260 |
* the configuration objects must have equal child configuration
|
|
261 |
* objects in identical orders or identical content values and
|
|
262 |
* must have the same attributes with the same values.
|
|
263 |
*
|
|
264 |
* @param configuration1 a configuration object
|
|
265 |
* @param configuration2 a configuration object
|
|
266 |
* @return true if the configuration objects are equal
|
|
267 |
*/
|
|
268 | 28 |
public static boolean equals( final Configuration configuration1, |
269 |
final Configuration configuration2 ) |
|
270 |
{ |
|
271 | 28 |
final String name1 = configuration1.getName(); |
272 | 28 |
final String name2 = configuration2.getName(); |
273 | 28 |
if( !name1.equals( name2 ) )
|
274 |
{ |
|
275 | 4 |
return false; |
276 |
} |
|
277 |
|
|
278 | 24 |
final Configuration[] children1 = configuration1.getChildren(); |
279 | 24 |
final Configuration[] children2 = configuration2.getChildren(); |
280 | 24 |
if( children1.length != children2.length )
|
281 |
{ |
|
282 | 2 |
return false; |
283 |
} |
|
284 |
else
|
|
285 |
{ |
|
286 | 22 |
for( int i = 0; i < children1.length; i++ ) |
287 |
{ |
|
288 | 4 |
if( !equals( children1[ i ], children2[ i ] ) )
|
289 |
{ |
|
290 | 2 |
return false; |
291 |
} |
|
292 |
} |
|
293 |
} |
|
294 |
|
|
295 | 20 |
final String[] names1 = configuration1.getAttributeNames(); |
296 | 20 |
final String[] names2 = configuration2.getAttributeNames(); |
297 | 20 |
if( names1.length != names2.length )
|
298 |
{ |
|
299 | 2 |
return false; |
300 |
} |
|
301 |
else
|
|
302 |
{ |
|
303 | 18 |
for( int i = 0; i < names1.length; i++ ) |
304 |
{ |
|
305 | 6 |
final String value1 = |
306 |
configuration1.getAttribute( names1[ i ], null );
|
|
307 | 6 |
final String value2 = |
308 |
configuration2.getAttribute( names1[ i ], null );
|
|
309 | 6 |
if( !value1.equals( value2 ) )
|
310 |
{ |
|
311 | 4 |
return false; |
312 |
} |
|
313 |
} |
|
314 |
} |
|
315 |
|
|
316 | 14 |
final String value1 = configuration1.getValue( null );
|
317 | 14 |
final String value2 = configuration2.getValue( null );
|
318 | 14 |
if( null == value1 && null == value2 ) |
319 |
{ |
|
320 | 8 |
return true; |
321 |
} |
|
322 | 6 |
else if( null != value1 && null != value2 ) |
323 |
{ |
|
324 | 4 |
return value1.equals( value2 );
|
325 |
} |
|
326 |
else
|
|
327 |
{ |
|
328 | 2 |
return false; |
329 |
} |
|
330 |
} |
|
331 |
} |
|
332 |
|
|