diff options
author | Android Build Coastguard Worker <android-build-coastguard-worker@google.com> | 2023-01-31 04:32:38 +0000 |
---|---|---|
committer | Android Build Coastguard Worker <android-build-coastguard-worker@google.com> | 2023-01-31 04:32:38 +0000 |
commit | c2d2a8ba528945b5abac66c986ed2a4fe45a0580 (patch) | |
tree | c8fa198d5990f94d95e0adc50c2b29837da42055 | |
parent | ba70ded966408026576e0ae5817eb7debbc3fc52 (diff) | |
parent | b06c8d16ee3c692726b6b3bdc3e91526f9f6601a (diff) | |
download | xsdc-c2d2a8ba528945b5abac66c986ed2a4fe45a0580.tar.gz |
Snap for 9541950 from b06c8d16ee3c692726b6b3bdc3e91526f9f6601a to udc-release
Change-Id: Id10956f946f4ec8b2d886493e5ddcc99cc6dc692
-rw-r--r-- | src/main/java/com/android/xsdc/cpp/CppCodeGenerator.java | 55 | ||||
-rw-r--r-- | src/main/java/com/android/xsdc/cpp/CppType.java | 7 | ||||
-rw-r--r-- | src/main/java/com/android/xsdc/java/JavaCodeGenerator.java | 60 | ||||
-rw-r--r-- | tests/resources/simple_type/api/current.txt | 2 | ||||
-rw-r--r-- | tests/resources/simple_type/simple_type.xsd | 1 | ||||
-rw-r--r-- | tests/resources/simple_type_root.xml | 2 | ||||
-rw-r--r-- | tests/simple_type.cpp | 12 | ||||
-rw-r--r-- | tests/src/com/android/xsdc/tests/XmlParserTest.java | 25 |
8 files changed, 112 insertions, 52 deletions
diff --git a/src/main/java/com/android/xsdc/cpp/CppCodeGenerator.java b/src/main/java/com/android/xsdc/cpp/CppCodeGenerator.java index bf8ebb7..e4e7037 100644 --- a/src/main/java/com/android/xsdc/cpp/CppCodeGenerator.java +++ b/src/main/java/com/android/xsdc/cpp/CppCodeGenerator.java @@ -411,7 +411,7 @@ public class CppCodeGenerator { elementTypes.add(cppType); } List<CppSimpleType> attributeTypes = new ArrayList<>(); - List<XsdAttribute> attributes = new ArrayList(); + List<XsdAttribute> attributes = new ArrayList<>(); for (XsdAttributeGroup attributeGroup : complexType.getAttributeGroups()) { attributes.addAll(getAllAttributes(resolveAttributeGroup(attributeGroup))); } @@ -554,7 +554,7 @@ public class CppCodeGenerator { } if (baseValueType != null) { - printNodeListGetString("root"); + printSetRawWithElementText("root"); parserCppFile.print(baseValueType.getParsingExpression()); parserCppFile.printf("instance.setValue(_value);\n"); parserCppFile.printf("}\n"); @@ -593,7 +593,7 @@ public class CppCodeGenerator { } if (type instanceof CppSimpleType) { - printNodeListGetString("_child"); + printSetRawWithElementText("_child"); } parserCppFile.print(type.getParsingExpression()); @@ -615,7 +615,7 @@ public class CppCodeGenerator { return (useTinyXml ? "tinyxml2::XMLElement" : "xmlNode"); } - private void printNodeListGetString(String varName) { + private void printSetRawWithElementText(String varName) { if (useTinyXml) { // The tinyxml version, in contrast to xmlNodeListGetString does not deal // with ENTITY_REF nodes @@ -654,8 +654,6 @@ public class CppCodeGenerator { */ private void printWriter(String name, String nameScope, XsdComplexType complexType) throws CppCodeGeneratorException { - CppSimpleType baseValueType = (complexType instanceof XsdSimpleContent) ? - getValueType((XsdSimpleContent) complexType, true) : null; List<XsdElement> allElements = new ArrayList<>(); List<XsdAttribute> allAttributes = new ArrayList<>(); stackComponents(complexType, allElements, allAttributes); @@ -862,7 +860,6 @@ public class CppCodeGenerator { List<XsdAttribute> allAttributes = new ArrayList<>(); stackComponents(complexType, allElements, allAttributes); - List<CppType> allElementTypes = new ArrayList<>(); for (XsdElement element : allElements) { XsdElement elementValue = resolveElement(element); CppType type = parseType(elementValue.getType(), elementValue.getName()); @@ -886,7 +883,6 @@ public class CppCodeGenerator { parentArgs.append(String.format(", %s", variableName)); } } - List<CppSimpleType> allAttributeTypes = new ArrayList<>(); for (XsdAttribute attribute : allAttributes) { CppType type = parseSimpleType(resolveAttribute(attribute).getType(), false); String variableName = Utils.toVariableName(resolveAttribute(attribute).getName()); @@ -989,20 +985,17 @@ public class CppCodeGenerator { parserCppFile.printf("}\n\n"); } - String className = Utils.toClassName(pkgName); - boolean isMultiRootElement = xmlSchema.getElementMap().values().size() > 1; for (XsdElement element : xmlSchema.getElementMap().values()) { CppType cppType = parseType(element.getType(), element.getName()); String elementName = element.getName(); - String VariableName = Utils.toVariableName(elementName); - String typeName = cppType instanceof CppSimpleType ? cppType.getName() : - Utils.toClassName(cppType.getName()); + String typeName = cppType.getName(); + String readerName = cppType instanceof CppSimpleType ? elementName : typeName; parserHeaderFile.printf("std::optional<%s> read%s(const char* configFile);\n\n", - typeName, isMultiRootElement ? Utils.capitalize(typeName) : ""); + typeName, isMultiRootElement ? Utils.capitalize(readerName) : ""); parserCppFile.printf("std::optional<%s> read%s(const char* configFile) {\n", - typeName, isMultiRootElement ? Utils.capitalize(typeName) : ""); + typeName, isMultiRootElement ? Utils.capitalize(readerName) : ""); if (useTinyXml) { parserCppFile.printf("tinyxml2::XMLDocument doc;\n" + "if (doc.LoadFile(configFile) != tinyxml2::XML_SUCCESS) {\n" @@ -1032,11 +1025,10 @@ public class CppCodeGenerator { } if (cppType instanceof CppSimpleType) { - parserCppFile.printf("%s value = getXmlAttribute(_child, \"%s\");\n", - elementName, elementName); - } else { - parserCppFile.printf(cppType.getParsingExpression()); + parserCppFile.print("std::string _raw;\n"); + printSetRawWithElementText("_child"); } + parserCppFile.printf(cppType.getParsingExpression()); parserCppFile.printf("return _value;\n}\n"); parserCppFile.printf("return std::nullopt;\n"); parserCppFile.printf("}\n\n"); @@ -1062,17 +1054,23 @@ public class CppCodeGenerator { for (XsdElement element : xmlSchema.getElementMap().values()) { CppType cppType = parseType(element.getType(), element.getName()); String elementName = element.getName(); - String VariableName = Utils.toVariableName(elementName); - String typeName = cppType instanceof CppSimpleType ? cppType.getName() : - Utils.toClassName(cppType.getName()); - parserHeaderFile.printf("void write(std::ostream& _out, const %s& %s);\n\n", - typeName, VariableName); - parserCppFile.printf("void write(std::ostream& _out, const %s& %s) {\n", - typeName, VariableName); - + String variableName = Utils.toVariableName(elementName); + String typeName = cppType.getName(); + String writerName = + cppType instanceof CppSimpleType ? Utils.toClassName(elementName) : ""; + parserHeaderFile.printf("void write%s(std::ostream& _out, const %s& %s);\n\n", + writerName, typeName, variableName); + parserCppFile.printf("void write%s(std::ostream& _out, const %s& %s) {\n", + writerName, typeName, variableName); parserCppFile.print( "_out << \"<?xml version=\\\"1.0\\\" encoding=\\\"utf-8\\\"?>\\n\";\n"); - parserCppFile.printf("%s.write(_out, \"%s\");\n", VariableName, elementName); + if (cppType instanceof CppSimpleType) { + parserCppFile.printf("_out << \"<%s>\";\n", elementName); + parserCppFile.print(cppType.getWritingExpression(variableName, "")); + parserCppFile.printf("_out << \"</%s>\" << std::endl;\n", elementName); + } else { + parserCppFile.printf("%s.write(_out, \"%s\");\n", variableName, elementName); + } parserCppFile.printf("}\n\n"); } @@ -1308,7 +1306,6 @@ public class CppCodeGenerator { } boolean results = false; for (XsdElement element : complexType.getElements()) { - XsdElement elementValue = resolveElement(element); if (element.getRef() == null && element.getType().getRef() == null && element.getType() instanceof XsdComplexType) { results = hasAttribute((XsdComplexType) element.getType()); diff --git a/src/main/java/com/android/xsdc/cpp/CppType.java b/src/main/java/com/android/xsdc/cpp/CppType.java index 05d4da9..1c72d3f 100644 --- a/src/main/java/com/android/xsdc/cpp/CppType.java +++ b/src/main/java/com/android/xsdc/cpp/CppType.java @@ -19,6 +19,13 @@ package com.android.xsdc.cpp; interface CppType { String getName(); + /** + * Statements to parse the input to this type. For simple types, + * `_raw(std::string)` should be available. For complex types, + * `_child(T)` should be available. + * + * @return statements declaring `_value` for this type from input + */ String getParsingExpression(); public String getWritingExpression(String getValue, String name); diff --git a/src/main/java/com/android/xsdc/java/JavaCodeGenerator.java b/src/main/java/com/android/xsdc/java/JavaCodeGenerator.java index f0a1495..c355d64 100644 --- a/src/main/java/com/android/xsdc/java/JavaCodeGenerator.java +++ b/src/main/java/com/android/xsdc/java/JavaCodeGenerator.java @@ -510,20 +510,24 @@ public class JavaCodeGenerator { boolean isMultiRootElement = xmlSchema.getElementMap().values().size() > 1; for (XsdElement element : xmlSchema.getElementMap().values()) { JavaType javaType = parseType(element.getType(), element.getName()); + String elementName = element.getName(); + String typeName = javaType.getName(); + String readerName = + javaType instanceof JavaSimpleType ? Utils.toClassName(elementName) : typeName; out.printf("public static %s%s read%s(%sjava.io.InputStream in)" - + " throws org.xmlpull.v1.XmlPullParserException, java.io.IOException, " - + "javax.xml.datatype.DatatypeConfigurationException {\n" - + "org.xmlpull.v1.XmlPullParser _parser = org.xmlpull.v1.XmlPullParserFactory" - + ".newInstance().newPullParser();\n" - + "_parser.setFeature(org.xmlpull.v1.XmlPullParser.FEATURE_PROCESS_NAMESPACES, " - + "true);\n" - + "_parser.setInput(in, null);\n" - + "_parser.nextTag();\n" - + "String _tagName = _parser.getName();\n" - + "String _raw = null;\n", getDefaultNullability(Nullability.NULLABLE), - javaType.getName(), isMultiRootElement ? Utils.capitalize(javaType.getName()) : "", - getDefaultNullability(Nullability.NON_NULL)); - out.printf("if (_tagName.equals(\"%s\")) {\n", element.getName()); + + " throws org.xmlpull.v1.XmlPullParserException, java.io.IOException, " + + "javax.xml.datatype.DatatypeConfigurationException {\n" + + "org.xmlpull.v1.XmlPullParser _parser = org.xmlpull.v1.XmlPullParserFactory" + + ".newInstance().newPullParser();\n" + + "_parser.setFeature(org.xmlpull.v1.XmlPullParser.FEATURE_PROCESS_NAMESPACES, " + + "true);\n" + + "_parser.setInput(in, null);\n" + + "_parser.nextTag();\n" + + "String _tagName = _parser.getName();\n" + + "String _raw = null;\n", getDefaultNullability(Nullability.NULLABLE), + typeName, isMultiRootElement ? readerName : "", + getDefaultNullability(Nullability.NON_NULL)); + out.printf("if (_tagName.equals(\"%s\")) {\n", elementName); if (javaType instanceof JavaSimpleType) { out.print("_raw = XmlParser.readText(_parser);\n"); } @@ -624,16 +628,26 @@ public class JavaCodeGenerator { for (XsdElement element : xmlSchema.getElementMap().values()) { JavaType javaType = parseType(element.getType(), element.getName()); String elementName = element.getName(); - String VariableName = Utils.toVariableName(elementName); - String typeName = javaType instanceof JavaSimpleType ? javaType.getName() : - Utils.toClassName(javaType.getName()); - out.printf("public static void write(%sXmlWriter out, %s%s %s) " - + "throws java.io.IOException {", getDefaultNullability(Nullability.NON_NULL), - getDefaultNullability(Nullability.NON_NULL), typeName, VariableName); - out.print("\nout.print(\"<?xml version=\\\"1.0\\\" encoding=\\\"utf-8\\\"?>\\n\");\n"); - out.printf("if (%s != null) {\n", VariableName); - out.printf("%s.write(out, \"%s\");\n}\n", VariableName, elementName); - out.print("out.printXml();\n}\n\n"); + String variableName = Utils.toVariableName(elementName); + String typeName = javaType.getName(); + String writerName + = javaType instanceof JavaSimpleType ? Utils.toClassName(elementName) : ""; + out.printf("public static void write%s(%sXmlWriter _out, %s%s %s) " + + "throws java.io.IOException {", + writerName, + getDefaultNullability(Nullability.NON_NULL), + getDefaultNullability(Nullability.NON_NULL), typeName, variableName); + out.print("\n_out.print(\"<?xml version=\\\"1.0\\\" encoding=\\\"utf-8\\\"?>\\n\");\n"); + out.printf("if (%s != null) {\n", variableName); + if (javaType instanceof JavaSimpleType) { + out.printf("_out.print(\"<%s>\");\n", elementName); + out.print(javaType.getWritingExpression(variableName, "")); + out.printf("_out.print(\"</%s>\\n\");\n", elementName); + } else { + out.printf("%s.write(_out, \"%s\");\n", variableName, elementName); + } + out.print("}\n"); + out.print("_out.printXml();\n}\n\n"); } out.printf("}\n"); } diff --git a/tests/resources/simple_type/api/current.txt b/tests/resources/simple_type/api/current.txt index bf8f711..8368c23 100644 --- a/tests/resources/simple_type/api/current.txt +++ b/tests/resources/simple_type/api/current.txt @@ -47,6 +47,7 @@ package simple.type { public class XmlParser { ctor public XmlParser(); method public static simple.type.MultiChoice readMultiChoice(java.io.InputStream) throws javax.xml.datatype.DatatypeConfigurationException, java.io.IOException, org.xmlpull.v1.XmlPullParserException; + method public static java.math.BigInteger readPercent(java.io.InputStream) throws javax.xml.datatype.DatatypeConfigurationException, java.io.IOException, org.xmlpull.v1.XmlPullParserException; method public static simple.type.SimpleTypes readSimpleTypes(java.io.InputStream) throws javax.xml.datatype.DatatypeConfigurationException, java.io.IOException, org.xmlpull.v1.XmlPullParserException; method public static simple.type.SingleChoice readSingleChoice(java.io.InputStream) throws javax.xml.datatype.DatatypeConfigurationException, java.io.IOException, org.xmlpull.v1.XmlPullParserException; method public static String readText(org.xmlpull.v1.XmlPullParser) throws java.io.IOException, org.xmlpull.v1.XmlPullParserException; @@ -59,6 +60,7 @@ package simple.type { method public static void write(simple.type.XmlWriter, simple.type.SimpleTypes) throws java.io.IOException; method public static void write(simple.type.XmlWriter, simple.type.MultiChoice) throws java.io.IOException; method public static void write(simple.type.XmlWriter, simple.type.SingleChoice) throws java.io.IOException; + method public static void writePercent(simple.type.XmlWriter, java.math.BigInteger) throws java.io.IOException; } } diff --git a/tests/resources/simple_type/simple_type.xsd b/tests/resources/simple_type/simple_type.xsd index ef5284f..9bbd283 100644 --- a/tests/resources/simple_type/simple_type.xsd +++ b/tests/resources/simple_type/simple_type.xsd @@ -57,4 +57,5 @@ </xs:choice> </xs:complexType> </xs:element> + <xs:element name="percent" type="percent" /> </xs:schema> diff --git a/tests/resources/simple_type_root.xml b/tests/resources/simple_type_root.xml new file mode 100644 index 0000000..a6fed4f --- /dev/null +++ b/tests/resources/simple_type_root.xml @@ -0,0 +1,2 @@ +<?xml version="1.0" encoding="utf-8"?> +<percent>100</percent> diff --git a/tests/simple_type.cpp b/tests/simple_type.cpp index 0e2df57..3f1b2de 100644 --- a/tests/simple_type.cpp +++ b/tests/simple_type.cpp @@ -16,6 +16,7 @@ #include <iostream> #include <fstream> +#include <sstream> #include <android-base/macros.h> @@ -64,3 +65,14 @@ TEST_F(XmlTest, Simpletype_AccessingEmptyOptionalAbortsWithMessage) { // trying to get the first value from optional element of list of simple values ASSERT_DEATH(simple.getFirstOptionalIntList(), "hasOptionalIntList()"); } + +TEST_F(XmlTest, SimpleTypeRoot) { + using namespace simple::type; + + string file_name = Resource("simple_type_root.xml"); + ASSERT_EQ(*readPercent(file_name.c_str()), 100); + + ostringstream out; + writePercent(out, 100); + ASSERT_EQ(out.str(), "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<percent>100</percent>\n"); +} diff --git a/tests/src/com/android/xsdc/tests/XmlParserTest.java b/tests/src/com/android/xsdc/tests/XmlParserTest.java index f3b5eaf..bb54099 100644 --- a/tests/src/com/android/xsdc/tests/XmlParserTest.java +++ b/tests/src/com/android/xsdc/tests/XmlParserTest.java @@ -248,6 +248,31 @@ public class XmlParserTest { } @Test + public void testSimpleTypeRoot() throws Exception { + BigInteger percent; + try (InputStream str = this.getClass().getClassLoader().getResourceAsStream( + "simple_type_root.xml")) { + percent = simple.type.XmlParser.readPercent(str); + } + + assertThat(percent, is(BigInteger.valueOf(100))); + + String actualStr, expectedStr; + try (InputStream str = this.getClass().getClassLoader().getResourceAsStream( + "simple_type_root.xml")) { + expectedStr = new String(str.readAllBytes()); + } + try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { + try(simple.type.XmlWriter writer = new simple.type.XmlWriter(new PrintWriter(baos))) { + simple.type.XmlWriter.writePercent(writer, percent); + } + actualStr = new String(baos.toByteArray()); + } + + assertThat(new String(actualStr), is(expectedStr)); + } + + @Test public void testReference() throws Exception { reference.Class _class; try (InputStream str = this.getClass().getClassLoader().getResourceAsStream( |