View Javadoc

    1   /*
    2    * joey-gen and its relative products are published under the terms
    3    * of the Apache Software License.
    4    *
    5    * Created on 2005/01/14 11:53:35
    6    */
    7   package org.asyrinx.joey.gen.task.impl;
    8
    9   import java.io.BufferedReader;
    10  import java.io.File;
    11  import java.io.FileInputStream;
    12  import java.io.IOException;
    13  import java.io.InputStreamReader;
    14  import java.util.ArrayList;
    15  import java.util.Iterator;
    16  import java.util.List;
    17
    18  import org.asyrinx.joey.gen.task.BizLogicDistiller;
    19
    20  /***
    21   * @author takeshi
    22   */
    23  public class BizLogicDistillerJavaSrc implements BizLogicDistiller {
    24
    25      /***
    26       *
    27       */
    28      public BizLogicDistillerJavaSrc(String bizLogicMarkerBegin, String bizLogicMarkerEnd) {
    29          super();
    30          this.bizLogicMarkerBegin = bizLogicMarkerBegin;
    31          this.bizLogicMarkerEnd = bizLogicMarkerEnd;
    32      }
    33
    34      private final String bizLogicMarkerBegin;
    35
    36      private final String bizLogicMarkerEnd;
    37
    38      public String getBizLogicMarkerBegin() {
    39          return bizLogicMarkerBegin;
    40      }
    41
    42      public String getBizLogicMarkerEnd() {
    43          return bizLogicMarkerEnd;
    44      }
    45
    46      public String distill(File source) throws IOException {
    47          final FileInputStream fis = new FileInputStream(source);
    48          final BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
    49          final List bufferes = new ArrayList();
    50          //
    51          StringBuffer buffer = distillPart(reader);
    52          while (buffer != null) {
    53              bufferes.add(buffer);
    54              buffer = distillPart(reader);
    55          }
    56          return concatBuffers(bufferes);
    57      }
    58
    59      private String concatBuffers(List buffers) {
    60          final StringBuffer result = new StringBuffer();
    61          for (Iterator i = buffers.iterator(); i.hasNext();) {
    62              final StringBuffer buffer = (StringBuffer) i.next();
    63              result.append(buffer);
    64          }
    65          return result.toString();
    66      }
    67
    68      /***
    69       * @param reader
    70       * @return
    71       * @throws IOException
    72       */
    73      private StringBuffer distillPart(BufferedReader reader) throws IOException {
    74          if (!skipTo(reader, getBizLogicMarkerBegin()))
    75              return null;
    76          final StringBuffer buffer = new StringBuffer();
    77          if (!appendTo(reader, buffer, getBizLogicMarkerEnd()))
    78              return null;
    79          else
    80              return buffer;
    81      }
    82
    83      /***
    84       * @param reader
    85       * @param match
    86       * @throws IOException
    87       */
    88      private boolean skipTo(BufferedReader reader, String match) throws IOException {
    89          final String line = reader.readLine();
    90          while (line != null) {
    91              if (line.trim().equals(match))
    92                  return true;
    93          }
    94          return false;
    95      }
    96
    97      /***
    98       * @param reader
    99       * @param dest
    100      * @param match
    101      * @throws IOException
    102      */
    103     private boolean appendTo(BufferedReader reader, StringBuffer dest, String match) throws IOException {
    104         final String line = reader.readLine();
    105         while (line != null) {
    106             if (line.trim().equals(match))
    107                 return true;
    108             dest.append(line);
    109         }
    110         return false;
    111     }
    112
    113 }