View Javadoc

1   package com.bradmcevoy.http;
2   
3   import java.util.Stack;
4   
5   import org.xml.sax.Attributes;
6   import org.xml.sax.SAXException;
7   import org.xml.sax.helpers.DefaultHandler;
8   
9   import com.bradmcevoy.http.LockInfo.LockScope;
10  import com.bradmcevoy.http.LockInfo.LockType;
11  import org.slf4j.Logger;
12  import org.slf4j.LoggerFactory;
13  
14  public class LockInfoSaxHandler extends DefaultHandler {
15  
16      private static final Logger log = LoggerFactory.getLogger( LockInfo.class );
17  
18      private LockInfo info = new LockInfo();
19      private StringBuilder owner;
20      private Stack<String> elementPath = new Stack<String>();
21  
22      @Override
23      public void startElement( String uri, String localName, String name, Attributes attributes ) throws SAXException {
24          elementPath.push( localName );
25          if( localName.equals( "owner" ) ) {
26              owner = new StringBuilder();
27          }
28          super.startElement( uri, localName, name, attributes );
29      }
30  
31      @Override
32      public void characters( char[] ch, int start, int length ) throws SAXException {
33          if( owner != null ) {
34              owner.append( ch, start, length );
35          }
36      }
37  
38      @Override
39      public void endElement( String uri, String localName, String name ) throws SAXException {
40          elementPath.pop();
41          if( localName.equals( "owner" ) ) {
42              log.debug( "owner: " + owner.toString());
43              getInfo().lockedByUser = owner.toString();
44          }
45          if( elementPath.size() > 1 ) {
46              if( elementPath.get( 1 ).equals( "lockscope" ) ) {
47                  if( localName.equals( "exclusive" ) ) {
48                      getInfo().scope = LockScope.EXCLUSIVE;
49                  } else if( localName.equals( "shared" ) ) {
50                      getInfo().scope = LockScope.SHARED;
51                  } else {
52                      getInfo().scope = LockScope.NONE;
53                  }
54              } else if( elementPath.get( 1 ).equals( "locktype" ) ) {
55                  if( localName.equals( "read" ) ) {
56                      getInfo().type = LockType.READ;
57                  } else if( localName.equals( "write" ) ) {
58                      getInfo().type = LockType.WRITE;
59                  } else {
60                      getInfo().type = LockType.WRITE;
61                  }
62              }
63  
64          }
65          super.endElement( uri, localName, name );
66      }
67  
68      public LockInfo getInfo() {
69          return info;
70      }
71  }