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