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 public static void serializeToResult( final Result result,
74 final Configuration configuration )
75 throws Exception
76 {
77 final SAXTransformerFactory factory =
78 (SAXTransformerFactory)TransformerFactory.newInstance();
79 final TransformerHandler handler = factory.newTransformerHandler();
80
81 final Properties format = new Properties();
82 format.put( OutputKeys.METHOD, "xml" );
83 format.put( OutputKeys.INDENT, "yes" );
84 handler.setResult( result );
85 handler.getTransformer().setOutputProperties( format );
86
87 final SAXConfigurationSerializer serializer = new SAXConfigurationSerializer();
88 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 public static Configuration buildFromXML( final InputSource input )
100 throws Exception
101 {
102 final SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
103 saxParserFactory.setNamespaceAware( false );
104 final SAXParser saxParser = saxParserFactory.newSAXParser();
105 final SAXConfigurationHandler handler = new SAXConfigurationHandler();
106 saxParser.parse( input, handler );
107 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 public static Configuration toConfiguration( final Element element )
117 {
118 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 private static Configuration toConfiguration( final Element element,
130 final String parentPath )
131 {
132 final DefaultConfiguration configuration =
133 new DefaultConfiguration( element.getNodeName(), ELEMENT_LOCATION, parentPath );
134 final NamedNodeMap attributes = element.getAttributes();
135 final int length = attributes.getLength();
136 for( int i = 0; i < length; i++ )
137 {
138 final Node node = attributes.item( i );
139 final String name = node.getNodeName();
140 final String value = node.getNodeValue();
141 configuration.setAttribute( name, value );
142 }
143
144 final String childPath =
145 generatePathName( parentPath, configuration.getName() );
146
147 String content = null;
148 final NodeList nodes = element.getChildNodes();
149 final int count = nodes.getLength();
150 for( int i = 0; i < count; i++ )
151 {
152 final Node node = nodes.item( i );
153 if( node instanceof Element )
154 {
155 final Configuration child = toConfiguration( (Element)node, childPath );
156 configuration.addChild( child );
157 }
158 else if( node instanceof Text )
159 {
160 final Text data = (Text)node;
161 if( null != content )
162 {
163 content += data.getData();
164 }
165 else
166 {
167 content = data.getData();
168 }
169 }
170 }
171
172 if( null != content )
173 {
174 configuration.setValue( content );
175 }
176
177 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 static String generatePathName( final String path,
188 final String name )
189 {
190 if( ROOT_PATH.equals( path ) )
191 {
192 return name;
193 }
194 else
195 {
196 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 public static Element toElement( final Configuration configuration )
207 {
208 try
209 {
210 final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
211 final DocumentBuilder builder = factory.newDocumentBuilder();
212 final Document document = builder.newDocument();
213
214 return createElement( document, configuration );
215 }
216 catch( final Throwable t )
217 {
218 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 private static Element createElement( final Document document,
231 final Configuration configuration )
232 {
233 final Element element = document.createElement( configuration.getName() );
234
235 final String content = configuration.getValue( null );
236 if( null != content )
237 {
238 final Text child = document.createTextNode( content );
239 element.appendChild( child );
240 }
241
242 final String[] names = configuration.getAttributeNames();
243 for( int i = 0; i < names.length; i++ )
244 {
245 final String name = names[ i ];
246 final String value = configuration.getAttribute( name, null );
247 element.setAttribute( name, value );
248 }
249 final Configuration[] children = configuration.getChildren();
250 for( int i = 0; i < children.length; i++ )
251 {
252 final Element child = createElement( document, children[ i ] );
253 element.appendChild( child );
254 }
255 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 public static boolean equals( final Configuration configuration1,
269 final Configuration configuration2 )
270 {
271 final String name1 = configuration1.getName();
272 final String name2 = configuration2.getName();
273 if( !name1.equals( name2 ) )
274 {
275 return false;
276 }
277
278 final Configuration[] children1 = configuration1.getChildren();
279 final Configuration[] children2 = configuration2.getChildren();
280 if( children1.length != children2.length )
281 {
282 return false;
283 }
284 else
285 {
286 for( int i = 0; i < children1.length; i++ )
287 {
288 if( !equals( children1[ i ], children2[ i ] ) )
289 {
290 return false;
291 }
292 }
293 }
294
295 final String[] names1 = configuration1.getAttributeNames();
296 final String[] names2 = configuration2.getAttributeNames();
297 if( names1.length != names2.length )
298 {
299 return false;
300 }
301 else
302 {
303 for( int i = 0; i < names1.length; i++ )
304 {
305 final String value1 =
306 configuration1.getAttribute( names1[ i ], null );
307 final String value2 =
308 configuration2.getAttribute( names1[ i ], null );
309 if( !value1.equals( value2 ) )
310 {
311 return false;
312 }
313 }
314 }
315
316 final String value1 = configuration1.getValue( null );
317 final String value2 = configuration2.getValue( null );
318 if( null == value1 && null == value2 )
319 {
320 return true;
321 }
322 else if( null != value1 && null != value2 )
323 {
324 return value1.equals( value2 );
325 }
326 else
327 {
328 return false;
329 }
330 }
331 }
This page was automatically generated by Maven