1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.myfaces.tobago.ant.sniplet;
21
22 import java.util.List;
23 import java.util.ArrayList;
24
25 public class CodeSniplet {
26
27 private String id;
28 private List<String> code;
29 private String fileName;
30 private int lineStart;
31 private int lineEnd;
32
33 public CodeSniplet(String id, String fileName, int lineStart) {
34 this.id = id;
35 this.fileName = fileName;
36 this.code = new ArrayList<String>();
37 this.lineStart = lineStart;
38 }
39
40 public void addLine(String line) {
41 code.add(line);
42 }
43
44 public String getId() {
45 return id;
46 }
47
48 public void setId(String id) {
49 this.id = id;
50 }
51
52 public StringBuffer getCode(boolean stripLeadingSpaces) {
53 int minSpaces = -1;
54 for (int i = 0; i < code.size(); i++) {
55 String s = code.get(i);
56 for (int j = 0; j < s.length(); j++) {
57 char c = s.charAt(j);
58 if (!Character.isWhitespace(c)) {
59 if (minSpaces == -1 || j < minSpaces) {
60 minSpaces = j;
61 }
62 break;
63 }
64 }
65 }
66 StringBuffer sb = new StringBuffer();
67 for (int i = 0; i < code.size(); i++) {
68 String s = code.get(i);
69 if (stripLeadingSpaces && s.length() > minSpaces && minSpaces != -1) {
70 sb.append(s.substring(minSpaces));
71 } else {
72 sb.append(s);
73 }
74 if (i < code.size() -1) {
75 sb.append("\n");
76 }
77 }
78 return sb;
79 }
80
81 public String getFileName() {
82 return fileName;
83 }
84
85 public void setFileName(String fileName) {
86 this.fileName = fileName;
87 }
88
89 public int getLineStart() {
90 return lineStart;
91 }
92
93 public void setLineStart(int lineStart) {
94 this.lineStart = lineStart;
95 }
96
97 public int getLineEnd() {
98 return lineEnd;
99 }
100
101 public void setLineEnd(int lineEnd) {
102 this.lineEnd = lineEnd;
103 }
104
105 public String toString() {
106 return fileName + ":" + id + "[" + lineStart + "-" + lineEnd + "]" + " {" + code.toString() + " }";
107 }
108
109 }