1 16 package org.apache.ws.jaxme.impl; 17 18 import java.util.Arrays ; 19 import java.util.Comparator ; 20 21 import org.xml.sax.Attributes ; 22 import org.xml.sax.SAXException ; 23 import org.xml.sax.helpers.AttributesImpl ; 24 25 26 31 public class OrderedAttributeXMLWriter extends XMLWriterImpl { 32 public void startElement(String pNamespaceURI, String pLocalName, 33 String pQName, final Attributes pAttrs) 34 throws SAXException { 35 Integer [] attributeNumbers = new Integer [pAttrs.getLength()]; 36 for (int i = 0; i < attributeNumbers.length; i++) { 37 attributeNumbers[i] = new Integer (i); 38 } 39 Arrays.sort(attributeNumbers, new Comparator (){ 40 public int compare(Object pNum1, Object pNum2) { 41 int i1 = ((Integer ) pNum1).intValue(); 42 int i2 = ((Integer ) pNum2).intValue(); 43 String uri1 = pAttrs.getURI(i1); 44 if (uri1 == null) { 45 uri1 = ""; 46 } 47 String uri2 = pAttrs.getURI(i2); 48 if (uri2 == null) { 49 uri2 = ""; 50 } 51 int result = uri1.compareTo(uri2); 52 if (result == 0) { 53 result = pAttrs.getLocalName(i1).compareTo(pAttrs.getLocalName(i2)); 54 } 55 return result; 56 } 57 }); 58 AttributesImpl orderedAttributes = new AttributesImpl (); 59 for (int i = 0; i < attributeNumbers.length; i++) { 60 int num = attributeNumbers[i].intValue(); 61 orderedAttributes.addAttribute(pAttrs.getURI(num), pAttrs.getLocalName(num), 62 pAttrs.getQName(num), pAttrs.getType(num), 63 pAttrs.getValue(num)); 64 } 65 super.startElement(pNamespaceURI, pLocalName, pQName, orderedAttributes); 66 } 67 } 68 | Popular Tags |