Search in sources :

Example 1 with BeanConverter

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>&lt;?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);
}
Also used : InputStreamReader(java.io.InputStreamReader) Configuration(net.sf.sdedit.config.Configuration) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) StringWriter(java.io.StringWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) BufferedReader(java.io.BufferedReader) BeanConverter(net.sf.sdedit.ui.components.configuration.BeanConverter) PrintWriter(java.io.PrintWriter) Pair(net.sf.sdedit.util.Pair)

Example 2 with BeanConverter

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();
    }
}
Also used : CDATASection(org.w3c.dom.CDATASection) Element(org.w3c.dom.Element) OutputStreamWriter(java.io.OutputStreamWriter) Document(org.w3c.dom.Document) BeanConverter(net.sf.sdedit.ui.components.configuration.BeanConverter) PrintWriter(java.io.PrintWriter)

Aggregations

PrintWriter (java.io.PrintWriter)2 BeanConverter (net.sf.sdedit.ui.components.configuration.BeanConverter)2 Document (org.w3c.dom.Document)2 Element (org.w3c.dom.Element)2 BufferedReader (java.io.BufferedReader)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 OutputStreamWriter (java.io.OutputStreamWriter)1 StringWriter (java.io.StringWriter)1 Configuration (net.sf.sdedit.config.Configuration)1 Pair (net.sf.sdedit.util.Pair)1 CDATASection (org.w3c.dom.CDATASection)1