1 17 package org.apache.servicemix.maven.plugin.jbi; 18 19 import java.io.File ; 20 import java.io.FileWriter ; 21 import java.io.IOException ; 22 import java.io.Writer ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 26 import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter; 27 import org.codehaus.plexus.util.xml.XMLWriter; 28 29 33 public class JbiServiceAssemblyDescriptorWriter { 34 35 private final String encoding; 36 37 public JbiServiceAssemblyDescriptorWriter(String encoding) { 38 this.encoding = encoding; 39 } 40 41 public void write(File descriptor, String name, String description, 42 List uris) throws JbiPluginException { 43 FileWriter w; 44 try { 45 w = new FileWriter (descriptor); 46 } catch (IOException ex) { 47 throw new JbiPluginException("Exception while opening file[" 48 + descriptor.getAbsolutePath() + "]", ex); 49 } 50 51 XMLWriter writer = new PrettyPrintXMLWriter(w, encoding, null); 52 writer.startElement("jbi"); 53 writer.addAttribute("xmlns", "http://java.sun.com/xml/ns/jbi"); 54 writer.addAttribute("version", "1.0"); 55 56 writer.startElement("service-assembly"); 57 58 writer.startElement("identification"); 59 writer.startElement("name"); 60 writer.writeText(name); 61 writer.endElement(); 62 writer.startElement("description"); 63 writer.writeText(description); 64 writer.endElement(); 65 writer.endElement(); 66 67 for (Iterator it = uris.iterator(); it.hasNext();) { 68 DependencyInformation serviceUnitInfo = (DependencyInformation) it 69 .next(); 70 writeServiceUnit(writer, serviceUnitInfo); 71 72 } 73 74 writer.endElement(); 75 writer.endElement(); 76 77 close(w); 78 } 79 80 private void writeServiceUnit(XMLWriter writer, 81 DependencyInformation serviceUnitInfo) throws JbiPluginException { 82 writer.startElement("service-unit"); 83 writer.startElement("identification"); 84 writer.startElement("name"); 85 writer.writeText(serviceUnitInfo.getName()); 86 writer.endElement(); 87 if (serviceUnitInfo.getDescription() != null) { 88 writer.startElement("description"); 89 writer.writeText(serviceUnitInfo.getDescription()); 90 writer.endElement(); 91 } 92 writer.endElement(); 93 94 writer.startElement("target"); 95 writer.startElement("artifacts-zip"); 96 writer.writeText(serviceUnitInfo.getFilename()); 97 writer.endElement(); 98 99 writer.startElement("component-name"); 100 writer.writeText(serviceUnitInfo.getComponent()); 101 writer.endElement(); 102 103 writer.endElement(); 104 105 writer.endElement(); 106 } 107 108 private void close(Writer closeable) { 109 if (closeable != null) { 110 try { 111 closeable.close(); 112 } catch (Exception e) { 113 } 115 } 116 } 117 118 } 119 | Popular Tags |