1 package org.apache.maven.doxia.macro.snippet;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.BufferedReader;
23 import java.io.IOException;
24 import java.io.InputStreamReader;
25 import java.net.URL;
26 import java.util.ArrayList;
27 import java.util.List;
28 import java.util.Locale;
29
30 import org.codehaus.plexus.util.IOUtil;
31
32
33
34
35
36
37 public class SnippetReader
38 {
39
40 private static final String EOL = System.getProperty( "line.separator" );
41
42
43 private URL source;
44
45
46 private String encoding;
47
48
49
50
51
52
53
54 public SnippetReader( URL src, String encoding )
55 {
56 this.source = src;
57 this.encoding = encoding;
58 }
59
60
61
62
63
64
65 public SnippetReader( URL src )
66 {
67 this( src, null ) ;
68 }
69
70
71
72
73
74
75
76
77 public StringBuffer readSnippet( String snippetId )
78 throws IOException
79 {
80 List<String> lines = readLines( snippetId );
81 int minIndent = minIndent( lines );
82 StringBuffer result = new StringBuffer();
83 for ( String line : lines )
84 {
85 result.append( line.substring( minIndent ) );
86 result.append( EOL );
87 }
88 return result;
89 }
90
91
92
93
94
95
96
97 int minIndent( List<String> lines )
98 {
99 int minIndent = Integer.MAX_VALUE;
100 for ( String line : lines )
101 {
102 minIndent = Math.min( minIndent, indent( line ) );
103 }
104 return minIndent;
105 }
106
107
108
109
110
111
112
113 int indent( String line )
114 {
115 char[] chars = line.toCharArray();
116 int indent = 0;
117 for ( ; indent < chars.length; indent++ )
118 {
119 if ( chars[indent] != ' ' )
120 {
121 break;
122 }
123 }
124 return indent;
125 }
126
127
128
129
130
131
132
133
134 private List<String> readLines( String snippetId )
135 throws IOException
136 {
137 BufferedReader reader;
138 if ( encoding == null || "".equals( encoding ) )
139 {
140 reader = new BufferedReader( new InputStreamReader( source.openStream() ) );
141 }
142 else
143 {
144 reader = new BufferedReader( new InputStreamReader( source.openStream(), encoding ) );
145 }
146
147 List<String> lines = new ArrayList<String>();
148 try
149 {
150 boolean capture = false;
151 String line;
152 while ( ( line = reader.readLine() ) != null )
153 {
154 if ( snippetId == null || "".equals( snippetId.trim() ) )
155 {
156 lines.add( line );
157 }
158 else
159 {
160 if ( isStart( snippetId, line ) )
161 {
162 capture = true;
163 }
164 else if ( isEnd( snippetId, line ) )
165 {
166 break;
167 }
168 else if ( capture )
169 {
170 lines.add( line );
171 }
172 }
173 }
174 }
175 finally
176 {
177 IOUtil.close( reader );
178 }
179 return lines;
180 }
181
182
183
184
185
186
187
188
189 protected boolean isStart( String snippetId, String line )
190 {
191 return isDemarcator( snippetId, "START", line );
192 }
193
194
195
196
197
198
199
200
201
202 protected boolean isDemarcator( String snippetId, String what, String line )
203 {
204 String upper = line.toUpperCase( Locale.ENGLISH );
205 return upper.contains( what.toUpperCase( Locale.ENGLISH ) )
206 && upper.contains( "SNIPPET" )
207 && line.contains( snippetId );
208 }
209
210
211
212
213
214
215
216
217 protected boolean isEnd( String snippetId, String line )
218 {
219 return isDemarcator( snippetId, "END", line );
220 }
221 }