CopybookTool MQ工具

public class CopybookTool {
 private final static String ERROR_MESSAGE = "**********ERROR************\n";
 private final static String LENGTH_WARNING_MESSAGE = "*******WARNING MESSAGE LENGTH OVER 90000*******\n";
 private static String targetName="";
 private static String dir = "C:\\copybook";
 public static void main(String[] args){
  CopybookTool cb=new CopybookForMLT();
  File file=new File(dir);
  String[] filename=file.list();
  
  for(int i=0;i<filename.length;i++){
   if(filename[i].indexOf(".TXT")!=-1 || filename[i].indexOf(".txt")!=-1){
    System.out.println(filename[i]);
    cb.generateFile(dir+"/"+filename[i]);
//    cb.addRequest(filename[i]);
   }
//   if(filename[i].indexOf(".txt")!=-1){
//    cb.haveS9(filename[i]);
//   }
  }
  
//  cb.generateFile(dir+"/"+"VCRP101C.txt");
 }
 
 private void haveS9(String filename){
  boolean have=false;
  FileReader fr;
  try {
   fr = new FileReader(dir+"/"+filename);
   BufferedReader br=new BufferedReader(fr);
   
   String strLine="";
   while ((strLine = br.readLine()) != null) {
    if(strLine.indexOf("S9")!=-1){
     System.out.println(filename);
     return;
    }
   }
  }catch(Exception e){
   e.printStackTrace();
  }
 }
 
 private void addRequest(String filename){
  FileReader fr;
  FileWriter wr;
  try {
   fr = new FileReader(dir+"/"+filename);
   BufferedReader br=new BufferedReader(fr);
   wr=new FileWriter(dir+"2"+"/"+filename);
   BufferedWriter bw=new BufferedWriter(wr);
   
   String add1="  objRecord.put(\"REQ-GRP-FUNCTION-ID\", MemfisGenUtility.emptyString(objRequest.getSystemId2()));\r\n";
   String add2="  objRecord.put(\"REQ-GRP-ACTION-ID\", MemfisGenUtility.emptyString(objRequest.getChargesSystemId()));\r\n";
   String add3="  objRecord.put(\"REQ-SRCBRNCH-NO\", MemfisGenUtility.emptyString(objRequest.getSrcBrnchNo()));\r\n";
   String add4="  objRecord.put(\"REQ-SRCTERM-NO\", MemfisGenUtility.emptyString(objRequest.getSrcTermNo()));\r\n";
    
   String strLine="";
   while ((strLine = br.readLine()) != null) {
    if(strLine.indexOf("MemfisMap objRecord = new MemfisMap();")==-1){
     wr.write(strLine);
     wr.write("\r\n");
     continue;
    } else {
     wr.write(strLine);
     wr.write("\r\n");
     wr.write(add1+add2+add3+add4);
    }
   }
   bw.flush();
   br.close();
   wr.close();
   bw.close();
   fr.close();
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 
 private void generateFile(String filename){
  String strXML = "";
  FileWriter wr;
  try{
   strXML=generateXML(filename);
//   System.out.println(strXML);
   wr=new FileWriter(dir+"/"+targetName+".XML");
   BufferedWriter bw=new BufferedWriter(wr);
   String outPut="";
   bw.write(strXML);
   
   if(!"".equals(strXML) && (strXML.indexOf("<CTRL-1\t")<0 || strXML.indexOf("<CTRL-2\t")<0 || strXML.indexOf("<CTRL-3\t")<0 || strXML.indexOf("<CTRL-4\t")<0))
    System.out.println("This mli has ctrl problem.");
   
   if("".equals(strXML))
    System.out.println(targetName+"not generated.");
   
//   while(strXML.indexOf("\n\t")>0 || strXML.indexOf("\n")>0){
//    if(strXML.indexOf("\n\t")>0){
//     outPut=strXML.substring(0,strXML.indexOf("\n\t"));
//     bw.write(outPut);
//     bw.write("\r\n");
//     strXML=strXML.substring(strXML.indexOf("\n\t")+2,strXML.length());
//    }else if(strXML.indexOf("\n")>0){
//     outPut=strXML.substring(0,strXML.indexOf("\n"));
//     bw.write(outPut);
//     bw.write("\r\n");
//     strXML=strXML.substring(strXML.indexOf("\n")+1,strXML.length());
//    }
//    System.out.print(outPut);
//   }
   bw.flush();
   bw.close();
   wr.close();
  }catch(Exception e){
   System.out.println(filename);
   e.printStackTrace();
  }
 }
 
 private String generateXML(String filename) throws Exception {
  FileReader fr;
  fr = new FileReader(filename);
  BufferedReader br=new BufferedReader(fr);
  StringBuffer sb = new StringBuffer("");
  generateComponent(sb, br, "", 0, 0, "");
  return sb.toString();
 }

 private static String generateComponent(StringBuffer sbReturn, BufferedReader br, String strGroupName, int iGenerateFlag, int iLevelNumber, String strOccurs) {

  try {
   String strLine = br.readLine();
   boolean bRead = true;
   boolean bHearderPrinted = false;
   int iPosition = 1;
   while (strLine != null) {
    switch (validateLine(strLine.trim())) {
     case 3 :
      strLine = br.readLine();
      strLine = br.readLine();
     case 0 :
      if (!bHearderPrinted) {
       if (!"".equals(strOccurs)) {
        sbReturn.append("<" + strGroupName + " occurs=\"" + strOccurs + "\" length=\"\">\n");
       } else {
        strGroupName = getMLIName(strLine);
        sbReturn.append("<" + strGroupName + " length=\"\">\n");
       }
       bHearderPrinted = true;
      } else if (!strGroupName.equals(getMLIName(strLine)) && getMLIName(strLine).length() > 0) {
       if (strGroupName.length() > 0 && "".equals(strOccurs)) {
        sbReturn.append("</" + strGroupName + ">\n\n\n");
        //Insert the total length
        int iReplaceIndex1 = sbReturn.toString().lastIndexOf("<" + strGroupName + " ");
        int iReplaceIndex = sbReturn.toString().indexOf("length=", iReplaceIndex1);
        sbReturn.insert(iReplaceIndex + 8, iPosition - 1);

        if (iPosition > 90000) {
         sbReturn.append(LENGTH_WARNING_MESSAGE);
        }
        iPosition = 1;
        strGroupName = getMLIName(strLine);
        sbReturn.append("<" + strGroupName + " length=\"\">\n");
       }
      }
      getMLIName(strLine);
      String strFieldName = getFieldName(strLine);
      String strType = getType(strLine);
      String strSize = getSize(strLine);
      int iLevel = getLevel(strLine);
      String strPrecision = getPrecision(strLine);
      if (!strType.equalsIgnoreCase("")) {
       if (iGenerateFlag == 0) {

       } else if (iGenerateFlag > 0) {
        if (iLevel <= iLevelNumber) {
         sbReturn.append("</" + strGroupName + ">\n");

         int iReplaceIndex1 = sbReturn.toString().lastIndexOf("<" + strGroupName + " ");
         String strReplacedLine = sbReturn.substring(sbReturn.toString().lastIndexOf("<" + strGroupName + " "));
         int iReplaceIndex2 = strReplacedLine.indexOf("length=");
         sbReturn.insert(iReplaceIndex1 + iReplaceIndex2 + 8, iPosition - 1);

         return strLine;
        }
       }
       if (strType.equalsIgnoreCase("DOMLOOP")) {
        String strCurrentLable = strFieldName.substring(0, strFieldName.indexOf("OCCURS")).trim();
        String strOccursTemp = strFieldName.substring(strFieldName.indexOf("OCCURS") + 6, strFieldName.indexOf("TIMES")).trim();
        strLine = generateComponent(sbReturn, br, strCurrentLable, 1, iLevel, strOccursTemp);

        if (strOccursTemp != null && !strOccursTemp.equalsIgnoreCase("")) {
         int indexOccurs = sbReturn.toString().indexOf("occurs=", sbReturn.toString().lastIndexOf("<" + strCurrentLable + " "));
         int iLastDomSize = Integer.parseInt(sbReturn.substring(sbReturn.toString().indexOf("length=", indexOccurs) + 8, sbReturn.toString().indexOf("\"", sbReturn.toString().indexOf("length=", indexOccurs) + 8)));
         iPosition = iPosition + Integer.parseInt(strOccursTemp) * iLastDomSize;
        }
        bRead = false;
       } else if (strType.equalsIgnoreCase("GROUP")) {
       } else {
        String strWriter = convertXML(strFieldName, strType, strSize, 1, strPrecision);
        sbReturn.append(strWriter + "\n");
       }
      }
      iPosition += Integer.parseInt(strSize);
      break;
     case 9 :
      sbReturn.append(ERROR_MESSAGE);
      break;
     case 1 :
      break;
     case 5 :
      if(strLine.trim().startsWith("***") && !strLine.trim().equals("***START DATE-FIELDS***") && !strLine.trim().equals("***END DATE-FIELDS***")){
       String dateField = "<"+getFieldName(strLine.trim());
       StringBuffer temp = sbReturn;
       int start = 0;
       while(sbReturn.indexOf(dateField,start+dateField.length())>0){
        
        int indexDate = sbReturn.toString().indexOf(dateField,start+dateField.length());
        int insert = sbReturn.indexOf("/>",indexDate);
        sbReturn.insert(insert," default=\"0001-01-01\"");
        
        start = indexDate;
       }
      }
    }
    if (bRead) {
     strLine = br.readLine();
//     if(strLine!=null && strLine.length()<=1){
//      strLine=null;
//     }
    } else {
     bRead = true;
    }
   }
   if (strGroupName.length() > 0) {
    sbReturn.append("</" + strGroupName + ">\n");
    //Insert the total length
    int iReplaceIndex1 = sbReturn.toString().lastIndexOf("<" + strGroupName + " ");
    int iReplaceIndex = sbReturn.toString().indexOf("length=", iReplaceIndex1);
    sbReturn.insert(iReplaceIndex + 8, iPosition - 1);
    if (iPosition > 90000) {
     sbReturn.append(LENGTH_WARNING_MESSAGE);
    }
   }
  } catch (Exception e) {
   sbReturn.append(e);
   System.out.println(e);
   System.out.println(targetName);
   e.printStackTrace();
  }

  return "";
 }

 private static int validateLine(String strLine) {
  if (strLine.length() == 0) {
   return 1;
  } else if (strLine.startsWith("***")){
   return 5;
  } else if (strLine.indexOf("***END DATE-FIELDS***")>=0){
   return 1;
  } else if(strLine.indexOf("-I-") == -1 && strLine.indexOf("-O-") == -1 && strLine.indexOf("OCCURS")==-1 && strLine.indexOf("PIC")==-1){
   return 1;
  } else if (strLine.indexOf("*") != -1) {
   return 1;
  } else if (strLine.indexOf(".") == -1) {
   return 9;
//  } else if (strLine.indexOf("-I-") == -1 && strLine.indexOf("-O-") == -1 && strLine.indexOf("FILLER") == -1) {
  } else if (strLine.indexOf("-I-") == -1 && strLine.indexOf("-O-") == -1 && strLine.indexOf("FILLER") == -1) {
   return 9;
  } else if (strLine.endsWith("REDEFINES")){
   return 3;
  }
  return 0;
 }
 private static String getMLIName(String strLine) {
  if (strLine == null)
   return "";
  String strName = "";

  if (strLine.indexOf("-I-") != -1) {
   strName = strLine.substring(strLine.indexOf("-I-") - 7, strLine.indexOf("-I-"));
   strName = strName.substring(0, 2) + "RQ" + strName.substring(4);
  } else if (strLine.indexOf("-O-") != -1) {
   strName = strLine.substring(strLine.indexOf("-O-") - 7, strLine.indexOf("-O-"));
   strName = strName.substring(0, 2) + "RP" + strName.substring(4);
  }
  if(!"".equals(strName)){
   targetName=strName;
  }
  return strName;
 }
 /*
  * To generate the field name from the copybook.
  * Parm strLine one line's information from the copybook.
  * Return the lower case of the full MLI fields
  * eg.mfml063-i-type-of-record
  */
 private static String getFieldName(String strLine) {
  if (strLine == null)
   return "";
  String strName = "";

//  if(strLine.indexOf("PICOP-NON-FIRST-BUY")>0){
//   System.out.println(strLine);
//  }
  
  if (strLine.indexOf("-I-") != -1 || strLine.indexOf("-O-") != -1) {
   if (strLine.indexOf("-I-") != -1) {
    if (strLine.indexOf("PIC ") != -1) {
     strName = strLine.substring(strLine.indexOf("-I-") + 3, strLine.indexOf("PIC ")).trim();
    } else {
     strName = strLine.substring(strLine.indexOf("-I-") + 3).trim();
    }
   } else if (strLine.indexOf("-O-") != -1) {
    if (strLine.indexOf("PIC ") != -1) {
     strName = strLine.substring(strLine.indexOf("-O-") + 3, strLine.indexOf("PIC ")).trim();
    } else {
     strName = strLine.substring(strLine.indexOf("-O-") + 3).trim();
    }
   }
  } else if (strLine.indexOf("FILLER") != -1) {
   strName = strLine.substring(strLine.indexOf("FILLER"), strLine.indexOf("PIC ")).trim();
  }
  return strName;
 }
 /*
  * To generate the field Type from the copybook.
  * Parm strLine one line's information from the copybook.
  * Return the Type of the MLI fields
  * A for Character, D for Decimal, DOMLOOP for the loop occurs.
  */
 private static String getType(String strLine) {
  String str = strLine;
  if (strLine == null)
   return "";
  String strReturn = "";
  if ((strLine.indexOf("-I-") != -1 || strLine.indexOf("-O-") != -1) && strLine.indexOf("PIC ") != -1) {

   String strType = str.substring(strLine.indexOf("PIC ") + 3).trim();

   String strType1 = strType.substring(0, strType.indexOf("("));

   if (strType.indexOf("S") != -1) { //Signed
    strReturn = "S";
   } else if (strType.indexOf("V") != -1) { //Bigdecimal
    strReturn = "D";
   } else {
    if (strType1.equalsIgnoreCase("9")) { //numeric
     strReturn = "D";
    } else if (strType1.equalsIgnoreCase("X")) { //String     
     strReturn = "A";
    }
   }
  } else if (strLine.indexOf("OCCURS") != -1 && strLine.indexOf("TIMES") != -1) {
   strReturn = "DOMLOOP";
  } else if (strLine.indexOf("FILLER") != -1 && strLine.indexOf("PIC ") != -1) {
   strReturn = "A";
  } else {
   strReturn = "GROUP";
  }
  return strReturn;
 }
 /*
  * To generate the field size from the copybook.
  * Parm strLine one line's information from the copybook.
  */
 private static String getSize(String strLine) {

  String str = strLine;

  if (strLine == null)
   return "0";
  String strReturn = "0";
  if ((strLine.indexOf("-I-") != -1 || strLine.indexOf("-O-") != -1) && strLine.indexOf("PIC ") != -1) {
   String strType = str.substring(strLine.indexOf("PIC ") + 3).trim();

   String strType2 = strType.substring(strType.indexOf("(") + 1, strType.indexOf(")")).trim();

   if (strType.indexOf("V9") != -1) { //Bigdecimal
    String strType3 = strType.substring(strType.indexOf("V") + 1).trim();
    strType3 = strType3.substring(strType3.indexOf("(") + 1, strType3.indexOf(")")).trim();
    strReturn = String.valueOf(Integer.parseInt(strType2) + Integer.parseInt(strType3));
   } else {
    strReturn = String.valueOf(Integer.parseInt(strType2));
   }
  } else if (strLine.indexOf("FILLER") != -1 && strLine.indexOf("PIC") != -1) {
   String strType = str.substring(strLine.indexOf("PIC ") + 3).trim();

   //String strType1 = strType.substring(0, strType.indexOf("("));
   String strType2 = strType.substring(strType.indexOf("(") + 1, strType.indexOf(")")).trim();
   strReturn = String.valueOf(Integer.parseInt(strType2));
  }
  return strReturn;
 }
 /*
  * To generate the field Level from the copybook.
  * Parm strLine one line's information from the copybook.
  */
 private static int getLevel(String strLine) {

  if (strLine == null)
   return 0;
  int iLevel = 0;
  int iFlag = strLine.indexOf("-I-") >= 0 ? strLine.indexOf("-I-") : strLine.indexOf("-O-");
  if (iFlag != -1) {
   String str = strLine.substring(0, strLine.lastIndexOf(" ", iFlag)).trim();
   iLevel = Integer.parseInt(str.substring(str.lastIndexOf(" ") == -1 ? 0 : str.lastIndexOf(" ")).trim());
  } else if (strLine.indexOf("FILLER") != -1) {
   String str = strLine.substring(0, strLine.lastIndexOf("FILLER")).trim();
   iLevel = Integer.parseInt(str.substring(str.lastIndexOf(" ") == -1 ? 0 : str.lastIndexOf(" ")).trim());
  }
  return iLevel;
 }
 /*
  * To generate the field Type from the copybook.
  * Parm strLine one line's information from the copybook.
  * Return the Type of the MLI fields
  * A for Character, D for Decimal, DOMLOOP for the loop occurs.
  */
 private static String getPrecision(String strLine) {

  if (strLine == null)
   return "";
  String strReturn = "";
  if ((strLine.indexOf("-I-") != -1 || strLine.indexOf("-O-") != -1) && strLine.indexOf(" PIC ") != -1) {
   String strType = strLine.substring(strLine.indexOf(" PIC ") + 4).trim();
   if (strType.indexOf("V9") != -1) { //Bigdecimal
    String strType1 = strType.substring(strType.indexOf("V") + 1);
    String strType2 = strType1.substring(strType1.indexOf("(") + 1, strType1.indexOf(")"));
    strReturn = strType2.trim();
    strReturn = Integer.valueOf(strReturn).toString();
   }
  }
  return strReturn;
 }

 /*
  * To generate the data which is used for writing into the XML.
  * Parm strFieldName the generated field name.
  */
 private static String convertXML(String strFieldName, String strType, String strLength, int iIndent, String strPrecision) {
  String str = "";
  for (int i = 0; i < iIndent; i++) {
   str += "\t";
  }
  if (strPrecision != null && !strPrecision.equalsIgnoreCase("")) {
   //str += "<" + strFieldName + "\ttype=\"" + strType + "\"\tpos=\""+iPosition+"\"\tlength=\"" + strLength + "\"\tprecision=\""+strPrecision+"\"></" + strFieldName + ">";
   str += "<" + strFieldName + "\ttype=\"" + strType + "\"\tlength=\"" + strLength + "\"\tprecision=\"" + strPrecision + "\" />";
  } else {
   //str += "<" + strFieldName + "\ttype=\"" + strType + "\"\tpos=\""+iPosition+"\"\tlength=\"" + strLength + "\"></" + strFieldName + ">";
   if(strFieldName.endsWith("CTRL-1") && targetName.indexOf("RP")<0){
    str += "<" + strFieldName + "\ttype=\"" + strType + "\"\tlength=\"" + strLength + "\" default=\"9999\"/>";
   } else if(strFieldName.endsWith("CTRL-2") && targetName.indexOf("RP")<0){
    str += "<" + strFieldName + "\ttype=\"" + strType + "\"\tlength=\"" + strLength + "\" default=\"000\"/>";
   } else if(strFieldName.endsWith("CTRL-3") && targetName.indexOf("RP")<0){
    str += "<" + strFieldName + "\ttype=\"" + strType + "\"\tlength=\"" + strLength + "\" default=\"000\"/>";
   } else if(strFieldName.endsWith("CTRL-4") && targetName.indexOf("RP")<0){
    str += "<" + strFieldName + "\ttype=\"" + strType + "\"\tlength=\"" + strLength + "\" default=\"0000\"/>";
   } else {
    str += "<" + strFieldName + "\ttype=\"" + strType + "\"\tlength=\"" + strLength + "\" />";
   }
  }

  return str;
 }
 
 private ArrayList getFileNameList(String filename,ArrayList a){
  File file=new File(filename);
  String[] filesAndDir=file.list();
 
  for(int i=0;i<filesAndDir.length;i++){
   File filesAndDir_file=new File(filename+"/"+filesAndDir[i]);
   if(filesAndDir_file.isDirectory()){
    a=getFileNameList(filename+"/"+filesAndDir[i],a);
   } else if(filesAndDir_file.isFile()){
    a.add(filename+"/"+filesAndDir[i]);
   }   
  }

  return a;
 }
 
 public static String[] split(String strInput, String regex){
  if (strInput == null) {
   System.out.println("Input String is null!");
  }
  if (regex.equals("")) {
   System.out.println("Input regex is null or empty!");
  }
  ArrayList arrReturn = new ArrayList();
  int l = regex.length();
  int i = strInput.indexOf(regex);
  while (i != -1) {
   arrReturn.add(strInput.substring(0, i));
   strInput = strInput.substring(i + l);
   i = strInput.indexOf(regex);
  }
  arrReturn.add(strInput);
  int n = arrReturn.size();
  String[] strReturn = new String[n];
  for (int m = 0; m < n; m++) {
   strReturn[m] = (String) arrReturn.get(m);
  }
  return strReturn;
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

折腾数据折腾代码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值