Inspector | Message | Severity | Location |
---|---|---|---|
Java Inspector 048 | Copyrights information should be present in each file. | 1 | |
Java Inspector 068 | Do not use System.out and System.err to output logging messages. Use loggers instead. | 2 | 120:51 |
Java Inspector 068 | Do not use System.out and System.err to output logging messages. Use loggers instead. | 2 | 123:43 |
Java Inspector 089 | Parameter resourceName is not documented | 2 | 47:9 |
Java Inspector 089 | Parameter resourceListName is not documented | 2 | 60:9 |
Java Inspector 089 | Parameter args is not documented | 2 | 110:9 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 63:47 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 70:46 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 71:65 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 72:74 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 92:55 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 123:44 |
1/*
2 * hgcommons 9
3 * Hammurapi Group Common Library
4 * Copyright (C) 2003 Hammurapi Group
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 * URL: http://www.hammurapi.biz/hammurapi-biz/ef/xmenu/hammurapi-group/products/products/hgcommons/index.html
21 * e-Mail: support@hammurapi.biz
22 */
23package biz.hammurapi.util;
24
25import java.io.BufferedReader;
26import java.io.ByteArrayInputStream;
27import java.io.ByteArrayOutputStream;
28import java.io.IOException;
29import java.io.InputStream;
30import java.io.InputStreamReader;
31import java.util.ArrayList;
32import java.util.Iterator;
33import java.util.List;
34
35/**
36 * Aggregates multiple classpath resources in one resource stream.
37 * @author Pavel Vlasov
38 * @revision $Revision$
39 */
40public class ResourceAggregator {
41 private List resourceList=new ArrayList();
42
43 /**
44 * Adds resource to the list of resources to be aggregated.
45 * @param resourceName
46 */
47 public void addResource(String resourceName) {
48 resourceList.add(resourceName);
49 }
50
51 /**
52 * Reads each line from 'resourceListName' resource and adds
53 * to the list of resources to be aggregated.
54 * Directive #include allows to include another resource
55 * list. If line has # as its first non-blank character then this
56 * line is treated as comment line.
57 * @param resourceListName
58 * @throws IOException
59 */
60 public void addResourceList(String resourceListName) throws IOException {
61 InputStream resourceStream = getClass().getClassLoader().getResourceAsStream(resourceListName);
62 if (resourceStream==null) {
63 throw new IOException("Script list resource not found: "+resourceListName);
64 }
65
66 BufferedReader br=new BufferedReader(new InputStreamReader(resourceStream));
67 String s;
68 while ((s=br.readLine())!=null) {
69 String strim = s.trim();
70 if (strim.startsWith("#include")) {
71 addResourceList(strim.substring("#include".length()).trim());
72 } else if (strim.length()>0 && !strim.startsWith("#")) {
73 resourceList.add(strim);
74 }
75 }
76 br.close();
77 }
78
79 /**
80 * Aggregates all resources in one resource.
81 * @return Aggregated input stream.
82 * @throws IOException
83 */
84 public InputStream aggregate() throws IOException {
85 ByteArrayOutputStream baos=new ByteArrayOutputStream();
86 Iterator it=resourceList.iterator();
87 ClassLoader classLoader=getClass().getClassLoader();
88 while (it.hasNext()) {
89 String resourceName=(String) it.next();
90 InputStream in=classLoader.getResourceAsStream(resourceName);
91 if (in==null) {
92 throw new IOException("Resource not found: "+resourceName);
93 }
94 byte[] buf=new byte[4096];
95 int l;
96 while ((l=in.read(buf))!=-1) {
97 baos.write(buf, 0, l);
98 }
99 in.close();
100 }
101 baos.close();
102
103 return new ByteArrayInputStream(baos.toByteArray());
104 }
105
106 /**
107 * Aggregates resource lists listed in command line arguments and outputs them to console.
108 * @param args
109 */
110 public static void main(String[] args) {
111 try {
112 ResourceAggregator aggregator=new ResourceAggregator();
113 for (int i=0; i<args.length; i++) {
114 aggregator.addResourceList(args[i]);
115 }
116
117 BufferedReader br=new BufferedReader(new InputStreamReader(aggregator.aggregate()));
118 String line;
119 while ((line=br.readLine())!=null) {
120 System.out.println(line);
121 }
122 } catch (IOException e) {
123 System.err.println("ERROR: "+e.getMessage());
124 System.exit(1);
125 }
126
127
128 }
129
130}
131