1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package com.isenshi.util;
17
18 import org.seasar.tuigwaa.cms.core.CmsConstants;
19
20 /***
21 * @author someda
22 */
23 public class TextStringBuffer {
24
25 private static final int DEFAULT_LINESIZE = 80;
26
27 private StringBuffer buf = new StringBuffer();
28 private int linesize;
29 private int pos = 0;
30
31 private String newlineHeading = "";
32
33 public TextStringBuffer(){
34 this.linesize = DEFAULT_LINESIZE;
35 }
36
37 public TextStringBuffer(int linesize){
38 this.linesize = linesize;
39 }
40
41 public void append(String str){
42
43 int length = str.getBytes().length;
44 int current = buf.toString().getBytes().length;
45
46 if(CmsConstants.LINEBREAK_CODE.equals(str)){
47 mark();
48 }else if(length > linesize){
49 buf.append(CmsConstants.LINEBREAK_CODE);
50 buf.append(str);
51 mark();
52 }else if((current + length - pos) > linesize){
53 mark();
54 buf.append(str);
55 }else{
56 buf.append(str);
57 }
58 }
59
60 public void appendNewline(){
61 mark();
62 }
63
64 public void appendLink(String alias, String url){
65 String str = alias + " [" + url + "]";
66 append(str);
67 }
68
69 public String toString(){
70 return buf.toString();
71 }
72
73
74 public void appendRepeatString(String str, int num){
75 StringBuffer rep = new StringBuffer();
76 for(int i=0;i<num;i++){
77 rep.append(str);
78 }
79 append(rep.toString());
80 }
81
82 public int nextIndex(){
83 return buf.length();
84 }
85
86 public String cut(int start,int end){
87 String s = buf.substring(start,end);
88 buf.delete(start,end);
89 return s;
90 }
91
92 public void setNewlineHeading(String newlineHeading){
93 this.newlineHeading = newlineHeading;
94 }
95
96 public String getNewlineHeading(){
97 return newlineHeading;
98 }
99
100 private void mark(){
101 buf.append(CmsConstants.LINEBREAK_CODE + newlineHeading);
102 pos = buf.toString().getBytes().length;
103 }
104
105 }