001 /* 002 @license.text@ 003 */ 004 package biz.hammurapi.util; 005 006 import java.io.BufferedReader; 007 import java.io.ByteArrayInputStream; 008 import java.io.ByteArrayOutputStream; 009 import java.io.IOException; 010 import java.io.InputStream; 011 import java.io.InputStreamReader; 012 import java.util.ArrayList; 013 import java.util.Iterator; 014 import java.util.List; 015 016 /** 017 * Aggregates multiple classpath resources in one resource stream. 018 * @author Pavel Vlasov 019 * @revision $Revision$ 020 */ 021 public class ResourceAggregator { 022 private List resourceList=new ArrayList(); 023 024 /** 025 * Adds resource to the list of resources to be aggregated. 026 * @param resourceName 027 */ 028 public void addResource(String resourceName) { 029 resourceList.add(resourceName); 030 } 031 032 /** 033 * Reads each line from 'resourceListName' resource and adds 034 * to the list of resources to be aggregated. 035 * Directive #include allows to include another resource 036 * list. If line has # as its first non-blank character then this 037 * line is treated as comment line. 038 * @param resourceListName 039 * @throws IOException 040 */ 041 public void addResourceList(String resourceListName) throws IOException { 042 InputStream resourceStream = getClass().getClassLoader().getResourceAsStream(resourceListName); 043 if (resourceStream==null) { 044 throw new IOException("Script list resource not found: "+resourceListName); 045 } 046 047 BufferedReader br=new BufferedReader(new InputStreamReader(resourceStream)); 048 String s; 049 while ((s=br.readLine())!=null) { 050 String strim = s.trim(); 051 if (strim.startsWith("#include")) { 052 addResourceList(strim.substring("#include".length()).trim()); 053 } else if (strim.length()>0 && !strim.startsWith("#")) { 054 resourceList.add(strim); 055 } 056 } 057 br.close(); 058 } 059 060 /** 061 * Aggregates all resources in one resource. 062 * @return Aggregated input stream. 063 * @throws IOException 064 */ 065 public InputStream aggregate() throws IOException { 066 ByteArrayOutputStream baos=new ByteArrayOutputStream(); 067 Iterator it=resourceList.iterator(); 068 ClassLoader classLoader=getClass().getClassLoader(); 069 while (it.hasNext()) { 070 String resourceName=(String) it.next(); 071 InputStream in=classLoader.getResourceAsStream(resourceName); 072 if (in==null) { 073 throw new IOException("Resource not found: "+resourceName); 074 } 075 byte[] buf=new byte[4096]; 076 int l; 077 while ((l=in.read(buf))!=-1) { 078 baos.write(buf, 0, l); 079 } 080 in.close(); 081 } 082 baos.close(); 083 084 return new ByteArrayInputStream(baos.toByteArray()); 085 } 086 087 /** 088 * Aggregates resource lists listed in command line arguments and outputs them to console. 089 * @param args 090 */ 091 public static void main(String[] args) { 092 try { 093 ResourceAggregator aggregator=new ResourceAggregator(); 094 for (int i=0; i<args.length; i++) { 095 aggregator.addResourceList(args[i]); 096 } 097 098 BufferedReader br=new BufferedReader(new InputStreamReader(aggregator.aggregate())); 099 String line; 100 while ((line=br.readLine())!=null) { 101 System.out.println(line); 102 } 103 } catch (IOException e) { 104 System.err.println("ERROR: "+e.getMessage()); 105 System.exit(1); 106 } 107 108 109 } 110 111 }