home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / CHIP_CD_1998_11_PL.iso / offline / internet / asp / asp2html / IISASP2HTM1.EXE / Source / Asp2Htm.java < prev    next >
Encoding:
Java Source  |  1997-07-15  |  4.5 KB  |  254 lines

  1. package IISSample;
  2.  
  3. import java.net.URL;
  4. import java.net.URLConnection;
  5. import java.net.MalformedURLException;
  6.  
  7. import java.io.*;
  8. import java.io.IOException;
  9. import java.io.InputStream;
  10.  
  11. //
  12. //
  13. // Asp2Htm
  14. //
  15. //
  16.  
  17. class Asp2Htm 
  18. {
  19.     URL myURL = null;
  20.     URLConnection myURLConnection = null;
  21.     String sData = "<no data>";
  22.     String sTitle = "<no title>";
  23.     String sBody = "<no body>";
  24.     boolean fGottenData = false;
  25.  
  26.     public void URL(String tempURL)
  27.     {
  28.         
  29.         try
  30.         {
  31.             myURL = new URL(tempURL);
  32.         }
  33.         catch (MalformedURLException e)
  34.         {
  35.         }
  36.         
  37.         try
  38.         {
  39.             myURLConnection = myURL.openConnection();
  40.         }
  41.         catch (IOException e)
  42.         {
  43.         }
  44.     
  45.     }
  46.  
  47.     public String Title()
  48.     {
  49.         GetData();
  50.  
  51.         return sTitle;
  52.     }
  53.     
  54.     public String Body()
  55.     {
  56.         GetData();
  57.  
  58.         return sBody;
  59.     }
  60.     
  61.     public String File()
  62.     {
  63.         return myURL.getFile();
  64.     }
  65.  
  66.     public String Host()
  67.     {
  68.         return myURL.getHost();
  69.     }
  70.  
  71.     public int Port()
  72.     {
  73.         return myURL.getPort();
  74.     }
  75.  
  76.     public String Protocol()
  77.     {
  78.         return myURL.getProtocol();
  79.     }
  80.  
  81.     public String Ref()
  82.     {
  83.         return myURL.getRef();
  84.     }
  85.  
  86.     public String ContentEncoding()
  87.     {
  88.         return myURLConnection.getContentEncoding();
  89.     }
  90.  
  91.     public int ContentLength()
  92.     {
  93.         return myURLConnection.getContentLength();
  94.     }
  95.  
  96.     public String ContentType()
  97.     {
  98.         return myURLConnection.getContentType();
  99.     }
  100.  
  101.     public long Date()
  102.     {
  103.         return myURLConnection.getDate();
  104.     }
  105.     
  106.     public long Expiration()
  107.     {
  108.         return myURLConnection.getExpiration();
  109.     }
  110.     
  111.     public String LastModified()
  112.     {
  113.         return myURLConnection.getHeaderField("Last-Modified");
  114.     }
  115.     
  116.     public String Server()
  117.     {
  118.         return myURLConnection.getHeaderField("Server");
  119.     }
  120.  
  121.     public boolean GetData()
  122.     {
  123.         if (fGottenData)
  124.             return true;
  125.  
  126.         // REVIEW: this could be large
  127.         int cb =  ContentLength();
  128.  
  129.         if (cb < 0)
  130.             cb = 16000; // no Content-Length specified, so guess
  131.  
  132.         byte bt[]= new byte[cb];
  133.         InputStream inStream = null;
  134.         String st = new String();
  135.  
  136.         try
  137.         {
  138.             inStream = myURLConnection.getInputStream();
  139.         }
  140.         catch (IOException e)
  141.         {
  142.             return false;
  143.         }
  144.  
  145.         try
  146.         {
  147.             do
  148.             {
  149.                 cb = inStream.read(bt);
  150.                 if (cb > 0)
  151.                 {
  152.                     String stBuff =  new String(bt, 0);
  153.                     st = st.concat(stBuff.substring(0, cb));
  154.                 }
  155.             } while (cb > 0);
  156.         }
  157.         catch (IOException e)
  158.         {
  159.             return false;
  160.         }
  161.  
  162.         sData = st.toLowerCase();
  163.         
  164.         int iHtml = sData.indexOf("<html>");
  165.         if ( iHtml >= 0 )
  166.         {
  167.             int iEndHtml = sData.indexOf("</html>");
  168.             if ( iEndHtml > 0 )
  169.             {
  170.                 sData = sData.substring(iHtml,iEndHtml + 7);
  171.                 st = st.substring(iHtml,iEndHtml + 7);
  172.             }
  173.         }
  174.  
  175.         int iHead = sData.indexOf("<head>");
  176.         int iEndHead = -1;
  177.  
  178.         if (iHead >= 0)
  179.         {
  180.             int iTitle = sData.indexOf("<title>", iHead);
  181.  
  182.             if (iTitle > 0)
  183.             {
  184.                 int iEndTitle = sData.indexOf("</title>", iTitle);
  185.  
  186.                 if (iEndTitle > 0)
  187.                 {
  188.                     iEndHead = sData.indexOf("</head>", iEndTitle);
  189.                     sTitle = st.substring(iTitle + 7, iEndTitle);
  190.                 }
  191.             }
  192.         }
  193.                 
  194.         int iBody = sData.indexOf("<body");
  195.  
  196.         if (iBody >= 0)
  197.         {
  198.             int iEndBody = sData.indexOf("</body>", iBody);
  199.  
  200.             if (iEndBody > 0)
  201.                 sBody = st.substring(iBody + 6, iEndBody);
  202.             else
  203.                 sBody = st.substring(iBody + 6);
  204.         }
  205.         else
  206.         {
  207.             if (iEndHead > 0)
  208.                 sBody = st.substring(iEndHead + 7);
  209.         }
  210.  
  211.         sData = st;
  212.         fGottenData = true;
  213.  
  214.         return true;
  215.     }
  216.  
  217.     public boolean WriteToFile(String sFileName)
  218.     {
  219.         if (!GetData())
  220.             return false;
  221.         
  222.         File outFile = new File(sFileName);
  223.         FileOutputStream outStream = null;
  224.  
  225.         try
  226.         {
  227.             outStream = new FileOutputStream(outFile);
  228.         }
  229.         catch (IOException ioe)
  230.         {
  231.             System.out.println("Could not open output file: " + sFileName);
  232.             return false;
  233.         }
  234.  
  235.         PrintStream prStream = null;
  236.  
  237.         try
  238.         {
  239.             prStream = new PrintStream(outStream);
  240.         }
  241.         catch (IOException ioe)
  242.         {
  243.             System.out.println("Could not open output print stream.");
  244.             return false;
  245.         }
  246.  
  247.         prStream.print(sData);
  248.         prStream.close();
  249.         
  250.         return true;
  251.     }
  252. }
  253.  
  254.