1 55 package org.apache.avalon.framework.configuration; 56 57 import java.io.File ; 58 import java.io.FileOutputStream ; 59 import java.io.IOException ; 60 import java.io.OutputStream ; 61 import java.io.StringWriter ; 62 import java.net.URL ; 63 import java.util.Properties ; 64 import javax.xml.transform.OutputKeys ; 65 import javax.xml.transform.Result ; 66 import javax.xml.transform.TransformerFactory ; 67 import javax.xml.transform.sax.SAXTransformerFactory ; 68 import javax.xml.transform.sax.TransformerHandler ; 69 import javax.xml.transform.stream.StreamResult ; 70 import org.xml.sax.ContentHandler ; 71 import org.xml.sax.SAXException ; 72 import org.xml.sax.helpers.AttributesImpl ; 73 import org.xml.sax.helpers.NamespaceSupport ; 74 75 81 public class DefaultConfigurationSerializer 82 { 83 private SAXTransformerFactory m_tfactory; 84 private Properties m_format = new Properties (); 85 86 91 public void setIndent( boolean indent ) 92 { 93 if( indent ) 94 { 95 m_format.put( OutputKeys.INDENT, "yes" ); 96 } 97 else 98 { 99 m_format.put( OutputKeys.INDENT, "no" ); 100 } 101 } 102 103 108 protected ContentHandler createContentHandler( final Result result ) 109 { 110 try 111 { 112 TransformerHandler handler = getTransformerFactory().newTransformerHandler(); 113 114 m_format.put( OutputKeys.METHOD, "xml" ); 115 handler.setResult( result ); 116 handler.getTransformer().setOutputProperties( m_format ); 117 118 return handler; 119 } 120 catch( final Exception e ) 121 { 122 throw new RuntimeException ( e.toString() ); 123 } 124 } 125 126 131 protected SAXTransformerFactory getTransformerFactory() 132 { 133 if( m_tfactory == null ) 134 { 135 m_tfactory = (SAXTransformerFactory )TransformerFactory.newInstance(); 136 } 137 138 return m_tfactory; 139 } 140 141 148 public void serialize( final ContentHandler handler, final Configuration source ) 149 throws SAXException , ConfigurationException 150 { 151 handler.startDocument(); 152 serializeElement( handler, new NamespaceSupport (), source ); 153 handler.endDocument(); 154 } 155 156 164 protected void serializeElement( final ContentHandler handler, 165 final NamespaceSupport namespaceSupport, 166 final Configuration element ) 167 throws SAXException , ConfigurationException 168 { 169 namespaceSupport.pushContext(); 170 171 AttributesImpl attr = new AttributesImpl (); 172 String [] attrNames = element.getAttributeNames(); 173 174 if( null != attrNames ) 175 { 176 for( int i = 0; i < attrNames.length; i++ ) 177 { 178 attr.addAttribute( "", attrNames[ i ], attrNames[ i ], "CDATA", element.getAttribute( attrNames[ i ], "" ) ); 184 } 185 } 186 187 final String nsURI = element.getNamespace(); 188 String nsPrefix = ""; 189 190 if( element instanceof AbstractConfiguration ) 191 { 192 nsPrefix = ( (AbstractConfiguration)element ).getPrefix(); 193 } 194 196 boolean nsWasDeclared = false; 197 198 final String existingURI = namespaceSupport.getURI( nsPrefix ); 199 200 if( existingURI == null || !existingURI.equals( nsURI ) ) 203 { 204 nsWasDeclared = true; 205 if( nsPrefix.equals( "" ) && nsURI.equals( "" ) ) 206 { 207 } 209 else if( nsPrefix.equals( "" ) ) 210 { 211 attr.addAttribute( "", "xmlns", "xmlns", "CDATA", nsURI ); 213 } 214 else 215 { 216 attr.addAttribute( "", "xmlns:" + nsPrefix, "xmlns:" + nsPrefix, "CDATA", nsURI ); 218 } 219 handler.startPrefixMapping( nsPrefix, nsURI ); 220 namespaceSupport.declarePrefix( nsPrefix, nsURI ); 221 } 222 223 String localName = element.getName(); 224 String qName = element.getName(); 225 if( nsPrefix == null || nsPrefix.length() == 0 ) 226 { 227 qName = localName; 228 } 229 else 230 { 231 qName = nsPrefix + ":" + localName; 232 } 233 234 handler.startElement( nsURI, localName, qName, attr ); 235 236 String value = element.getValue( null ); 237 238 if( null == value ) 239 { 240 Configuration[] children = element.getChildren(); 241 242 for( int i = 0; i < children.length; i++ ) 243 { 244 serializeElement( handler, namespaceSupport, children[ i ] ); 245 } 246 } 247 else 248 { 249 handler.characters( value.toCharArray(), 0, value.length() ); 250 } 251 252 handler.endElement( nsURI, localName, qName ); 253 254 if( nsWasDeclared ) 255 { 256 handler.endPrefixMapping( nsPrefix ); 257 } 258 259 namespaceSupport.popContext(); 260 } 261 262 270 public void serializeToFile( final String filename, final Configuration source ) 271 throws SAXException , IOException , ConfigurationException 272 { 273 serializeToFile( new File ( filename ), source ); 274 } 275 276 284 public void serializeToFile( final File file, final Configuration source ) 285 throws SAXException , IOException , ConfigurationException 286 { 287 OutputStream outputStream = null; 288 try 289 { 290 outputStream = new FileOutputStream ( file ); 291 serialize( outputStream, source ); 292 } 293 finally 294 { 295 if( outputStream != null ) 296 { 297 outputStream.close(); 298 } 299 } 300 } 301 302 310 public void serialize( final OutputStream outputStream, final Configuration source ) 311 throws SAXException , IOException , ConfigurationException 312 { 313 serialize( createContentHandler( new StreamResult ( outputStream ) ), source ); 314 } 315 316 325 public void serialize( final String uri, final Configuration source ) 326 throws SAXException , IOException , ConfigurationException 327 { 328 OutputStream outputStream = null; 329 try 330 { 331 outputStream = new URL ( uri ).openConnection().getOutputStream(); 332 serialize( outputStream, source ); 333 } 334 finally 335 { 336 if( outputStream != null ) 337 { 338 outputStream.close(); 339 } 340 } 341 } 342 343 350 public String serialize( final Configuration source ) 351 throws SAXException , ConfigurationException 352 { 353 final StringWriter writer = new StringWriter (); 354 355 serialize( createContentHandler( new StreamResult ( writer ) ), source ); 356 357 return writer.toString(); 358 } 359 } 360 | Popular Tags |