1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.orekit.files.ccsds.utils.generation;
18
19 import java.io.IOException;
20 import java.util.List;
21
22 import org.orekit.files.ccsds.utils.FileFormat;
23 import org.orekit.utils.AccurateFormatter;
24 import org.orekit.utils.units.Unit;
25
26
27
28
29
30 public class XmlGenerator extends AbstractGenerator {
31
32
33 public static final int DEFAULT_INDENT = 2;
34
35
36 public static final String UNITS = "units";
37
38
39 private static final String PROLOG = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>%n";
40
41
42 private static final String ROOT_START = "<%s id=\"%s\" version=\"%.1f\">%n";
43
44
45 private static final String START_TAG = "<%s>%n";
46
47
48 private static final String END_TAG = "</%s>%n";
49
50
51 private static final String LEAF_0_ATTRIBUTES = "<%s>%s</%s>%n";
52
53
54 private static final String LEAF_1_ATTRIBUTE = "<%s %s=\"%s\">%s</%s>%n";
55
56
57 private static final String LEAF_2_ATTRIBUTES = "<%s %s=\"%s\" %s=\"%s\">%s</%s>%n";
58
59
60 private static final String COMMENT = "COMMENT";
61
62
63 private final int indentation;
64
65
66 private int level;
67
68
69
70
71
72
73
74
75
76 public XmlGenerator(final Appendable output, final int indentation,
77 final String outputName, final boolean writeUnits) throws IOException {
78 super(output, outputName, writeUnits);
79 this.indentation = indentation;
80 this.level = 0;
81 writeRawData(String.format(AccurateFormatter.STANDARDIZED_LOCALE, PROLOG));
82 }
83
84
85 @Override
86 public FileFormat getFormat() {
87 return FileFormat.XML;
88 }
89
90
91 @Override
92 public void startMessage(final String root, final String messageTypeKey, final double version) throws IOException {
93 indent();
94 writeRawData(String.format(AccurateFormatter.STANDARDIZED_LOCALE, ROOT_START,
95 root, messageTypeKey, version));
96 ++level;
97 }
98
99
100 @Override
101 public void endMessage(final String root) throws IOException {
102 --level;
103 indent();
104 writeRawData(String.format(AccurateFormatter.STANDARDIZED_LOCALE, END_TAG,
105 root));
106 }
107
108
109 @Override
110 public void writeComments(final List<String> comments) throws IOException {
111 for (final String comment : comments) {
112 writeEntry(COMMENT, comment, null, false);
113 }
114 }
115
116
117
118
119
120
121
122
123 public void writeOneAttributeElement(final String name, final String value,
124 final String attributeName, final String attributeValue)
125 throws IOException {
126 indent();
127 writeRawData(String.format(AccurateFormatter.STANDARDIZED_LOCALE, LEAF_1_ATTRIBUTE,
128 name, attributeName, attributeValue, value, name));
129 }
130
131
132
133
134
135
136
137
138
139
140 public void writeTwoAttributesElement(final String name, final String value,
141 final String attribute1Name, final String attribute1Value,
142 final String attribute2Name, final String attribute2Value)
143 throws IOException {
144 indent();
145 writeRawData(String.format(AccurateFormatter.STANDARDIZED_LOCALE, LEAF_2_ATTRIBUTES,
146 name,
147 attribute1Name, attribute1Value, attribute2Name, attribute2Value,
148 value, name));
149 }
150
151
152 @Override
153 public void writeEntry(final String key, final String value, final Unit unit, final boolean mandatory) throws IOException {
154 if (value == null) {
155 complain(key, mandatory);
156 } else {
157 indent();
158 if (writeUnits(unit)) {
159 writeRawData(String.format(AccurateFormatter.STANDARDIZED_LOCALE, LEAF_1_ATTRIBUTE,
160 key, UNITS, siToCcsdsName(unit.getName()), value, key));
161 } else {
162 writeRawData(String.format(AccurateFormatter.STANDARDIZED_LOCALE, LEAF_0_ATTRIBUTES,
163 key, value, key));
164 }
165 }
166 }
167
168
169 @Override
170 public void enterSection(final String name) throws IOException {
171 indent();
172 writeRawData(String.format(AccurateFormatter.STANDARDIZED_LOCALE, START_TAG, name));
173 ++level;
174 super.enterSection(name);
175 }
176
177
178 @Override
179 public String exitSection() throws IOException {
180 final String name = super.exitSection();
181 --level;
182 indent();
183 writeRawData(String.format(AccurateFormatter.STANDARDIZED_LOCALE, END_TAG, name));
184 return name;
185 }
186
187
188
189
190 private void indent() throws IOException {
191 for (int i = 0; i < level * indentation; ++i) {
192 writeRawData(' ');
193 }
194 }
195
196 }