1 | |
package org.apache.maven.archiva.xml; |
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.Reader; |
25 | |
import java.util.regex.Matcher; |
26 | |
import java.util.regex.Pattern; |
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
public class LatinEntityResolutionReader |
34 | |
extends Reader |
35 | |
{ |
36 | |
private BufferedReader originalReader; |
37 | |
|
38 | |
private char leftover[]; |
39 | |
|
40 | |
private Pattern entityPattern; |
41 | |
|
42 | |
public LatinEntityResolutionReader( Reader reader ) |
43 | 0 | { |
44 | 0 | this.originalReader = new BufferedReader( reader ); |
45 | 0 | this.entityPattern = Pattern.compile( "\\&[a-zA-Z]+\\;" ); |
46 | 0 | } |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
public int read( char[] destbuf, int offset, int length ) |
59 | |
throws IOException |
60 | |
{ |
61 | |
int tmp_length; |
62 | 0 | int current_requested_offset = offset; |
63 | 0 | int current_requested_length = length; |
64 | |
|
65 | |
|
66 | 0 | if ( leftover != null ) |
67 | |
{ |
68 | 0 | if ( leftover.length > length ) |
69 | |
{ |
70 | |
|
71 | 0 | System.arraycopy( leftover, 0, destbuf, current_requested_offset, length ); |
72 | |
|
73 | |
|
74 | 0 | char tmp[] = new char[length]; |
75 | 0 | System.arraycopy( leftover, length, tmp, 0, length ); |
76 | 0 | leftover = new char[tmp.length]; |
77 | 0 | System.arraycopy( tmp, 0, leftover, 0, length ); |
78 | |
|
79 | |
|
80 | 0 | return length; |
81 | |
} |
82 | |
else |
83 | |
{ |
84 | 0 | tmp_length = leftover.length; |
85 | |
|
86 | |
|
87 | 0 | System.arraycopy( leftover, 0, destbuf, current_requested_offset, tmp_length ); |
88 | |
|
89 | |
|
90 | 0 | leftover = null; |
91 | |
|
92 | |
|
93 | 0 | current_requested_offset += tmp_length; |
94 | 0 | current_requested_length -= tmp_length; |
95 | |
} |
96 | |
} |
97 | |
|
98 | 0 | StringBuffer sbuf = getExpandedBuffer( current_requested_length ); |
99 | |
|
100 | |
|
101 | 0 | if ( sbuf == null ) |
102 | |
{ |
103 | |
|
104 | 0 | if ( current_requested_offset > offset ) |
105 | |
{ |
106 | |
|
107 | 0 | return ( current_requested_offset - offset ); |
108 | |
} |
109 | |
|
110 | |
|
111 | 0 | return -1; |
112 | |
} |
113 | |
|
114 | |
|
115 | 0 | tmp_length = Math.min( sbuf.length(), current_requested_length ); |
116 | 0 | sbuf.getChars( 0, tmp_length, destbuf, current_requested_offset ); |
117 | |
|
118 | |
|
119 | 0 | if ( tmp_length < sbuf.length() ) |
120 | |
{ |
121 | 0 | leftover = new char[sbuf.length() - tmp_length]; |
122 | 0 | sbuf.getChars( tmp_length, tmp_length + leftover.length, leftover, 0 ); |
123 | |
} |
124 | |
|
125 | |
|
126 | 0 | return ( current_requested_offset - offset ) + tmp_length; |
127 | |
} |
128 | |
|
129 | |
private StringBuffer getExpandedBuffer( int minimum_length ) |
130 | |
throws IOException |
131 | |
{ |
132 | 0 | StringBuffer buf = null; |
133 | 0 | String line = this.originalReader.readLine(); |
134 | 0 | boolean done = ( line == null ); |
135 | |
|
136 | 0 | while ( !done ) |
137 | |
{ |
138 | 0 | if ( buf == null ) |
139 | |
{ |
140 | 0 | buf = new StringBuffer(); |
141 | |
} |
142 | |
|
143 | 0 | buf.append( expandLine( line ) ); |
144 | |
|
145 | |
|
146 | 0 | if ( this.originalReader.ready() ) |
147 | |
{ |
148 | 0 | buf.append( "\n" ); |
149 | |
} |
150 | |
|
151 | 0 | if ( buf.length() > minimum_length ) |
152 | |
{ |
153 | 0 | done = true; |
154 | |
} |
155 | |
else |
156 | |
{ |
157 | 0 | line = this.originalReader.readLine(); |
158 | 0 | done = ( line == null ); |
159 | |
} |
160 | |
} |
161 | |
|
162 | 0 | return buf; |
163 | |
} |
164 | |
|
165 | |
private String expandLine( String line ) |
166 | |
{ |
167 | 0 | StringBuffer ret = new StringBuffer(); |
168 | |
|
169 | 0 | int offset = 0; |
170 | |
String entity; |
171 | 0 | Matcher mat = this.entityPattern.matcher( line ); |
172 | 0 | while ( mat.find( offset ) ) |
173 | |
{ |
174 | 0 | ret.append( line.substring( offset, mat.start() ) ); |
175 | 0 | entity = mat.group(); |
176 | 0 | ret.append( LatinEntities.resolveEntity( entity ) ); |
177 | 0 | offset = mat.start() + entity.length(); |
178 | |
} |
179 | 0 | ret.append( line.substring( offset ) ); |
180 | |
|
181 | 0 | return ret.toString(); |
182 | |
} |
183 | |
|
184 | |
public void close() |
185 | |
throws IOException |
186 | |
{ |
187 | 0 | this.originalReader.close(); |
188 | 0 | } |
189 | |
} |