Commit 3faf3eb4 authored by Luc Maisonobe's avatar Luc Maisonobe

Fixed writing XML files.

parent fa591d49
......@@ -29,6 +29,12 @@ import org.orekit.files.ccsds.section.CommentsContainer;
*/
public class UserDefined extends CommentsContainer {
/** Tag name for user defined parameters keys. */
public static final String USER_DEFINED_XML_TAG = "USER_DEFINED";
/** Attribute name for user defined parameters keys. */
public static final String USER_DEFINED_XML_ATTRIBUTE = "parameter";
/** Prefix for user defined parameters keys. */
public static final String USER_DEFINED_PREFIX = "USER_DEFINED_";
......
......@@ -20,9 +20,7 @@ package org.orekit.files.ccsds.ndm.odm;
import java.io.IOException;
import java.util.Map;
import org.orekit.errors.OrekitInternalError;
import org.orekit.files.ccsds.section.AbstractWriter;
import org.orekit.files.ccsds.utils.FileFormat;
import org.orekit.files.ccsds.utils.generation.Generator;
/** Writer for user defined parameters data.
......@@ -48,12 +46,7 @@ public class UserDefinedWriter extends AbstractWriter {
@Override
protected void writeContent(final Generator generator) throws IOException {
for (Map.Entry<String, String> entry : userDefined.getParameters().entrySet()) {
if (generator.getFormat() == FileFormat.XML) {
// TODO
throw new OrekitInternalError(null);
} else {
generator.writeEntry(UserDefined.USER_DEFINED_PREFIX + entry.getKey(), entry.getValue(), false);
}
generator.writeUserDefined(entry.getKey(), entry.getValue());
}
}
......
......@@ -59,6 +59,13 @@ public interface Generator extends AutoCloseable {
*/
void writeComments(List<String> comments) throws IOException;
/** Write a user defined entry.
* @param parameter name of the user defined parameter
* @param value the value to write
* @throws IOException if an I/O error occurs.
*/
void writeUserDefined(String parameter, String value) throws IOException;
/** Write a single key/value entry.
* @param key the keyword to write
* @param value the value to write
......
......@@ -20,6 +20,7 @@ import java.io.IOException;
import java.util.List;
import org.hipparchus.util.FastMath;
import org.orekit.files.ccsds.ndm.odm.UserDefined;
import org.orekit.files.ccsds.utils.FileFormat;
import org.orekit.utils.AccurateFormatter;
......@@ -94,6 +95,12 @@ public class KvnGenerator extends AbstractGenerator {
}
}
/** {@inheritDoc} */
@Override
public void writeUserDefined(final String parameter, final String value) throws IOException {
writeEntry(UserDefined.USER_DEFINED_PREFIX + parameter, value, false);
}
/** {@inheritDoc} */
@Override
public void writeEntry(final String key, final String value, final boolean mandatory) throws IOException {
......
......@@ -19,6 +19,7 @@ package org.orekit.files.ccsds.utils.generation;
import java.io.IOException;
import java.util.List;
import org.orekit.files.ccsds.ndm.odm.UserDefined;
import org.orekit.files.ccsds.utils.FileFormat;
import org.orekit.utils.AccurateFormatter;
......@@ -46,6 +47,9 @@ public class XmlGenerator extends AbstractGenerator {
/** Leaf element format. */
private static final String LEAF = "<%s>%s</%s>%n";
/** User defined parameter element format. */
private static final String USER_DEFINED = "<%s %s=\"%s\">%s</%s>%n";
/** Comment key. */
private static final String COMMENT = "COMMENT";
......@@ -100,6 +104,18 @@ public class XmlGenerator extends AbstractGenerator {
}
}
/** {@inheritDoc} */
@Override
public void writeUserDefined(final String parameter, final String value) throws IOException {
indent();
append(String.format(AccurateFormatter.STANDARDIZED_LOCALE, USER_DEFINED,
UserDefined.USER_DEFINED_XML_TAG,
UserDefined.USER_DEFINED_XML_ATTRIBUTE,
parameter,
value,
UserDefined.USER_DEFINED_XML_TAG));
}
/** {@inheritDoc} */
@Override
public void writeEntry(final String key, final String value, final boolean mandatory) throws IOException {
......
......@@ -16,6 +16,7 @@
*/
package org.orekit.files.ccsds.utils.lexical;
import org.orekit.files.ccsds.ndm.odm.UserDefined;
import org.xml.sax.Attributes;
/** Builder for user-defined parameters.
......@@ -27,12 +28,6 @@ import org.xml.sax.Attributes;
*/
public class UserDefinedXmlTokenBuilder implements XmlTokenBuilder {
/** Prefix for user defined parameters keys. */
public static final String USER_DEFINED_PREFIX = "USER_DEFINED_";
/** Attribute for user defined parameters. */
public static final String PARAMETER = "parameter";
/** {@inheritDoc} */
@Override
public ParseToken buildToken(final boolean startTag, final String qName,
......@@ -45,7 +40,9 @@ public class UserDefinedXmlTokenBuilder implements XmlTokenBuilder {
TokenType.ENTRY;
// final build
return new ParseToken(tokenType, USER_DEFINED_PREFIX + attributes.getValue(PARAMETER),
final String name = attributes.getValue(UserDefined.USER_DEFINED_XML_ATTRIBUTE);
return new ParseToken(tokenType,
UserDefined.USER_DEFINED_PREFIX + name,
content, null,
lineNumber, fileName);
......
......@@ -20,7 +20,6 @@ import java.io.ByteArrayInputStream;
import java.io.CharArrayWriter;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
......@@ -74,13 +73,10 @@ public abstract class AbstractNdmWriterTest<H extends Header, S extends Segment<
new KvnGenerator(caw, 25, "dummy.kvn") :
new XmlGenerator(caw, XmlGenerator.DEFAULT_INDENT, "dummy.xml");
getWriter().writeMessage(generator, original);
if (format == FileFormat.XML) {
System.out.println(caw);
}
// reparse the written file
final ByteBuffer buffer = StandardCharsets.UTF_8.encode(caw.toString());
final DataSource source2 = new DataSource(name, () -> new ByteArrayInputStream(buffer.array()));
final byte[] bytes = caw.toString().getBytes(StandardCharsets.UTF_8);
final DataSource source2 = new DataSource(name, () -> new ByteArrayInputStream(bytes));
final F rebuilt = getParser().parseMessage(source2);
checkEquals(original, rebuilt);
......
......@@ -26,7 +26,6 @@ import java.io.ByteArrayInputStream;
import java.io.CharArrayWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
......@@ -108,10 +107,10 @@ public class AttitudeWriterTest {
buildAemWriter();
final CharArrayWriter caw = new CharArrayWriter();
writer.writeMessage(new KvnGenerator(caw, 0, ""), aemFile);
final ByteBuffer buffer = StandardCharsets.UTF_8.encode(caw.toString());
final byte[] bytes = caw.toString().getBytes(StandardCharsets.UTF_8);
final AemFile generatedOemFile = new ParserBuilder().buildAemParser().
parseMessage(new DataSource("", () -> new ByteArrayInputStream(buffer.array())));
parseMessage(new DataSource("", () -> new ByteArrayInputStream(bytes)));
compareAemFiles(aemFile, generatedOemFile);
}
......@@ -178,10 +177,10 @@ public class AttitudeWriterTest {
AemWriter writer = new WriterBuilder().buildAemWriter();
final CharArrayWriter caw = new CharArrayWriter();
writer.writeMessage(new KvnGenerator(caw, 0, ""), aemFile);
final ByteBuffer buffer = StandardCharsets.UTF_8.encode(caw.toString());
final byte[] bytes = caw.toString().getBytes(StandardCharsets.UTF_8);
final AemFile generatedAemFile = new ParserBuilder().buildAemParser().
parseMessage(new DataSource("", () -> new ByteArrayInputStream(buffer.array())));
parseMessage(new DataSource("", () -> new ByteArrayInputStream(bytes)));
assertEquals(aemFile.getSegments().get(0).getMetadata().getObjectID(),
generatedAemFile.getSegments().get(0).getMetadata().getObjectID());
}
......@@ -214,17 +213,17 @@ public class AttitudeWriterTest {
null, metadata, FileFormat.KVN, "");
final CharArrayWriter caw = new CharArrayWriter();
writer.write(caw, file);
final ByteBuffer buffer = StandardCharsets.UTF_8.encode(caw.toString());
final byte[] bytes = caw.toString().getBytes(StandardCharsets.UTF_8);
int count = 0;
try (ByteArrayInputStream bais = new ByteArrayInputStream(buffer.array());
try (ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
InputStreamReader isr = new InputStreamReader(bais, StandardCharsets.UTF_8);
BufferedReader br = new BufferedReader(isr)) {
for (String line = br.readLine(); line != null; line = br.readLine()) {
++count;
}
}
assertEquals(83, count);
assertEquals(82, count);
}
......@@ -239,10 +238,10 @@ public class AttitudeWriterTest {
FileFormat.KVN, "TestAEMIssue723.aem");
final CharArrayWriter caw = new CharArrayWriter();
writer.write(caw, aemFile);
final ByteBuffer buffer = StandardCharsets.UTF_8.encode(caw.toString());
final byte[] bytes = caw.toString().getBytes(StandardCharsets.UTF_8);
final AemFile generatedAemFile = new ParserBuilder().buildAemParser().
parseMessage(new DataSource("", () -> new ByteArrayInputStream(buffer.array())));
parseMessage(new DataSource("", () -> new ByteArrayInputStream(bytes)));
assertEquals(aemFile.getHeader().getComments().get(0), generatedAemFile.getHeader().getComments().get(0));
}
......
......@@ -20,7 +20,6 @@ import java.io.ByteArrayInputStream;
import java.io.CharArrayWriter;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
......@@ -172,8 +171,8 @@ public class APMParserTest {
new WriterBuilder().buildApmWriter().writeMessage(generator, original);
// reparse the written file
final ByteBuffer buffer = StandardCharsets.UTF_8.encode(caw.toString());
final DataSource source2 = new DataSource(name, () -> new ByteArrayInputStream(buffer.array()));
final byte[] bytes = caw.toString().getBytes(StandardCharsets.UTF_8);
final DataSource source2 = new DataSource(name, () -> new ByteArrayInputStream(bytes));
final ApmFile rebuilt = new ParserBuilder().buildApmParser().parseMessage(source2);
validateAPM2(rebuilt);
}
......
......@@ -20,7 +20,6 @@ import java.io.ByteArrayInputStream;
import java.io.CharArrayWriter;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Locale;
......@@ -458,8 +457,8 @@ public class OcmParserTest {
new WriterBuilder().buildOcmWriter().writeMessage(generator, original);
// reparse the written file
final ByteBuffer bb = StandardCharsets.UTF_8.encode(caw.toString());
final DataSource source2 = new DataSource(name, () -> new ByteArrayInputStream(bb.array()));
final byte[] bytes = caw.toString().getBytes(StandardCharsets.UTF_8);
final DataSource source2 = new DataSource(name, () -> new ByteArrayInputStream(bytes));
final OcmFile rebuilt = new ParserBuilder().buildOcmParser().parseMessage(source2);
validateOCM2XML(rebuilt);
......
......@@ -26,7 +26,6 @@ import java.io.ByteArrayInputStream;
import java.io.CharArrayWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
......@@ -89,11 +88,11 @@ public class EphemerisWriterTest {
buildOemWriter();
final CharArrayWriter caw = new CharArrayWriter();
writer.writeMessage(new KvnGenerator(caw, 0, ""), oemFile);
final ByteBuffer buffer = StandardCharsets.UTF_8.encode(caw.toString());
final byte[] bytes = caw.toString().getBytes(StandardCharsets.UTF_8);
final OemFile generatedOemFile = new OemParser(IERSConventions.IERS_2010, true, DataContext.getDefault(),
null, CelestialBodyFactory.getMars().getGM(), 1).
parseMessage(new DataSource("", () -> new ByteArrayInputStream(buffer.array())));
parseMessage(new DataSource("", () -> new ByteArrayInputStream(bytes)));
compareOemFiles(oemFile, generatedOemFile);
}
......@@ -155,12 +154,12 @@ public class EphemerisWriterTest {
OemWriter writer = new WriterBuilder().buildOemWriter();
final CharArrayWriter caw = new CharArrayWriter();
writer.writeMessage(new KvnGenerator(caw, 0, ""), oemFile);
final ByteBuffer buffer = StandardCharsets.UTF_8.encode(caw.toString());
final byte[] bytes = caw.toString().getBytes(StandardCharsets.UTF_8);
final OemFile generatedOemFile = new ParserBuilder().
withMu(CelestialBodyFactory.getEarth().getGM()).
buildOemParser().
parseMessage(new DataSource("", () -> new ByteArrayInputStream(buffer.array())));
parseMessage(new DataSource("", () -> new ByteArrayInputStream(bytes)));
assertEquals(oemFile.getSegments().get(0).getMetadata().getObjectID(),
generatedOemFile.getSegments().get(0).getMetadata().getObjectID());
}
......@@ -178,12 +177,12 @@ public class EphemerisWriterTest {
FileFormat.KVN, "TestOEMIssue723.aem");
final CharArrayWriter caw = new CharArrayWriter();
writer.write(caw, oemFile);
final ByteBuffer buffer = StandardCharsets.UTF_8.encode(caw.toString());
final byte[] bytes = caw.toString().getBytes(StandardCharsets.UTF_8);
final OemFile generatedOemFile = new ParserBuilder().
withMu(CelestialBodyFactory.getEarth().getGM()).
buildOemParser().
parseMessage(new DataSource("", () -> new ByteArrayInputStream(buffer.array())));
parseMessage(new DataSource("", () -> new ByteArrayInputStream(bytes)));
assertEquals(oemFile.getHeader().getComments().get(0), generatedOemFile.getHeader().getComments().get(0));
}
......@@ -236,10 +235,10 @@ public class EphemerisWriterTest {
null, metadata, FileFormat.KVN, "");
final CharArrayWriter caw = new CharArrayWriter();
writer.write(caw, file);
final ByteBuffer buffer = StandardCharsets.UTF_8.encode(caw.toString());
final byte[] bytes = caw.toString().getBytes(StandardCharsets.UTF_8);
int count = 0;
try (ByteArrayInputStream bais = new ByteArrayInputStream(buffer.array());
try (ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
InputStreamReader isr = new InputStreamReader(bais, StandardCharsets.UTF_8);
BufferedReader br = new BufferedReader(isr)) {
for (String line = br.readLine(); line != null; line = br.readLine()) {
......
......@@ -20,7 +20,6 @@ import java.io.ByteArrayInputStream;
import java.io.CharArrayWriter;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.HashMap;
......@@ -166,8 +165,8 @@ public class OmmParserTest {
new WriterBuilder().buildOmmWriter().writeMessage(generator, original);
// reparse the written file
final ByteBuffer bb = StandardCharsets.UTF_8.encode(caw.toString());
final DataSource source2 = new DataSource(name, () -> new ByteArrayInputStream(bb.array()));
final byte[] bytes = caw.toString().getBytes(StandardCharsets.UTF_8);
final DataSource source2 = new DataSource(name, () -> new ByteArrayInputStream(bytes));
final OmmFile rebuilt = new ParserBuilder().buildOmmParser().parseMessage(source2);
validateOMM2(rebuilt);
......
......@@ -20,7 +20,6 @@ import java.io.ByteArrayInputStream;
import java.io.CharArrayWriter;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
......@@ -468,8 +467,8 @@ public class OpmParserTest {
new WriterBuilder().buildOpmWriter().writeMessage(generator, original);
// reparse the written file
final ByteBuffer bb = StandardCharsets.UTF_8.encode(caw.toString());
final DataSource source2 = new DataSource(name, () -> new ByteArrayInputStream(bb.array()));
final byte[] bytes = caw.toString().getBytes(StandardCharsets.UTF_8);
final DataSource source2 = new DataSource(name, () -> new ByteArrayInputStream(bytes));
final OpmFile rebuilt = new ParserBuilder().buildOpmParser().parseMessage(source2);
validateOPM3XML(rebuilt);
......
......@@ -21,7 +21,6 @@ import java.io.CharArrayWriter;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
......@@ -188,8 +187,8 @@ public class TdmParserTest {
new WriterBuilder().buildTdmWriter(null).writeMessage(generator, original);
// reparse the written file
final ByteBuffer bb = StandardCharsets.UTF_8.encode(caw.toString());
final DataSource source2 = new DataSource(name, () -> new ByteArrayInputStream(bb.array()));
final byte[] bytes = caw.toString().getBytes(StandardCharsets.UTF_8);
final DataSource source2 = new DataSource(name, () -> new ByteArrayInputStream(bytes));
final TdmFile rebuilt = new ParserBuilder().buildTdmParser(null).parseMessage(source2);
validateTDMExample2(rebuilt);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment