00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 package dochelper;
00011
00012 import dochelper.util.FileUtils;
00013 import java.io.File;
00014 import java.io.FileNotFoundException;
00015 import java.io.IOException;
00016 import java.util.Iterator;
00017 import java.util.Map;
00018
00025 public class DocumentSection {
00026
00028 private final String start;
00029
00031 private final String end;
00032
00034 private static final int START_LEN = 9;
00035
00037 private static final int END_LEN = 7;
00038
00053 public Map params;
00054
00063 public DocumentSection(final String start, final String end) {
00064 this.start = start;
00065 this.end = end;
00066 }
00067
00079 public DocumentSection(final File docRef) throws FileNotFoundException, IOException {
00080
00081
00082 String fileStr = FileUtils.FileToString(docRef);
00083
00084 int startIndex = fileStr.indexOf("||START||");
00085 int endIndex = fileStr.indexOf("||END||");
00086
00087 if (startIndex != -1 && endIndex != -1) {
00088
00089 start = fileStr.substring(startIndex + START_LEN, endIndex);
00090
00091
00092 end = fileStr.substring(endIndex + END_LEN);
00093 } else {
00094 start = "";
00095 end = "";
00096 }
00097
00098 }
00099
00115 public DocumentSection(final File docRef, final Map params) throws Exception {
00116 this(docRef);
00117 this.params = params;
00118 }
00119
00125 public void setParams(Map params) {
00126 this.params = params;
00127 }
00128
00134 public String getSTART() {
00135 return applyParams(start);
00136 }
00137
00143 public String getEND() {
00144 return applyParams(end);
00145 }
00146
00158 private String applyParams(String in) {
00159 String ret = in;
00160 if (params != null && params.keySet() != null) {
00161 for (Iterator iter = params.keySet().iterator();iter.hasNext();) {
00162 String key = (String)iter.next();
00163 ret = ret.replaceAll("\\$" + key, params.get(key).toString());
00164 }
00165 }
00166 return ret;
00167 }
00168 }