Xcos writers: indent ".xcos" files
[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     protected static final Logger LOG = Logger.getLogger("org.scilab.modules.xcos.io.writer");
30
31     /*
32      * Shared data
33      */
34     protected final JavaController controller;
35     protected final ScilabList dictionary;
36     protected final RawDataWriter rawDataWriter;
37     protected final XMLStreamWriter stream;
38     protected final Stack<String> layers;
39
40     public XcosWriter(ScilabList dictionary, XMLStreamWriter writer) {
41         this.controller = new JavaController();
42         this.dictionary = dictionary;
43         this.rawDataWriter = new RawDataWriter(this);
44         this.stream = writer;
45
46         this.layers = new Stack<>();
47     }
48
49     public void write(long uid, Kind kind) throws XMLStreamException {
50
51         if (LOG.isLoggable(Level.FINER)) {
52             LOG.entering(XcosWriter.class.getName(), "write");
53         }
54
55         switch (kind) {
56             case ANNOTATION:
57             case BLOCK:
58                 new BlockWriter(this).write(uid, kind);
59                 break;
60             case DIAGRAM:
61                 new CustomWriter(this).write(uid, kind);
62                 break;
63             case LINK:
64                 new LinkWriter(this).write(uid, kind);
65                 break;
66             case PORT:
67                 // this will be managed by the BlockWriter
68                 break;
69         }
70
71         if (LOG.isLoggable(Level.FINER)) {
72             LOG.exiting(XcosWriter.class.getName(), "write");
73         }
74     }
75 }