Skip to content

Commit 8b6df7b

Browse files
authored
Fix modello-plugin-snakeyaml (#56)
* Downgrade snakeyaml - use the highest working version * IT for snake yaml * Bump snakeyaml to version which breaks plugin * Use new enums * Upgrade back to the newest snakeyaml
1 parent 2f8ca1b commit 8b6df7b

File tree

5 files changed

+146
-5
lines changed

5 files changed

+146
-5
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
invoker.java.version = 1.5+
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<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">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>org.codehaus.modello.its</groupId>
6+
<artifactId>snakeyaml</artifactId>
7+
<version>0.1-SNAPSHOT</version>
8+
9+
<dependencies>
10+
<dependency>
11+
<groupId>org.codehaus.modello</groupId>
12+
<artifactId>modello-plugin-snakeyaml</artifactId>
13+
<version>@project.version@</version>
14+
</dependency>
15+
</dependencies>
16+
17+
18+
<build>
19+
<defaultGoal>test</defaultGoal>
20+
<plugins>
21+
<plugin>
22+
<groupId>org.apache.maven.plugins</groupId>
23+
<artifactId>maven-compiler-plugin</artifactId>
24+
<version>3.8.0</version>
25+
<configuration>
26+
<source>@java.version@</source>
27+
<target>@java.version@</target>
28+
</configuration>
29+
</plugin>
30+
<plugin>
31+
<groupId>org.apache.maven.plugins</groupId>
32+
<artifactId>maven-surefire-plugin</artifactId>
33+
<version>2.22.2</version>
34+
</plugin>
35+
<plugin>
36+
<groupId>org.codehaus.modello</groupId>
37+
<artifactId>modello-maven-plugin</artifactId>
38+
<version>@project.version@</version>
39+
<configuration>
40+
<version>1.0.0</version>
41+
<useJava5>true</useJava5>
42+
<models>
43+
<model>src/main/mdo/model.mdo</model>
44+
</models>
45+
</configuration>
46+
<executions>
47+
<execution>
48+
<id>standard</id>
49+
<goals>
50+
<goal>java</goal>
51+
<goal>snakeyaml-writer</goal>
52+
</goals>
53+
</execution>
54+
</executions>
55+
</plugin>
56+
</plugins>
57+
</build>
58+
</project>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor
2+
license agreements. See the NOTICE file distributed with this work for additional
3+
information regarding copyright ownership. The ASF licenses this file to
4+
you under the Apache License, Version 2.0 (the "License"); you may not use
5+
this file except in compliance with the License. You may obtain a copy of
6+
the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required
7+
by applicable law or agreed to in writing, software distributed under the
8+
License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
9+
OF ANY KIND, either express or implied. See the License for the specific
10+
language governing permissions and limitations under the License. -->
11+
12+
<model xmlns="http://codehaus-plexus.github.io/MODELLO/1.8.0"
13+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
14+
xsi:schemaLocation="http://codehaus-plexus.github.io/MODELLO/1.8.0 http://codehaus-plexus.github.io/modello/xsd/modello-1.8.0.xsd">
15+
<id>model</id>
16+
<name>Model</name>
17+
<defaults>
18+
<default>
19+
<key>package</key>
20+
<value>org.plexus.modello.demo.model</value>
21+
</default>
22+
</defaults>
23+
24+
<classes>
25+
<class rootElement="true">
26+
<name>Root</name>
27+
<version>1.0.0+</version>
28+
<fields>
29+
<field>
30+
<name>simpleField</name>
31+
<type>String</type>
32+
<version>1.0.0+</version>
33+
</field>
34+
</fields>
35+
</class>
36+
</classes>
37+
</model>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package org.plexus.modello.demo.model;
2+
3+
import org.plexus.modello.demo.model.io.snakeyaml.*;
4+
5+
import java.io.*;
6+
7+
public class RootTest {
8+
private static Root createRoot(String fieldValue) {
9+
Root r = new Root();
10+
r.setSimpleField(fieldValue);
11+
return r;
12+
}
13+
14+
private static String asYamlString(Root root) throws IOException {
15+
ModelSnakeYamlWriter writer = new ModelSnakeYamlWriter();
16+
17+
try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
18+
OutputStreamWriter osw = new OutputStreamWriter(baos)) {
19+
writer.write(osw, root);
20+
return baos.toString();
21+
}
22+
}
23+
24+
public void testWritingYaml() throws IOException {
25+
Root root = createRoot("modello IT");
26+
27+
String rootAsYaml = asYamlString(root);
28+
29+
String expected = "%YAML 1.1" + "\n" // directive used to identify the version of YAML
30+
+ "---" + "\n" // document separator
31+
+ "\"simpleField\": \"modello IT\"" + "\n"; // actual Root
32+
assert expected.equals(rootAsYaml): "Actual: [" + rootAsYaml + "]";
33+
}
34+
}
35+

modello-plugins/modello-plugin-snakeyaml/src/main/java/org/codehaus/modello/plugin/snakeyaml/SnakeYamlWriterGenerator.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,11 @@ private void generateSnakeYamlWriter()
8282

8383
jClass.addImport( "org.yaml.snakeyaml.DumperOptions" );
8484
jClass.addImport( "org.yaml.snakeyaml.DumperOptions.Version" );
85+
jClass.addImport( "org.yaml.snakeyaml.DumperOptions.FlowStyle" );
86+
jClass.addImport( "org.yaml.snakeyaml.DumperOptions.ScalarStyle" );
8587
jClass.addImport( "org.yaml.snakeyaml.emitter.Emitable" );
8688
jClass.addImport( "org.yaml.snakeyaml.emitter.Emitter" );
89+
jClass.addImport( "org.yaml.snakeyaml.error.Mark" );
8790
jClass.addImport( "org.yaml.snakeyaml.events.DocumentEndEvent" );
8891
jClass.addImport( "org.yaml.snakeyaml.events.DocumentStartEvent" );
8992
jClass.addImport( "org.yaml.snakeyaml.events.ImplicitTuple" );
@@ -199,7 +202,7 @@ private void writeClass( ModelClass modelClass, JClass jClass )
199202

200203
JSourceCode sc = marshall.getSourceCode();
201204

202-
sc.add( "generator.emit( new MappingStartEvent( null, null, true, null, null, false ) );" );
205+
sc.add( "generator.emit( new MappingStartEvent( null, null, true, null, null, FlowStyle.BLOCK ) );" );
203206

204207
ModelField contentField = null;
205208

@@ -361,7 +364,7 @@ private void writeClass( ModelClass modelClass, JClass jClass )
361364
}
362365
else
363366
{
364-
sc.add( "generator.emit( new MappingStartEvent( null, null, true, null, null, false ) );" );
367+
sc.add( "generator.emit( new MappingStartEvent( null, null, true, null, null, FlowStyle.BLOCK ) );" );
365368
}
366369

367370

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

407410
if ( xmlAssociationMetadata.isMapExplode() )
408411
{
409-
sc.add( "generator.emit( new MappingStartEvent( null, null, true, null, null, false ) );" );
412+
sc.add( "generator.emit( new MappingStartEvent( null, null, true, null, null, FlowStyle.BLOCK) );" );
410413
writeScalarKey( sc, "key" );
411414
writeScalar( sc, "key" );
412415
writeScalarKey( sc, "value" );
@@ -463,9 +466,16 @@ private void writeScalarKey( JSourceCode sc, String key )
463466

464467
private void writeScalar( JSourceCode sc, String value )
465468
{
466-
sc.add( "generator.emit( new ScalarEvent( null, null, new ImplicitTuple( true, true ), "
469+
sc.add( "{" );
470+
sc.indent();
471+
sc.add( "String anchor = null, tag = null;" );
472+
sc.add( "Mark startMark = null, endMark = null;" );
473+
sc.add( "ScalarStyle style = ScalarStyle.DOUBLE_QUOTED;" );
474+
sc.add( "generator.emit( new ScalarEvent( anchor, tag, new ImplicitTuple( true, true ), "
467475
+ value
468-
+ ", null, null, ' ' ) );" );
476+
+ ", startMark, endMark, style ) );" );
477+
sc.unindent();
478+
sc.add( "}" );
469479
}
470480

471481
}

0 commit comments

Comments
 (0)