Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion src/main/java/com/ethlo/jsons2xsd/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

public class Config
{
Expand All @@ -40,6 +41,8 @@ public class Config
private final boolean validateXsdSchema;
private final Map<String, String> typeMapping;
private final boolean ignoreUnknownFormats;
private final String rootElement;
private final Function<String,String> itemNameMapper;

public boolean isAttributesQualified()
{
Expand Down Expand Up @@ -68,7 +71,7 @@ public String getNsAlias()

public boolean isCreateRootElement()
{
return createRootElement;
return createRootElement || rootElement != null;
}

public boolean isValidateXsdSchema()
Expand All @@ -86,6 +89,14 @@ public boolean isIgnoreUnknownFormats()
return ignoreUnknownFormats;
}

public String getRootElement() {
return rootElement != null ? rootElement : name;
}

public Function<String,String> getItemNameMapper() {
return itemNameMapper;
}

public String getType(String type, String format)
{
final String key = (type + (format != null ? ("|" + format) : "")).toLowerCase();
Expand All @@ -103,6 +114,8 @@ public static class Builder
private boolean validateXsdSchema = true;
private final Map<String, String> typeMapping = new HashMap<>();
private boolean ignoreUnknownFormats;
private String rootElement;
private Function<String,String> itemNameMapper = Function.identity();

public Builder targetNamespace(String targetNamespace)
{
Expand Down Expand Up @@ -176,6 +189,16 @@ public Builder ignoreUnknownFormats(final boolean b)
this.ignoreUnknownFormats = b;
return this;
}

public Builder rootElement(final String name) {
this.rootElement = name;
return this;
}

public Builder mapArrayItemNames(final Function<String, String> mapper) {
this.itemNameMapper = mapper;
return this;
}
}

private Config(Builder builder)
Expand All @@ -189,5 +212,7 @@ private Config(Builder builder)
this.validateXsdSchema = builder.validateXsdSchema;
this.typeMapping = builder.typeMapping;
this.ignoreUnknownFormats = builder.ignoreUnknownFormats;
this.rootElement = builder.rootElement;
this.itemNameMapper = builder.itemNameMapper;
}
}
4 changes: 2 additions & 2 deletions src/main/java/com/ethlo/jsons2xsd/Jsons2Xsd.java
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ private static Element createRootElementIfNeeded(Config cfg, Element schemaRoot)
if (cfg.isCreateRootElement())
{
final Element wrapper = element(schemaRoot, XSD_ELEMENT);
wrapper.setAttribute(FIELD_NAME, cfg.getName());
wrapper.setAttribute(FIELD_NAME, cfg.getRootElement());
wrapper.setAttribute("type", cfg.getNsAlias() + ":" + cfg.getName());
}

Expand Down Expand Up @@ -388,7 +388,7 @@ private static void handleReference(Set<String> neededElements, Element nodeElem

if (oldName.length() <= 0)
{
nodeElem.setAttribute(FIELD_NAME, name);
nodeElem.setAttribute(FIELD_NAME, cfg.getItemNameMapper().apply(name));
}
nodeElem.setAttribute("type", fixRef);

Expand Down
18 changes: 18 additions & 0 deletions src/test/java/com/ethlo/jsons2xsd/ConversionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,24 @@ public void testSimpleTypeRefs() throws IOException, TransformerException
}
}

@Test
public void testCustomNames() throws IOException, TransformerException
{
try (final Reader r = reader("/schema/custom-names.json"))
{
final Config cfg = new Config.Builder()
.createRootElement(true)
.targetNamespace("http://ethlo.com/schema/group-test-1.0.xsd")
.name("Group")
.rootElement("group")
.mapArrayItemNames( name -> name.toUpperCase() )
.validateXsdSchema(true)
.build();
final Document doc = Jsons2Xsd.convert(r, cfg);
assertThat(XmlUtil.asXmlString(doc.getDocumentElement())).isXmlEqualTo(load("schema/custom-names.xsd"));
}
}

private Reader reader(String path)
{
return new InputStreamReader(getClass().getResourceAsStream(path));
Expand Down
29 changes: 29 additions & 0 deletions src/test/resources/schema/custom-names.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"name": { "type": "string" },
"permissions": {
"type": "array",
"items": {
"type": "string"
}
},
"members": {
"type": "array",
"items": {
"$ref": "#/definitions/Person"
}
}
},
"definitions": {
"Person": {
"type": "object",
"properties": {
"name": {
"type": "string"
}
}
}
}
}
32 changes: 32 additions & 0 deletions src/test/resources/schema/custom-names.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<schema elementFormDefault="qualified"
targetNamespace="http://ethlo.com/schema/group-test-1.0.xsd"
xmlns="http://www.w3.org/2001/XMLSchema" xmlns:x="http://ethlo.com/schema/group-test-1.0.xsd">
<element name="group" type="x:Group"/>
<complexType name="Group">
<sequence>
<element minOccurs="0" name="name" type="string"/>
<element minOccurs="0" name="permissions">
<complexType>
<sequence>
<element maxOccurs="unbounded" minOccurs="0"
name="item" type="string"/>
</sequence>
</complexType>
</element>
<element minOccurs="0" name="members">
<complexType>
<sequence>
<element maxOccurs="unbounded" minOccurs="0"
name="PERSON" type="x:Person"/>
</sequence>
</complexType>
</element>
</sequence>
</complexType>
<complexType name="Person">
<sequence>
<element minOccurs="0" name="name" type="string"/>
</sequence>
</complexType>
</schema>