KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ws > jaxme > impl > OrderedAttributeXMLWriter


1 /*
2  * Copyright 2003, 2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.ws.jaxme.impl;
17
18 import java.util.Arrays JavaDoc;
19 import java.util.Comparator JavaDoc;
20
21 import org.xml.sax.Attributes JavaDoc;
22 import org.xml.sax.SAXException JavaDoc;
23 import org.xml.sax.helpers.AttributesImpl JavaDoc;
24
25
26 /** This is a subclass of {@link org.apache.ws.jaxme.impl.XMLWriterImpl},
27  * that writes the attributes ordered alphabetically. This is mainly
28  * useful for test purposes, when a canonical representation of the
29  * result is required for comparing against an expected value.
30  */

31 public class OrderedAttributeXMLWriter extends XMLWriterImpl {
32     public void startElement(String JavaDoc pNamespaceURI, String JavaDoc pLocalName,
33                              String JavaDoc pQName, final Attributes JavaDoc pAttrs)
34             throws SAXException JavaDoc {
35         Integer JavaDoc[] attributeNumbers = new Integer JavaDoc[pAttrs.getLength()];
36         for (int i = 0; i < attributeNumbers.length; i++) {
37             attributeNumbers[i] = new Integer JavaDoc(i);
38         }
39         Arrays.sort(attributeNumbers, new Comparator JavaDoc(){
40             public int compare(Object JavaDoc pNum1, Object JavaDoc pNum2) {
41                 int i1 = ((Integer JavaDoc) pNum1).intValue();
42                 int i2 = ((Integer JavaDoc) pNum2).intValue();
43                 String JavaDoc uri1 = pAttrs.getURI(i1);
44                 if (uri1 == null) {
45                     uri1 = "";
46                 }
47                 String JavaDoc 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 JavaDoc orderedAttributes = new AttributesImpl JavaDoc();
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