1 49 50 package org.apache.avalon.meta.info.writer; 51 52 import java.io.IOException ; 53 import java.io.OutputStream ; 54 import java.io.OutputStreamWriter ; 55 import java.io.Writer ; 56 57 import org.apache.avalon.meta.info.Descriptor; 58 import org.apache.avalon.meta.info.Service; 59 60 66 public class XMLServiceWriter 67 implements ServiceWriter 68 { 69 70 77 public void writeService( final Service service, final OutputStream stream ) 78 throws Exception 79 { 80 final Writer writer = new OutputStreamWriter ( stream ); 81 writeHeader( writer ); 82 writeDoctype( writer, "service" ); 83 writer.write( "\n\n<service>" ); 84 85 writer.write( "\n <version>"); 86 writer.write( service.getVersion().toString() ); 87 writer.write( "</version>"); 88 89 writeAttributes( writer, service ); 90 writer.write( "\n</service>" ); 91 writer.flush(); 92 } 93 94 99 private void writeHeader( final Writer writer ) 100 throws IOException 101 { 102 writer.write( "<?xml version=\"1.0\" ?>" ); 103 } 104 105 112 private void writeDoctype( final Writer writer, 113 final String root ) 114 throws IOException 115 { 116 final String doctype = 117 "\n<!DOCTYPE " + root 118 + " PUBLIC \"-//AVALON/Service DTD Version 1.0//EN\" " 119 + "\"http://avalon.apache.org/dtds/meta/service_1_0.dtd\" >"; 120 writer.write( doctype ); 121 } 122 123 130 private void writeAttributes( final Writer writer, final Descriptor descriptor ) 131 throws IOException 132 { 133 String [] names = descriptor.getAttributeNames(); 134 if( names.length > 0 ) 135 { 136 writer.write( "\n <attributes>" ); 137 for( int i = 0; i < names.length; i++ ) 138 { 139 writeAttribute( writer, names[i], descriptor.getAttribute( names[i] ) ); 140 } 141 writer.write( "\n </attributes>" ); 142 } 143 } 144 145 153 private void writeAttribute( final Writer writer, 154 final String key, final String value ) 155 throws IOException 156 { 157 writer.write( "\n <attribute key=\"" ); 158 writer.write( key ); 159 writer.write( "\" value=\"" ); 160 writer.write( value ); 161 writer.write( "\"/>" ); 162 } 163 } 164 | Popular Tags |