1 55 56 package org.apache.avalon.framework.configuration; 57 58 import javax.xml.parsers.DocumentBuilder ; 59 import javax.xml.parsers.DocumentBuilderFactory ; 60 import javax.xml.parsers.ParserConfigurationException ; 61 import org.w3c.dom.Document ; 62 import org.w3c.dom.Element ; 63 import org.w3c.dom.Text ; 64 import java.util.ArrayList ; 65 import java.util.Arrays ; 66 import java.util.Iterator ; 67 68 75 public class ConfigurationUtil 76 { 77 80 private ConfigurationUtil() 81 { 82 } 83 84 90 public static Element toElement( final Configuration configuration ) 91 { 92 try 93 { 94 final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 95 final DocumentBuilder builder = factory.newDocumentBuilder(); 96 final Document document = builder.newDocument(); 97 98 return createElement( document, configuration ); 99 } 100 catch( final ParserConfigurationException pce ) 101 { 102 throw new IllegalStateException ( pce.toString() ); 103 } 104 } 105 106 115 public static boolean equals( final Configuration c1, final Configuration c2 ) 116 { 117 return c1.getName().equals( c2.getName() ) 118 && areValuesEqual( c1, c2 ) 119 && areAttributesEqual( c1, c2 ) 120 && areChildrenEqual( c1, c2 ); 121 } 122 123 130 private static boolean areChildrenEqual( final Configuration c1, 131 final Configuration c2 ) 132 { 133 final Configuration[] kids1 = c1.getChildren(); 134 final ArrayList kids2 = new ArrayList ( Arrays.asList( c2.getChildren() ) ); 135 if( kids1.length != kids2.size() ) 136 { 137 return false; 138 } 139 140 for( int i = 0; i < kids1.length; i++ ) 141 { 142 if( !findMatchingChild( kids1[ i ], kids2 ) ) 143 { 144 return false; 145 } 146 } 147 148 return kids2.isEmpty() ? true : false; 149 } 150 151 158 private static boolean findMatchingChild( final Configuration c, 159 final ArrayList matchAgainst ) 160 { 161 final Iterator i = matchAgainst.iterator(); 162 while( i.hasNext() ) 163 { 164 if( equals( c, (Configuration)i.next() ) ) 165 { 166 i.remove(); 167 return true; 168 } 169 } 170 171 return false; 172 } 173 174 181 private static boolean areAttributesEqual( final Configuration c1, 182 final Configuration c2 ) 183 { 184 final String [] names1 = c1.getAttributeNames(); 185 final String [] names2 = c2.getAttributeNames(); 186 if( names1.length != names2.length ) 187 { 188 return false; 189 } 190 191 for( int i = 0; i < names1.length; i++ ) 192 { 193 final String name = names1[ i ]; 194 final String value1 = c1.getAttribute( name, null ); 195 final String value2 = c2.getAttribute( name, null ); 196 if( !value1.equals( value2 ) ) 197 { 198 return false; 199 } 200 } 201 202 return true; 203 } 204 205 212 private static boolean areValuesEqual( final Configuration c1, 213 final Configuration c2 ) 214 { 215 final String value1 = c1.getValue( null ); 216 final String value2 = c2.getValue( null ); 217 return ( value1 == null && value2 == null ) 218 || ( value1 != null && value1.equals( value2 ) ); 219 } 220 221 229 private static Element createElement( final Document document, 230 final Configuration configuration ) 231 { 232 final Element element = document.createElement( configuration.getName() ); 233 234 final String content = configuration.getValue( null ); 235 if( null != content ) 236 { 237 final Text child = document.createTextNode( content ); 238 element.appendChild( child ); 239 } 240 241 final String [] names = configuration.getAttributeNames(); 242 for( int i = 0; i < names.length; i++ ) 243 { 244 final String name = names[ i ]; 245 final String value = configuration.getAttribute( name, null ); 246 element.setAttribute( name, value ); 247 } 248 final Configuration[] children = configuration.getChildren(); 249 for( int i = 0; i < children.length; i++ ) 250 { 251 final Element child = createElement( document, children[ i ] ); 252 element.appendChild( child ); 253 } 254 return element; 255 } 256 } 257 | Popular Tags |