001 /* 002 * Jpkg - Java library and tools for operating system package creation. 003 * 004 * Copyright (c) 2007 Three Rings Design, Inc. 005 * All rights reserved. 006 * 007 * Redistribution and use in source and binary forms, with or without 008 * modification, are permitted provided that the following conditions 009 * are met: 010 * 1. Redistributions of source code must retain the above copyright 011 * notice, this list of conditions and the following disclaimer. 012 * 2. Redistributions in binary form must reproduce the above copyright 013 * notice, this list of conditions and the following disclaimer in the 014 * documentation and/or other materials provided with the distribution. 015 * 3. Neither the name of the copyright owner nor the names of contributors 016 * may be used to endorse or promote products derived from this software 017 * without specific prior written permission. 018 * 019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 020 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 021 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 022 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 023 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 024 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 025 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 026 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 027 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 028 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 029 * POSSIBILITY OF SUCH DAMAGE. 030 */ 031 package com.threerings.jpkg.ant.dpkg.scripts; 032 033 import java.io.IOException; 034 import java.io.InputStream; 035 import java.util.Arrays; 036 import java.util.Collections; 037 import java.util.HashMap; 038 import java.util.Map; 039 import java.util.Set; 040 import java.util.TreeSet; 041 import java.util.Map.Entry; 042 043 import org.apache.velocity.VelocityContext; 044 045 import com.threerings.jpkg.ant.dpkg.DpkgData; 046 import com.threerings.jpkg.debian.MaintainerScript; 047 import com.threerings.jpkg.debian.MaintainerScript.Type; 048 049 /** 050 * A simple implementation of {@link BaseTemplateScript} which provides helpers to populate the 051 * velocity context and inserts the {@link DpkgData} object into the context by default. 052 */ 053 public abstract class TemplateScript extends BaseTemplateScript 054 { 055 /** 056 * Construct a {@link TemplateScript} with the single script type implemented. 057 */ 058 public TemplateScript (MaintainerScript.Type type) 059 { 060 _types = Collections.singleton(type); 061 } 062 063 /** 064 * Construct a {@link TemplateScript} with a set of the script types implemented. 065 * @see #typeList(MaintainerScript.Type...) 066 */ 067 public TemplateScript (Set<MaintainerScript.Type> types) 068 { 069 _types = types; 070 } 071 072 /** 073 * A static helper to pass a list of types to the constructor. 074 */ 075 public static Set<MaintainerScript.Type> typeList (MaintainerScript.Type...types) 076 { 077 return Collections.unmodifiableSet(new TreeSet<MaintainerScript.Type>(Arrays.asList(types))); 078 } 079 080 // from PackageScript 081 public Set<Type> getTypes () 082 { 083 return _types; 084 } 085 086 // from PackageScript 087 public InputStream getSource (DpkgData data) 088 throws IOException 089 { 090 addSubstitution(DPKG_DATA_KEY, data); 091 return mergeTemplate(); 092 } 093 094 // from VelocityTemplate 095 public void populateContext (VelocityContext context) 096 { 097 for (final Entry<String, Object> entry : _substitutions.entrySet()) { 098 context.put(entry.getKey(), entry.getValue()); 099 } 100 } 101 102 /** 103 * Add a Velocity template substitution, converting the supplied key into the supplied value. 104 */ 105 protected void addSubstitution (String key, Object value) 106 { 107 _substitutions.put(key, value); 108 } 109 110 /** 111 * The default key name to use when adding the {@link DpkgData} object to the context. 112 */ 113 protected static final String DPKG_DATA_KEY = "dpkg"; 114 115 /** 116 * The map of template substitutions. 117 */ 118 private final Map<String, Object> _substitutions = new HashMap<String, Object>(); 119 120 /** 121 * The set of maintainer script types this script implements. 122 */ 123 private final Set<MaintainerScript.Type> _types; 124 }