]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/SedFilteredResource.java
Remove unnecessary check causing failures
[gpl/argeo-slc.git] / runtime / org.argeo.slc.core / src / main / java / org / argeo / slc / core / execution / SedFilteredResource.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
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 package org.argeo.slc.core.execution;
17
18 import java.io.File;
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;
29 import java.util.Map;
30 import java.util.StringTokenizer;
31 import java.util.regex.Matcher;
32 import java.util.regex.Pattern;
33
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;
40
41 /** Experimental and suboptimal */
42 public class SedFilteredResource implements FactoryBean, InitializingBean {
43 private Resource source;
44
45 private List<String> filters = new ArrayList<String>();
46 private Map<Pattern, String> patterns = new HashMap<Pattern, String>();
47
48 private String charset = "UTF-8";
49 private Charset cs;
50 private CharsetDecoder decoder;
51
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 FileInputStream fis = null;
64 try {
65 File file = source.getFile();
66 fis = new FileInputStream(file);
67 FileChannel fc = fis.getChannel();
68
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);
76 // int read = 0;
77 // do {
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
82 .getInputStream()));
83 } finally {
84 IOUtils.closeQuietly(fis);
85 }
86 }
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);
92 }
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());
98 return res;
99 }
100
101 public Class<?> getObjectType() {
102 return Resource.class;
103 }
104
105 public boolean isSingleton() {
106 return true;
107 }
108
109 public void afterPropertiesSet() throws Exception {
110 cs = Charset.forName(charset);
111 decoder = cs.newDecoder();
112 // encoder = cs.newEncoder();
113
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));
128 }
129 }
130
131 public void setSource(Resource source) {
132 this.source = source;
133 }
134
135 public void setFilters(List<String> filters) {
136 this.filters = filters;
137 }
138
139 public void setCharset(String charset) {
140 this.charset = charset;
141 }
142
143 }