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