use of net.sf.sdedit.ui.components.configuration.BeanConverter in project abstools by abstools.
the class DiagramLoader method load.
/**
* Loads a diagram from the text transmitted through the given
* <tt>stream</tt>. If the text contains a line that starts with
* <tt><?xml</tt>, it is interpreted as an XML file, containing the
* diagram source as a CDATA section along with a configuration. Otherwise
* the whole of the text is interpreted as a diagram source, and a default
* configuration is used.
*
* @param stream
* the stream from where the diagram specification is read
* @param encoding
* the encoding of the diagram specification
* @return a pair of the diagram source and the configuration to be used for
* generating the diagram
*
* @throws IOException
* @throws DocUtil.XMLException
*/
public static Pair<String, Bean<Configuration>> load(InputStream stream, String encoding) throws IOException, DocUtil.XMLException {
InputStreamReader reader = new InputStreamReader(stream, encoding);
BufferedReader buffered = new BufferedReader(reader);
StringWriter stringWriter = new StringWriter();
PrintWriter writer = new PrintWriter(stringWriter);
boolean xml = false;
String line = buffered.readLine();
while (line != null) {
xml |= line.trim().startsWith("<?xml");
writer.println(line);
line = buffered.readLine();
}
writer.close();
String source;
Bean<Configuration> configuration = ConfigurationManager.createNewDefaultConfiguration();
if (xml) {
InputStream inputStream = new ByteArrayInputStream(stringWriter.toString().getBytes(encoding));
try {
Document document = DocUtil.readDocument(inputStream, encoding);
source = DocUtil.evaluateCDATA(document, "/diagram/source");
Element confElement = (Element) DocUtil.evalXPathAsNode(document, "/diagram/configuration");
BeanConverter converter = new BeanConverter(configuration, document);
converter.setValues(confElement);
} finally {
inputStream.close();
}
} else {
source = stringWriter.toString();
}
return new Pair<String, Bean<Configuration>>(source, configuration);
}
use of net.sf.sdedit.ui.components.configuration.BeanConverter in project abstools by abstools.
the class DiagramLoader method saveDiagram.
/**
* Saves a diagram specification (and a configuration), using a stream.
*
* @param source
* the source text of the diagram
* @param configuration
* a configuration of the diagram, or null if it is to be saved
* without a configuration (as plain text)
* @param stream
* the stream to use for saving the diagram source and
* configuration
* @param encoding
* the encoding to be used
*
* @throws IOException
* @throws XMLException
*/
public static void saveDiagram(String source, Bean<Configuration> configuration, OutputStream stream, String encoding) throws IOException, XMLException {
if (configuration != null) {
Document document = DocUtil.newDocument();
Element root = document.createElement("diagram");
document.appendChild(root);
BeanConverter converter = new BeanConverter(configuration, document);
Element sourceElem = document.createElement("source");
CDATASection sourceNode = document.createCDATASection(source);
sourceElem.appendChild(sourceNode);
root.appendChild(sourceElem);
Element configurationNode = converter.createElement("configuration");
root.appendChild(configurationNode);
DocUtil.writeDocument(document, encoding, stream);
} else {
OutputStreamWriter osw = new OutputStreamWriter(stream, encoding);
PrintWriter pw = new PrintWriter(osw);
pw.print(source);
pw.flush();
}
}
Aggregations