全球参数APN工具代码集成

来源:互联网 发布:高洛峰2014版php视频 编辑:程序博客网 时间:2024/05/01 05:23

ApnToolView.java 主类,负责导入Excel表格然后解析

package com.hq.lipeng;import java.awt.Dimension;import java.awt.GridLayout;import java.awt.Toolkit;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import javax.swing.JButton;import javax.swing.JCheckBox;import javax.swing.JFileChooser;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTextArea;import javax.swing.JTextField;import javax.swing.filechooser.FileNameExtensionFilter;public class ApnToolView extends JFrame { private JLabel jl1, jl2, jl3, jl4; private JPanel jp0, jp1, jp2, jp3, jp4, jp5, jp6, jp7, jp8, jp9; private JTextField jtf1, jtf2, jtf3, jtf4; private JButton jb1, jb2, jb3, jb4, jb5, jb6, jb7; private JCheckBox jcb1, jcb2, jcb3; private JTextArea jta1, jta2, jta3; private boolean mark1 = false, mark2 = false; private String excelpath = ""; String currentpath = System.getProperty("user.dir"); String eccwithsim = "", eccwithoutsim = "", apnxml = "", voicemailxml = ""; public static void main(String[] args) {  new ApnToolView(); } public ApnToolView() {  jp1= new JPanel();  jp2=new JPanel();  jtf1 = new JTextField(15);  jb1 = new JButton("ChooseExcel");  jb2 = new JButton("CreateTheXML");  jp1.add(jtf1);  jp1.add(jb1);  jp2.add(jb2);  this.add(jp1);  this.add(jp2);  super.setTitle("xml生成及比对工具");  this.setLayout(new GridLayout(2, 2, 0, 0));  Dimension d = Toolkit.getDefaultToolkit().getScreenSize();  this.setBounds((d.width - 300) / 2, (d.height - 300) / 2, 310, 150);  this.setResizable(true);  this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  this.setVisible(true);  jb1.addActionListener(new ActionListener() {   public void actionPerformed(ActionEvent e) {    JFileChooser chooser = new JFileChooser(new File(""));    FileNameExtensionFilter filter = new FileNameExtensionFilter(      "excel(*.xls)", "xls");    chooser.setFileFilter(filter);    int returnVal = chooser.showOpenDialog(null);    if (returnVal == JFileChooser.APPROVE_OPTION) {     try {      excelpath = chooser.getSelectedFile().getAbsolutePath();      String str = "XML文件已在当前目录生成生成";      jtf1.setText(excelpath);     } catch (Exception ex) {     }    }   }  });  jb2.addActionListener(new ActionListener() {   @Override   public void actionPerformed(ActionEvent e) {    CustomType apne2x = new CustomType();    apne2x.getExcel(excelpath);    if(apne2x.mark){     JOptionPane.showMessageDialog(rootPane, "apn-conf.xml文件已生成");    }else{     JOptionPane.showMessageDialog(rootPane, "生成程序有错",       "WARMING", JOptionPane.WARNING_MESSAGE);    }   }}); }}


CystomType.java 解析类,将excel表格里面内容转化为xml格式

package com.hq.lipeng;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import org.apache.poi.hssf.usermodel.HSSFRow;import org.apache.poi.hssf.usermodel.HSSFSheet;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.poi.ss.usermodel.Cell;/*carrier="Tele2"numeric="24007"ons_name="Tele2 NO"voicemail_number=""voicemail_tag="voicemail"num_match="7"spn=""pnn=""virtual_net_rule="1"gid=""gid_mask=""imsi_start="2400768"num_match_short=""match_path=""match_file=""match_value=""match_mask=""sms_7bit_enabled=""sms_coding_national=""apn_filter=""real_ecc_withcard="112"special_ecc_withcard="110,113"ecc_without_card="110,112,113"/>*/public class CustomType { boolean mark = true; int[] column={26,27,0,3,4,5,6,7,8,9,11,12}; ArrayList<HashMap<String, String>> apnlist = new ArrayList<HashMap<String, String>>(); String[] apnElement = new String[]{"carrier","voicemail_number","voicemail_tag","num_match","real_ecc_withcard","special_ecc_withcard","ecc_without_card","sms_7bit_enabled","ons_name","apn_filter",  "virtual_net_rule","gid","gid_mask","imsi_start","num_match_short","match_path","match_file","match_value","match_mask"};  String currentpath = "";  public  void getExcel(String excelpath) { //DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();//DocumentBuilder dbu = null;  // Workbook wb = null;  InputStream is = null;  try {   is = new FileInputStream(excelpath);  } catch (FileNotFoundException e2) {   e2.printStackTrace();  }   HSSFWorkbook wb = null;  try {   wb = new HSSFWorkbook(is);  } catch (IOException e2) {   // TODO Auto-generated catch block   e2.printStackTrace();  }  File cp = new File("");  try {   currentpath=cp.getCanonicalPath();  } catch (IOException e) {   // TODO Auto-generated catch block   e.printStackTrace();  }    System.out.println("currentpath::"+currentpath);//File file = new File("apns-conf.xml");  //Element apns = doc.createElement("apns"); //apnlist.put("version", "8");   int sheetLength = wb.getNumberOfSheets();  for (int i = 0; i < sheetLength; i++) {   HSSFSheet sheet = wb.getSheetAt(i);   if (sheet == null) {    continue;   } else {    int rows = sheet.getPhysicalNumberOfRows();    System.out.println("the rows::"+rows);    for (int j = 1; j < rows; j++) {     HSSFRow row = sheet.getRow(j);     // int cellNum = row.getPhysicalNumberOfCells();     if (row == null) {      continue;     } else {      String mvno_type = "";          //      String imsi_start = "";      String gid = "";      String gid_mask = "";      String num_match_short = "";      String match_path = "";      String match_file = "";      String match_value = "";      String match_mask = "";      String spn = "";      String pnn = "";      String virtual_net_rule= "";           //String mvno_match_data = "";//TR6           for (int m = 0; m < column.length; m++) {       String content = "";        if(row.getCell(column[m])==null){         continue;        }else{         row.getCell(column[m]).setCellType(Cell.CELL_TYPE_STRING);         content = row.getCell(column[m]).getStringCellValue().trim();         //System.out.println("content_lipeng"+content.toString());        }             if(m==0&&!content.isEmpty()){       virtual_net_rule = content.toString();       if(content.equals("1")){mvno_type="imsi";}//tr6       if(content.equals("2")){mvno_type="gid1";}//tr6       if(content.equals("3")){mvno_type="spn";}//tr6       if(content.equals("4")){mvno_type="file";}//tr6       continue;      }      if(m==1&&!content.isEmpty()){       if(mvno_type.equals("imsi")){        String []datas=content.split(":");        imsi_start = datas[1].trim();        System.out.println("imsi_start"+imsi_start);       }       if(mvno_type.equals("gid1")){        String[] lines = content.split("\n");        for (int k = 0; k < lines.length; k++) {         String temp[] = lines[k].trim().split(":",2);                  if (temp[0].equals("gid")) {          gid = temp[1].trim();          System.out.println("gid"+gid);          continue;                  }         if (temp[0].equals("gid_mask")) {          gid_mask = temp[1].trim();          System.out.println("gid_mask"+gid_mask);          continue;                  }        }       }       if(mvno_type.equals("spn")){                                String[] lines = content.split("\n");        for (int k = 0; k < lines.length; k++) {         String temp[] = lines[k].trim().split(":",2);                  if (temp[0].equals("spn")) {          spn = temp[1].trim();          System.out.println("spn"+spn);          continue;                  }         if (temp[0].equals("pnn")) {          pnn = temp[1].trim();          continue;                  }        }       }       if(mvno_type.equals("file")){        String[] lines = content.split("\n");        for (int k = 0; k < lines.length; k++) {         String temp[] = lines[k].trim().split(":",2);         if (temp[0].equals("match_path")) {          match_path = temp[1].trim();          System.out.println("match_path"+match_path);          continue;                  }         if (temp[0].equals("match_file")) {          match_file = temp[1].trim();          continue;                  }         if (temp[0].equals("match_value")) {          match_value = temp[1].trim();          continue;                  }         if (temp[0].equals("match_mask")) {          match_mask = temp[1].trim();          continue;                  }            }       }       continue;      }                if (!content.isEmpty()) {       String apn_filer = "";       String voicemail_tag = "";       String numeric= "";       String carrier= "";       String voicemail_number= "";       String voicemail= "";       String num_match= "";       String ons_name= "";              String sms_7bit_enabled= "";       String real_ecc_withcard= "";       String special_ecc_withcard= "";       String ecc_without_card= "";       String mcc= "";       String mnc= "";      HashMap<String, String> apn = new HashMap<String, String>();      //int[] column={26,27,1,3,4,5,6,7,8,9,11,12};           String all[] = content.split("\n");// 单元格内的 内容数组      // 按行分                        System.out.println("content's lines number"+all.length);                        for (int k = 0; k < all.length; k++) {                              String temp = all[k].trim().toString();                 if (m == 2 && !content.isEmpty()) {        carrier = temp.toString();        System.out.println("carrier"+carrier);        apn.put("carrier", carrier);        continue;       }       if (m == 3 && !content.isEmpty()) {        voicemail_number = temp.toString();        apn.put("voicemail_number", voicemail_number);        apn.put("voicemail_tag", "voicemail");        continue;       }       if (m == 4 && !content.isEmpty()) {        num_match = temp.toString();        //System.out.println("num_match"+num_match);        apn.put("num_match", num_match);        continue;       }       if (m == 5 && !content.isEmpty()) {        real_ecc_withcard = temp.toString();        apn.put("real_ecc_withcard", real_ecc_withcard);        continue;       }       if (m == 6 && !content.isEmpty()) {        special_ecc_withcard = temp.toString();        apn.put("special_ecc_withcard", special_ecc_withcard);        continue;       }       if (m == 7 && !content.isEmpty()) {        ecc_without_card = temp.toString();        apn.put("ecc_without_card", ecc_without_card);        continue;       }       if (m == 8 && !content.isEmpty()) {        String seventValue = "";        seventValue = temp.toString();        //String sms_7bit_enabled = cell9.toString() + "";    // 7bit support        if(seventValue!=null && !"".equals(seventValue) && "7bit".equals(seventValue)){         sms_7bit_enabled = "1";         System.out.println("sms_7bit_enabled "+sms_7bit_enabled);        }else if(seventValue!=null && !"".equals(seventValue)&& (seventValue.contains("7bit(葡萄牙,无损)")||seventValue.contains("7bit(葡萄牙,无损)"))){         sms_7bit_enabled = "1";         System.out.println("add line 葡语: "+i);        }        apn.put("sms_7bit_enabled", sms_7bit_enabled);        continue;       }       if (m == 9 && !content.isEmpty()) {        ons_name = temp.toString();        apn.put("ons_name", ons_name);        continue;       }       if (m == 10 && !content.isEmpty()) {        mcc = temp.toString();        continue;       }       if (m == 11 && !content.isEmpty()) {        mnc = temp.toString();        numeric=mcc+mnc;        System.out.println("numeric "+numeric);        apn.put("numeric", numeric);        continue;       }                        }                if(!mvno_type.isEmpty()){//tr6       System.out.println("hugo1");//apn.put("mvno_type", mvno_type);//apn.put("mvno_match_data", mvno_match_data);       apn.put("virtual_net_rule", virtual_net_rule);       apn.put("imsi_start", imsi_start);       apn.put("gid", gid);       apn.put("gid_mask", gid_mask);       apn.put("num_match_short", num_match_short);       apn.put("match_path", match_path);       apn.put("match_file", match_file);       apn.put("match_value", match_value);       apn.put("match_mask", match_mask);       apn.put("spn", spn);       apn.put("pnn", pnn);      }//tr6      if(apn!=null){       System.out.println("hugo2");       apnlist.add(apn);       System.out.println("apnlist.size"+apnlist.size());      }      }                          }     }          }    }   }       WriteToXml(apnlist);    //lipeng        }  private  void WriteToXml(List<HashMap<String, String>> apns) {  System.out.println("WriteToXml");  OutputStream out = null;  File xmlFile;  final String header = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n<apns version=\"8\">\n";  final String heal = "</apns>";  byte[] buffer;  if(apns.size() >0){   System.out.println("total apns size : " + apns.size());   buffer = header.getBytes();   xmlFile = new File(currentpath+ "apns-conf.xml");   try {    out = new FileOutputStream(xmlFile);    out.write(buffer);       WriteCustomApns(out,apns);    System.out.println("WriteCustomApns");    out.write(heal.getBytes());    out.close();   } catch (Exception e) {    if(out != null)     try {      out.close();     } catch (IOException e1) {     }     e.printStackTrace();   }  } } private  void WriteCustomApns(OutputStream out,List<HashMap<String, String>> apns) {  String body="";  String key;  String value = null;//String authtype;  HashMap<String, String> apn;  int size = apns.size();  System.out.println("total apns size : " + size);  try {   for (int i = 0; i < size; i++) {    apn = apns.get(i);    out.write("  <apn ".getBytes());    for (int j = 0; j < apnElement.length; j++) {     //System.out.println(" key : "+apnElement[j]+"  value : "+apn.get(apnElement[j]));     if(apn.get(apnElement[j])!= null){      key = apnElement[j].trim();      value = apn.get(apnElement[j]).trim();      //System.out.println(key+" & & & "+value);           body ="\t"+key + "=\"" + value + "\"\n";      out.write(body.getBytes("UTF-8"));     }else{      key = apnElement[j].trim();      if(j==13){       break;      }      value="";      body = "\t"+key + "=\"" + value + "\"\n";      out.write(body.getBytes("UTF-8"));     }    }    //System.out.println("***************************  " + i);    out.write("  />\n\n".getBytes());   }  } catch (Exception e) {   e.printStackTrace();  } } }



0 0
原创粉丝点击