001: /*
002: * hgcommons 7
003: * Hammurapi Group Common Library
004: * Copyright (C) 2003 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.hammurapi.biz/hammurapi-biz/ef/xmenu/hammurapi-group/products/products/hgcommons/index.html
021: * e-Mail: support@hammurapi.biz
022: */
023: package biz.hammurapi.util;
024:
025: import java.io.BufferedReader;
026: import java.io.ByteArrayInputStream;
027: import java.io.ByteArrayOutputStream;
028: import java.io.IOException;
029: import java.io.InputStream;
030: import java.io.InputStreamReader;
031: import java.util.ArrayList;
032: import java.util.Iterator;
033: import java.util.List;
034:
035: /**
036: * Aggregates multiple classpath resources in one resource stream.
037: * @author Pavel Vlasov
038: * @revision $Revision$
039: */
040: public class ResourceAggregator {
041: private List resourceList = new ArrayList();
042:
043: /**
044: * Adds resource to the list of resources to be aggregated.
045: * @param resourceName
046: */
047: public void addResource(String resourceName) {
048: resourceList.add(resourceName);
049: }
050:
051: /**
052: * Reads each line from 'resourceListName' resource and adds
053: * to the list of resources to be aggregated.
054: * Directive #include allows to include another resource
055: * list. If line has # as its first non-blank character then this
056: * line is treated as comment line.
057: * @param resourceListName
058: * @throws IOException
059: */
060: public void addResourceList(String resourceListName)
061: throws IOException {
062: InputStream resourceStream = getClass().getClassLoader()
063: .getResourceAsStream(resourceListName);
064: if (resourceStream == null) {
065: throw new IOException("Script list resource not found: "
066: + resourceListName);
067: }
068:
069: BufferedReader br = new BufferedReader(new InputStreamReader(
070: resourceStream));
071: String s;
072: while ((s = br.readLine()) != null) {
073: String strim = s.trim();
074: if (strim.startsWith("#include")) {
075: addResourceList(strim.substring("#include".length())
076: .trim());
077: } else if (strim.length() > 0 && !strim.startsWith("#")) {
078: resourceList.add(strim);
079: }
080: }
081: br.close();
082: }
083:
084: /**
085: * Aggregates all resources in one resource.
086: * @return Aggregated input stream.
087: * @throws IOException
088: */
089: public InputStream aggregate() throws IOException {
090: ByteArrayOutputStream baos = new ByteArrayOutputStream();
091: Iterator it = resourceList.iterator();
092: ClassLoader classLoader = getClass().getClassLoader();
093: while (it.hasNext()) {
094: String resourceName = (String) it.next();
095: InputStream in = classLoader
096: .getResourceAsStream(resourceName);
097: if (in == null) {
098: throw new IOException("Resource not found: "
099: + resourceName);
100: }
101: byte[] buf = new byte[4096];
102: int l;
103: while ((l = in.read(buf)) != -1) {
104: baos.write(buf, 0, l);
105: }
106: in.close();
107: }
108: baos.close();
109:
110: return new ByteArrayInputStream(baos.toByteArray());
111: }
112:
113: /**
114: * Aggregates resource lists listed in command line arguments and outputs them to console.
115: * @param args
116: */
117: public static void main(String[] args) {
118: try {
119: ResourceAggregator aggregator = new ResourceAggregator();
120: for (int i = 0; i < args.length; i++) {
121: aggregator.addResourceList(args[i]);
122: }
123:
124: BufferedReader br = new BufferedReader(
125: new InputStreamReader(aggregator.aggregate()));
126: String line;
127: while ((line = br.readLine()) != null) {
128: System.out.println(line);
129: }
130: } catch (IOException e) {
131: System.err.println("ERROR: " + e.getMessage());
132: System.exit(1);
133: }
134:
135: }
136:
137: }
|