]>
git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/SedFilteredResource.java
2 * Copyright (C) 2007-2012 Mathieu Baudier
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org
.argeo
.slc
.core
.execution
;
19 import java
.io
.FileInputStream
;
20 import java
.io
.IOException
;
21 import java
.nio
.ByteBuffer
;
22 import java
.nio
.CharBuffer
;
23 import java
.nio
.channels
.FileChannel
;
24 import java
.nio
.charset
.Charset
;
25 import java
.nio
.charset
.CharsetDecoder
;
26 import java
.util
.ArrayList
;
27 import java
.util
.HashMap
;
28 import java
.util
.List
;
30 import java
.util
.StringTokenizer
;
31 import java
.util
.regex
.Matcher
;
32 import java
.util
.regex
.Pattern
;
34 import org
.apache
.commons
.io
.IOUtils
;
35 import org
.argeo
.slc
.SlcException
;
36 import org
.springframework
.beans
.factory
.FactoryBean
;
37 import org
.springframework
.beans
.factory
.InitializingBean
;
38 import org
.springframework
.core
.io
.ByteArrayResource
;
39 import org
.springframework
.core
.io
.Resource
;
41 /** Experimental and suboptimal */
42 public class SedFilteredResource
implements FactoryBean
, InitializingBean
{
43 private Resource source
;
45 private List
<String
> filters
= new ArrayList
<String
>();
46 private Map
<Pattern
, String
> patterns
= new HashMap
<Pattern
, String
>();
48 private String charset
= "UTF-8";
50 private CharsetDecoder decoder
;
51 //private CharsetEncoder encoder;
53 public Object
getObject() throws Exception
{
54 if (filters
.size() == 0)
57 //int capacity = 100 * 1024;// 100 KB
59 if (source
instanceof ByteArrayResource
) {
60 bb
= ByteBuffer
.wrap(((ByteArrayResource
) source
).getByteArray());
63 File file
= source
.getFile();
64 FileInputStream fis
= new FileInputStream(file
);
65 FileChannel fc
= fis
.getChannel();
67 // Get the file's size and then map it into memory
68 int sz
= (int) fc
.size();
69 bb
= fc
.map(FileChannel
.MapMode
.READ_ONLY
, 0, sz
);
70 } catch (IOException e
) {
71 // ReadableByteChannel channel = Channels.newChannel(source
72 // .getInputStream());
73 // bb = ByteBuffer.allocateDirect(capacity);
76 // read = channel.read(bb);
77 // } while (read > 0);
78 // FIXME : use nio to parse the stream as it goes
79 bb
= ByteBuffer
.wrap(IOUtils
.toByteArray(source
83 CharBuffer cb
= decoder
.decode(bb
);
84 for (Pattern pattern
: patterns
.keySet()) {
85 Matcher matcher
= pattern
.matcher(cb
);
86 String output
= matcher
.replaceAll(patterns
.get(pattern
));
87 cb
= CharBuffer
.wrap(output
);
89 // ByteBuffer bbout = encoder.encode(cb);
90 // ByteArrayOutputStream out = new ByteArrayOutputStream(capacity);
91 // WritableByteChannel wchannel = Channels.newChannel(out);
92 // wchannel.write(bbout);
93 ByteArrayResource res
= new ByteArrayResource(cb
.toString().getBytes());
97 public Class
<?
> getObjectType() {
98 return Resource
.class;
101 public boolean isSingleton() {
105 public void afterPropertiesSet() throws Exception
{
106 cs
= Charset
.forName(charset
);
107 decoder
= cs
.newDecoder();
108 //encoder = cs.newEncoder();
110 for (String sedStr
: filters
) {
111 sedStr
= sedStr
.trim();
112 if (sedStr
.length() < 4)
113 throw new SlcException(sedStr
+ " not properly formatted.");
114 if (sedStr
.charAt(0) != 's')
115 throw new SlcException(sedStr
+ " not properly formatted.");
116 Character sep
= sedStr
.charAt(1);
117 List
<String
> tokens
= new ArrayList
<String
>(4);
118 StringTokenizer st
= new StringTokenizer(sedStr
, sep
.toString());
119 while (st
.hasMoreTokens())
120 tokens
.add(st
.nextToken());
121 if (tokens
.size() != 3 && tokens
.size() != 4)
122 throw new SlcException(sedStr
+ " not properly formatted.");
123 patterns
.put(Pattern
.compile(tokens
.get(1)), tokens
.get(2));
127 public void setSource(Resource source
) {
128 this.source
= source
;
131 public void setFilters(List
<String
> filters
) {
132 this.filters
= filters
;
135 public void setCharset(String charset
) {
136 this.charset
= charset
;