View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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(final String id, final String fileName, final 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(final String line) {
41      code.add(line);
42    }
43  
44    public String getId() {
45      return id;
46    }
47  
48    public void setId(final String id) {
49      this.id = id;
50    }
51  
52    public StringBuffer getCode(final boolean stripLeadingSpaces) {
53      int minSpaces = -1;
54      for (int i = 0; i < code.size(); i++) {
55        final String s = code.get(i);
56        for (int j = 0; j < s.length(); j++) {
57          final 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      final StringBuffer sb = new StringBuffer();
67      for (int i = 0; i < code.size(); i++) {
68        final 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(final String fileName) {
86      this.fileName = fileName;
87    }
88  
89    public int getLineStart() {
90      return lineStart;
91    }
92  
93    public void setLineStart(final int lineStart) {
94      this.lineStart = lineStart;
95    }
96  
97    public int getLineEnd() {
98      return lineEnd;
99    }
100 
101   public void setLineEnd(final int lineEnd) {
102     this.lineEnd = lineEnd;
103   }
104 
105   public String toString() {
106     return fileName + ":" + id + "[" + lineStart + "-" + lineEnd + "]" + " {" + code.toString() + " }";
107   }
108 
109 }