Xcos writers: add the "defaultParent" to the XML
[scilab.git] / scilab / modules / xcos / src / java / org / scilab / modules / xcos / io / writer / XcosWriter.java
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2015-2015 - Scilab Enterprises - Clement DAVID
4  *
5  * This file must be used under the terms of the CeCILL.
6  * This source file is licensed as described in the file COPYING, which
7  * you should have received as part of this distribution.  The terms
8  * are also available at
9  * http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
10  *
11  */
12 package org.scilab.modules.xcos.io.writer;
13
14 import java.util.logging.Level;
15 import java.util.logging.Logger;
16
17 import javax.xml.stream.XMLStreamException;
18 import javax.xml.stream.XMLStreamWriter;
19
20 import org.scilab.modules.types.ScilabList;
21 import org.scilab.modules.xcos.JavaController;
22 import org.scilab.modules.xcos.Kind;
23 import org.scilab.modules.xcos.utils.Stack;
24
25 /**
26  * Implement an XML writer for any kind of model object
27  */
28 public class XcosWriter {
29
30     protected static final Logger LOG = Logger.getLogger("org.scilab.modules.xcos.io.writer");
31
32     /*
33      * Shared data
34      */
35     protected final JavaController controller;
36     protected final ScilabList dictionary;
37     protected final RawDataWriter rawDataWriter;
38     protected final XMLStreamWriter stream;
39     protected final Stack<String> layers;
40
41     public XcosWriter(ScilabList dictionary, XMLStreamWriter writer) {
42         this.controller = new JavaController();
43         this.dictionary = dictionary;
44         this.rawDataWriter = new RawDataWriter(this);
45         this.stream = writer;
46
47         this.layers = new Stack<>();
48     }
49
50     public void write(long uid, Kind kind) throws XMLStreamException {
51
52         if (LOG.isLoggable(Level.FINER)) {
53             LOG.entering(XcosWriter.class.getName(), "write");
54         }
55
56         switch (kind) {
57             case ANNOTATION:
58             case BLOCK:
59                 new BlockWriter(this).write(uid, kind);
60                 break;
61             case DIAGRAM:
62                 new CustomWriter(this).write(uid, kind);
63                 break;
64             case LINK:
65                 new LinkWriter(this).write(uid, kind);
66                 break;
67             case PORT:
68                 // this will be managed by the BlockWriter
69                 break;
70         }
71
72         if (LOG.isLoggable(Level.FINER)) {
73             LOG.exiting(XcosWriter.class.getName(), "write");
74         }
75     }
76 }