Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into review-the-project
Browse files Browse the repository at this point in the history
  • Loading branch information
cstamas committed Aug 27, 2023
2 parents 81f25ea + 64c31c9 commit 746ef51
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 32 deletions.
2 changes: 1 addition & 1 deletion polyglot-yaml/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.33</version>
<version>2.2</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import org.apache.maven.model.*;
import org.codehaus.plexus.util.xml.Xpp3Dom;
import org.yaml.snakeyaml.LoaderOptions;
import org.yaml.snakeyaml.TypeDescription;
import org.yaml.snakeyaml.constructor.Construct;
import org.yaml.snakeyaml.constructor.Constructor;
Expand Down Expand Up @@ -39,8 +40,8 @@ public final class ModelConstructor extends Constructor {
*/
private final Map<Class<?>, Construct> pomConstructors = new HashMap<>();

public ModelConstructor() {
super(Model.class);
public ModelConstructor(LoaderOptions loadingConfig) {
super(Model.class, loadingConfig);

yamlConstructors.put(XPP3DOM_TAG, new ConstructXpp3Dom());
yamlClassConstructors.put(NodeId.mapping, new MavenObjectConstruct());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@
import org.apache.maven.model.Model;
import org.apache.maven.model.Plugin;
import org.codehaus.plexus.util.xml.Xpp3Dom;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.error.YAMLException;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.introspector.Property;
import org.yaml.snakeyaml.nodes.Node;
import org.yaml.snakeyaml.nodes.NodeTuple;
import org.yaml.snakeyaml.nodes.Tag;
import org.yaml.snakeyaml.representer.Represent;
import org.yaml.snakeyaml.representer.Representer;

import java.beans.IntrospectionException;
import java.util.*;

import static java.lang.String.format;
Expand All @@ -34,8 +35,8 @@
* @since 0.7
*/
class ModelRepresenter extends Representer {
public ModelRepresenter() {
setDefaultScalarStyle( DumperOptions.ScalarStyle.PLAIN );
public ModelRepresenter(DumperOptions options) {
super(options);
this.representers.put(Xpp3Dom.class, new RepresentXpp3Dom());
Represent stringRepresenter = this.representers.get(String.class);
this.representers.put(Boolean.class, stringRepresenter);
Expand Down Expand Up @@ -97,7 +98,7 @@ private class RepresentXpp3Dom implements Represent {
private static final String ATTRIBUTE_PREFIX = "attr/";

public Node representData(Object data) {
return representMapping(Tag.MAP, toMap((Xpp3Dom) data), DumperOptions.FlowStyle.BLOCK);
return representMapping(Tag.MAP, toMap((Xpp3Dom) data), DumperOptions.FlowStyle.AUTO);
}

private Map<String, Object> toMap(Xpp3Dom node) {
Expand Down Expand Up @@ -217,22 +218,27 @@ private List<Object> toList(Xpp3Dom node, String childName) {
*/
@Override
protected Set<Property> getProperties(Class<? extends Object> type) {
if (type.isAssignableFrom(Model.class)) {
return sortTypeWithOrder(type, ORDER_MODEL);
} else if (type.isAssignableFrom(Developer.class)) {
return sortTypeWithOrder(type, ORDER_DEVELOPER);
} else if (type.isAssignableFrom(Contributor.class)) {
return sortTypeWithOrder(type, ORDER_CONTRIBUTOR);
} else if (type.isAssignableFrom(Dependency.class)) {
return sortTypeWithOrder(type, ORDER_DEPENDENCY);
} else if (type.isAssignableFrom(Plugin.class)) {
return sortTypeWithOrder(type, ORDER_PLUGIN);
} else {
return super.getProperties(type);
try {
if (type.isAssignableFrom(Model.class)) {
return sortTypeWithOrder(type, ORDER_MODEL);
} else if (type.isAssignableFrom(Developer.class)) {
return sortTypeWithOrder(type, ORDER_DEVELOPER);
} else if (type.isAssignableFrom(Contributor.class)) {
return sortTypeWithOrder(type, ORDER_CONTRIBUTOR);
} else if (type.isAssignableFrom(Dependency.class)) {
return sortTypeWithOrder(type, ORDER_DEPENDENCY);
} else if (type.isAssignableFrom(Plugin.class)) {
return sortTypeWithOrder(type, ORDER_PLUGIN);
} else {
return super.getProperties(type);
}
} catch (IntrospectionException e) {
throw new YAMLException(e);
}
}

private Set<Property> sortTypeWithOrder(Class<? extends Object> type, List<String> order) {
private Set<Property> sortTypeWithOrder(Class<? extends Object> type, List<String> order)
throws IntrospectionException {
Set<Property> standard = super.getProperties(type);
Set<Property> sorted = new TreeSet<Property>(new ModelPropertyComparator(order));
sorted.addAll(standard);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* @since 0.7
*/
@Singleton
@Named( "yaml" )
@Named("yaml")
public class YamlMapping extends MappingSupport {
public YamlMapping() {
super("yaml");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.apache.maven.model.io.ModelParseException;
import org.sonatype.maven.polyglot.io.ModelReaderSupport;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.LoaderOptions;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.representer.Representer;

Expand All @@ -30,13 +31,14 @@
* @since 0.7
*/
@Singleton
@Named( "yaml" )
@Named("yaml")
public class YamlModelReader extends ModelReaderSupport {
private final Yaml yaml;

public YamlModelReader() {
ModelConstructor constructor = new ModelConstructor();
yaml = new Yaml(constructor, new Representer(), new DumperOptions(), new ModelResolver());
ModelConstructor constructor = new ModelConstructor(new LoaderOptions());
DumperOptions options = new DumperOptions();
yaml = new Yaml(constructor, new Representer(options), options, new ModelResolver());
}

public Model read(Reader input, Map<String, ?> options) throws IOException, ModelParseException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@
* @since 0.7
*/
@Singleton
@Named( "yaml" )
@Named("yaml")
public class YamlModelWriter extends ModelWriterSupport {
public void write(Writer output, Map<String, Object> o, Model model) throws IOException {
//TODO improve SnakeYAML API (A. Somov)
DumperOptions dumperOptions = new DumperOptions();
dumperOptions.setIndent(2);
dumperOptions.setWidth(80);
dumperOptions.setDefaultScalarStyle(DumperOptions.ScalarStyle.PLAIN);
Serializer serializer = new Serializer(new Emitter(output, dumperOptions), new ModelResolver(), dumperOptions, Tag.MAP);
Representer representer = new ModelRepresenter();
Representer representer = new ModelRepresenter(dumperOptions);
try {
serializer.open();
Node node = representer.represent(model);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

import org.apache.maven.model.*;
import org.junit.Test;
import org.yaml.snakeyaml.error.YAMLException;

import java.io.InputStream;
import java.util.List;
Expand Down Expand Up @@ -99,12 +98,13 @@ public void testModelReader() throws Exception {

@Test
public void testColonInFlowContext() throws Exception {
// support for colon in the flow context is improved
// in SnakeYAML 2.0
try {
getModel("dependencies-colon-issue.yaml");
throw new UnsupportedOperationException("Colon in flow context should not be accepted.");
} catch (Exception e) {
// just assert it failed, but the message below was removed from Snakeyaml
// assertTrue(e.getMessage().contains("http://pyyaml.org/wiki/YAMLColonInFlowContext"));
String message = e.getMessage();
assertTrue(message, message.contains("Unable to find property 'groupId:log4j' on class: org.apache.maven.model.Exclusion"));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class ModelRepresenterTest {

@Before
public void setUp() throws Exception {
modelRepresenter = new ModelRepresenter();
modelRepresenter = new ModelRepresenter(new DumperOptions());
}

@Test
Expand Down

0 comments on commit 746ef51

Please sign in to comment.