Skip to content

Commit e436ff9

Browse files
committed
Fix Snakeyaml
1 parent d968924 commit e436ff9

File tree

5 files changed

+1744
-4
lines changed

5 files changed

+1744
-4
lines changed

modello-plugins/modello-plugin-snakeyaml/pom.xml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,15 @@
2424
<dependency>
2525
<groupId>org.yaml</groupId>
2626
<artifactId>snakeyaml</artifactId>
27-
<version>1.33</version>
27+
<version>2.2</version>
2828
</dependency>
2929
</dependencies>
30+
31+
<build>
32+
<plugins>
33+
<plugin>
34+
<artifactId>maven-dependency-plugin</artifactId>
35+
</plugin>
36+
</plugins>
37+
</build>
3038
</project>

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ private void writeClassReaders(ModelClass modelClass, JClass jClass, boolean roo
210210

211211
sc = unmarshall.getSourceCode();
212212

213-
sc.add("Parser parser = new ParserImpl( new StreamReader( reader ) );");
213+
sc.add("Parser parser = new ParserImpl( new StreamReader( reader ), new LoaderOptions() );");
214214

215215
sc.add("return " + readerMethodName + "( parser, strict );");
216216

@@ -288,6 +288,7 @@ private void generateSnakeYamlReader() throws ModelloException, IOException {
288288
jClass.addImport("org.yaml.snakeyaml.parser.ParserException");
289289
jClass.addImport("org.yaml.snakeyaml.parser.ParserImpl");
290290
jClass.addImport("org.yaml.snakeyaml.reader.StreamReader");
291+
jClass.addImport("org.yaml.snakeyaml.LoaderOptions");
291292
jClass.addImport("java.io.InputStream");
292293
jClass.addImport("java.io.InputStreamReader");
293294
jClass.addImport("java.io.IOException");
@@ -820,6 +821,8 @@ private void writeHelpers(JClass jClass) {
820821

821822
sc = method.getSourceCode();
822823

824+
sc.add("if (!(event instanceof ScalarEvent))");
825+
sc.addIndented("return false;");
823826
sc.add("String currentName = ( (ScalarEvent) event ).getValue();");
824827

825828
sc.add("");
@@ -855,9 +858,17 @@ private void writeHelpers(JClass jClass) {
855858

856859
sc.add("if ( strict )");
857860

861+
sc.add("{");
862+
sc.indent();
863+
sc.add("if ( event instanceof ScalarEvent )");
858864
sc.add("{");
859865
sc.addIndented(
860866
"throw new ParserException( \"Unrecognised tag: '\" + ( (ScalarEvent) event ).getValue() + \"'\", event.getStartMark(), \"\", null );");
867+
sc.add("} else {");
868+
sc.addIndented(
869+
"return ; // throw new ParserException( \"Unrecognised : '\" + event.getEventId() + \"'\", event.getStartMark(), \"\", null );");
870+
sc.add("}");
871+
sc.unindent();
861872
sc.add("}");
862873

863874
sc.add("");
@@ -1041,7 +1052,8 @@ private void writeNewLocation(String trackerVariable, JSourceCode sc) {
10411052
return;
10421053
}
10431054

1044-
String constr = "new " + locationTracker.getName() + "( parser.getLineNumber(), parser.getColumnNumber()";
1055+
String constr = "new " + locationTracker.getName()
1056+
+ "( parser.peekEvent().getStartMark().getLine(), parser.peekEvent().getStartMark().getColumn()";
10451057
constr += (sourceTracker != null) ? ", " + SOURCE_PARAM : "";
10461058
constr += " )";
10471059

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ private void generateSnakeYamlWriter() throws ModelloException, IOException {
7474

7575
JClass jClass = new JClass(packageName + '.' + marshallerName);
7676
initHeader(jClass);
77+
suppressAllWarnings(objectModel, jClass);
7778

7879
jClass.addImport("org.yaml.snakeyaml.DumperOptions");
7980
jClass.addImport("org.yaml.snakeyaml.DumperOptions.Version");
@@ -290,7 +291,8 @@ private void writeClass(ModelClass modelClass, JClass jClass) throws ModelloExce
290291
sc.indent();
291292

292293
writeScalarKey(sc, fieldTagName);
293-
sc.add("generator.emit( new SequenceStartEvent( null, null, true, null, null, false ) );");
294+
sc.add(
295+
"generator.emit( new SequenceStartEvent( null, null, true, null, null, FlowStyle.AUTO ) );");
294296

295297
sc.add("for ( " + toType + " o : " + value + " )");
296298

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package org.codehaus.modello.plugin.snakeyaml;
2+
3+
/*
4+
* Copyright (c) 2004, Codehaus.org
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy of
7+
* this software and associated documentation files (the "Software"), to deal in
8+
* the Software without restriction, including without limitation the rights to
9+
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10+
* of the Software, and to permit persons to whom the Software is furnished to do
11+
* so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
import java.util.Map;
26+
27+
import org.codehaus.modello.AbstractModelloJavaGeneratorTest;
28+
import org.codehaus.modello.core.ModelloCore;
29+
import org.codehaus.modello.model.Model;
30+
31+
public class SnakeYamlGeneratorTest extends AbstractModelloJavaGeneratorTest {
32+
public SnakeYamlGeneratorTest() {
33+
super("snakeyaml");
34+
}
35+
36+
public void testYamlGenerator() throws Throwable {
37+
ModelloCore modello = (ModelloCore) lookup(ModelloCore.ROLE);
38+
39+
Model model = modello.loadModel(getXmlResourceReader("/models/maven.mdo"));
40+
41+
Map<String, Object> parameters = getModelloParameters("4.0.0");
42+
43+
modello.generate(model, "java", parameters);
44+
modello.generate(model, "snakeyaml-writer", parameters);
45+
modello.generate(model, "snakeyaml-reader", parameters);
46+
47+
addDependency("org.yaml", "snakeyaml");
48+
compileGeneratedSources();
49+
}
50+
}

0 commit comments

Comments
 (0)