Skip to content

Fix modello-plugin-snakeyaml #56

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

Merged
merged 5 commits into from
Aug 17, 2020
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
1 change: 1 addition & 0 deletions modello-maven-plugin/src/it/snakeyaml/invoker.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
invoker.java.version = 1.5+
58 changes: 58 additions & 0 deletions modello-maven-plugin/src/it/snakeyaml/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.codehaus.modello.its</groupId>
<artifactId>snakeyaml</artifactId>
<version>0.1-SNAPSHOT</version>

<dependencies>
<dependency>
<groupId>org.codehaus.modello</groupId>
<artifactId>modello-plugin-snakeyaml</artifactId>
<version>@project.version@</version>
</dependency>
</dependencies>


<build>
<defaultGoal>test</defaultGoal>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>@java.version@</source>
<target>@java.version@</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
</plugin>
<plugin>
<groupId>org.codehaus.modello</groupId>
<artifactId>modello-maven-plugin</artifactId>
<version>@project.version@</version>
<configuration>
<version>1.0.0</version>
<useJava5>true</useJava5>
<models>
<model>src/main/mdo/model.mdo</model>
</models>
</configuration>
<executions>
<execution>
<id>standard</id>
<goals>
<goal>java</goal>
<goal>snakeyaml-writer</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
37 changes: 37 additions & 0 deletions modello-maven-plugin/src/it/snakeyaml/src/main/mdo/model.mdo
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor
license agreements. See the NOTICE file distributed with this work for additional
information regarding copyright ownership. The ASF licenses this file to
you under the Apache License, Version 2.0 (the "License"); you may not use
this file except in compliance with the License. You may obtain a copy of
the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required
by applicable law or agreed to in writing, software distributed under the
License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
OF ANY KIND, either express or implied. See the License for the specific
language governing permissions and limitations under the License. -->

<model xmlns="http://codehaus-plexus.github.io/MODELLO/1.8.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://codehaus-plexus.github.io/MODELLO/1.8.0 http://codehaus-plexus.github.io/modello/xsd/modello-1.8.0.xsd">
<id>model</id>
<name>Model</name>
<defaults>
<default>
<key>package</key>
<value>org.plexus.modello.demo.model</value>
</default>
</defaults>

<classes>
<class rootElement="true">
<name>Root</name>
<version>1.0.0+</version>
<fields>
<field>
<name>simpleField</name>
<type>String</type>
<version>1.0.0+</version>
</field>
</fields>
</class>
</classes>
</model>
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.plexus.modello.demo.model;

import org.plexus.modello.demo.model.io.snakeyaml.*;

import java.io.*;

public class RootTest {
private static Root createRoot(String fieldValue) {
Root r = new Root();
r.setSimpleField(fieldValue);
return r;
}

private static String asYamlString(Root root) throws IOException {
ModelSnakeYamlWriter writer = new ModelSnakeYamlWriter();

try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(baos)) {
writer.write(osw, root);
return baos.toString();
}
}

public void testWritingYaml() throws IOException {
Root root = createRoot("modello IT");

String rootAsYaml = asYamlString(root);

String expected = "%YAML 1.1" + "\n" // directive used to identify the version of YAML
+ "---" + "\n" // document separator
+ "\"simpleField\": \"modello IT\"" + "\n"; // actual Root
assert expected.equals(rootAsYaml): "Actual: [" + rootAsYaml + "]";
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,11 @@ private void generateSnakeYamlWriter()

jClass.addImport( "org.yaml.snakeyaml.DumperOptions" );
jClass.addImport( "org.yaml.snakeyaml.DumperOptions.Version" );
jClass.addImport( "org.yaml.snakeyaml.DumperOptions.FlowStyle" );
jClass.addImport( "org.yaml.snakeyaml.DumperOptions.ScalarStyle" );
jClass.addImport( "org.yaml.snakeyaml.emitter.Emitable" );
jClass.addImport( "org.yaml.snakeyaml.emitter.Emitter" );
jClass.addImport( "org.yaml.snakeyaml.error.Mark" );
jClass.addImport( "org.yaml.snakeyaml.events.DocumentEndEvent" );
jClass.addImport( "org.yaml.snakeyaml.events.DocumentStartEvent" );
jClass.addImport( "org.yaml.snakeyaml.events.ImplicitTuple" );
Expand Down Expand Up @@ -199,7 +202,7 @@ private void writeClass( ModelClass modelClass, JClass jClass )

JSourceCode sc = marshall.getSourceCode();

sc.add( "generator.emit( new MappingStartEvent( null, null, true, null, null, false ) );" );
sc.add( "generator.emit( new MappingStartEvent( null, null, true, null, null, FlowStyle.BLOCK ) );" );

ModelField contentField = null;

Expand Down Expand Up @@ -361,7 +364,7 @@ private void writeClass( ModelClass modelClass, JClass jClass )
}
else
{
sc.add( "generator.emit( new MappingStartEvent( null, null, true, null, null, false ) );" );
sc.add( "generator.emit( new MappingStartEvent( null, null, true, null, null, FlowStyle.BLOCK ) );" );
}


Expand Down Expand Up @@ -406,7 +409,7 @@ private void writeClass( ModelClass modelClass, JClass jClass )

if ( xmlAssociationMetadata.isMapExplode() )
{
sc.add( "generator.emit( new MappingStartEvent( null, null, true, null, null, false ) );" );
sc.add( "generator.emit( new MappingStartEvent( null, null, true, null, null, FlowStyle.BLOCK) );" );
writeScalarKey( sc, "key" );
writeScalar( sc, "key" );
writeScalarKey( sc, "value" );
Expand Down Expand Up @@ -463,9 +466,16 @@ private void writeScalarKey( JSourceCode sc, String key )

private void writeScalar( JSourceCode sc, String value )
{
sc.add( "generator.emit( new ScalarEvent( null, null, new ImplicitTuple( true, true ), "
sc.add( "{" );
sc.indent();
sc.add( "String anchor = null, tag = null;" );
sc.add( "Mark startMark = null, endMark = null;" );
sc.add( "ScalarStyle style = ScalarStyle.DOUBLE_QUOTED;" );
sc.add( "generator.emit( new ScalarEvent( anchor, tag, new ImplicitTuple( true, true ), "
+ value
+ ", null, null, ' ' ) );" );
+ ", startMark, endMark, style ) );" );
sc.unindent();
sc.add( "}" );
}

}