-
Notifications
You must be signed in to change notification settings - Fork 152
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support transformation schema for XML #549
Merged
Merged
Changes from 1 commit
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
5c8367a
Add XML transformer
RVRhub 9bb7849
Add test for XML transformer
RVRhub da76153
Add test for XML transformer
RVRhub 84b3271
Update schema docs for XML Transformer and YAML Transformer
RVRhub 8971216
Add XML transformer
RVRhub b39ad8a
Add test for XML transformer
RVRhub 43c90c9
Add test for XML transformer
RVRhub 2deb6d4
Update schema docs for XML Transformer and YAML Transformer
RVRhub 48fd268
Merge remote-tracking branch 'origin/xml' into xml
RVRhub 8867be5
Update schema doc for XML Transformer
RVRhub 0c15926
Update schema doc for YAML Transformer
RVRhub 7a76170
Merge remote-tracking branch 'upstream/main' into xml
RVRhub 904850b
Simplify code for XML transformer
RVRhub File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
Add XML transformer
- Loading branch information
commit 5c8367afd3a793ac14644ea5fce8f71f531c0a43
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
164 changes: 164 additions & 0 deletions
164
src/main/java/net/datafaker/transformations/XmlTransformer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
package net.datafaker.transformations; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import net.datafaker.sequence.FakeSequence; | ||
|
||
public class XmlTransformer<IN> implements Transformer<IN, CharSequence> { | ||
|
||
private static final Map<Character, String> ESCAPING_MAP = createEscapeMap(); | ||
private static final int INDENTATION_STEP = 4; | ||
|
||
private int tagIndex = 0; | ||
|
||
private final boolean pretty; | ||
|
||
private XmlTransformer(boolean pretty) { | ||
this.pretty = pretty; | ||
} | ||
|
||
@Override | ||
public CharSequence apply(IN input, Schema<IN, ?> schema) { | ||
StringBuilder sb = new StringBuilder(); | ||
Arrays.stream(schema.getFields()).forEach(it -> apply(input, sb, it)); | ||
return sb.toString(); | ||
} | ||
|
||
private void apply(IN input, StringBuilder sb, Field<IN, ?> xmlNode) { | ||
|
||
if (pretty && tagIndex > 0) { | ||
sb.append(System.lineSeparator()).append(offset(tagIndex)); | ||
} | ||
|
||
final String tag = xmlNode.getName().trim(); | ||
sb.append("<").append(tag); | ||
if (xmlNode instanceof CompositeField) { | ||
Field<IN, ?>[] attrs = ((CompositeField) xmlNode).getFields(); | ||
applyAttributes(input, sb, attrs); | ||
|
||
Field<IN, ?> content = Arrays.stream(attrs) | ||
.filter(inField -> !isAttribute(inField.getName())).findFirst() | ||
.orElse(null); | ||
applyTag(input, sb, content, tag); | ||
} else { | ||
applyTag(input, sb, xmlNode, tag); | ||
} | ||
} | ||
|
||
private void applyTag(IN input, StringBuilder sb, Field<IN, ?> field, String tag) { | ||
if (field == null ) { | ||
applyValue(sb, tag, null); | ||
return; | ||
} | ||
|
||
Object xmlNodeValue = field.transform(input); | ||
if (xmlNodeValue instanceof Collection) { | ||
Collection<?> children = (Collection<?>) xmlNodeValue; | ||
if (children.isEmpty()) { | ||
applyValue(sb, tag, null); | ||
} else { | ||
sb.append(">"); | ||
tagIndex++; | ||
for (Object xmlNodeElem : children) { | ||
apply(input, sb, (Field<IN, ?>) xmlNodeElem); | ||
} | ||
tagIndex--; | ||
if (pretty) { | ||
sb.append(System.lineSeparator()).append(offset(tagIndex)); | ||
} | ||
sb.append("</").append(tag).append(">"); | ||
} | ||
|
||
} else if (xmlNodeValue instanceof String) { | ||
applyValue(sb, tag, (String) xmlNodeValue); | ||
} else if (xmlNodeValue == null) { | ||
applyValue(sb, tag, null); | ||
} | ||
} | ||
|
||
private boolean isAttribute(String name) { | ||
return name != null; | ||
} | ||
|
||
private void applyAttributes(IN input, StringBuilder sb, Field<IN, ?>[] attrs) { | ||
for (Field<IN, ?> attr : attrs) { | ||
String name = attr.getName(); | ||
if(isAttribute(name)) { | ||
String value = (String) attr.transform(input); | ||
sb.append(" ").append(name).append("=\"").append(escape(value)).append("\""); | ||
} | ||
} | ||
} | ||
|
||
private void applyValue(StringBuilder sb, String tag, String xmlNodeValue) { | ||
if (xmlNodeValue != null) { | ||
sb.append(">"); | ||
sb.append(escape(xmlNodeValue)); | ||
sb.append("</").append(tag).append(">"); | ||
} else { | ||
sb.append("/>"); | ||
} | ||
} | ||
|
||
@Override | ||
public CharSequence generate(FakeSequence<IN> input, Schema<IN, ?> schema) { | ||
return null; | ||
} | ||
|
||
@Override | ||
public CharSequence generate(Schema<IN, ?> schema, int limit) { | ||
StringBuilder sb = new StringBuilder(); | ||
for (int i = 0; i < limit; i++) { | ||
sb.append(apply(null, schema)); | ||
if (i < limit - 1) { | ||
sb.append(LINE_SEPARATOR); | ||
} | ||
} | ||
return sb.toString(); | ||
} | ||
|
||
public static class XmlTransformerBuilder<IN> { | ||
|
||
private boolean pretty= false; | ||
|
||
public XmlTransformer.XmlTransformerBuilder<IN> pretty(boolean pretty) { | ||
this.pretty = pretty; | ||
return this; | ||
} | ||
|
||
public XmlTransformer<IN> build() { | ||
return new XmlTransformer<>(pretty); | ||
} | ||
} | ||
|
||
private String offset(int length) { | ||
StringBuilder sb = new StringBuilder(); | ||
for (int i = 0; i < length * INDENTATION_STEP; i++) { | ||
sb.append(" "); | ||
} | ||
return sb.toString(); | ||
} | ||
|
||
private String escape(String str) { | ||
StringBuilder sb = new StringBuilder(); | ||
for (int i = 0; i < str.length(); i++) { | ||
final char c = str.charAt(i); | ||
sb.append(ESCAPING_MAP.getOrDefault(c, c + "")); | ||
} | ||
return sb.toString(); | ||
} | ||
|
||
private static Map<Character, String> createEscapeMap() { | ||
final Map<Character, String> map = new HashMap<>(); | ||
map.put('<', "<"); | ||
map.put('>', ">"); | ||
map.put('&', "&"); | ||
map.put('\'', "'"); | ||
map.put('"', """); | ||
return Collections.unmodifiableMap(map); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could a bit simpler like