Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/thermidor/xml/Attribs.java


1   package com.thermidor.xml;
2   /*@LEGAL@*/
3   import org.xml.sax.Attributes;
4   import org.xml.sax.helpers.AttributesImpl;
5   /**
6    * Puts a simple wrapper around {@link org.xml.sax.Attributes} for when we 
7    * only want to deal with simple attribute names.
8    * @author Richard Rutter
9    * @author Edward Turnock
10   */
11  public class Attribs extends AttributesImpl {
12  
13      /**
14       * Adds an attribute to the parent {@link org.xml.sax.AttributesImpl}. 
15       * Type defaulted
16       * to CDATA.
17       * @param qname the name of the attribute.
18       * @param value the <code>String</code> value of the attribute.
19       */
20      public void addAttribute( String qname, String value ) {
21          addAttribute( qname, "CDATA", value );
22      }
23  
24      /**
25       * Adds an attribute to the parent <code>AttributesImpl</code>.
26       * @param qname the name of the attribute.
27       * @param type the type of the attribute.
28       * @param value <code>String</code> value of the attribute.
29       */
30      public void addAttribute( String qname, String type, String value ) {
31          super.addAttribute( "", "", qname, type, value );
32      }
33  
34      /**
35       * Converts the list of attributes to a <code>String</code>.
36       * @return the <code>String</code> representation of the attribute list.
37       */
38      public String toString() {
39          StringBuffer sb = new StringBuffer();
40          int len = getLength();
41  
42          for ( int i = 0; i < len; i++ ) {
43              sb.append( " " );
44              sb.append( getQName( i ) );
45              sb.append( "=\"" );
46              sb.append( SAXWriter.normalize( getValue( i ) ) );
47              sb.append( '"' );
48          }
49  
50          return sb.toString();
51      }
52  
53      /**
54       * Converts the list of attributes to a prettified <code>String</code>.
55       * @param indentSize indent tab size.
56       * @param sort <code>true</code> if the list should be sorted 
57       * alphabetically.
58       * @return the <code>String</code> representation of the attribute list.
59       */
60      public String prettyString( int indentSize, boolean sort ) {
61          StringBuffer sb = new StringBuffer();
62          String indent = "";
63  
64          for ( int i = 0;i < indentSize;i++ ) {
65              sb.append( "\t" );
66          }
67  
68          indent = sb.toString();
69          sb.delete( 0, indent.length() - 1 );
70          int len = getLength();
71          Attributes a = sort ? sort() : this;
72  
73          for ( int i = 0; i < len; i++ ) {
74              sb.append( indent );
75              sb.append( getQName( i ) );
76              sb.append( "=\"" );
77              sb.append( SAXWriter.normalize( getValue( i ) ) );
78              sb.append( '"' );
79          }
80  
81          return sb.toString();
82      }
83  
84      /**
85       * Returns a sorted list of attributes. 
86       * @return the sorted list. 
87       */
88      private Attributes sort() {
89  
90          AttributesImpl attributes = new AttributesImpl();
91  
92          int len = getLength();
93  
94          for ( int i = 0; i < len; i++ ) {
95              String name = getQName( i );
96              int count = attributes.getLength();
97              int j = 0;
98  
99              while ( j < count ) {
100                 if ( name.compareTo( attributes.getQName( j ) ) < 0 ) {
101                     break;
102                 }
103 
104                 j++;
105             }
106 
107             attributes.setAttribute( j, 
108                                      getURI( i ), 
109                                      getLocalName( i ), 
110                                      name,
111                                      getType( i ), 
112                                      getValue( i ) );
113         }
114 
115         return attributes;
116     }
117 }
118