Java tutorial
/* * Copyright (c) 2008, 2012 Kasper Nielsen. All rights reserved. * * Licensed 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. */ package org.cakeframework.website; import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.Writer; import java.nio.file.DirectoryStream; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.HashSet; import java.util.Properties; import java.util.Set; import org.apache.maven.plugin.logging.Log; import org.apache.velocity.VelocityContext; import org.apache.velocity.app.Velocity; import org.cakeframework.website.model.Model; import org.cakeframework.website.model.Release; import org.cakeframework.website.model.Version; import org.yaml.snakeyaml.TypeDescription; import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.constructor.AbstractConstruct; import org.yaml.snakeyaml.constructor.Constructor; import org.yaml.snakeyaml.nodes.Node; import org.yaml.snakeyaml.nodes.Tag; /** * * @author Kasper Nielsen */ public class Load { public final static String VERSIONS = "./versions"; static Set<Model> allModels(String dir) throws IOException { HashSet<Model> models = new HashSet<>(); try (DirectoryStream<Path> stream = Files.newDirectoryStream(Paths.get(dir), "*.yaml")) { for (Path p : stream) { models.add(loadModel(p.toFile())); } } return models; } public static void forVersion2(String version) throws IOException { Version v = Version.parseVersion(version); String dir = VERSIONS + "/" + version; String output = "./target/" + version + "/"; Files.createDirectories(Paths.get(output)); Model model = loadModel(new File(dir + ".yaml")); for (Model m : allModels(VERSIONS)) { for (Release r : m.getReleases()) { // only add previous versions???? model.addAllRelease(r); } } try (DirectoryStream<Path> stream = Files.newDirectoryStream(Paths.get(dir), "*.template")) { for (Path p : stream) { Properties props = new Properties(); props.setProperty("file.resource.loader.path", "./templates/darktheme, ./versions/" + version); props.setProperty("runtime.log.logsystem.class", "org.apache.velocity.runtime.log.NullLogSystem"); Velocity.init(props); VelocityContext context = new VelocityContext(); String file = p.getFileName().toString().replace(".vm", ".html"); try (Writer writer = new BufferedWriter( new OutputStreamWriter(new FileOutputStream(output + file)))) { context.put("body", p.getFileName()); context.put("model", model); context.put("version", v); Velocity.getTemplate("site.vm").merge(context, writer); } System.out.println(p); } } } public static void forVersion(Log log, File baseDir, File target) throws IOException { Model model = loadModel(new File(baseDir, "src/site.yaml")); // for (Model m : allModels(VERSIONS)) { // for (Release r : m.getReleases()) { // // only add previous versions???? // model.addAllRelease(r); // } // } File f = new File(baseDir, "src/vm"); try (DirectoryStream<Path> stream = Files.newDirectoryStream(f.toPath(), "*.vm")) { for (Path p : stream) { Properties props = new Properties(); props.setProperty("resource.loader", "file, class"); props.setProperty("file.resource.loader.path", f.toString() + ", " + new File(baseDir, "src/configuration")); props.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); props.setProperty("runtime.log.logsystem.class", "org.apache.velocity.runtime.log.NullLogSystem"); Velocity.init(props); VelocityContext context = new VelocityContext(); String file = p.getFileName().toString().replace(".vm", ".html"); File outputFile = new File(target, file); try (Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputFile)))) { context.put("body", p.getFileName()); context.put("model", model); context.put("version", Version.parseVersion("0.8")); Velocity.getTemplate("site.vm").merge(context, writer); } log.info("Wrote " + outputFile); } } } static Model loadModel(File file) throws FileNotFoundException { Constructor constructor = new Constructor(Model.class); TypeDescription modelDescription = new TypeDescription(Model.class); modelDescription.putListPropertyType("releases", Release.class); constructor.addTypeDescription(modelDescription); Yaml yaml = new Yaml(constructor); return (Model) yaml.load(new FileReader(file)); } static class CustomConstructor extends Constructor { CustomConstructor() { super(Model.class); this.yamlConstructors.put(new Tag("!version"), new ConstructVersion()); } class ConstructVersion extends AbstractConstruct { public Object construct(Node node) { String val = node.toString(); System.out.println(val); return Version.parseVersion("1.0"); } } } }