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