KSpreadOutputFilter.java
上传用户:xiekaiwei
上传日期:2015-07-04
资源大小:620k
文件大小:6k
源码类别:

Telnet客户端

开发平台:

Java

  1. package org.tn5250j.tools.filters;
  2. /*
  3.  * @(#)iOhioSession.java
  4.  * Copyright:    Copyright (c) 2001
  5.  *
  6.  * This program is free software; you can redistribute it and/or modify
  7.  * it under the terms of the GNU General Public License as published by
  8.  * the Free Software Foundation; either version 2, or (at your option)
  9.  * any later version.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this software; see the file COPYING.  If not, write to
  18.  * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  19.  * Boston, MA 02111-1307 USA
  20.  *
  21.  */
  22. import java.io.*;
  23. import java.util.ArrayList;
  24. public class KSpreadOutputFilter implements OutputFilterInterface {
  25.    private int row;
  26.    StringBuffer sb;
  27.    PrintStream fout = null;
  28.    // create instance of file for output
  29.    public void createFileInstance(String fileName) throws
  30.                               FileNotFoundException {
  31.       fout = new PrintStream(new FileOutputStream(fileName));
  32.       // initialize work variables
  33.       row = 0;
  34.       sb = new StringBuffer();
  35.    }
  36.    /**
  37.     * Write the html header of the output file
  38.     */
  39.    public void parseFields(byte[] cByte, ArrayList ffd, StringBuffer rb) {
  40.       FileFieldDef f;
  41.       // write out the html record information for each field that is selected
  42.       row++;
  43.       int column = 1;
  44.       for (int x = 0; x < ffd.size(); x++) {
  45.          f = (FileFieldDef)ffd.get(x);
  46.          if (f.isWriteField()) {
  47.             rb.append ("    <cell row=" + """ + row+ """);
  48.             rb.append(" column=" + """ + column++ + "" > n");
  49.             switch (f.getFieldType()) {
  50.                case 'P':
  51.                case 'S':
  52.                   rb.append("     <format precision="" + f.getPrecision() +
  53.                               "" > n     </format>n" );
  54.                   break;
  55.                default:
  56.                   rb.append("     <format/>n" );
  57.                   break;
  58.             }
  59.             rb.append("    <text>" );
  60.             rb.append(tr2xml(f.parseData(cByte)));
  61.             rb.append ("</text> </cell>n");
  62.          }
  63.       }
  64.       fout.println(rb);
  65.       fout.flush();
  66.    }
  67.    private String tr2xml(String s) {
  68.       sb.setLength(0);
  69.       for (int x =0;x < s.length(); x++) {
  70.          switch (s.charAt(x)) {
  71.             case '<':
  72.                sb.append("&lt;");
  73.                break;
  74.             case '>':
  75.                sb.append("&gt;");
  76.                break;
  77.             case '&':
  78.                sb.append("&amp;");
  79.                break;
  80.             default:
  81.                sb.append(s.charAt(x));
  82.          }
  83.       }
  84.       return sb.toString();
  85.    }
  86.    /**
  87.     * Write the html header of the output file
  88.     */
  89.    public void writeHeader(String fileName, String host,
  90.                                  ArrayList ffd, char decChar) {
  91.       final String head = "<?xml version="1.0" encoding="UTF-8"?>" +
  92.                            "<!DOCTYPE spreadsheet >" +
  93.                            " <spreadsheet mime="application/x-kspread" editor="KSpread" >n" +
  94.                            " <paper format="A4" orientation="Portrait" >n" +
  95.                            "  <borders right="20" left="20" bottom="20" top="20" />n" +
  96.                            "  <head/>n" +
  97.                            "  <foot/>n" +
  98.                            " </paper>n" +
  99.                            " <locale positivePrefixCurrencySymbol="True"" +
  100.                            " negativeMonetarySignPosition="4"" +
  101.                            " negativePrefixCurrencySymbol="True"" +
  102.                            " fracDigits="2"" +
  103.                            " thousandsSeparator="."" +
  104.                            " dateFormat="%A %d %B %Y"" +
  105.                            " timeFormat="%H:%M:%S"" +
  106.                            " monetaryDecimalSymbol="" + decChar + """ +
  107.                            " weekStartsMonday="True"" +
  108.                            " negativeSign="-"" +
  109.                            " positiveSign="+"" +
  110.                            " positiveMonetarySignPosition="4"" +
  111.                            " decimalSymbol="" + decChar + """ +
  112. //                           " monetaryThousandsSeparator="."" +
  113.                            " dateFormatShort="%d.%m.%Y" />n" +
  114.                            " <map markerColumn="1" activeTable="Table1" markerRow="1" >n" +
  115.                            "  <table columnnumber="0" borders="0" hide="0" grid="1" formular="0" lcmode="0" name="Table1" >";
  116.       try {
  117.          fout.write(head.getBytes());
  118.          fout.write('n');
  119.          // write out the header names
  120.          FileFieldDef f;
  121.          // write out the record information for each field that is selected
  122.          row++;
  123.          int column = 1;
  124.          for (int x = 0; x < ffd.size(); x++) {
  125.             f = (FileFieldDef)ffd.get(x);
  126.             if (f.isWriteField()) {
  127.                fout.print ("   <cell row=" + """ + row+ """);
  128.                fout.print(" column=" + """ + column++ + "" >n");
  129.                fout.print("    <format>n");
  130.                fout.print("     <font size="11" style="" bold="yes" weight="75" />n");
  131.                fout.print("    </format>n    <text>" );
  132.                fout.print(f.getFieldName());
  133.                fout.print("</text>n   </cell>n");
  134.             }
  135.          }
  136.          fout.flush();
  137.       }
  138.       catch (IOException ioex) {
  139.          System.out.println(ioex.getMessage());
  140.       }
  141.    }
  142.    /**
  143.     * write the footer of the xml output
  144.     */
  145.    public void writeFooter(ArrayList ffd) {
  146.       try {
  147.         fout.write ("  </table>n".getBytes());
  148.         fout.write (" </map>n".getBytes());
  149.         fout.write ("</spreadsheet>n".getBytes());
  150.         fout.flush();
  151.         fout.close();
  152.       }
  153.       catch (IOException ioex) {
  154.          System.out.println(ioex.getMessage());
  155.       }
  156.    }
  157.    public boolean isCustomizable() {
  158.       return false;
  159.    }
  160.    public void setCustomProperties() {
  161.    }
  162. }