diff --git a/scm-core/src/main/java/sonia/scm/template/DefaultEngine.java b/scm-core/src/main/java/sonia/scm/template/DefaultEngine.java new file mode 100644 index 0000000000..9c1b600ab9 --- /dev/null +++ b/scm-core/src/main/java/sonia/scm/template/DefaultEngine.java @@ -0,0 +1,53 @@ +/** + * Copyright (c) 2010, Sebastian Sdorra All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. 2. Redistributions in + * binary form must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. 3. Neither the name of SCM-Manager; + * nor the names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * http://bitbucket.org/sdorra/scm-manager + * + */ + + + +package sonia.scm.template; + +//~--- non-JDK imports -------------------------------------------------------- + +import com.google.inject.BindingAnnotation; + +//~--- JDK imports ------------------------------------------------------------ + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * + * @author Sebastian Sdorra + * @since 1.19 + */ +@BindingAnnotation +@Target({ ElementType.TYPE, ElementType.PARAMETER, ElementType.FIELD }) +@Retention(RetentionPolicy.RUNTIME) +public @interface DefaultEngine {} diff --git a/scm-core/src/main/java/sonia/scm/template/Template.java b/scm-core/src/main/java/sonia/scm/template/Template.java new file mode 100644 index 0000000000..c4b1589941 --- /dev/null +++ b/scm-core/src/main/java/sonia/scm/template/Template.java @@ -0,0 +1,60 @@ +/** + * Copyright (c) 2010, Sebastian Sdorra All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. 2. Redistributions in + * binary form must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. 3. Neither the name of SCM-Manager; + * nor the names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * http://bitbucket.org/sdorra/scm-manager + * + */ + + + +package sonia.scm.template; + +//~--- JDK imports ------------------------------------------------------------ + +import java.io.IOException; +import java.io.Writer; + +import java.util.Map; + +/** + * + * @author Sebastian Sdorra + * @since 1.19 + */ +public interface Template +{ + + /** + * Method description + * + * + * @param writer + * @param environment + * + * @throws IOException + */ + public void execute(Writer writer, Map environment) + throws IOException; +} diff --git a/scm-core/src/main/java/sonia/scm/template/TemplateEngine.java b/scm-core/src/main/java/sonia/scm/template/TemplateEngine.java new file mode 100644 index 0000000000..3506834a23 --- /dev/null +++ b/scm-core/src/main/java/sonia/scm/template/TemplateEngine.java @@ -0,0 +1,65 @@ +/** + * Copyright (c) 2010, Sebastian Sdorra All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. 2. Redistributions in + * binary form must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. 3. Neither the name of SCM-Manager; + * nor the names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * http://bitbucket.org/sdorra/scm-manager + * + */ + + + +package sonia.scm.template; + +//~--- JDK imports ------------------------------------------------------------ + +import java.io.IOException; + +/** + * + * @author Sebastian Sdorra + * @since 1.19 + */ +public interface TemplateEngine +{ + + /** + * Method description + * + * + * @param templatePath + * + * @return + * + * @throws IOException + */ + public Template getTemplate(String templatePath) throws IOException; + + /** + * Method description + * + * + * @return + */ + public TemplateType getType(); +} diff --git a/scm-core/src/main/java/sonia/scm/template/TemplateEngineFactory.java b/scm-core/src/main/java/sonia/scm/template/TemplateEngineFactory.java new file mode 100644 index 0000000000..d12e597501 --- /dev/null +++ b/scm-core/src/main/java/sonia/scm/template/TemplateEngineFactory.java @@ -0,0 +1,178 @@ +/** + * Copyright (c) 2010, Sebastian Sdorra All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. 2. Redistributions in + * binary form must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. 3. Neither the name of SCM-Manager; + * nor the names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * http://bitbucket.org/sdorra/scm-manager + * + */ + + + +package sonia.scm.template; + +//~--- non-JDK imports -------------------------------------------------------- + +import com.google.common.collect.Maps; +import com.google.inject.Inject; +import com.google.inject.Singleton; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +//~--- JDK imports ------------------------------------------------------------ + +import java.util.Collection; +import java.util.Map; +import java.util.Set; + +/** + * + * @author Sebastian Sdorra + * @since 1.19 + */ +@Singleton +public final class TemplateEngineFactory +{ + + /** + * the logger for TemplateEngineFactory + */ + private static final Logger logger = + LoggerFactory.getLogger(TemplateEngineFactory.class); + + //~--- constructors --------------------------------------------------------- + + /** + * Constructs ... + * + * + * @param engines + * @param defaultEngine + */ + @Inject + public TemplateEngineFactory(Set engines, + @DefaultEngine TemplateEngine defaultEngine) + { + if (logger.isDebugEnabled()) + { + logger.debug("register {} as default template engine", + defaultEngine.getType()); + } + + engineMap = Maps.newHashMap(); + + for (TemplateEngine engine : engines) + { + engineMap.put(engine.getType().getName(), engine); + + if (logger.isDebugEnabled()) + { + logger.debug("register template engin {}", engine.getType()); + } + } + + if (!engineMap.containsKey(defaultEngine.getType().getName())) + { + engineMap.put(defaultEngine.getType().getName(), defaultEngine); + } + + this.defaultEngine = defaultEngine; + } + + //~--- get methods ---------------------------------------------------------- + + /** + * Method description + * + * + * @return + */ + public TemplateEngine getDefaultEngine() + { + return defaultEngine; + } + + /** + * Method description + * + * + * @param engineName + * + * @return + */ + public TemplateEngine getEngine(String engineName) + { + return engineMap.get(engineName); + } + + /** + * Method description + * + * + * @param path + * + * @return + */ + public TemplateEngine getEngineByExtension(String path) + { + TemplateEngine engine = null; + int index = path.lastIndexOf('.'); + + if (index > 0) + { + path = path.substring(index); + } + + for (TemplateEngine e : engineMap.values()) + { + if (e.getType().getExtensions().contains(path)) + { + engine = e; + + break; + } + } + + return engine; + } + + /** + * Method description + * + * + * @return + */ + public Collection getEngines() + { + return engineMap.values(); + } + + //~--- fields --------------------------------------------------------------- + + /** Field description */ + private TemplateEngine defaultEngine; + + /** Field description */ + private Map engineMap; +} diff --git a/scm-core/src/main/java/sonia/scm/template/TemplateHandler.java b/scm-core/src/main/java/sonia/scm/template/TemplateHandler.java index 0a06b0c6f5..457cc5f006 100644 --- a/scm-core/src/main/java/sonia/scm/template/TemplateHandler.java +++ b/scm-core/src/main/java/sonia/scm/template/TemplateHandler.java @@ -58,6 +58,6 @@ public interface TemplateHandler * @throws IOException */ public void render(String templateName, Writer writer, - Map params) - throws IOException; + Map params) + throws IOException; } diff --git a/scm-core/src/main/java/sonia/scm/template/TemplateRenderException.java b/scm-core/src/main/java/sonia/scm/template/TemplateRenderException.java new file mode 100644 index 0000000000..45f51bdc43 --- /dev/null +++ b/scm-core/src/main/java/sonia/scm/template/TemplateRenderException.java @@ -0,0 +1,90 @@ +/** + * Copyright (c) 2010, Sebastian Sdorra All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. 2. Redistributions in + * binary form must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. 3. Neither the name of SCM-Manager; + * nor the names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * http://bitbucket.org/sdorra/scm-manager + * + */ + + + +package sonia.scm.template; + +//~--- JDK imports ------------------------------------------------------------ + +import java.io.IOException; + +/** + * + * @author Sebastian Sdorra + * @since 1.19 + */ +public class TemplateRenderException extends IOException +{ + + /** Field description */ + private static final long serialVersionUID = 2176596549717814756L; + + //~--- constructors --------------------------------------------------------- + + /** + * Constructs ... + * + */ + public TemplateRenderException() {} + + /** + * Constructs ... + * + * + * @param message + */ + public TemplateRenderException(String message) + { + super(message); + } + + /** + * Constructs ... + * + * + * @param cause + */ + public TemplateRenderException(Throwable cause) + { + super(cause); + } + + /** + * Constructs ... + * + * + * @param message + * @param cause + */ + public TemplateRenderException(String message, Throwable cause) + { + super(message, cause); + } +} diff --git a/scm-core/src/main/java/sonia/scm/template/TemplateType.java b/scm-core/src/main/java/sonia/scm/template/TemplateType.java new file mode 100644 index 0000000000..2618e82a7c --- /dev/null +++ b/scm-core/src/main/java/sonia/scm/template/TemplateType.java @@ -0,0 +1,105 @@ +/** + * Copyright (c) 2010, Sebastian Sdorra All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. 2. Redistributions in + * binary form must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. 3. Neither the name of SCM-Manager; + * nor the names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * http://bitbucket.org/sdorra/scm-manager + * + */ + + + +package sonia.scm.template; + +//~--- non-JDK imports -------------------------------------------------------- + +import com.google.common.collect.Lists; + +import sonia.scm.Type; + +//~--- JDK imports ------------------------------------------------------------ + +import java.io.Serializable; + +import java.util.Collection; + +/** + * + * @author Sebastian Sdorra + */ +public class TemplateType extends Type implements Serializable +{ + + /** Field description */ + private static final long serialVersionUID = 7947596921895752539L; + + //~--- constructors --------------------------------------------------------- + + /** + * Constructs ... + * + * + * @param name + * @param displayName + * @param extensions + */ + public TemplateType(String name, String displayName, + Collection extensions) + { + super(name, displayName); + this.extensions = extensions; + } + + /** + * Constructs ... + * + * + * @param name + * @param displayName + * @param extension + * @param extensions + */ + public TemplateType(String name, String displayName, String extension, + String... extensions) + { + this(name, displayName, Lists.asList(extension, extensions)); + } + + //~--- get methods ---------------------------------------------------------- + + /** + * Method description + * + * + * @return + */ + public Collection getExtensions() + { + return extensions; + } + + //~--- fields --------------------------------------------------------------- + + /** Field description */ + private Collection extensions; +}