1 /*
2 * Copyright (c) 2003-2008, by Henrik Arro and Contributors
3 *
4 * This file is part of JSeq, a tool to automatically create
5 * sequence diagrams by tracing program execution.
6 *
7 * See <http://jseq.sourceforge.net> for more information.
8 *
9 * JSeq is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License as
11 * published by the Free Software Foundation, either version 3 of
12 * the License, or (at your option) any later version.
13 *
14 * JSeq is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with JSeq. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 package th.co.edge.jseq;
24
25 import java.io.File;
26 import java.io.FileWriter;
27 import java.io.IOException;
28
29 /**
30 * A <code>TextDiagram</code> that can be fully defined by a string, for
31 * example using XML.
32 */
33 public class TextDiagram implements Diagram {
34 private String text;
35
36 /**
37 * Creates a new <code>TextDiagram</code> defined by the given string.
38 *
39 * @param text
40 * the string defining this <code>TextDiagram</code>
41 */
42 public TextDiagram(String text) {
43 this.text = text;
44 }
45
46 /**
47 * Writes this <code>TextDiagram</code> to file using a
48 * <code>Writer</code> so as to support different character encodings.
49 *
50 * @param file
51 * the <code>File</code> to write to
52 *
53 * @throws IOException
54 * if the diagram could not be saved
55 */
56 public void save(File file) throws IOException {
57 FileWriter writer = null;
58 try {
59 writer = new FileWriter(file);
60 writer.write(text);
61 } finally {
62 if (writer != null) {
63 try {
64 writer.close();
65 } catch (IOException e) {
66 System.err.println(e);
67 }
68 }
69 }
70 }
71
72 /**
73 * Returns the string defining this <code>TextDiagram</code>.
74 *
75 * @return the string defining this <code>TextDiagram</code>
76 */
77 @Override
78 public String toString() {
79 return text;
80 }
81 }