]> git.argeo.org Git - gpl/argeo-slc.git/blob - legacy/org.argeo.slc.spring/src/org/argeo/slc/core/execution/SedFilteredResource.java
Merge remote-tracking branch 'origin/master' into testing
[gpl/argeo-slc.git] / legacy / org.argeo.slc.spring / src / org / argeo / slc / core / execution / SedFilteredResource.java
1 package org.argeo.slc.core.execution;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.IOException;
6 import java.nio.ByteBuffer;
7 import java.nio.CharBuffer;
8 import java.nio.channels.FileChannel;
9 import java.nio.charset.Charset;
10 import java.nio.charset.CharsetDecoder;
11 import java.util.ArrayList;
12 import java.util.HashMap;
13 import java.util.List;
14 import java.util.Map;
15 import java.util.StringTokenizer;
16 import java.util.regex.Matcher;
17 import java.util.regex.Pattern;
18
19 import org.apache.commons.io.IOUtils;
20 import org.argeo.slc.SlcException;
21 import org.springframework.beans.factory.FactoryBean;
22 import org.springframework.beans.factory.InitializingBean;
23 import org.springframework.core.io.ByteArrayResource;
24 import org.springframework.core.io.Resource;
25
26 /** Experimental and suboptimal */
27 public class SedFilteredResource implements FactoryBean<Resource>,
28 InitializingBean {
29 private Resource source;
30
31 private List<String> filters = new ArrayList<String>();
32 private Map<Pattern, String> patterns = new HashMap<Pattern, String>();
33
34 private String charset = "UTF-8";
35 private Charset cs;
36 private CharsetDecoder decoder;
37
38 // private CharsetEncoder encoder;
39
40 public Resource getObject() throws Exception {
41 if (filters.size() == 0)
42 return source;
43
44 // int capacity = 100 * 1024;// 100 KB
45 ByteBuffer bb;
46 if (source instanceof ByteArrayResource) {
47 bb = ByteBuffer.wrap(((ByteArrayResource) source).getByteArray());
48 } else {
49 FileInputStream fis = null;
50 try {
51 File file = source.getFile();
52 fis = new FileInputStream(file);
53 FileChannel fc = fis.getChannel();
54
55 // Get the file's size and then map it into memory
56 int sz = (int) fc.size();
57 bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, sz);
58 } catch (IOException e) {
59 // ReadableByteChannel channel = Channels.newChannel(source
60 // .getInputStream());
61 // bb = ByteBuffer.allocateDirect(capacity);
62 // int read = 0;
63 // do {
64 // read = channel.read(bb);
65 // } while (read > 0);
66 // FIXME : use nio to parse the stream as it goes
67 bb = ByteBuffer.wrap(IOUtils.toByteArray(source
68 .getInputStream()));
69 } finally {
70 IOUtils.closeQuietly(fis);
71 }
72 }
73 CharBuffer cb = decoder.decode(bb);
74 for (Pattern pattern : patterns.keySet()) {
75 Matcher matcher = pattern.matcher(cb);
76 String output = matcher.replaceAll(patterns.get(pattern));
77 cb = CharBuffer.wrap(output);
78 }
79 // ByteBuffer bbout = encoder.encode(cb);
80 // ByteArrayOutputStream out = new ByteArrayOutputStream(capacity);
81 // WritableByteChannel wchannel = Channels.newChannel(out);
82 // wchannel.write(bbout);
83 ByteArrayResource res = new ByteArrayResource(cb.toString().getBytes());
84 return res;
85 }
86
87 public Class<?> getObjectType() {
88 return Resource.class;
89 }
90
91 public boolean isSingleton() {
92 return true;
93 }
94
95 public void afterPropertiesSet() throws Exception {
96 cs = Charset.forName(charset);
97 decoder = cs.newDecoder();
98 // encoder = cs.newEncoder();
99
100 for (String sedStr : filters) {
101 sedStr = sedStr.trim();
102 if (sedStr.length() < 4)
103 throw new SlcException(sedStr + " not properly formatted.");
104 if (sedStr.charAt(0) != 's')
105 throw new SlcException(sedStr + " not properly formatted.");
106 Character sep = sedStr.charAt(1);
107 List<String> tokens = new ArrayList<String>(4);
108 StringTokenizer st = new StringTokenizer(sedStr, sep.toString());
109 while (st.hasMoreTokens())
110 tokens.add(st.nextToken());
111 if (tokens.size() != 3 && tokens.size() != 4)
112 throw new SlcException(sedStr + " not properly formatted.");
113 patterns.put(Pattern.compile(tokens.get(1)), tokens.get(2));
114 }
115 }
116
117 public void setSource(Resource source) {
118 this.source = source;
119 }
120
121 public void setFilters(List<String> filters) {
122 this.filters = filters;
123 }
124
125 public void setCharset(String charset) {
126 this.charset = charset;
127 }
128
129 }