Facebook
From Tacky Marten, 5 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 255
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package newsfeedreader_kbeham17;
  7.  
  8. import java.io.File;
  9. import java.io.IOException;
  10. import java.io.InputStream;
  11.  
  12. import java.net.URLConnection;
  13. import java.util.Enumeration;
  14. import java.util.Hashtable;
  15. import java.util.logging.Level;
  16. import java.util.logging.Logger;
  17. import javax.xml.parsers.ParserConfigurationException;
  18. import javax.xml.parsers.SAXParser;
  19. import javax.xml.parsers.SAXParserFactory;
  20. import org.xml.sax.Attributes;
  21. import org.xml.sax.SAXException;
  22. import org.xml.sax.helpers.DefaultHandler;
  23.  
  24. /**
  25.  *
  26.  * @author kbeham17
  27.  */
  28. public class SAXLocalNameCount extends DefaultHandler {
  29.  
  30.     InputStream is = null;
  31.     private Hashtable tags;
  32.  
  33.     public SAXLocalNameCount(InputStream is) {
  34.         this.is = is;
  35.     }
  36.  
  37.     public void start() {
  38.  
  39.         SAXParserFactory spf = SAXParserFactory.newInstance();
  40.         if (spf != null) {
  41.             SAXParser sp = null;
  42.             try {
  43.                 sp = spf.newSAXParser();
  44.                 sp.parse(is, this);
  45.  
  46.             } catch (ParserConfigurationException | SAXException | IOException ex) {
  47.                 Logger.getLogger(SAXLocalNameCount.class.getName()).log(Level.SEVERE, null, ex);
  48.             }
  49.         }
  50.     }
  51.  
  52.     @Override
  53.     public void endElement(String namespaceURL, String localName, String qName) throws SAXException {
  54.        
  55.        
  56.     }
  57.  
  58.     @Override
  59.     public void startElement(String namespaceURL,String localName,String qName, Attributes atts) throws SAXException
  60.     {
  61.         String key = localName;
  62.         Object value = tags.get(key);
  63.  
  64.         if (value == null) {
  65.             tags.put(key, new Integer(1));
  66.         }
  67.         else {
  68.             int count = ((Integer)value).intValue();
  69.             count++;
  70.             tags.put(key, new Integer(count));
  71.         }
  72.     }
  73.  
  74.     public String convertToFileURL(String filename) {
  75.         String path = new File(filename).getAbsolutePath();
  76.         if (File.separatorChar != '/') {
  77.             path = path.replace(File.separatorChar, '/');
  78.         }
  79.  
  80.         if (!path.startsWith("/")) {
  81.             path = "/" + path;
  82.         }
  83.         return path;
  84.     }
  85.  
  86. }
  87.