Java には、XML メタデータをある標準から別の標準に変換するパッケージがあります。このパッケージは、最終的に単一の関数を介してアクセスされ、PrintStream オブジェクトを介してすべての出力を送信します。送信される出力は、各ファイルのステータスと、翻訳されたかどうかだけです。
System.out に出力するだけであれば、これは非常にうまく機能しますが、実際には、変換中にこれを JTextPane に出力したいと考えています(進行状況テキスト ボックスのようなものです)。XML の翻訳が完了した後にステータスを出力するだけでも大したことではありませんが、XML ファイルが何千もある可能性があるため、それは現実的ではありません。
私が試したことの 1 つは、(ByteArrayOutputStream に接続されている) PrintStream からすべての情報を取得するスレッドを使用し、新しい情報をテキスト ペインに送信させることです。残念ながら、これでも翻訳の最後に一度に情報が送信されます。これは、System.out に対して正しく機能します。
変換を行い、出力を表示しようとするコードは次のとおりです。
public class ConverterGUI extends javax.swing.JFrame {
boolean printToResultsBox = false;
PrintStream printStream = null;
ByteArrayOutputStream baos = null;
private class ResultsPrinter implements Runnable {
public ResultsPrinter() {
baos = new ByteArrayOutputStream();
printStream = new PrintStream(baos);
}
public void run() {
String tempString = "";
while (printToResultsBox) {
try {
if (!baos.toString().equals(tempString)) {
tempString = baos.toString();
resultsBox.setText(tempString);
}
} catch (Exception ex) {
}
}
}
}
...
ResultsPrinter rp = new ResultsPrinter();
Thread thread = new Thread(rp);
thread.start();
// Do the translation.
try {
printToResultsBox = true;
boolean success = false;
TranslationEngine te = new TranslationEngine();
// fileOrFolderToConvert is a text box in the GUI.
// linkNeeded and destinationFile are just parameters for the translation process.
success = te.translate(fileOrFolderToConvert.getText(), linkNeeded, destinationFile, printStream);
if (success) {
printStream.println("File/folder translation was a success.");
}
resultsBox.setText(baos.toString());
} catch (Exception ex) {
printStream.println("File translation failed.");
} finally {
printToResultsBox = false;
}
...
}
最終的に、このコードはすべての変換が完了した後に JTextPane に正常に出力されますが、その間は出力されません。助言がありますか?PrintStream を別のものに変更する必要がありますか?