Debian Codegraph @

src/com/adobe/internal/xmp/impl/FixASCIIControlsReader.java

raw symbols SCIP
1 // =================================================================================================
2 // ADOBE SYSTEMS INCORPORATED
3 // Copyright 2006 Adobe Systems Incorporated
4 // All Rights Reserved
5 //
6 // NOTICE: Adobe permits you to use, modify, and distribute this file in accordance with the terms
7 // of the Adobe license agreement accompanying it.
8 // =================================================================================================
9
10 package com.adobe.internal.xmp.impl;
11
12 import java.io.IOException;
13 import java.io.PushbackReader;
14 import java.io.Reader;
15
16
17 /**
18 * @author Stefan Makswit
19 * @version $Revision$
20 * @since 22.08.2006
21 */
22 public class FixASCIIControlsReader extends PushbackReader
23 {
24 /** */
25 private static final int STATE_START = 0;
26 /** */
27 private static final int STATE_AMP = 1;
28 /** */
29 private static final int STATE_HASH = 2;
30 /** */
31 private static final int STATE_HEX = 3;
32 /** */
33 private static final int STATE_DIG1 = 4;
34 /** */
35 private static final int STATE_ERROR = 5;
36 /** */
37 private static final int BUFFER_SIZE = 8;
38 /** the state of the automaton */
39 private int state = STATE_START;
40 /** the result of the escaping sequence */
41 private int control = 0;
42 /** count the digits of the sequence */
43 private int digits = 0;
44
45 /**
46 * The look-ahead size is 6 at maximum («)
47 * @see PushbackReader#PushbackReader(Reader, int)
48 * @param in a Reader
49 */
50 public FixASCIIControlsReader(Reader in)
51 {
52 super(in, BUFFER_SIZE);
53 }
54
55
56 /**
57 * @see Reader#read(char[], int, int)
58 */
59 public int read(char[] cbuf, int off, int len) throws IOException
60 {
61 int readAhead = 0;
62 int read = 0;
63 int pos = off;
64 char[] readAheadBuffer = new char[BUFFER_SIZE];
65
66 boolean available = true;
67 while (available && read < len)
68 {
69 available = super.read(readAheadBuffer, readAhead, 1) == 1;
70 if (available)
71 {
72 char c = processChar(readAheadBuffer[readAhead]);
73 if (state == STATE_START)
74 {
75 // replace control chars with space
76 if (Utils.isControlChar(c))
77 {
78 c = ' ';
79 }
80 cbuf[pos++] = c;
81 readAhead = 0;
82 read++;
83 }
84 else if (state == STATE_ERROR)
85 {
86 unread(readAheadBuffer, 0, readAhead + 1);
87 readAhead = 0;
88 }
89 else
90 {
91 readAhead++;
92 }
93 }
94 else if (readAhead > 0)
95 {
96 // handles case when file ends within excaped sequence
97 unread(readAheadBuffer, 0, readAhead);
98 state = STATE_ERROR;
99 readAhead = 0;
100 available = true;
101 }
102 }
103
104
105 return read > 0 || available ? read : -1;
106 }
107
108
109 /**
110 * Processes numeric escaped chars to find out if they are a control character.
111 * @param ch a char
112 * @return Returns the char directly or as replacement for the escaped sequence.
113 */
114 private char processChar(char ch)
115 {
116 switch (state)
117 {
118 case STATE_START:
119 if (ch == '&')
120 {
121 state = STATE_AMP;
122 }
123 return ch;
124
125 case STATE_AMP:
126 if (ch == '#')
127 {
128 state = STATE_HASH;
129 }
130 else
131 {
132 state = STATE_ERROR;
133 }
134 return ch;
135
136 case STATE_HASH:
137 if (ch == 'x')
138 {
139 control = 0;
140 digits = 0;
141 state = STATE_HEX;
142 }
143 else if ('0' <= ch && ch <= '9')
144 {
145 control = Character.digit(ch, 10);
146 digits = 1;
147 state = STATE_DIG1;
148 }
149 else
150 {
151 state = STATE_ERROR;
152 }
153 return ch;
154
155 case STATE_DIG1:
156 if ('0' <= ch && ch <= '9')
157 {
158 control = control * 10 + Character.digit(ch, 10);
159 digits++;
160 if (digits <= 5)
161 {
162 state = STATE_DIG1;
163 }
164 else
165 {
166 state = STATE_ERROR; // sequence too long
167 }
168 }
169 else if (ch == ';' && Utils.isControlChar((char) control))
170 {
171 state = STATE_START;
172 return (char) control;
173 }
174 else
175 {
176 state = STATE_ERROR;
177 }
178 return ch;
179
180 case STATE_HEX:
181 if (('0' <= ch && ch <= '9') ||
182 ('a' <= ch && ch <= 'f') ||
183 ('A' <= ch && ch <= 'F'))
184 {
185 control = control * 16 + Character.digit(ch, 16);
186 digits++;
187 if (digits <= 4)
188 {
189 state = STATE_HEX;
190 }
191 else
192 {
193 state = STATE_ERROR; // sequence too long
194 }
195 }
196 else if (ch == ';' && Utils.isControlChar((char) control))
197 {
198 state = STATE_START;
199 return (char) control;
200 }
201 else
202 {
203 state = STATE_ERROR;
204 }
205 return ch;
206
207 case STATE_ERROR:
208 state = STATE_START;
209 return ch;
210
211 default:
212 // not reachable
213 return ch;
214 }
215 }
216 }