]>
git.argeo.org Git - gpl/argeo-slc.git/blob - SedFilteredResource.java
676918e981f5902e24d3c0d2bf2958f8f7ad291c
2 * Copyright (C) 2007-2012 Argeo GmbH
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
;
52 // private CharsetEncoder encoder;
54 public Object
getObject() throws Exception
{
55 if (filters
.size() == 0)
58 // int capacity = 100 * 1024;// 100 KB
60 if (source
instanceof ByteArrayResource
) {
61 bb
= ByteBuffer
.wrap(((ByteArrayResource
) source
).getByteArray());
63 FileInputStream fis
= null;
65 File file
= source
.getFile();
66 fis
= new FileInputStream(file
);
67 FileChannel fc
= fis
.getChannel();
69 // Get the file's size and then map it into memory
70 int sz
= (int) fc
.size();
71 bb
= fc
.map(FileChannel
.MapMode
.READ_ONLY
, 0, sz
);
72 } catch (IOException e
) {
73 // ReadableByteChannel channel = Channels.newChannel(source
74 // .getInputStream());
75 // bb = ByteBuffer.allocateDirect(capacity);
78 // read = channel.read(bb);
79 // } while (read > 0);
80 // FIXME : use nio to parse the stream as it goes
81 bb
= ByteBuffer
.wrap(IOUtils
.toByteArray(source
84 IOUtils
.closeQuietly(fis
);
87 CharBuffer cb
= decoder
.decode(bb
);
88 for (Pattern pattern
: patterns
.keySet()) {
89 Matcher matcher
= pattern
.matcher(cb
);
90 String output
= matcher
.replaceAll(patterns
.get(pattern
));
91 cb
= CharBuffer
.wrap(output
);
93 // ByteBuffer bbout = encoder.encode(cb);
94 // ByteArrayOutputStream out = new ByteArrayOutputStream(capacity);
95 // WritableByteChannel wchannel = Channels.newChannel(out);
96 // wchannel.write(bbout);
97 ByteArrayResource res
= new ByteArrayResource(cb
.toString().getBytes());
101 public Class
<?
> getObjectType() {
102 return Resource
.class;
105 public boolean isSingleton() {
109 public void afterPropertiesSet() throws Exception
{
110 cs
= Charset
.forName(charset
);
111 decoder
= cs
.newDecoder();
112 // encoder = cs.newEncoder();
114 for (String sedStr
: filters
) {
115 sedStr
= sedStr
.trim();
116 if (sedStr
.length() < 4)
117 throw new SlcException(sedStr
+ " not properly formatted.");
118 if (sedStr
.charAt(0) != 's')
119 throw new SlcException(sedStr
+ " not properly formatted.");
120 Character sep
= sedStr
.charAt(1);
121 List
<String
> tokens
= new ArrayList
<String
>(4);
122 StringTokenizer st
= new StringTokenizer(sedStr
, sep
.toString());
123 while (st
.hasMoreTokens())
124 tokens
.add(st
.nextToken());
125 if (tokens
.size() != 3 && tokens
.size() != 4)
126 throw new SlcException(sedStr
+ " not properly formatted.");
127 patterns
.put(Pattern
.compile(tokens
.get(1)), tokens
.get(2));
131 public void setSource(Resource source
) {
132 this.source
= source
;
135 public void setFilters(List
<String
> filters
) {
136 this.filters
= filters
;
139 public void setCharset(String charset
) {
140 this.charset
= charset
;