lyngsat网站卫星数据解析

 package com.wh.sattp;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.OutputStreamWriter;
public class Dealhtml {
 
 private static String filepath="";
 private static String filepath_common="";
 private static String filepath_android="";
 
 /*处理的文件夹。在D:/satdeal/下根据地区创建文件夹,每个文件夹 下面 分为 txt common android 三个子文件夹
  txt中的数据为网站的源代码保存的txt文本数据。common为解析出的通用数据*/
 //private static String FilePath="D:/satdeal/atlantic/";
 //private static String FilePath="D:/satdeal/europe/";
 //private static String FilePath="D:/satdeal/asia/";
 private static String FilePath="D:/satdeal/test/";
 
 public static void main(String[] args) {
  //列出当前文件夹里面的内容,删除掉common和android里面的文件
  File file=new File(FilePath+"txt");
  File files[]=file.listFiles();
  int size=files.length;
  filepath_common=FilePath+"/common/";
  filepath_android=FilePath+"/android/";
  System.out.println(System.currentTimeMillis());
  fileinit();
  for(int i=0;i<size;i++)
  {
  filepath=files[i].getAbsolutePath();
  filepath_common=FilePath+"/common/";
  filepath_android=FilePath+"/android/";
  //System.out.println("str==" + filepath);
  dealfile(filepath);
  }
  System.out.println(System.currentTimeMillis());
 }
 
 /*
  * 删除掉文件夹中原有的文件
  * */
 private static void fileinit()
 {
  File file=new File(filepath_common);
  File files[]=file.listFiles();
  int csize=files.length;
  for(int i=0;i<csize;i++)
  {
  files[i].delete();
  }
  file=new File(filepath_android);
  files=file.listFiles();
  csize=files.length;
  for(int i=0;i<csize;i++)
  {
  files[i].delete();
  }
 }
 /*
  * 拿到指定的txt文本 开始解析数据 
  * 1.解析<title> 里面包含卫星名及经度,
  * 2.确定common 和android下面的文件名
  * */
 private static void dealfile(String path) {
  File file = new File(path);
  FileInputStream ins;
  
  try {
   ins = new FileInputStream(file);
   int readsize;
   byte b[] = new byte[1024000];
   String str=null;
   while ((readsize = ins.read(b)) >= 0) {
    System.out.println("readsize=====" + readsize);
    byte first[]=new byte[100];
    for(int g=0;g<100;g++)
    {
     first[g]=b[g];
    }
    String fis=new String(first);
    int index1=fis.indexOf("<title>");
    int index2=fis.indexOf("</title>");
    String filename=fis.substring(index1+7, index2).replace('\\',' ');
    filename=filename.replace('/', ' ');
    filepath_common+=filename+".txt";
    filepath_android+=filename+".xml";
    System.out.println("filename=====" + filename);
    //<title>Thor 5 at 0.8°W - LyngSat</title>
    str += new String(Byte_dealto_String(b,readsize));
    //System.out.println("str=====" + str);
   }
   dealstr(str); 
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 
 
 /*
  * 处理传入的byte数组
  * 规则:1.先取出所有的 HVRL字符;
  *     2.原样过滤掉前五个和最后五个数据    实际传入的数据是要<1024000的
  *     3.判断连续的四个数字或者五个数字(频率和符号率都是4位或者5位的)
  *    freq的两种格式:  >123456&   和> 1234&
  *    symb的两种格式: >12345-    和>1234-
  *   取出用count标示:
  *      小于4时跳过相同个数
  *      count==10 4位freq
  *      count==11 4位symb
  *      count==20 5位freq
  *      count==21 5位symb
  *   4.freq 用<>包裹起来   symb 用* #并转行包裹  返回有效byte数组
  * 
  * */
 private static byte[] Byte_dealto_String(byte[] b,int size) {
  byte c[] = new byte[10240];
  int j = 0;
  //int size = b.length;
  //System.out.println("size===="+size);
  for (int i = 0; i < size; i++) {
   int count=1;
   if((b[i]=='H'||b[i]=='V')||(b[i]=='R'||b[i]=='L'))
   {
    if(i-2>=0&&b[i-1]==';'&&b[i-2]=='p'&&b[i+1]=='<')
    {
    c[j] = b[i];
    j++;
    }
   }
   if (i < 5 || i > size-5) {
    
    if (b[i] >= '0' && b[i] <= '9') {
     c[j] = b[i];
     j++;
    }
   } else {
    if (b[i] >= '0' && b[i] <= '9') {
     count=1;
     if (b[i + 1] >= '0' && b[i + 1] <= '9') {
      count=2;
      if (b[i + 2] >= '0' && b[i + 2] <= '9') {
       count=3;
       if (b[i + 3] >= '0' && b[i + 3] <= '9') {
        count=4;
        if(b[i-1]=='>'||(b[i-2]=='>'&&b[i-1]==' '))
        {
         if(b[i + 4] >= '0' && b[i + 4] <= '9')
         { 
         
          count=5;
          if(b[i + 5] == '&')
          {
           count=20; 
          }else if(b[i + 5] == '-')
          {
           count=21; 
          }
         }else if(b[i + 4] == '&')
         {
          count=10;
         }else if(b[i + 4]=='-')
         {
          count=11;
         }
        } 
       }
      }
     }
    }
   }
   
   
   if(count<4)
   {
    i+=count-1;
   }else
   {
    if(count>5&&count<20)
    {
     //freq
    if(count==10)
    {
     c[j]='<';
     j++;
    }else if(count==11)
    {
     c[j]='*';
     j++;
    }
       c[j]=b[i];
    j++;
    i++;
    c[j]=b[i];
    j++;
    i++;
    c[j]=b[i];
    j++;
    i++;
    c[j]=b[i];
    j++;
    i++;
     if(count==10)
     {
      c[j]='>';
      j++;
     }else if(count==11)
     {
      c[j]='#';
      j++;
      c[j]='\n';
      j++;
     }
    }
    
    if(count>12)
    {
     //freq
    if(count==20)
    {
     c[j]='<';
     j++;
    }else if(count==21)
    {
     c[j]='*';
     j++;
    }
       c[j]=b[i];
    j++;
    i++;
    c[j]=b[i];
    j++;
    i++;
    c[j]=b[i];
    j++;
    i++;
    c[j]=b[i];
    j++;
    i++;
    c[j]=b[i];
    j++;
    i++;
     if(count==20)
     {
      c[j]='>';
      j++;
     }else if(count==21)
     {
      c[j]='#';
      j++;
      c[j]='\n';
      j++;
     }
    }
    
   }
   
  }
  
  c[j]=' ';
  j++;
  c[j]=' ';
  byte d[] = new byte[j];
  int dsize=d.length;
  
  for(int k=0;k<j;k++)
  {
   d[k]=c[k];
  }
  return d;
 }
 
 /*
  * 处理得到的数据。
  * 根据 \n来切出字符串数组   此时有效数据为<12345>V*67890#
  *   解析数组 先找到 VHRL*的组合定位 < #的位置
  *   规则 : 1.已VHRL为起始 前6和前7 定位<  后6后7定位#
  *    2.按照定位切割有用数据 保存为 <12345>V*67890
  *      判断检测切割的数据  
  *         1.检测 >  位置应该大于等于5且小于等于6  
  *         2.判断<>中的数字 应该在3000-13000之间
  *      
  *      
  *    解析完毕 开始存储数据到指定文件夹    
  * */
 private static void dealstr(String str)
 {
  String all[]=str.split("\n"); 
  //System.out.println("str===" + all.length);
  int size=all.length;
  int count=0;
  String strnew[]=new String[200];
  for(int i=0;i<size;i++)
  {
   byte bstr[]=all[i].getBytes();
   int len= bstr.length;
 
   for(int j=0;j<len;j++)
   {
    if((bstr[j]=='R'||bstr[j]=='L'||bstr[j]=='H'||bstr[j]=='V')&&bstr[j+1]=='*')
    {
     int first=0;
     int end=0;
     if(j-7>=0&&bstr[j-7]=='<')
     {
      first=j-7;
     }
     if(j-6>=0&&bstr[j-6]=='<')
     {
      first=j-6;
     }
     if(j+7<len&&bstr[j+7]=='#')
     {
      end=j+7;
     }
     if(j+6<len&&bstr[j+6]=='#')
     {
      end=j+6;
      
     }
     String str_us=all[i].substring(first, end);
     //System.out.println("str_us==" + str_us);
     int freqindex=str_us.indexOf('>');
     
     if(freqindex<=4)
     {
      System.out.println(" Error**** freqindex=" + freqindex+"  str_us="+str_us);
      continue;
     }
     String strfreq=str_us.substring(1, freqindex);
     
     int freq=Integer.valueOf(strfreq);
     if(freq>13000||freq<3000)
     {
      System.out.println("Error****  freq==" + strfreq);
      //continue;
     }else{
     strnew[count]=all[i].substring(first, end);
     count++;
     }
     break;
    }
   }
  }
  
  try {
   FileWriter fw = new FileWriter(filepath_common,true);    
   String allstr="";
   for(int i=0;i<count;i++)
   { 
    int lens=strnew[i].length();
    if(lens<11||lens>15)
    {
     System.out.println("Error:****"+filepath_common+"****"+strnew[i]);
    }else{
    fw.write(strnew[i]+"\n",0,lens+1);
    }  
   }
   fw.flush(); 
  }catch(Exception e)
  {
   System.out.println(e.toString());
  }
  String_toandroid_file(strnew,count);
 }
 
 /*
  * 将数据保存为需要的android  xml数据
  * 数据原样为<12345>V*67890
  *   1.先替换<> 数据变为    <transponder frequency="12345000" symbol_rate="V*67890
  *   2.在将字串切为   <transponder frequency="12345000" symbol_rate="V    67890
  *   3.在切为  <transponder frequency="12345000" symbol_rate="   V 
  *   4.拼凑 <transponder frequency="12345000" symbol_rate="67890000"
  *   5.最后<transponder frequency="12345000" symbol_rate="67890000" polarisation="V"/>
  * 
  * */
 private static void String_toandroid_file(String allstr[],int count)
 {
  int size=allstr.length;
  
  for(int i=0;i<count;i++)
  {
  //<10962>H*1550
  String str=allstr[i];
  str=str.replaceAll( "<","<transponder frequency=\"");
  str=str.replaceAll( ">","000\" symbol_rate=\"");
  String strw[]=new String[2];
  
  int index=str.indexOf('*');
  strw[0]=str.substring(0, index);
  strw[1]=str.substring(index+1, str.length());
  
  int length=strw[0].length();
  
  String strq=strw[0].substring(length-1, length);
  String stra=strw[0].substring(0,length-1);
  stra+=""+strw[1]+"000\"";
  if(strq.equals("H")||strq.equals("L"))
  {
  stra+= " polarisation=\"H\"/>";
  }else
  {
  stra+= " polarisation=\"V\"/>";
  }
  stra+="\n";
  allstr[i]=stra;
  }
  
  try {
   FileWriter fw = new FileWriter(filepath_android,true);    
   //String allstr="";
   for(int i=0;i<count;i++)
   { 
    fw.write(allstr[i],0,allstr[i].length());   
   }
   fw.flush(); 
  }catch(Exception e)
  {
   System.out.println(e.toString());
  }
 }
}

转载于:https://my.oschina.net/u/865093/blog/372272

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值