Advertisement
Guest User

Bad code.

a guest
Nov 26th, 2010
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.12 KB | None | 0 0
  1. package com.google.android.iopex;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. import org.xml.sax.Attributes;
  7. import org.xml.sax.SAXException;
  8. import org.xml.sax.helpers.DefaultHandler;
  9.  
  10. import static com.google.android.iopex.BaseXMLParser.*;
  11.  
  12. public class SaxXMLHandler extends DefaultHandler{
  13.     private List<DbEntry> entries;
  14.     private DbEntry currentEntry;
  15.     private StringBuilder builder;
  16.    
  17.     public List<DbEntry> getMessages(){
  18.         return this.entries;
  19.     }
  20.     @Override
  21.     public void characters(char[] ch, int start, int length)
  22.             throws SAXException {
  23.         super.characters(ch, start, length);
  24.         builder.append(ch, start, length);
  25.     }
  26.  
  27.     @Override
  28.     public void endElement(String uri, String localName, String name)
  29.             throws SAXException {
  30.         super.endElement(uri, localName, name);
  31.         if (this.currentEntry != null){
  32.             String escapedString = builder.toString();
  33.             if (localName.equalsIgnoreCase(EMP_ID)){
  34.                 String str = escapedString.trim();
  35.                 currentEntry.setEmployeeID(str);
  36.             } else if (localName.equalsIgnoreCase(EMP_NAME)){
  37.                 currentEntry.setEmployeeName(escapedString);
  38.             } else if (localName.equalsIgnoreCase(DESIGNATION)){
  39.                 currentEntry.setDesignation(escapedString);
  40.             } else if (localName.equalsIgnoreCase(DEPARTMENT)){
  41.                 currentEntry.setDepartment(escapedString);
  42.             } else if (localName.equalsIgnoreCase(EMP_PHOTO)){
  43.                 currentEntry.setImageForEmployee(escapedString);
  44.             } else if (localName.equalsIgnoreCase(ELIGIBLE_FOR)){
  45.                 currentEntry.setEligibleFor(escapedString);
  46.             }
  47.             else if (localName.equalsIgnoreCase(ROOT_EMP_DET)){
  48.                 entries.add(currentEntry);
  49.                 currentEntry = null;
  50.             }
  51.             builder.setLength(0);  
  52.         }
  53.     }
  54.    
  55.     @Override
  56.     public void startDocument() throws SAXException {
  57.         super.startDocument();
  58.         entries = new ArrayList<DbEntry>();
  59.         builder = new StringBuilder();
  60.     }
  61.  
  62.     @Override
  63.     public void startElement(String uri, String localName, String name,
  64.             Attributes attributes) throws SAXException {
  65.         super.startElement(uri, localName, name, attributes);
  66.         if (localName.equalsIgnoreCase(ROOT_EMP_DET)){
  67.             this.currentEntry = new DbEntry();
  68.         }
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement