public void toXML(InputStream in, OutputStream out)
throws IOException, InvalidFormatException
{
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line;
Document doc = new DocumentImpl();
Element root = doc.createElement("table");
doc.appendChild(root);
Element row, col;
char c;
StringBuffer text;
char quote;
int escape;
char prevc, prevquote;
while ((line = br.readLine()) != null) {
row = doc.createElement("row");
root.appendChild(row);
quote = 0;
escape = 0;
text = new StringBuffer();
c = 0;
prevquote = 0;
for (int i = 0; i < line.length(); i++) {
prevc = c;
c = line.charAt(i);
if (escape > 0) {
escape--;
}
else
if (c == '\\') {
escape = 1;
continue;
}
else
if (quote == 0 && prevquote == c && prevc == c) {
prevquote = c;
quote = c;
}
else
if (quote == 0 && (c == '"' || c == '\'')) {
prevquote = quote;
quote = c;
continue;
}
else
if (c == quote) {
prevquote = quote;
quote = 0;
continue;
}
else
if (c == m_comma && quote == 0) {
col = doc.createElement("column");
if (text != null)
col.appendChild(doc.createTextNode(text.toString()));
row.appendChild(col);
text = new StringBuffer();
continue;
}
text.append(c);
}
if (text != null) {
col = doc.createElement("column");
col.appendChild(doc.createTextNode(text.toString()));
row.appendChild(col);
}
}
OutputFormat format = new OutputFormat(doc);
format.setIndent(4);
format.setLineSeparator(getEndOfLine());
XMLSerializer serial = new XMLSerializer(out, format);
serial.serialize(doc);
out.flush();
}