Advertisement
Guest User

Untitled

a guest
Oct 3rd, 2010
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.53 KB | None | 0 0
  1.  /**
  2.      * Gets the title of webpages
  3.      * @param none
  4.      *
  5.      * @return the title
  6.      */
  7.     private void getTitle()
  8.     {
  9.         String html = "";
  10.         String tmp = "";
  11.         String charSet = "UTF-8"; //this is later overwritten
  12.         Pattern pTitle = Pattern.compile("<title>(.*?)</title>");
  13.         Pattern pCharSet = Pattern.compile("charset=(.*?)\"");
  14.        
  15.         try
  16.         {
  17.            
  18.             //sets the url
  19.             URL url = new URL(line);
  20.             //opens the connection
  21.             URLConnection urlConnection = url.openConnection();
  22.             //gets the input stream and stores it
  23.             //DataInputStream dis = new DataInputStream(urlConnection.getInputStream());
  24.             BufferedReader d = new BufferedReader(
  25.                                 new InputStreamReader(
  26.                                     urlConnection.getInputStream()));
  27.             charSet = new InputStreamReader(
  28.                                     urlConnection.getInputStream()
  29.                                     ).getEncoding();
  30. //String content = urlConnection.getContentEncoding();
  31. //Map Content = urlConnection.getHeaderFields();
  32.             //the html string to store it
  33.             //tmp = d.readLine();
  34.             //html = d.readLine();
  35.             //add all the lines to a String
  36.             while ((tmp = d.readLine()) != null)
  37.             {
  38.                 html += " " + tmp;
  39.                 //replace all the \s[ \t\n\x0B\f\r] with an whitespace
  40.                 html = html.replaceAll("\\s+", " ");
  41.                 Matcher mCharSet = pCharSet.matcher(html);
  42.                 if(mCharSet.find() == true)
  43.                 {
  44.                     System.out.println("CharSet Match: "+ mCharSet.group(1));
  45.                     charSet = mCharSet.group(1);
  46.                     break;
  47.                 }
  48.             }
  49.            
  50.              
  51.             //opens the connection again
  52.             urlConnection = url.openConnection();
  53.            
  54.             //tmp = new String(tmp.getBytes(charSet), charSet);
  55.             d = new BufferedReader(
  56.                                 new InputStreamReader(
  57.                                     urlConnection.getInputStream(), charSet));
  58.  
  59.  
  60.             while ((tmp = d.readLine()) != null)
  61.             {
  62.                 html += " " + tmp;
  63.                
  64.                 Matcher mTitle = pTitle.matcher(html);
  65.                 if(mTitle.find() == true)
  66.                 {
  67.                     //replace all the \s[ \t\n\x0B\f\r] with an whitespace
  68.                 mTitle.group(1).replaceAll("\\s+", " ");
  69. // choose an encoding
  70. //Charset cs = Charset.forName( charSet );
  71.  
  72. // for byte to char
  73. //CharsetDecoder decoder = cs.newDecoder();
  74.  
  75. // for char to byte
  76. //CharsetEncoder encoder = cs.newEncoder();
  77.  
  78. // Presuming you have ByteBuffers and CharBuffer objects
  79. // as a side effect of doing nio-style i/o.
  80. //CharBuffer ss = CharBuffer.wrap( mTitle.group(1) );
  81.  
  82. // effectively convert byte[] to char[] after a read
  83. //CharBuffer charBuffer = decoder.decode( ss  );
  84.  
  85. // effectively convert char[] to byte[] before a write
  86. //ByteBuffer byteBuffer = encoder.encode( charBuffer );
  87.  
  88.                     //System.out.println("LINE_0: "+m.group(0));
  89.                     /*
  90.                     System.out.println("LINE_1: "+tmp);
  91.                     String test = "";
  92.                     test = new String(tmp.getBytes(), charSet);
  93.                     System.out.println("TEST_1: "+ test);
  94.                     test = new String(tmp.getBytes(), "Shift-JIS");
  95.                     System.out.println("TEST_2: "+ test);
  96.                     */
  97.                    
  98.                     System.out.println("LINE_1: "+mTitle.group(1));
  99.                     anArrayOfStrings[0] = mTitle.group(1) + " 00";
  100.  
  101.                     String test = "";
  102.                     test = new String(mTitle.group(1).getBytes(), charSet);
  103.                     System.out.println("TEST_1: "+ test);
  104.                     anArrayOfStrings[1] = test + " 01";
  105.                    
  106.                     test = new String(mTitle.group(1).getBytes(), "UTF8");
  107.                     System.out.println("TEST_2: "+ test);
  108.                     anArrayOfStrings[2] = test + " 02";
  109.                    
  110.                     test = new String(mTitle.group(1).getBytes(), "Shift_JIS");
  111.                     System.out.println("TEST_3: "+ test);
  112.                     anArrayOfStrings[3] = test + " 03";
  113.                    
  114.                     test = new String(mTitle.group(1).getBytes(), "UTF-16");
  115.                     System.out.println("TEST_4: "+ test);
  116.                     anArrayOfStrings[4] = test + " 04";
  117.                    
  118.                     test = new String(mTitle.group(1).getBytes(), "UTF-8");
  119.                     System.out.println("TEST_5: "+ test);
  120.                     anArrayOfStrings[5] = test + " 05";
  121.                    
  122.                     test = new String(mTitle.group(1).getBytes(), "ISO-8859-1");
  123.                     System.out.println("TEST_6: "+ test);
  124.                     anArrayOfStrings[6] = test + " 06";
  125.                    
  126.                     test = new String(mTitle.group(1).getBytes(), "US-ASCII");
  127.                     System.out.println("TEST_7: "+ test);
  128.                     anArrayOfStrings[7] = test + " 07";
  129.                    
  130.                     test = new String(mTitle.group(1).getBytes(), "Windows-1250");
  131.                     System.out.println("TEST_8: "+ test);
  132.                     anArrayOfStrings[8] = test + " 08";
  133.                    
  134.                     test = new String(mTitle.group(1).getBytes(), "ISO-8859-15");
  135.                     System.out.println("TEST_9: "+ test);
  136.                     anArrayOfStrings[9] = test + " 09";
  137.                    
  138.                    // setLine(mTitle.group(1), charSet);
  139.                     setLine(new String(mTitle.group(1).getBytes(), charSet));
  140.                    
  141.                     break;
  142.                 }
  143.                
  144.             }
  145.             //close the stream
  146.             //dis.close();
  147.             //replace all the \s[ \t\n\x0B\f\r] with an whitespace
  148.             //html = html.replaceAll("\\s+", " ");
  149.             //set a pattern to match against
  150.             //Pattern p = Pattern.compile("<title>(.*?)</title>");
  151.             //matches
  152.             //Matcher m = p.matcher(html);
  153.             //if(m.find() == true)
  154.             //{
  155.                 //System.out.println("LINE_0: "+m.group(0));
  156.                 //System.out.println("LINE_1: "+m.group(1));
  157.                 //setLine(m.group(1));
  158.             //}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement