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.HashMap ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 import java.util.Map ; 27 28 import javax.xml.namespace.QName ; 29 30 import org.apache.servicemix.common.packaging.Consumes; 31 import org.apache.servicemix.common.packaging.Provides; 32 import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter; 33 import org.codehaus.plexus.util.xml.XMLWriter; 34 35 39 public class JbiServiceUnitDescriptorWriter { 40 41 private final String encoding; 42 43 public JbiServiceUnitDescriptorWriter(String encoding) { 44 this.encoding = encoding; 45 } 46 47 public void write(File descriptor, String name, String description, 48 List uris, List consumes, List provides) throws JbiPluginException { 49 FileWriter w; 50 try { 51 w = new FileWriter (descriptor); 52 } catch (IOException ex) { 53 throw new JbiPluginException("Exception while opening file[" 54 + descriptor.getAbsolutePath() + "]", ex); 55 } 56 57 XMLWriter writer = new PrettyPrintXMLWriter(w, encoding, null); 58 writer.startElement("jbi"); 59 writer.addAttribute("xmlns", "http://java.sun.com/xml/ns/jbi"); 60 writer.addAttribute("version", "1.0"); 61 62 writer.startElement("services"); 63 64 Map namespaceMap = getNamespaceMap(provides, consumes); 67 68 for (Iterator iterator = namespaceMap.keySet().iterator(); iterator 70 .hasNext();) { 71 String key = (String ) iterator.next(); 72 StringBuffer namespaceDecl = new StringBuffer (); 73 namespaceDecl.append("xmlns:"); 74 namespaceDecl.append(namespaceMap.get(key)); 75 writer.addAttribute(namespaceDecl.toString(), key); 76 } 77 78 for (Iterator iterator = provides.iterator(); iterator.hasNext();) { 80 Provides providesEntry = (Provides) iterator.next(); 81 writer.startElement("provides"); 82 addQNameAttribute(writer, "interface-name", providesEntry 83 .getInterfaceName(), namespaceMap); 84 addQNameAttribute(writer, "service-name", providesEntry 85 .getServiceName(), namespaceMap); 86 addStringAttribute(writer, "endpoint-name", providesEntry 87 .getEndpointName()); 88 writer.endElement(); 89 } 90 91 for (Iterator iterator = consumes.iterator(); iterator.hasNext();) { 93 Consumes consumesEntry = (Consumes) iterator.next(); 94 writer.startElement("consumes"); 95 addQNameAttribute(writer, "interface-name", consumesEntry 96 .getInterfaceName(), namespaceMap); 97 addQNameAttribute(writer, "service-name", consumesEntry 98 .getServiceName(), namespaceMap); 99 addStringAttribute(writer, "endpoint-name", consumesEntry 100 .getEndpointName()); 101 102 104 writer.endElement(); 105 } 106 107 writer.endElement(); 108 109 writer.endElement(); 110 111 close(w); 112 } 113 114 private void addStringAttribute(XMLWriter writer, String attributeName, 115 String attributeValue) { 116 if (attributeValue != null) 117 writer.addAttribute(attributeName, attributeValue); 118 } 119 120 private void addQNameAttribute(XMLWriter writer, String attributeName, 121 QName attributeValue, Map namespaceMap) { 122 System.out.println("attributeName=" + attributeValue); 123 if (attributeValue != null) { 124 StringBuffer attributeStringValue = new StringBuffer (); 125 attributeStringValue.append(namespaceMap.get(attributeValue 126 .getNamespaceURI())); 127 attributeStringValue.append(":"); 128 attributeStringValue.append(attributeValue.getLocalPart()); 129 writer.addAttribute(attributeName, attributeStringValue.toString()); 130 } 131 132 } 133 134 private Map getNamespaceMap(List provides, List consumes) { 135 Map namespaceMap = new HashMap (); 136 int namespaceCounter = 1; 137 for (Iterator iterator = provides.iterator(); iterator.hasNext();) { 138 Provides providesEntry = (Provides) iterator.next(); 139 resolveMapEntry(namespaceMap, providesEntry.getInterfaceName(), 140 namespaceCounter); 141 resolveMapEntry(namespaceMap, providesEntry.getServiceName(), 142 namespaceCounter); 143 } 144 145 for (Iterator iterator = consumes.iterator(); iterator.hasNext();) { 146 Consumes consumesEntry = (Consumes) iterator.next(); 147 resolveMapEntry(namespaceMap, consumesEntry.getInterfaceName(), 148 namespaceCounter); 149 resolveMapEntry(namespaceMap, consumesEntry.getServiceName(), 150 namespaceCounter); 151 } 152 153 return namespaceMap; 154 } 155 156 private void resolveMapEntry(Map namespaceMap, QName qname, 157 int namespaceCounter) { 158 if ((qname != null) 159 && (!namespaceMap.containsKey(qname.getNamespaceURI()))) { 160 if (qname.getPrefix() == null || qname.getPrefix().equals("") ) { 161 namespaceMap.put(qname.getNamespaceURI(), "ns" 162 + namespaceCounter++); 163 } else 164 namespaceMap.put(qname.getNamespaceURI(), qname.getPrefix()); 165 } 166 } 167 168 private void close(Writer closeable) { 169 if (closeable != null) { 170 try { 171 closeable.close(); 172 } catch (Exception e) { 173 } 175 } 176 } 177 178 } 179 | Popular Tags |