]>
git.argeo.org Git - gpl/argeo-slc.git/blob - cms/org.argeo.slc.spring/src/org/argeo/slc/core/execution/SedFilteredResource.java
1 package org
.argeo
.slc
.core
.execution
;
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
;
15 import java
.util
.StringTokenizer
;
16 import java
.util
.regex
.Matcher
;
17 import java
.util
.regex
.Pattern
;
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
;
26 /** Experimental and suboptimal */
27 public class SedFilteredResource
implements FactoryBean
<Resource
>,
29 private Resource source
;
31 private List
<String
> filters
= new ArrayList
<String
>();
32 private Map
<Pattern
, String
> patterns
= new HashMap
<Pattern
, String
>();
34 private String charset
= "UTF-8";
36 private CharsetDecoder decoder
;
38 // private CharsetEncoder encoder;
40 public Resource
getObject() throws Exception
{
41 if (filters
.size() == 0)
44 // int capacity = 100 * 1024;// 100 KB
46 if (source
instanceof ByteArrayResource
) {
47 bb
= ByteBuffer
.wrap(((ByteArrayResource
) source
).getByteArray());
49 FileInputStream fis
= null;
51 File file
= source
.getFile();
52 fis
= new FileInputStream(file
);
53 FileChannel fc
= fis
.getChannel();
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);
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
70 IOUtils
.closeQuietly(fis
);
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
);
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());
87 public Class
<?
> getObjectType() {
88 return Resource
.class;
91 public boolean isSingleton() {
95 public void afterPropertiesSet() throws Exception
{
96 cs
= Charset
.forName(charset
);
97 decoder
= cs
.newDecoder();
98 // encoder = cs.newEncoder();
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));
117 public void setSource(Resource source
) {
118 this.source
= source
;
121 public void setFilters(List
<String
> filters
) {
122 this.filters
= filters
;
125 public void setCharset(String charset
) {
126 this.charset
= charset
;