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.Formatter;
25 import org.orekit.utils.units.Unit;
26
27
28
29
30
31 public class XmlGenerator extends AbstractGenerator {
32
33
34 public static final int DEFAULT_INDENT = 2;
35
36
37 public static final String UNITS = "units";
38
39
40
41
42 public static final String NDM_XML_V3_SCHEMA_LOCATION = "https://sanaregistry.org/r/ndmxml_unqualified/ndmxml-3.0.0-master-3.0.xsd";
43
44
45 private static final String PROLOG = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>%n";
46
47
48
49
50 private static final String ROOT_START_WITH_SCHEMA = "<%s xmlns:xsi=\"%s\" xsi:noNamespaceSchemaLocation=\"%s\" id=\"%s\" version=\"%.1f\">%n";
51
52
53
54
55 private static final String ROOT_START_WITHOUT_SCHEMA = "<%s id=\"%s\" version=\"%.1f\">%n";
56
57
58
59
60 private static final String XMLNS_XSI = "http://www.w3.org/2001/XMLSchema-instance";
61
62
63
64
65 private static final String START_TAG_WITH_SCHEMA = "<%s xmlns:xsi=\"%s\" xsi:noNamespaceSchemaLocation=\"%s\">%n";
66
67
68
69
70 private static final String START_TAG_WITHOUT_SCHEMA = "<%s>%n";
71
72
73 private static final String END_TAG = "</%s>%n";
74
75
76 private static final String LEAF_0_ATTRIBUTES = "<%s>%s</%s>%n";
77
78
79 private static final String LEAF_1_ATTRIBUTE = "<%s %s=\"%s\">%s</%s>%n";
80
81
82 private static final String LEAF_2_ATTRIBUTES = "<%s %s=\"%s\" %s=\"%s\">%s</%s>%n";
83
84
85 private static final String COMMENT = "COMMENT";
86
87
88 private final String schemaLocation;
89
90
91 private final int indentation;
92
93
94 private int level;
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109 public XmlGenerator(final Appendable output, final int indentation,
110 final String outputName, final double maxRelativeOffset,
111 final boolean writeUnits, final String schemaLocation,
112 final Formatter formatter) throws IOException {
113 super(output, outputName, maxRelativeOffset, writeUnits, formatter);
114 this.schemaLocation = schemaLocation;
115 this.indentation = indentation;
116 this.level = 0;
117 writeRawData(String.format(Formatter.STANDARDIZED_LOCALE, PROLOG));
118 }
119
120
121
122
123
124
125
126
127
128
129
130
131
132 public XmlGenerator(final Appendable output, final int indentation,
133 final String outputName, final double maxRelativeOffset,
134 final boolean writeUnits, final String schemaLocation) throws IOException {
135 this(output, indentation, outputName, maxRelativeOffset, writeUnits, schemaLocation, new AccurateFormatter());
136 }
137
138
139
140 @Override
141 public FileFormat getFormat() {
142 return FileFormat.XML;
143 }
144
145
146 @Override
147 public void startMessage(final String root, final String messageTypeKey, final double version) throws IOException {
148 indent();
149 if (schemaLocation == null || level > 0) {
150 writeRawData(String.format(Formatter.STANDARDIZED_LOCALE, ROOT_START_WITHOUT_SCHEMA,
151 root, messageTypeKey, version));
152 } else {
153 writeRawData(String.format(Formatter.STANDARDIZED_LOCALE, ROOT_START_WITH_SCHEMA,
154 root, XMLNS_XSI, schemaLocation, messageTypeKey, version));
155 }
156 ++level;
157 }
158
159
160 @Override
161 public void endMessage(final String root) throws IOException {
162 --level;
163 indent();
164 writeRawData(String.format(Formatter.STANDARDIZED_LOCALE, END_TAG,
165 root));
166 }
167
168
169 @Override
170 public void writeComments(final List<String> comments) throws IOException {
171 for (final String comment : comments) {
172 writeEntry(COMMENT, comment, null, false);
173 }
174 }
175
176
177
178
179
180
181
182
183 public void writeOneAttributeElement(final String name, final String value,
184 final String attributeName, final String attributeValue)
185 throws IOException {
186 indent();
187 writeRawData(String.format(Formatter.STANDARDIZED_LOCALE, LEAF_1_ATTRIBUTE,
188 name, attributeName, attributeValue, value, name));
189 }
190
191
192
193
194
195
196
197
198
199
200 public void writeTwoAttributesElement(final String name, final String value,
201 final String attribute1Name, final String attribute1Value,
202 final String attribute2Name, final String attribute2Value)
203 throws IOException {
204 indent();
205 writeRawData(String.format(Formatter.STANDARDIZED_LOCALE, LEAF_2_ATTRIBUTES,
206 name,
207 attribute1Name, attribute1Value, attribute2Name, attribute2Value,
208 value, name));
209 }
210
211
212 @Override
213 public void writeEntry(final String key, final String value, final Unit unit, final boolean mandatory) throws IOException {
214 if (value == null) {
215 complain(key, mandatory);
216 } else {
217 indent();
218 if (writeUnits(unit)) {
219 writeRawData(String.format(Formatter.STANDARDIZED_LOCALE, LEAF_1_ATTRIBUTE,
220 key, UNITS, siToCcsdsName(unit.getName()), value, key));
221 } else {
222 writeRawData(String.format(Formatter.STANDARDIZED_LOCALE, LEAF_0_ATTRIBUTES,
223 key, value, key));
224 }
225 }
226 }
227
228
229 @Override
230 public void enterSection(final String name) throws IOException {
231 indent();
232 if (schemaLocation != null && level == 0) {
233
234 writeRawData(String.format(Formatter.STANDARDIZED_LOCALE, START_TAG_WITH_SCHEMA,
235 name, XMLNS_XSI, schemaLocation));
236 } else {
237 writeRawData(String.format(Formatter.STANDARDIZED_LOCALE, START_TAG_WITHOUT_SCHEMA, name));
238 }
239 ++level;
240 super.enterSection(name);
241 }
242
243
244 @Override
245 public String exitSection() throws IOException {
246 final String name = super.exitSection();
247 --level;
248 indent();
249 writeRawData(String.format(Formatter.STANDARDIZED_LOCALE, END_TAG, name));
250 return name;
251 }
252
253
254
255
256 private void indent() throws IOException {
257 for (int i = 0; i < level * indentation; ++i) {
258 writeRawData(' ');
259 }
260 }
261
262 }