XMLpidfParser.java
Upload User: liulanlin
Upload Date: 2017-12-08
Package Size: 1274k
Code Size: 8k
Category:

VOIP program

Development Platform:

Java

  1. package gov.nist.applet.phone.ua.pidf.parser;
  2. import java.io.IOException;
  3. import java.io.StringReader;
  4. import org.xml.sax.Attributes;
  5. import org.xml.sax.InputSource;
  6. import org.xml.sax.SAXException;
  7. import org.xml.sax.SAXParseException;
  8. import org.xml.sax.XMLReader;
  9. import org.xml.sax.helpers.DefaultHandler;
  10. import javax.xml.parsers.SAXParserFactory;
  11. import javax.xml.parsers.SAXParser;
  12. /** parser for a XML file
  13.  */
  14. public class XMLpidfParser extends DefaultHandler {
  15.     
  16.     private PresenceTag presenceTag;
  17.     private PresentityTag presentityTag;
  18.     private AtomTag atomTag;
  19.     private AddressTag addressTag;
  20.     private StatusTag statusTag;
  21.     private MSNSubStatusTag msnSubStatusTag;
  22.     
  23.     private XMLReader xmlReader;
  24.     
  25.     private static long id=1000;
  26.     
  27.     /** start the parsing
  28.      * @param file to parse
  29.      * @return Vector containing the test cases
  30.      */
  31.     public XMLpidfParser(String fileLocation) {
  32.          try {
  33.             SAXParserFactory saxParserFactory=SAXParserFactory.newInstance();
  34. SAXParser saxParser=saxParserFactory.newSAXParser();
  35.             xmlReader = saxParser.getXMLReader();
  36. xmlReader.setContentHandler(this);
  37. xmlReader.setFeature
  38.             ("http://xml.org/sax/features/validation",false);
  39.             // parse the xml specification for the event tags.
  40. xmlReader.parse(fileLocation);
  41.            
  42.         } catch (SAXParseException spe) {
  43.             spe.printStackTrace();
  44.         } catch (SAXException sxe) {
  45.             sxe.printStackTrace();
  46.         } catch (IOException ioe) {
  47.             // I/O error
  48.             ioe.printStackTrace();
  49.         } catch (Exception pce) {
  50.             // Parser with specified options can't be built
  51.             pce.printStackTrace();
  52.         }
  53.     }
  54.     /** start the parsing
  55.      * @param file to parse
  56.      * @return Vector containing the test cases
  57.      */
  58.     public XMLpidfParser() {
  59.         try {
  60. SAXParserFactory saxParserFactory=SAXParserFactory.newInstance();
  61. SAXParser saxParser=saxParserFactory.newSAXParser();
  62. xmlReader = saxParser.getXMLReader();
  63. xmlReader .setContentHandler(this);
  64. //xmlReader .setFeature
  65. //("http://xml.org/sax/features/validation",false);
  66. // parse the xml specification for the event tags.
  67.    
  68.         } catch (Exception e) {
  69.             e.printStackTrace();
  70.         }
  71.        
  72.     }
  73.     
  74.     public void parsePidfString(String body) {
  75.         try {
  76.             StringReader stringReader=new StringReader(body);
  77.             InputSource inputSource=new InputSource(stringReader);
  78.             inputSource.setSystemId("file://");
  79.             xmlReader.parse(inputSource);
  80.         } catch (Exception e) {
  81.             e.printStackTrace();
  82.         }
  83.     }
  84.     
  85.     public PresenceTag getPresenceTag() {
  86.         return presenceTag;
  87.     }
  88.     
  89.     //===========================================================
  90.     // SAX DocumentHandler methods
  91.     //===========================================================
  92.     public void startDocument() throws SAXException {
  93.         try {
  94.              System.out.println("Parsing XML pidf string");
  95.         } 
  96.         catch (Exception e) {
  97.             throw new SAXException("XMLpidfParser error", e);
  98.         }
  99.     }
  100.     public void endDocument() throws SAXException {
  101.         try {
  102. System.out.println("XML pidf string parsed successfully!!!");
  103.         } 
  104.         catch (Exception e) {
  105.             throw new SAXException("XMLpidfParser error", e);
  106.         }
  107.     }
  108.     public void startElement(String namespaceURI,
  109.                              String lName, // local name
  110.                              String qName, // qualified name
  111.                              Attributes attrs)
  112.                              throws SAXException
  113.     {
  114.         String element=qName;
  115.         if (element.compareToIgnoreCase("presence") ==0 ) {
  116.             presenceTag=new PresenceTag();
  117.         }
  118.         if (element.compareToIgnoreCase("presentity") ==0 ) {
  119.             presentityTag=new PresentityTag();
  120.             String uri= attrs.getValue("uri");
  121.             if (uri!=null) {
  122.                 uri=uri.trim();
  123.                 presentityTag.setURI(uri);
  124.             }
  125.             else {
  126.                 System.out.println("ERROR, XMLpidfParser, the presentity uri is null");
  127.             }
  128.         }
  129.         if (element.compareToIgnoreCase("atom") ==0 ) {
  130.             atomTag=new AtomTag();
  131.             String id=attrs.getValue("id");
  132.             if (id!=null) {
  133.                 id=id.trim();
  134.                 atomTag.setId(id);
  135.             }
  136.             else {
  137.                 System.out.println("ERROR, XMLpidfParser, the atom id is null");
  138.             }
  139.         }
  140.         if (element.compareToIgnoreCase("address") ==0 ) {
  141.             addressTag=new AddressTag();
  142.             String uri=attrs.getValue("uri");
  143.             if (uri!=null) {
  144.                 uri=uri.trim();
  145.                 addressTag.setURI(uri);
  146.             }
  147.             else {
  148.                 System.out.println("ERROR, XMLpidfParser, the address uri is null");
  149.             }
  150.             
  151.             String priority=attrs.getValue("priority");
  152.             if (priority!=null) {
  153.                 try {
  154.                     addressTag.setPriority(Float.parseFloat(priority.trim()));
  155.                 }
  156.                 catch(Exception e) {
  157.                     e.printStackTrace();
  158.                 }
  159.             }
  160.             else {
  161.                 System.out.println("DEBUG, XMLpidfParser, the priority is null");
  162.             }
  163.         }
  164.         if (element.compareToIgnoreCase("status") ==0 ) {
  165.             statusTag=new StatusTag();
  166.             String status=attrs.getValue("status");
  167.             if (status!=null) {
  168.                 status=status.trim();
  169.                 statusTag.setStatus(status);
  170.             }
  171.             else {
  172.                 System.out.println("ERROR, XMLpidfParser, the status status is null");
  173.             }
  174.            
  175.         }
  176.         if (element.compareToIgnoreCase("msnsubstatus") ==0 ) {
  177.             msnSubStatusTag=new MSNSubStatusTag();
  178.             String msnSubStatus=attrs.getValue("substatus");
  179.             if (msnSubStatus!=null) {
  180.                 msnSubStatus=msnSubStatus.trim();
  181.                 msnSubStatusTag.setMSNSubStatus(msnSubStatus);
  182.             }
  183.             else {
  184.                 System.out.println("ERROR, XMLpidfParser, the msnsubstatus substatus is null");
  185.             }
  186.             
  187.         }
  188.     }
  189.     
  190.     public void endElement(String namespaceURI,
  191.     String sName, // simple name
  192.     String qName  // qualified name
  193.     )
  194.     throws SAXException
  195.     {
  196.         String element=qName;
  197.         if (element.compareToIgnoreCase("presence") ==0 ) {
  198.         }
  199.         if (element.compareToIgnoreCase("presentity") ==0 ) {
  200.            presenceTag.setPresentityTag(presentityTag);
  201.         }
  202.         if (element.compareToIgnoreCase("atom") ==0 ) {
  203.            presenceTag.addAtomTag(atomTag);
  204.         }
  205.         if (element.compareToIgnoreCase("address") ==0 ) {
  206.            atomTag.setAddressTag(addressTag);
  207.         }
  208.         if (element.compareToIgnoreCase("status") ==0 ) {
  209.             addressTag.setStatusTag(statusTag);
  210.         }
  211.         if (element.compareToIgnoreCase("msnsubstatus") ==0 ) {
  212.             addressTag.setMSNSubStatusTag(msnSubStatusTag);
  213.         }
  214.     }
  215.     public void characters(char buf[], int offset, int len)
  216.     throws SAXException
  217.     {
  218.         String str = new String(buf, offset, len);
  219.     }
  220.     public static String createXMLBody(String status,String subStatus,String subscriberName,
  221.     String contactAddress) {
  222.         PresenceTag presenceTag=new PresenceTag();
  223.         PresentityTag presentityTag=new PresentityTag();
  224.         presentityTag.setURI(subscriberName);
  225.         presenceTag.setPresentityTag(presentityTag);
  226.         AtomTag atomTag=new AtomTag();
  227.         StatusTag statusTag=new StatusTag();
  228.         statusTag.setStatus(status);
  229.         MSNSubStatusTag msnSubStatusTag=new MSNSubStatusTag();
  230.         msnSubStatusTag.setMSNSubStatus(subStatus);
  231.         AddressTag addressTag=new AddressTag();
  232.         addressTag.setStatusTag(statusTag);
  233.         addressTag.setMSNSubStatusTag(msnSubStatusTag);
  234.         addressTag.setURI("sip:"+contactAddress);
  235.         atomTag.setAddressTag(addressTag);
  236.         atomTag.setId("nist-sipId"+id);
  237.         id++;
  238.         presenceTag.addAtomTag(atomTag);
  239.         
  240.         String result=presenceTag.toString();
  241.         
  242.         return result;
  243.     }
  244.     
  245. }